So basically what you have to find are the limits left/right that your servo has to turn to achieve the desired range of the needle.
Usually an SG90 can handle 180(90 degrees left or right), but your needle on the gauge to move from from 0 to 4 has to turn over 240 degrees, hence the use of gears.
You stated earlier that the gear reduction was 2.5, so 1 complete turn of the servo should yield to 2.5 turn of the needle.
The Sg90 is mechanically limited to half a turn (180 degrees) only, so the needle maximum « theoretical » range can be 1.25 turn.
Our needle goal is maximum 240 degrees which is 0.66th of a full turn (240/360=0.666), roughly half of the max servo capacity of 1.25 turn (with this gear reduction) ie 90 degrees total range of movement of the servi
So the max left/right positions of the servo for our particular application should be -45/+45.
It is now up to you to limit your max/min duty cycle to these values to have the desired servo rotation and needle rotation.
For the SG90 we have a frequency of 50Hertz for a cycle of of 20ms (1000/20=50hz) and the full duty cycle is ranging between 1.0 and 2.0ms.
But first we have to convert the Duty cycles durations into percentages, since this is the way it is handled in AM.
Hence the duty cycle value will be of 5% or 0.05 (1ms/20ms=0.05) as a minimum (-90 degrées) and of 10% or 0.1 (2ms/20ms=0.1) as a maximum (+90degrees)
In AM the duty cycle values of PWM for our specific type of servo will hence vary between 0.05 (full left) and 0.1(full right) , giving a Delta value of (Max value-Min Value) of (1.0-0.05)=0.05.
But in our specific example we will issue commands for only +/-45 so +/-0.25ms around the 1.5ms midrange value.
So we end up with a duty cycle ranging only from 0.0625 (1,25/20) up to 0.0875 (1.75/20) for our application. This gives a new Delta range of 0.0250 (0.0875-0.0625)
The max brake press PSI value returned by PMDG is 4000, so 1 PSI =0.0250/4000 which will be the increment value added to the min value of 0.0625 (position 0 of the needle)
So the following code should work, unless other problems due to the servo physical position not being at -45 when the needle is indicating 0, which was assumed here. But at least your needle should no longer make two full turns or more!
Code: Select all
-- Add the TowerPro sg90 servo motor to the script
pwm_brake_press = hw_output_pwm_add("Brake Press servo", 50, 0.0625) — sends the needle to zero
function new_brake_press(bp)
hw_output_pwm_duty_cycle(pwm_brake_press, ((0.0250/4000) * bp)+0.0625)
end
xpl_dataref_subscribe("laminar/B738/brake/brake_press", "INT", new_brake_press)