Multiple Servo Control

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
0
No votes
4
2
22%
5
7
78%
 
Total votes: 9

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Multiple Servo Control

Post by Sean »

Image


Servo basics

The servo motors used in radio controlled models represent a simple solution for applications requiring accurate position control. The servos are self-contained control devices that require only a power supply (usually 0v and 4.5v - 6v) and a position control signal.
The position control signal consists of a high pulse, usually between 0.7ms and 2.3ms (the exact requirements vary slightly depending on the servo being used), usually representing a rotation range of approximately 150 degrees. The pulse must be repeated every 20ms.
Many microcontrollers now contain the dedicated timing hardware that is capable of generating the required signals.
A typical device, like the PIC16F877A contains a flexible timer module (Timer 1) that can be used to generate accurate timing signals suitable for R/C servo control. With the additional use of interrupts, precise control of up to eight servo channels can be achieved as a background task. The main control program can operate independently, simply loading the required servo positions into a shared data array.

Timer 1 and the CCP modules

Timer 1 is a 16-bit timer that also contains two 16-bit capture/compare modules (CCP1, CCP2) that can be configured to perform different tasks. These functions are particularly useful when combined with their ability to generate interrupts when events occur, requiring the creation of two Flowcode Custom Interrupts.
The CCPR1 module can be used to reset Timer 1 when the value of the timer register matches the value in the CCPR1 registers, generating an interrupt (CCP1IF) when this event occurs, allowing the output for the next servo channel to be set in the interrupt service routine.
The CCPR2 module can be configured to only generate an interrupt (CCP2IF) when the value of the timer 1 register matches the value in the CCPR2 registers. The value of CCPR2 during each servo channel period can be used to control the pulse width of the position control signal for that servo, allowing the output for the current servo channel to be cleared when the match event occurs (within the overall 2.5ms period of the servo channel).
Setting a value in CCPR1 that equates to 2.5ms will provide the channel timing for each servo being controlled
Sequentially servicing eight 2.5ms servo channels will produce the required 20ms overall cycle time.

Image

Simple calculations

Values for each channel can be written as byte values to data arrays (strings) and accessed by the CCP1 interrupt service routine.
The values written to the CCP registers depend on the device clock frequency and Timer 1 prescaler value. Careful selection of these values will allow simple calculations to be used throughout.
Attachments
MultiServo2.fcf
(8 KiB) Downloaded 1478 times

J Waters
Posts: 1
Joined: Thu Aug 14, 2008 10:46 am
Contact:

Re: Multiple Servo Control

Post by J Waters »

Great article

markkkk
Posts: 21
Joined: Fri Jul 28, 2006 3:30 am
Location: Brisbane Australia
Contact:

Re: Multiple Servo Control

Post by markkkk »

excellent example this is a great way to learn

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Multiple Servo Control with Brushless motors

Post by Benj »

Using the program and content from this article it is possible to drive the new generation of brushless motors. You will need to get a new style of brushless motor ESC (Electronic Speed Control) unit. The motors can be found on Ebay or a good R/C hobbiest shop for about Β£8 and the speed controllers are slightly less. The speed controllers require a servo style signal to run so the example programs shown above work perfectly for controlling up to 8 brushless motors. The only other requirement would be a power supply that is capable of generating a few amps of current to drive the motors.

Boza
Posts: 2
Joined: Sun Mar 01, 2009 9:40 am
Contact:

Re: Multiple Servo Control

Post by Boza »

Hello,
I am traying to convert this example for ATMEGA8. (File->Import...) The simulation past through, but copilation to hex doesn't.
Does anybody have this example sucesfully converted for ATMEGA8 yet ? Can you post it, please?
Thanks Boza

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: Multiple Servo Control

Post by Sean »

The servo control program uses C code blocks within the Flowcode program to provide direct access to device specific timer registers. This causes the AVR code to fail to compile when imported directly from the PIC version.
A quick check of the ATmega8 data sheet indicates that the necessary functionality is available, so I will try to get an AVR version of the program working in the next few days.

User avatar
tanlipseong
Posts: 31
Joined: Thu Aug 27, 2009 3:11 pm
Location: Malaysia
Contact:

Re: Multiple Servo Control

Post by tanlipseong »

Hi Matrix support team,

I have change the Chip Config and add C Code to "MultiServo2" - to make use of internal 8MHz oscillator of 16F887.

The servo now no longer having interval of 20ms and the pulse width also is no longer correct.

how do i change the code so that the servo can run properly again?

