Difference between revisions of "NU32: Driving RC servo motors"

From Mech
Jump to navigationJump to search
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
'''THIS PAGE REFERS TO A PRE-RELEASE VERSION OF THE NU32 PIC32 DEVELOPMENT BOARD. FOR INFORMATION, SAMPLE CODE, AND VIDEOS RELATED TO THE PRODUCTION VERSION (2016 AND LATER), AND TO THE CORRESPONDING BOOK "EMBEDDED COMPUTING AND MECHATRONICS WITH THE PIC32 MICROCONTROLLER," VISIT [[NU32|THE NU32 PAGE]].'''


Driving RC Servo motors with the NU32.
Driving RC Servo motors with the NU32.


Line 10: Line 13:
An easy way to create the pulse is with PWM. We can set a timer (2 or 3) to 50Hz, and then set the OC module to use that timer, and set the duty cycle to only be in the range that will create pulses of at least 0.5ms and at most 2.5ms. This method is fine if you have a spare timer and OC pin available, but limits you to 5 RC servo motors because there are only 5 OC pins. If your project uses several DC motors and several RC motors, you will run out of PWM pins!
An easy way to create the pulse is with PWM. We can set a timer (2 or 3) to 50Hz, and then set the OC module to use that timer, and set the duty cycle to only be in the range that will create pulses of at least 0.5ms and at most 2.5ms. This method is fine if you have a spare timer and OC pin available, but limits you to 5 RC servo motors because there are only 5 OC pins. If your project uses several DC motors and several RC motors, you will run out of PWM pins!


Another way to create the pulses is to do so using a timer interrupt and manually toggling the digital pins. With this method you could control as many RC motors as you want, depending on how you want to set up the code, and what resolution you want to be able to control the RC servo angle to.
Another way to create the pulses is to do so using a timer interrupt and manually toggling the digital pins. With this method you could control as many RC motors as you want, depending on how you want to set up the code, and with what resolution you need to be able to control the RC servo angle.


== Wiring ==
== Wiring ==
Line 18: Line 21:


== Details ==
== Details ==
Try the PWM method first. Set up timer 2 to 50Hz. There are several options for prescaler, use the prescaler so that you have the largest PR value (remember it has to be less than 2^16-1, or 65535). We will see why in a moment.
Try the PWM method first. Set up timer 2 to 50Hz. There are several options for prescaler: use the prescaler so that you have a large PR value (remember it has to be less than 2^16-1, or 65535). We will see why in a moment.


