Set Output PWM at Specific Waypoint

Is there a way to set PWM of an AUX channel at a specific waypoint? For example, I’d like to set A6 to 1800 PWM when the plane crosses waypoint 3.

Currently, my script changes PWM based on time - this is cumbersome as it requires precisely measuring the time it takes to fly between waypoints.

As an example, the current script below updates pwm every 5 seconds, but I would like to modify the code so that it updates at each waypoint.

You could use the function mission:get_current_nav_index()

local UPDATE_RATE_HZ = 1
local last_cnum = 0
function update()
	local cnum = mission:get_current_nav_index()
	if cnum ~= last_cnum then
      	--  new command
		last_cnum = cnum
		gcs:send_text(4, "New Command")
	end 
	return update, 1000/UPDATE_RATE_HZ
end
return update(), 5000
3 Likes

Thank you Rolf - mission:get_current_nav_index() is what I was looking for

Another possibility, if you want to trigger an action only at single waypoints, is to add a waypoint of type “SCRIPT_TIME” behind each of these waypoints. This way you can pass parameters from the mission directly to the script with the LUA function “vehicle:nav_script_time()”.

Example:

local UPDATE_RATE_HZ = 1
local last_id = 0
function update()
   id, cmd, arg1, arg2, arg3, arg4 = vehicle:nav_script_time()
   if id then
      if id ~= last_id then
		last_id=id
		gcs:send_text(4, "SCRIPT_TIME")
	  end
	end
	return update, 1000/UPDATE_RATE_HZ
end
return update(), 5000
1 Like

Thanks, that is good to know.

The mission I’m flying is 4 consecutive loops, where each loop contains 4 waypoints (so 16 waypoints in total). It is desired to progressively increase PWM during each loop. To accomplish this, I implemented a series of if statements to check whether the current waypoint is within the range of the waypoints for a given loop and update PWM accordingly. I think this should work, but a sanity check would be appreciated.

You should not block the telemetry with four gcs:send_text messages every time you call the function (0.5 sec). The content of the messages changes anyway only when switching to the next waypoint. So I would use a variable wp_last. Only if wp_last is different from wp, the essential part of the function needs to run in an if-end part. In this part wp_last = wp is set.

Just saw this. I ended up flying with the code as is before making these changes and experienced a crash at the start of the 2nd loop. Still reviewing data to determine root cause. Is it possible that blocking the telemetry could have contributed to the crash? Thanks

No one would hope so. As to the cause of the crash, without a log file (*.bin) one can only make assumptions or speculate. It would be best if you post a link to the logfile.

Rolf, your review would be much appreciated. I’ve created a new post with links to logs here: https://discuss.ardupilot.org/t/in-flight-crash-root-cause-investigation/94084

Thanks