Please help.

Thanks in advance.

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: Multiple Servo Control

Post by Sean »

Some minor value and configuration changes chould allow the program to run at 8MHz.

Change the Capture/Compare 1 values to:
ccpr1h = 0x09;
ccpr1l = 0xc4;

This sets the terminal count to 2500

In the Enable Code section of the CCP1isr interrupt properties, set:
t1con = 0x11;

This sets the clock prescaler to 1:2 (1MHz from an 8MHz clock source after system division by 4).

The values in the servo_min[] array should be set to around 245 to provide the initial 1ms delay for each channel.

Flowcode V4 contains a Servo component that makes automatic adjustmentss for clock frequency

User avatar
tanlipseong
Posts: 31
Joined: Thu Aug 27, 2009 3:11 pm
Location: Malaysia
Contact:

Re: Multiple Servo Control

Post by tanlipseong »

Hi Sean,

I have done the change as recommended, result is not the same compare to running at 19.6608Mhz. below is the result: -

Running at 19.6608MHz external X'tal
---------------------------------------
Servo Interval = 20.0mS
Servo Pulse Min (adc1val=00) =0.44mS
Servo Pulse Max (adc1val=ff) = 2.10mS
Servo rotation = +/- 170deg

Running at 8MHz Internal Osc (as per recommended change to code)
------------------------------------------------------------------------------------
Servo Interval = 19.8mS
Servo Pulse Min (adc1val=00) =0.90mS
Servo Pulse Max (adc1val=ff) = 1.98mS
Servo rotation =+/- 100deg

Please help to let me know how to culculate the
t1con, ccpr1h and ccpr1l to get a closer result.

Thanks in advance
TanLS

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: Multiple Servo Control

Post by Sean »

The slight timing inaccuracy is probably due to the use of the RC clock. There is a calibration procedure for the device that can improve its accuracy, but it will not be as accurate, or stable, as a crystal.

The specified pulse range for most servos is 1ms to 2ms. The servo_min array in the Flowcode programs allows the minimum pulse with to be independently set for each channel.

The original program allowed an adjustment range of approximately 1.6ms and was intended to operate from 0.7ms to 2.3ms. Most servos seem to be able to operate over this extended adjustment range.

The values used in the programs are intended to allow quick and simple calculations - avoiding any multiplication or division. Conversion to the 8MHz clock, from the 19.6608MHz clock of the original program, reduced the adjustment range to slightly greater than 1ms. The figures you have provided indicate that it is working correctly. A small change of the servo-min value would allow the adjustment range to be centred on 1.5ms (0.96ms - 2.04ms).

The main calculations are performed in the CCP1isr macro. The servo_pos and servo_min values are added together to provide a channel delay. The result is then multiplied by 4 (using simple bit-shift operations) and split into the two byte values for the timer registers.

Instead of multiplying the channel delay by 4, it could be multiplied by 5 or 6 at this point if you wanted to increase the adjustment range. The resulting integer value would need to be split into two bytes before sending to the timer registers. The servo_min values would also need to be reduced, to approximately 180.

User avatar
tanlipseong
Posts: 31
Joined: Thu Aug 27, 2009 3:11 pm
Location: Malaysia
Contact:

Re: Multiple Servo Control

Post by tanlipseong »

Hi Sean,

I have been using MultiServo2.fcf with 16F887 successfully.

How do i change the "Custom interrupt properties" for CCP1 and CCP2 so that i can use it for 16F690?

Do i need to change any thing else to make it work?

Thanks in advance.
TanLS

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: Multiple Servo Control

Post by Sean »

The PIC16F690, and other similar devices, can not be used for this method of servo signal generation because they only have one CCP module connected to TMR1.

There are probably alternative methods of generating servo signals using the timing facilities available in the 16F690 (especially if fewer than 8 channels are required).

The servo component included in Flowcode V4 performs all the calculations necessary to allow the code to run on different target devices containing the required timer hardware.

TomasS
Posts: 75
Joined: Wed Oct 28, 2009 2:37 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Multiple Servo Control

Post by TomasS »

Sean wrote:The servo control program uses C code blocks within the Flowcode program to provide direct access to device specific timer registers. This causes the AVR code to fail to compile when imported directly from the PIC version.
A quick check of the ATmega8 data sheet indicates that the necessary functionality is available, so I will try to get an AVR version of the program working in the next few days.
Hi Sean

Did you get the AVR version up and running?

BR.

Tomas

Post Reply