The 3D printed face plate for the flap control limits the range of motion of the indicator arm so the function scales the 0 to 1 input from the flight simulator to the reduced range of motion of the indicator. I also applied a log function to the indicator range to match the log scale flap leaver stops on the flap control.
Not sure if this could be improved but if anyone is interested this is the code that works for me:
Code: Select all
-- Flap Position Indicator for Cessna 172
---
-- Add the servo motor to the script
-- 50 hertz for micro servo 9g S51
-- 0.021 dutycycle initially or no flap extension
pwm_flap_pos = hw_output_pwm_add("Flap Indicator servo", 50, 0.021)
-- In this case the duty cycle ranges from 0.02 to 0.10
-- We have flaps that go from 0 to 1.0 (full extension)
function new_pos(pos)
-- configurable parameters based on the servo being used.
usable_fraction = 0.20 -- the fraction of the available range of motion for the attached indicator arm.
start_of_range = 0.021 -- the starting dutycycle. When attaching the indicator make sure it is near the start of the servo range.
end_of_range = 0.1 -- the maximum dutycycle of the servo.
dutycycle_range = (end_of_range - start_of_range)
-- scale the position 0..1 to the dutycycle range 0.02..0.1.
-- y = m * x + c or y = 0.08 * x + 0.02
x = dutycycle_range * pos + start_of_range
-- scale x to a log scale and subtract that from the max dutycycle ie. end_of_range
y = end_of_range - (dutycycle_range *(math.log(x)/math.log(dutycycle_range))) + start_of_range
-- scale the result to the usable range of motion for the servo & add the base offset for the start of the range.
y = (y * usable_fraction) + start_of_range
-- Clamp values to physical limits.
if y <= 0.021 then y = 0.021 end
if y >= 0.031 then y = 0.031 end
hw_output_pwm_duty_cycle(pwm_flap_pos, y)
end
xpl_dataref_subscribe("sim/flightmodel2/controls/flap1_deploy_ratio", "FLOAT", new_pos)