Lua script not logging sensor data

Hi,
I’m trying to stop the motor when RPM threshold reached, using Lua script. Now I’m able to read the RPM value in QUICK, meaning the sensor works fine. But when I run the script it shows " RPM value not available" as per script, in the messages. Could you guide me with what I have missed ?

The RPM sensor is connected to AUX PWM pin1 (GPIO) and powered by ESC BEC connected to PWM AUX pin2 , SERVO10_FUNCTION = motor1.
SCR_ENABLE is enabled.

local motor_channel = 10 -- Channel for the motor 
local rpm_threshold = 100 -- RPM threshold for disengagement 

function update()
    local rpm_value = RPM:get_rpm(1) -- Read the RPM value from instance 1
    if rpm_value then
        gcs:send_text(6, "RPM Value: " .. tostring(rpm_value))
        if rpm_value >= rpm_threshold then
            gcs:send_text(6, "RPM threshold reached, disengaging motor")
            SRV_Channels:set_output_pwm_chan_timeout(motor_channel, 1000, 1000) -- Stop the motor
        end
    else
        gcs:send_text(6, "RPM Value not available")
    end
    return update, 1000 -- Schedule the update function to run again in 1000 milliseconds
end

return update, 1000 -- Schedule the update function to run for the first time in 1000 milliseconds

Thanks

I suspect you want instance 0.

local rpm_value = RPM:get_rpm(0)

Internally (and thus in scripting) we tend to be 0 indexed. Whereas on the GCS side things are presented as 1 indexed to be more user friendly.

2 Likes

It works, big thank. I spent two days trying to fix it :slight_smile: