Hi, I’m new to Lua scripting and I’m looking for some advice. What I’m currently trying to do is create a function where if I call it, my rover immediately returns to 1 or 2 waypoints behind it (ones it’s already past) in order to make sure when losing a fix that it maintains the exact same line. From what I can tell from testing is that the mission:num_commands() - 1 is a good way to manually set which WP to return to but I’m looking to make it hands free. Here’s the function:
function return_wp()
local last_wp = mission:num_commands() - 1
if mission:num_commands() > 1 then
mission:set_current_cmd(last_wp)
gcs:send_text(4, 'Heading to last WP')
end
end
return return_wp()
Any advice and getting current waypoints or is this a limitation of the command? If so, is there another and/or better way to achieve this?
It’s important to note that crosstrack error will not simply increase with a degradation in fix type. Crosstrack error is only the difference between the present AHRS location (which could be inaccurate with a poor fix or large HDOP) and the calculated course.
By hands off I just meant not setting the rewind WP manually. Looks like you nailed it though. Adding mission:get_current_nav_index and simply doing a -2 on it worked perfectly. Now if I call the function it rewinds the script by 2 waypoints and continues on. I’ll look into replacing it with crosstrack error in the future because there’s no reason to rewind if my rover was still on target but for now I’m jsut glad it works.
I think the logic you want goes something like this:
If fix type is less than desired, stop
When fix type improves, go back 2 waypoints and retrace the course
Depending on the expected distance between waypoints, it might be prudent to loop through the last few waypoints (once fix type is good), measuring the distance between present position and each previous waypoint until an adequate “do over” distance exists, rather than blindly rewinding by 2.
The location binding has a get_distance() method for that.
Definitely something to consider. Currently my missions are all short wp to wp jumps but in the future your right, it would be good to check distance before blindly jumping back waypoints. Hopefully with the addition of crosstrack error monitoring I wont be needing to rewind at all unless absolutely necessary.
Determine an appropriate rewind distance and set the associated waypoint as current
Of course, this all assumes you will always have the appropriate amount of maneuvering room to turn around (probably mostly true if it’s a skid steered Rover). But be careful if you have sharp corners that shouldn’t be cut across.