I have made this lua script for you to try, essentially its switching to guided mode to perform a circle to the left or right until the rangefinder distance is above the minimum, i haven’t tested it you will probably need to tweak some things like the size of the circle and speed. you just need a pair of rangefinders set to position 6 and 2 for left and right .
-- This scirpt uses Rover's turn rate controller to make the vehicle move in circles of fixed radius
-- Edit these variables
local rad_xy_m = 0.5 -- circle radius in xy plane in m
local target_speed_xy_mps = 0.5 -- target speed in m/s
local rc_channel_switch = 7 -- switch this channel to "high" to get the script working
local min_range = 100 -- min distance in cm before turning
-- Fixed variables
local omega_radps = target_speed_xy_mps/rad_xy_m
local rover_guided_mode_num = 15
local left_range = 0
local right_range = 0
local direction = 1
end
-- Script Start --
gcs:send_text(0,"Script started")
gcs:send_text(0,"Trajectory period: " .. tostring(2 * math.rad(180) / omega_radps))
local circle_active = false
local last_mode = 0
function update()
if not circle_active then
last_mode = vehicle:get_mode()
end
local left_range = rangefinder:distance_cm_orient(6) (true)
if not left_range then
return update, 1000
end
local right_range = rangefinder:distance_cm_orient(2) (true)
if not right_range then
return update, 1000
end
if arming:is_armed() and rc:get_pwm(rc_channel_switch) > 1700 and not circle_active and (vehicle:get_mode() == 10) and left_range < min_range and left_range < right_range then
-- set guided mode since rc switch is now high
vehicle:set_mode(rover_guided_mode_num)
direction = 1
circle_active = true
if arming:is_armed() and rc:get_pwm(rc_channel_switch) > 1700 and not circle_active and (vehicle:get_mode() == 10) and right range < min range and right_range < left range then
-- set guided mode since rc switch is now high
vehicle:set_mode(rover_guided_mode_num)
direction = -1
circle_active = true
elseif arming:is_armed() and rc:get_pwm(rc_channel_switch) < 1200 and circle_active then
-- set back to last mode since rc switch is low
vehicle:set_mode(last_mode)
circle_active = false
end
if circle_active then
--target turn rate in degrees including direction
local target_turn_rate = math.deg(omega_radps * direction)
-- send guided message
if not vehicle:set_desired_turn_rate_and_speed(target_turn_rate, target_speed_xy_mps) then
gcs:send_text(0, "Failed to send target ")
end
end
return update, 100
end
return update()