Rover enable slide behavior

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()

2 Likes

Hey, geofransis, thanks a lot.I will try, and experiment with it.

With your help i was even getting somewhere with arduino script, but i think this lua is even better idea.

1 Like

I finally was able to test the script and got an error

expected near ‘end’

It refered to the end of fixed variables, after deleting the “end” in that part i got "unexpected simbol near > and refers to now line 78 : < max_range and > 0 then …

Thanks

Edit: I am eager to make it work, trying to modify it on google help, but not solved yet.

try the simplified version thats pasted in the forum post rather than the version from github, its much more lightly to work its just a slight edit of an existing script.

Oh, i see.Got it.

Thanks

I have been busy recently, I haven’t had a chance to look at it again. im not that good with lua so it will probably need some work.

Thats ok, i am very enthusiastic about it, i fixed some typos with missing “_” and there was an error at
“vehicle:get_mode()” i changed it to " last_mode = auto" as is in git version, but than i got two errors left, one is at
“function update()” and the other at the end, aggain
Complaining about “end”

I did not have time anymore at my vineyard today, so i will try it on sitl, and on another board .Will do a field test when i get rid of the errors. With your help hopefully.

I have added another “end” towards the bottom and now in SITL i have " attempt to call a number value" error which i guess might be a good thing. The error is on the line where it checks the left sensor.

I did setup rangefinders in SITL, but i dont get any obstacle values that is why an error ( allthough polygon fences and bendy ruller work) so next test is on FMU with some sensors

After being busy harvesting in september, i finaly made some progress and
came to verry final error in the script : "Failed to send target ". It is tested on fmu v3 on the bench, with sensors, but not yet on rover with everything on, so auto mode is not possible yet. Hopefully it will work

script with changes (through trial and error) :

– 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

– 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) * 1.0
if not left_range then
return update, 1000
end

local right_range = rangefinder:distance_cm_orient(2) * 1.0
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
end
– 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) > 1200 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

else if arming:is_armed() and rc:get_pwm(rc_channel_switch) < 1700 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
target_turn_rate = math.deg(omega_radps * direction)
end
– 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()

1 Like