# SetPulse method on PulseInOut module

I have been using the ZX servo16 in a number of FEZ projects running up to 16 servos, and it has worked very well.

The driver that I used in those projects is located here: https://www.ghielectronics.com/community/codeshare/entry/27

Now I am trying to move to the gadgeteer boards and replace the ZX Servo16 with the PulseInOut (or if that doesn’t work, get the old servo16 driver to work with the Gadgeteer boards).

The Servo16 driver has a SetServoPosition method that takes the channel on the servo16 board that the servo is connected to, the ramp (how fast you want it move from the current position to new position), and the angle that you want it to move to - typically between 0 and 180 degrees.

The PulseInOut on the other hand, has a SetPulse method that takes an ID, a period, and a highTime.

How exactly do you use the SetPulse method to set the ramp and the angle?

Attempting to answer my own question - this entry in Wikipedia is helpful. Servo control - Wikipedia

According to Wikipedia, a 1.5ms pulse is the neutral position (90 degrees). So this probably corresponds to a highTime of 1500.

Looking at the sample code in the driver, it has a for loop that goes from 1100 to 1900. So maybe 1100 is 0 degrees, and 1900 is 180 degrees… Probably add a method in my code to translate this into degrees.

Still not sure how to set the ramp…

Reading around a bit more, the standard for rc servo’s seems to be 1ms for minimum (0 degrees), and 2ms for maximum (180 degrees). But it can vary depending on the servo, so this should be something put into the configuration for each channel. I see some brushless ESCs that have a throttle range from 1.1 to 1.9ms - which is what the test code is using.

Also note that the test code sets the pulse frequency at 50Hz. But this method is commented out in the code. So it is unclear what pulse frequency is being used.

From reading around I see that analog servos require a signal from 30Hz to 50Hz, with 50Hz being typical. But digital servos can run up to 300 to 400Hz with 300 being typical. You can run a digital servo at a lower frequency, you just lose most of the benefit of having a digital servo.

The ESCs for helicopters typically require 400Hz. Reports abound of helis being unstable when trying to use ESCs that can only update at 50Hz.

The method for setting the pulse frequency (if it wasn’t commented out) doesn’t take a channel, so I assume this is a global setting. So I guess you have to use the lowest common denominator - meaning you have to run at 50Hz if you have any analog servos. You can only run higher if all of your servos and ESCs are capable of the higher frequency.

Thinking aloud some more, it appears that the middle parameter, period, perhaps goes by the little formula in the code that says: period = 1000000 / freqHz

So if you want to set the channel to 50Hz, you set the period to 20,000…

If you want to set the channel to 400Hz, you set the period to 2500…

Still don’t see any way to set the ramp.

Which I guess means the servo will go as fast as it can between settings… Of course you could write a For loop that transitions the signal over a set interval, but then that code would be running and using resources on the gadgeteer board, when you really want it to run on the PulseInOut…

@ hvelo - I don’t have a PulseInOut module, so I cannot confirm this, but looking at the code for the driver

SetPulse timings are specified in microseconds (us), so as you have determined, a 50Hz signal has a period of 20,000us, the neutral high time will be 1,500us

You will need to implement ramp yourself, looking at the ZX Servo 16 datasheet the ramp is the time taken for the servo to move from it’s current position to the target position. The ZX Servo 16 has 63 ramp rates ranging from 0.75 seconds to 60 seconds.

The way I understand the documentation on the ramp function is that the ramp time is the time the servo will take to cover the full 180 degree range. You will need to manually implement this functionality by calculating the delta from your current position to the new target position and then move the servo ie. change the high time incrementally at a rate that will achieve the target position within the specified ramp time.