Hi everybody,
I looking for a programme to command 2 servo.
Please help me.
Code for multiple servo
- 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:
Hello
The easiest way is to get a PICmicro with 2 PWM outputs and then use the Flowcode PWM component to specify the servo positions.
The next easiest way is to do an interrupt driven method. Eg use the timer and set the prescalar to a value as close as possible to achieve a timeout of a multiple of 50hz. Then every time the PICinterrupts you can change or not change the logic of the servo output.
The easiest way is to get a PICmicro with 2 PWM outputs and then use the Flowcode PWM component to specify the servo positions.
The next easiest way is to do an interrupt driven method. Eg use the timer and set the prescalar to a value as close as possible to achieve a timeout of a multiple of 50hz. Then every time the PICinterrupts you can change or not change the logic of the servo output.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Steve
- Matrix Staff
- Posts: 3433
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
I'm not sure if the first suggestion would work. I'm not good with servos, but I think the period of the pulse into a servo has to be 20ms. The PWM module within a PIC typically runs faster than this. To get a 20ms period, I had to reduce the clock speed of the PIC to 800kHz - this may not be practical.
The second one is a good method. Basically, set the timer to interrupt every millisecond and set 2 global variables (Servo1 and Servo2) to values of between 0 and 20 (these would set your servo duty cycle). On each interrupt, increment a global counter (ServoCount). If the counter reaches 20, reset it to 0. Also, set the output pin for the first servo high if (ServoCount < Servo1). Do the same for the second servo.
A better resolution can be achieved by incrementing the timer interrupt frequency (and incrementing the counter max value appropriately to achieve 20ms).
The second one is a good method. Basically, set the timer to interrupt every millisecond and set 2 global variables (Servo1 and Servo2) to values of between 0 and 20 (these would set your servo duty cycle). On each interrupt, increment a global counter (ServoCount). If the counter reaches 20, reset it to 0. Also, set the output pin for the first servo high if (ServoCount < Servo1). Do the same for the second servo.
A better resolution can be achieved by incrementing the timer interrupt frequency (and incrementing the counter max value appropriately to achieve 20ms).