Lua Script control map error

I have not tried to run a lua script on the Cube before so please let me know if I have missed a step. I am trying to limit the output on the servo channels assigned to the elevon left and right when using the elevator. This is the code I thought would work but I am getting a control map error in MP.

My code I would like to get working:
( I added in a few extra text strings to see if it would push through the messages)

local scripting_rc_1 = rc:find_channel_for_option(300)
local scripting_rc_2 = rc:find_channel_for_option(301)

function update()
pwm1 = rc:get_pwm(1)
pwm2 = rc:get_pwm(2)
pwm3 = rc:get_pwm(3)
pwm4 = rc:get_pwm(4)
gcs:send_text(0, “RCIN 1:” … tostring(pwm1) … " 2:" … tostring(pwm2)… " 3:" … tostring(pwm3)… " 4:" … tostring(pwm4))

– Define input and output channels
local channel1 = 1
local channel2 = 2
local channel4 = 4

– Define input value ranges
local range1 = {1100, 1500}
local range2 = {1501, 1900}

– Read input values
local input1 = pmw1
local input2 = pmw2

– Process channel 1 input
if input1 >= range1[1] and input1 <= range1[2] then
– Input value is in range, output on channel 1
servo.set_output_pwm(78, input1)
elseif input1 >= range2[1] and input1 <= range2[2] then
– Input value is in range, output on channel 4
local output4 = input1 – Output value on channel 4 is the same as input value
servo.set_output_pwm(78, 1500)
servo.set_output_pwm(77, output4)
end

– Process channel 2 input
if input2 >= range1[1] and input2 <= range1[2] then
– Input value is in range, output on channels 1 and 4
local output1 = input2 – Output value on channel 1 is the same as input value
local output4 = input2 – Output value on channel 4 is the same as input value
servo.set_output_pwm(78, output1)
servo.set_output_pwm(77, output4)

elseif input2 >= range2[1] and input2 <= range2[2] then
– Input value is in range, output fixed value on channels 1 and 4
servo.set_output_pwm(78, 1500 )
servo.set_output_pwm(77, 1500)

return update, 1000 – reschedules the loop
end

return update()

That post is nearly impossible to read, but the common errors are:

Not placing the script in the proper directory.
Not saving the SCR_ENABLE parameter after setting it to 1.
Accidentally allowing Windows to add a .txt extension (the file must end in .lua).

If all of that is done, and the script is actually running, then post the exact error message from Mission Planner.

And encapsulate your posted code in code tags so it’s actually legible.

2 Likes

4/18/2023 5:03:55 PM : .lua:49
4/18/2023 5:03:55 PM : PreArm: Scripting: Error: /APM/scripts/Small Trial

I am not sure where I can find what the lua:49 error means. The goal is to write a Lua script to take the input of channel 1 and output the channel 1 input value on channel 1 from value 1100-1500. The value from 1501 to 1900 will take the input from channel 1 and output on channel 4 using the input value. The input of channel 2 from 1100-1500 will output on channels 1 and 4. The input of channel 2 from 1501-1900 will result in an output of 1500 on channel 1 and 4. I am not a programmer so this is new to me.

The error is on line 49.

Otherwise, we aren’t communicating very well. I cannot follow the logic you’re trying to describe.

Your script is riddled with errors, and even after combing through them a little, I still don’t understand what it’s trying to accomplish. If you’re trying to do some sort of elevon mixing, there’s probably a better way than this (which will only ever be truly used in manual mode and will very inelegantly override any automated stabilization or control).

  • Avoid using spaces in your filename.
  • You’re missing an end statement for the last if condition.
  • servo.set_output_pwm() is not a valid method. I think you’re looking for SRV_Channels:set_output_pwm()
  • pwm1 through 4 are needlessly declared as global variables
  • There are several instances of pwm misspelled as pmw
  • channel1 through 4 are declared and assigned but never used.
  • Your script is set to trigger at 1Hz, which is probably way too slow to be useful

Please direct me to examples I can learn from.

I based this off of the example from github.

I think your first stop should be here:
Elevon Planes — Plane documentation (ardupilot.org)

I would still like proper examples to begin learning the fundamentals of scripts. I appreciate the help I just want better direction of good examples to learn from.

The entire examples directory on GitHub is chock full of them. The titles are fairly descriptive.