I recently got to know Lua Script through Yuri’s video and thanks to that, I was able to experiment with the 3 Position switch Demo using a USB gamepad. (I haven’t had a real drone and controller yet)
Next, I wanted to check the button input of the USB gamepad, so I tried the button_test example code, but it doesn’t work as expected.
RC6 is for RCIN test, But 1 is for button_test, USB GamePad and MissionPlanner are connected as above.
(For flight simulation, since RC1~RC4 are connected to the analog direction control stick of the joystick, RC6 and RC3 react simultaneously. RC3 is reversed)
--[[ Testing Environment
PC Windows10
USB GamePad Generic USB Joystick like Logitech F310
GCS MissionPlanner 1.3.80 build 1.3.8479.20539
SITL ArduCopter V4.5.0-dev(a47e125c)
]]
---------------------------------------------------------------------------------
--[[ Preparing for RCIN Testing
- Set the SCR_ENABLE parameter to 1 in MissionPlanner/CONFIG/Full Parameter List.
- Set the RC6 OPTION parameter to 300 in MP/CONFIG/Full Parameter List.
- Bind throttle stick of usb gamepad to RC6 in MP/Action/Joystick settings
- Confirm RC6 Overriding in MP/SETUP/Mandatory Hardware/Radio Calibration
]]
local rc_chan = rc:find_channel_for_option(300)
function rcin_test()
local sw_pos = rc_chan:get_aux_switch_pos() -- returns 0 or 1 or 2. 3 Position switch
local pwm = rc:get_pwm(6)
gcs:send_text(6, string.format('Lua: rc_switch pos %s pwm %s ',sw_pos,pwm) )
end
--------------------------------------------------------------------------------
--[[ Preparing for Button Testing
- Bind button 1 of the USB GamePad to But1 in the mission planner joystick settings
- It seems that in MP/SETUP/Mandatory Hardware/Radio Calibration,
the RC Overriding of button 1 bound in the Joystick settings cannot be confirmed.
]]
local button_number = 1 -- the button numbber we want to read, as deffined in AP_Button
local button_active_state = true -- the 'pressed' state of the button
local last_button_state
function button_test()
local button_new_state = ( button:get_button_state(button_number) == button_active_state )
gcs:send_text(6,string.format('Lua: button state: %s',button_new_state))
-- the button has changes since the last loop
if button_new_state ~= last_button_state then
last_button_state = button_new_state
if button_new_state then
gcs:send_text(0, "Lua: Button pressed")
else
gcs:send_text(0, "Lua: Button released")
end
end
end
---------------------------------------------------------------------------------
function update() -- this is the loop which periodically runs
rcin_test()
button_test()
return update,1000 -- reschedules the loop (1hz)
end
return update() -- run immediately before starting to reschedule
------------------------------------------------------------------------------------
--[[ Reference
ArduPilot's Documentation
Simulation https://ardupilot.org/planner/docs/mission-planner-simulation.html
Lua Scripts https://ardupilot.org/copter/docs/common-lua-scripts.html
Joystick/GamePad https://ardupilot.org/copter/docs/common-joystick.html
GitHub's Lua Script example
RC IN https://github.com/ArduPilot/ardupilot/blob/master/libraries/AP_Scripting/examples/RCIN_test.lua
button https://github.com/ArduPilot/ardupilot/blob/master/libraries/AP_Scripting/examples/button_test.lua
Yuri's ArduPilot Lua Demos 16 Apr 2022
https://www.youtube.com/watch?v=UdXGXjigxAo&t=781s
]]
test.lua (3.0 KB)
I wrote the script as above, The execution result is as follows.
The positions (0,1,2) and pwm values of the GamePad connected to the RC6 can be read normally, but
When button 1 connected to ‘But 1’ is pressed, contrary to expectations, true is not returned, only false is always output.
(The Arm function connected to ‘But 1’ was executed normally, and the flight simulation was possible.)
Is there anything wrong or missing in my attempt to check the GamePad’s button input with Lua Script?
Hope to get some help on how to check it.