LUA Servo BIT Scripting Difficulty

Hello,

I am trying to get a servo to perform a BIT using LUA and I am struggling. The objective is to get a servo to sweep between two specified PWM values at a moderate speed to show that the servo is unimpeded and functioning as expected. I have tried using both a dedicated series of functions as well as a FOR loop, neither have worked. With regard to the functions, I suspect that they are each too short and since some of them are recursive, it causes a failure and the script breaks down. With regard to the FOR loop, the script itself runs just fine but the PWM commands do not make it to the servo, it sits stationary at the value which I set it to beforehand.
Any help is appreciated!

Here are examples of the functions and the FOR loop:
**SERVO_TRIM and SERVO_BIT are custom parameters set up in my script as required, they work fine.

Functions:

servo_pwm_lower_limit = 1100 --prevent from going lower than 1100 to avoid hardware collision
function BIT_prep()
gcs:send_text(6,“entered BIT_prep”)
BIT_pwm_dn = param:get(SERVO_TRIM")
servo_trim = param:get(“SERVO_TRIM”)
BIT_pwm_up = servo_pwm_lower_limit
return BIT_dn()
end

function BIT_dn()
if BIT_pwm_dn > servo_pwm_lower_limit then
BIT_pwm_dn = BIT_pwm_dn - 1
SRV_Channels:set_output_pwm_chan_timeout(9, BIT_pwm_dn, 20)
return BIT_dn(), 1000
else
return BIT_up()
end
end

function BIT_up()
if BIT_pwm_up < servo_trim then
BIT_pwm_up = BIT_pwm_up + 1
SRV_Channels:set_output_pwm_chan_timeout(9, BIT_pwm_up, 20)
return BIT_up(), 1000
else
return BIT_complete()
end
end

function BIT_complete()
gcs:send_text(6,“completed BIT”)
param:set_and_save(“SERVO_BIT”,0)
gcs:send_text(6,“Reset BIT parameter”)
end

FOR Loop:

servo_pwm_lower_limit = 1100
if param:get(“SERVO_BIT”) == 1 then
local BIT_pwm_dn = param:get(“SERVO_TRIM”)
local BIT_pwm_up = servo_pwm_lower_limit
for i = BIT_pwm_dn,BIT_pwm_up do
SRV_Channels:set_output_pwm_chan(9, i)
end
for j = BIT_pwm_up,BIT_pwm_dn do
SRV_Channels:set_output_pwm_chan(9, j)
end
gcs:send_text(6,“completed BIT”)
param:set_and_save(“SERVO_BIT”,0)
gcs:send_text(6,“Reset BIT parameter”)
end

I figured it out, the problem was that I was including “()” after my recursive function calls. This prevents the loop from being carried out at the specified interval.

1 Like