- Published: Sunday, 14 February 2010
- Written by Graham Mitchell
- Hits: 11189
Download the Swordfish program and Gerber files here: Dedicated Servo Controller Source Files
This project offers a dedicated solution to control servo motors, which can be controlled via USART data packets. This approach has it advantages, though Andy has made an excellent feature enriched Servo Library which can be used in pretty much any program.
Servo Motor's are one of the many hobby electronic marvels that make life a lot easier when working with motion/kinetic applications. They are somewhat precision instruments that offer their full range of motion with pulse widths usually between 1mS and 2mS. That's not much play, and further more, the positional information must be transmitted to the servo approximately fifty times a second (50Hz).
Take a moment to consider the resolution required to get any real degree of granularity with a servo; 0.001 second pulse for minimum deflection, and 0.002 second pulse for maximum deflection. Also take into account that a PIC usually operates at around 10MIPS (10 Million Instructions Per Second), each program cycle will take 100nS.
If you wanted 0.000001 second resolution (the capability to change a pulse width in 1uS increments), then the PIC would only have 10 program cycles to accommodate pulse width maintenance. Considering most normal PIC applications are doing more then one thing at a time, dedicating such hefty resources for the servo does not leave much leeway for other jobs.
Controlling 8 Servo's With Ease
The on-board CCP events are very useful for such an application, and allow very accurate and quick interrupts on exact intervals. This program makes use of both the CCP1 special event and CCP2 normal event to control 8 servos in real time. Further more, the PIC will continually monitor the UART for new servo position data.
Given the demand placed on the PIC to accommodate 8 servos, there's not much resource scope for other tasks. The use of UART definitely made good use of what resources are left over - keep in mind that I am using the hardware UART module which has little overheads to consider. The protocol employed is very lean, extremely tolerant to noisy conditions and detecting transmission errors.
Here's what the program looks like in operation;
Figure 1 - Dedicated Servo Controller Simulation.
For clarity, the above schematic does not show the voltage regulator or crystal oscillator circuits.
Dedicated Servo Controller Code
Note 1: Servo positional information is received via UART at 38400 baud in the following manner:
Note 2: All data types are 16-Bit WORDS. This means that the header is of type WORD, so to is each Servo position and the CRC Checksum.
Note 3: CRC is calculated by XOR'ing all Servo positions together, for example:
Servo(0) = 1000
Servo(1) = 1100
Servo(2) = 1200
Servo(3) = 1300
Servo(4) = 1400
Servo(5) = 1500
Servo(6) = 1600
Servo(7) = 1700
CRC = 1000 XOR 1100 XOR 1200 XOR 1300 XOR 1400 XOR 1500 XOR 1600 XOR 1700
CRC = 1600
For example, to make every servo pulse equal 1.5mS, the following command would be used in SF:
Note 4: Program is designed to work with an 8Mhz External Oscillator.
Note 5: Ensure common ground connection between all circuits.
Thanks to XOR from the SF forum for the servo code, I've simply turned it into a dedicated controller with an UART interface.
Example Transmit Program
The following program is an example of how to transmit the required data to the dedicated servo controller (the previous simulation animation is with this program in use);
Note that the above program has the following hardware requirements:
- 10Mhz Crystal Oscillator
- MCLRE disabled
You can simply edit either the program clock or config settings to change the above hardware requirements. This was just an example how to send packets of data to the dedicated controller.
Servo Development Board
As always, to make life even easier I prefer to have a purpose built development board. The desired features were;
- Interface for 8 Servo Motors
- Direct PICKit 2 programming
- PICKit 2 UART interface
- Onboard Voltage Regulator (capable of driving all 8 servos)
- Small footprint
The end result managed to include all of the above. Here's the PCB layout for the board;
The above design was to suit my requirements as a quick and easy development board. The end result could differ depending on what end users require. All downloads can be found at the top of the page.
Thoughts comments and feedback are all welcome!