Now turn on the OC1 module (pin D0) using timer 2, and calculate what duty cycle is required to make a 0.5ms pulse. For example, if we use a 1:256 prescaler for timer 2, and PR2 = 6250 for 50Hz, we have a resolution of (20ms / 6250) = 0.0032ms. For a 0.5ms pulse, we would set OC1RS to 157. For a 2.5ms pulse, we would set it to 781. We would not want to set OC1RS outside of the range 157-781, or the motor might try to move to a position outside of the 0-180 degree range, and break itself trying to get there. Now we have 180/(781-157) = 0.29 degree per count of the OC module, which is fine control of the position of the servo motor.
Now turn on the OC1 module (pin D0) using timer 2, and calculate what duty cycle is required to make a 0.5ms pulse. For example, if we use a 1:256 prescaler for timer 2, and PR2 = 6250 for 50Hz, we have a resolution of (20ms / 6250) = 0.0032ms. For a 0.5ms pulse, we would set OC1RS to 157. For a 2.5ms pulse, we would set it to 781. We would not want to set OC1RS outside of the range 157-781, or the motor might try to move to a position outside of the 0-180 degree range, and break itself trying to get there. Now we have 180/(781-157) = 0.29 degree per count of the OC module, which is fine control of the position of the servo motor. If we use a different prescaler for the timer, we would have better resolution (but do you really need sub-degree resolution, and can the (very inexpensive) RC servo achieve those fine resolutions?


The following code implements this technique: [[Media:NU32_rcservo_pwm_example.c|NU32_rcservo_pwm_example.c]]
The following code implements this technique: [[Media:NU32_rcservo_pwm_example.c|NU32_rcservo_pwm_example.c]]


<!--
If your project requires several PWM pins already, maybe for controlling LED brightness or brushed DC motor speed, or you need many RC servos, the above method does not scale. Instead, manually toggle a digital IO pin (or several pins for several motors) in a timer based interrupt. First, pick the motor resolution you need. For example, lets do 1 degree. That will correspond to 2ms * 1 degree / 180 degree = 0.0111ms, or a timer interrupt at 90kHz. The pin needs to be high for at least 0.5ms, or 45 interrupts, and at most 2.5ms, or 225 interrupts (and 225-45 = 180, or resolution).


The following code implements this technique for one motor on pin D13: [[Media:NU32_rcservo_digio_example.c|NU32_rcservo_digio_example.c]]


[[Image:image.png|thumb|600px|image|center]]




[[Image:image.png|thumb|600px|image|center]] -->


== More Information ==
== More Information ==

Latest revision as of 05:38, 16 January 2016

THIS PAGE REFERS TO A PRE-RELEASE VERSION OF THE NU32 PIC32 DEVELOPMENT BOARD. FOR INFORMATION, SAMPLE CODE, AND VIDEOS RELATED TO THE PRODUCTION VERSION (2016 AND LATER), AND TO THE CORRESPONDING BOOK "EMBEDDED COMPUTING AND MECHATRONICS WITH THE PIC32 MICROCONTROLLER," VISIT THE NU32 PAGE.


Driving RC Servo motors with the NU32.

An RC Servo motor

Overview

You can read about RC Servo Theory and a method of creating a drive signal using a 555 timer here.

An RC servo has a position controller built in. It will position the output shaft between 0 and 180 degrees if given a pulse of ~0.5ms to ~2.5ms every 20ms (or 50Hz).

An easy way to create the pulse is with PWM. We can set a timer (2 or 3) to 50Hz, and then set the OC module to use that timer, and set the duty cycle to only be in the range that will create pulses of at least 0.5ms and at most 2.5ms. This method is fine if you have a spare timer and OC pin available, but limits you to 5 RC servo motors because there are only 5 OC pins. If your project uses several DC motors and several RC motors, you will run out of PWM pins!

Another way to create the pulses is to do so using a timer interrupt and manually toggling the digital pins. With this method you could control as many RC motors as you want, depending on how you want to set up the code, and with what resolution you need to be able to control the RC servo angle.

Wiring

An RC servo has 3 pins, Power (Red), Ground (Brown), and Signal (Orange). Hook up power to your 6V battery, and ground to your NU32 board. Make sure your battery has common ground with the NU32. Pick an OC pin, like D0, and hook it up to signal. If you are not using the PWM method, and digital IO pin will do.

To physically hook up the RC servo, jam wires into the female connector, they should stay.

Details

Try the PWM method first. Set up timer 2 to 50Hz. There are several options for prescaler: use the prescaler so that you have a large PR value (remember it has to be less than 2^16-1, or 65535). We will see why in a moment.

Now turn on the OC1 module (pin D0) using timer 2, and calculate what duty cycle is required to make a 0.5ms pulse. For example, if we use a 1:256 prescaler for timer 2, and PR2 = 6250 for 50Hz, we have a resolution of (20ms / 6250) = 0.0032ms. For a 0.5ms pulse, we would set OC1RS to 157. For a 2.5ms pulse, we would set it to 781. We would not want to set OC1RS outside of the range 157-781, or the motor might try to move to a position outside of the 0-180 degree range, and break itself trying to get there. Now we have 180/(781-157) = 0.29 degree per count of the OC module, which is fine control of the position of the servo motor. If we use a different prescaler for the timer, we would have better resolution (but do you really need sub-degree resolution, and can the (very inexpensive) RC servo achieve those fine resolutions?

The following code implements this technique: NU32_rcservo_pwm_example.c


More Information

NA