Lua: No scripts to run (even after repetition and not even hello world is produced)

Hi everyone, I would like to ask your help or opinion on our Lua file-related issue.

We intend to run a Lua script via Ardupilot (4.07 version) to lower or pull up a 1,5m-long tube by a servo motor. After running the script, we receive the error message below, even if we repeat it several times, the same message appears. (The servo motor functions properly but we cannot get there to make it work by the script)

image

By any chance, do you have any idea how we could at least launch the script?

Apparently, it doesn’t even find it. Folder structure with file position is attached as well.

image

Thanks a lot in advance!
Alex

can you post the script. It will give a error message, but it happens very soon after boot, you have to be quick to catch it on USB.

1 Like

Hi Peter, thanks for the quick turnaround.

Does what you just described above happen when there is constant USB connection to the Cube? As we kept the USB connection during the whole time and still did not see any reaction.

Lua script:

local pos_vertical = 2230
local pos_horizontal = 680

local pos_disarmed = pos_vertical
local pos_current = pos_disarmed

local step_size = 4
local step_increment = (pos_vertical - pos_horizontal) / step_size
local step_delay = 2000 -- in ms
local servo_number = 9
local rc_in = 9
local safe_alt = 500 


function check_rc()
	local rc_value = rc:get_pwm(rc_in)
	if (rc_value) then
		if (rc_value > 1900) then
			return "vertical"
		elseif (rc_value < 1200) then
			return "horizontal"
		end
		return "none"
	end
end


function update()
	if arming:is_armed() then
		local loc = ahrs:get_position()
		local home = ahrs:get_home()
		if (loc) then
			if ((loc:alt() - home:alt()) > safe_alt) then
				-- safe altitude reached
				switch = check_rc()
				if (switch == "vertical") then
				-- Moving to Vertical position
					--gcs:send_text(0,"Moving to vertical")
					if (pos_current < pos_vertical) then
						pos_current = pos_current + step_increment
						SRV_Channels:set_output_pwm_chan(servo_number,math.floor(pos_current))
						--gcs:send_text(0,"Servo pos" .. pos_current)
					end
				-- ***********
				elseif (switch == "horizontal") then
				-- Moving to horizontal position
				    --gcs:send_text(0,"Moving to horizontal")
					if (pos_current > pos_horizontal) then
						pos_current = pos_current - step_increment
						SRV_Channels:set_output_pwm_chan(servo_number,math.floor(pos_current))
						--gcs:send_text(0,"Servo pos" .. pos_current)
					end
				-- **********
				elseif (switch == "none") then
				    -- do nothing if switch is at center
				end
		
			end
		end

	else
	  -- if we are not armed and position is not pos_initial then we move it back
	  if (pos_current < pos_disarmed) then 
		pos_current = pos_current + step_increment
		SRV_Channels:set_output_pwm_chan(servo_number,math.floor(pos_current))
	  else
		SRV_Channels:set_output_pwm_chan(servo_number,math.floor(pos_disarmed))
	  end
	end

 return update, step_delay
end
return update()

You have two scripts directories. Delete the one in the root directory. I can’t be 100% certain, but I think a somewhat recent update allows scripts to be read from the higher level directory for dev purposes.

Unrelated to your errors, the check_rc() function is poorly written. You should probably set and return some “constants” to indicate PWM state and ensure that a value is returned in all cases, like this:

local NO_VALUE   =-1
local HORIZONTAL = 0
local VERTICAL   = 1

function check_rc()
    local rc_value = rc:get_pwm(rc_in)
    if (rc_value) then
        if (rc_value > 1900) then
            return VERTICAL
        end
        if (rc_value < 1200) then
            return HORIZONTAL
        end
    end
    return NO_VALUE
end

Technically, there’s a fourth state that you could call NEITHER if you wanted to differentiate between the case where a PWM value is present and simply not HORIZONTAL or VERTICAL vs the case where the rc:get_pwm() function doesn’t return a value (if that’s even possible). But the rest of your script would do nothing with that value as written.

You’d then change the update() function’s logic accordingly. The last elseif in the update() function is completely unnecessary unless it’s a placeholder for some sort of future functionality in the case of a “none” return value.

1 Like