Ok I have an update on what I sought out to accomplish and it looks very good,
I rewrote the script to incorporate greater safety features so that the likelihood of a crash or a runaway was reduced substantially. The features of the code are as follows,
Precautions
-Our experiment of seeing if the RC override was going to work would be done using a switch on a FrSky. When the switch is toggled we’ll want to perform the experiment.
-We also want the experiment to only be performed once, we don’t want to accidentally flip the switch again.
-We want to be able to stop the experiment when the switch is turned off incase anything goes wrong.
-We want the experiment to stop after a certain amount of time
-We want to have some safeguard in place in case the switch is already in the ON position, as we don’t want to accidentally start the experiment on bootup
-We want the script to not run when the battery failsafe is triggered
-We want the script to not run when the GCS failsafe is triggered
Code
-- Purpose
-- The purpose of this script is to test the RC throttle override for a quadplane which in QLoiter
-- We want to just see if by programatically overriding the throttle channel that it will cause the
-- craft to increase it's altitude while in QLoiter.
--
-- For the desire of safety several key precautions have been incorporated in this script.
-- 1) The Lower Right switch on the FrSky remote controller will be used to control RC channel3
-- 2) When the switch is toggled from OFF to ON the experiment will start
-- 3) We want the experiment to only last for a certain amount of time (controlled by timer_stop_threshold)
-- 4) When the switch is toggled from ON to OFF we want the experiment to stop
-- 5) When the experiment stops for any reason, either from a timer elapsing or from toggling the switch from
-- ON to OFF, the experiment cannot be performed again. This will be a one and done experiment.
-- 6) Impliment a warning system that prevents the script from running if the switch is in the ON state when
-- the system boots up. This is so that the experiment doesn't start immediately after bootup by accident
-- 7) We want to stop the script from running if the battery failsafe triggers
-- 8) We want to stop the script from running if the GCS failsafe triggers
--
--
release_triggered = true -- This flag toggles when the RC switch is released
experiment_performed = false -- This flag toggles when the experiment is performed
warning_event = false -- This flag toggles when some wanring event occurs, i.e. a failsafe or if the switch is toggled during start up
switch_radio_channel = 6
-- This method is run if the switch is detected to be on during start up, we don't want the switch to
-- be on during start up because that would start the experiment immediantly. So we'll put a guard here to
-- prevent the script from running
function event_switch_warning()
gcs:send_text(5, "Entering Warning Area")
if(rc:get_pwm(switch_radio_channel) <= 1600) then -- When the switch is in the ON state
gcs:send_text(5, "WARNING SWITCH IS ALREADY IN TRIGGER STATE, RESTART WITH SWITCH IN NON-TRIGGER STATE")
warning_event = true -- Toggle warning flag
end
return update, 100
end
-- This method initializes all the relevant experimental variables
function experiment_init()
count = 0 -- Used to keep track of the timer
timer_stop_threshold = 5 -- Value in seconds, when timer should stop
RC3 = rc:get_channel(3) -- The RC channel we wish to throttle
throttle_perc = 75 -- Throttle as a percentage
throttle_value = 8*throttle_perc + 1100 -- This is the throttle value we wish to use
end
-- This is the code for the actual experiment to run in
function experiment()
if(count <= (timer_stop_threshold-2)*10) then -- Check if the timer has run out, if it hasn't run the experiment
gcs:send_text(5, "Throttling Up") -- Indicate action
RC3:set_override(throttle_value) -- Perform the throttle override
else
experiment_performed = true
end
count = count + 1 -- Increment timer counter
end
function exit_script()
gcs:send_text(5, 'Experiment Finished')
end
-- This is the update method this will run every 100ms
function update()
--endgcs:send_text(5, 'In Loop')
--if(warning_event) then
-- gcs:send_text(5, 'True')
--else
-- gcs:send_text(5, 'False')
--end
-- This forces the script to wait until the AHRS system is initialized
if not ahrs:healthy() then
gcs:send_text(5, 'Script awaiting AHRS initialization...')
return update, 100
end
-- This checks if the vehicle is in battery failsafe mode
-- Once this is triggered and the flag is set this will prevent the script below from running
-- it cannot be reset without a reboot
if(vehicle:get_control_mode_reason() == 4) then
gcs:send_text(5, 'Battery Failsafe triggered, stopping script')
warning_event = true -- Toggle the warning flag if we're in battery failsafe mode
end
-- This checks if the vehicle is in GCS failsafe mode
-- Once this is triggered and the flag is set this will prevent the script below from running
-- it cannot be reset without a reboot
if(vehicle:get_control_mode_reason() == 5) then --GCS Failsafe
gcs:send_text(5, 'GCS Failsafe triggered, stopping script')
warning_event = true -- Toggle the warning flag if we're in GCS failsafe mode
end
-- If the warning event flag is ever toggled then do not run the following script
if(not warning_event) then
-- When the switch is in the ON position then
if(rc:get_pwm(switch_radio_channel) <= 1600) then
-- If the experiment hasn't been performed yet
-- Once the experiment is done we won't be able to do it again with out a reboot
if(not experiment_performed) then
experiment() -- Perform the experiment
end
release_triggered = false -- Toggle the release flag to tell the system the switch has been turned OFF
end
-- This will only trigger when the release flag is toggled and if the switch is turned OFF
-- Essentially triggering only after the switch has been turned OFF
if((rc:get_pwm(switch_radio_channel) > 1600) and (not release_triggered)) then
experiment_performed = true -- After releasing the switch the experiment should be stopped and not performed again
release_triggered = true -- Toggle the release flag to indicate that the switch has been released
end
end
if(experiment_performed) then
--gcs:send_text(5, 'Experiment Finished1')
return exit_script, 1000
end
--if(not experiment_performed) then
-- return update, 100
--end
return update, 100
end
-- Initialize experiment variables
experiment_init()
-- Check to see if the switch is ON before we start, it cannot be allowed to be on
return event_switch_warning, 3000
Experiments
So currently I have done 3 experiments before I test it on a quad plane in a real scenario.
- First experiment I did was in SITL
- Second experiment I did was on a real quad plane but with the propellers off
- Third experiment I did was on a real quad copter
I won’t go into detail about the experiments because they’re all straight forward, you upload the code, flip the switch and then you see if it goes up or not.
In SITL I did this and it went up.
In the second experiment I did this and we saw that the quad motors increased in throttle when the switch was toggled.
In the third experiment I did the switch was toggled and the quad copter went up in the air so that also worked.
The next step is to do the experiment with the quad plane with the propellers on in the air. From all the tests that were performed it’s essentially guaranteed that it will work, but it’s good to do these tests on other crafts to make sure nothing horrendous happens.