## Servo Duty cycles.

Support for Arduino in combination with Air Manager and Air Player

Moderators: Ralph, russ

Message
Author
JackZ
Posts: 1852
Joined: Mon Feb 22, 2016 1:02 pm

### Re: Servo Duty cycles.

@Keith Baxter I think your main problem here is that your gauge is using reduction gears to move the needle of your 737 brake press gauge, correct?
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)``````
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):

Keith Baxter
Posts: 2822
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

### Re: Servo Duty cycles.

@JackZ

Thank you for the input.
I had worked that out. Your effort will not go unnoticed and sure appreciated by us all.

This is the code for the CockpitSimParts Brake pressure gauge.

Code: Select all

``````-- Code for the CockpitSimParts Brake Pressure Gauge.

-- The dial scale go's from 90º to 330º which is 66.6666% of 360º   (6 O'clock to 2 O'clock)
-- The instrument is geared 40-16 (2.5x) so that 400µs is a 360º rotation of the pointer.
-- Therefore 66.6666% of 400µs = 266.664µs

-- Variables to rotate the pointer 240º
local frequency              = 50
local percent_start          = 0.02
local percent_travel         = 0.0266664
local dial_range             = 4000

-- Add the TowerPro sg90 servo motor to the script
pwm_brake_press = hw_output_pwm_add("Brake Press servo", frequency, percent_start)

--Rotate the servo using the sim dataref.
function new_brake_press(bp)

hw_output_pwm_duty_cycle(pwm_brake_press, percent_start + ((percent_travel/dial_range) * bp))

end

xpl_dataref_subscribe("laminar/B738/brake/brake_press", "INT", new_brake_press)

``````

This is the code for the CockpitSimParts Flap Indicator gauge.

Code: Select all

``````-- Code for the CockpitSimParts Flap Indicator Gauge.

-- The dial scale go's from 180º to 90º which is 75% of 360º   (9 O'clock to 6 O'clock)
-- The instrument is geared 40-16 (2.5x) so that 400µs is a 360º rotation of the pointer.
-- Therefore 75% of 400µs = 300µs (travel)

-- Variables to rotate the pointer 270º
local frequency              = 50
local percent_start          = 0.02
local percent_travel         = 0.030
local dial_range             = 1

-- Add the TowerPro sg90 servo motor to the script
pwm_flap_indicator = hw_output_pwm_add("Flap Indicator servo", frequency, percent_start)

--Rotate the servo using the sim dataref.
function new_flap_pos(deploy)
hw_output_pwm_duty_cycle(pwm_flap_indicator, percent_start + ((percent_travel/dial_range) * deploy))
end

xpl_dataref_subscribe("sim/flightmodel2/controls/flap_handle_deploy_ratio","FLOAT", new_flap_pos)

``````

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card

jph
Posts: 666
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

### Re: Servo Duty cycles.

Hi Keith //
@JackZ

Thank you for the input.
I had worked that out. Your effort will not go unnoticed and sure appreciated by us all.
I am sorry I was of absolutely no help, I will try harder next time.....
“Everything in moderation, including moderation.”
“Experience is merely the name men gave to their mistakes.”
Joe (CISSP) -IT security consultant

Keith Baxter
Posts: 2822
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

### Re: Servo Duty cycles.

jph wrote:
Mon May 17, 2021 10:28 am
Hi Keith //
@JackZ

Thank you for the input.
I had worked that out. Your effort will not go unnoticed and sure appreciated by us all.
I am sorry I was of absolutely no help, I will try harder next time.....
Ha Ha,

I was replying to my other cousin dude. Giving him a warm fuzzy.

Here is a warm fuzzy for you Joe, and everyone else that added input. The input from all, spread a lot of light on the subject and hopefully makes it easier for others to understand. Thank you.

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card

jph
Posts: 666
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

### Re: Servo Duty cycles.

Keith Baxter wrote:
Mon May 17, 2021 10:38 am
jph wrote:
Mon May 17, 2021 10:28 am
Hi Keith //
@JackZ

Thank you for the input.
I had worked that out. Your effort will not go unnoticed and sure appreciated by us all.
I am sorry I was of absolutely no help, I will try harder next time.....

Ha Ha,

I was replying to my other cousin dude. Giving him a warm fuzzy.

Here is a warm fuzzy for you Joe, and everyone else that added input. The input from all, spread a lot of light on the subject and hopefully makes it easier for others to understand. Thank you.

I thank you for the consideration haha .
'and everyone else that added input'
appear to be myself, Jacques and a a post from Ralph.
Glad you got it sorted brother.
We will 'boldly go' (even though Kirk was using a split infinitive' ) - bloody yanks - .. ... Onto the next challenge
“Everything in moderation, including moderation.”
“Experience is merely the name men gave to their mistakes.”
Joe (CISSP) -IT security consultant

### Who is online

Users browsing this forum: No registered users and 5 guests