Hello, I’m currently attempting to configure a a flow sensor to be able to turn off my mosfet switch for a sprayer tank on my Pixhawk 6c.
Right now I’m just trying to test to get an out from the flow sensor, and push output of voltage change onto my mosfet whatsoever and having no success.
Test Code(working):
is_on = false
output_function = 94
-- Function to toggle the servo state
toggle_pin = function()
local status, err
if is_on then
status, err = pcall(function()
SRV_Channels:set_output_pwm(output_function, 0) -- Set servo to 800 PWM
end)
gcs:send_text(6, "FLAGA: " .. tostring(status))
gcs:send_text(6, "OUTPUT: " .. tostring(SRV_Channels:get_output_pwm(output_function)))
else
status, err = pcall(function()
SRV_Channels:set_output_pwm(output_function, 2000) -- Set servo to 2000 PWM
end)
gcs:send_text(6, "FLAGB: " .. tostring(status))
gcs:send_text(6, "OUTPUT: " .. tostring(SRV_Channels:get_output_pwm(output_function)))
end
-- Handle any errors
if not status then
gcs:send_text(6, "Servo error: " .. tostring(err))
else
is_on = not is_on -- Toggle the state
gcs:send_text(6, "Toggling servo state: " .. tostring(is_on))
end
end
-- Periodic function to be called by ArduPilot
loop = function()
toggle_pin() -- Toggle the servo state
return loop, 5000 -- Re-run this function every 1000 ms
end
return loop, 5000 -- Initial return to set the loop function and interval
For the Mosfet Port:
I have it on I/O PWM 8, with the lua script running on 94, and it is correctly changing the PWM values just fine(right now I have it from 0 and 2000) in attempts to turn the voltage on/off(so I can have it work for the mosfet switch eventually, but right now its just some wires out putting and testing on a multi meter. But even then the voltage is just always staying the same. (Also its only pulling ~.02 to 0.033 V not the supposed to be 3.3 V)
For the Flow Sensor Port:
I have it on the I/O PWM 7, with the lua script running on 56(RCIN 6) as it is outputting a square wave pulse output signal.
But currently even when I have it all hooked up, its reading the PWM of zero on the port(as in correctly configured in mission planner to RCIN 6). But whenever I put water through the sensor its not reading the flow rate, or even the frequency whats so ever currently. Just stays at flat 0’s for both which doesn’t seem right, as when we test the flow sensor separately it’s properly outputing.
Test Code(just outputing 0’s for both):
-- Specify servo function for the flow sensor
local flow_sensor_channel = 56 --56 -- RCIN 6
local servo_function = 95 -- SCRIPT_2
-- Initialize variables
local first_run = true
-- Function to calculate flow rate from frequency
local function calculate_flow_rate(frequency)
-- Flow rate (Q) = Frequency (F) / 23
local flow_rate = frequency / 23
return flow_rate
end
-- Function to convert PWM signal to frequency
local function pwm_to_frequency(pwm)
-- Assuming PWM in microseconds: frequency = 1 / period
-- period (seconds) = pwm (microseconds) / 1,000,000
-- frequency (Hz) = 1 / period
local period = pwm / 1000000
if period > 0 then
return 1 / period
else
return 0
end
end
-- Main update function
function update()
if first_run then
gcs:send_text(0, "LUA: Flow sensor script started...")
first_run = false
end
-- Get PWM value from the specified channel
local pwm_value = SRV_Channels:get_output_pwm(flow_sensor_channel)
if pwm_value == nil then
gcs:send_text(2, "LUA: Failed to read flow sensor channel.")
else
gcs:send_text(0, string.format("LUA: PWM Value: %.2f", pwm_value))
-- Convert PWM to frequency
local frequency = pwm_to_frequency(pwm_value)
-- Calculate flow rate and send to GCS
local flow_rate = calculate_flow_rate(frequency)
gcs:send_text(0, string.format("LUA: Flow rate: %.2f L/min", flow_rate))
end
return update, 1000 -- Update every 1 second
end
return update()
Message Console Output:
The one big issue we are realizing currently could be the fact it is only outputting such a low voltage, as the flow sensor needs 3 to 24 VDC to be powered in general. So if that likely seems to be the issue, then that’s a whole different issue to attack as well, so open for ideas on that as well.