SERVO MOTORS PHASE VARIATION

Hi guys,

I have just started using an arduino microprocessor.
I am operating 2 servo motors at once. The servos are supposed to do a simple sweep from with a pwm signal ranging from 1200 to 1800 microseconds. I have no clue as to how to incorporate a phase lag between the rotation of the two motors with both of them having a phase difference of 90deg. Here is the code that I am using. Please let me know if I can use a special library or something to do this.

#include <Servo.h>

Servo one;
Servo two;

int val;
int pos=900;
int num=900;

void setup() {
Serial.begin(9600);
mid.attach(4);
right.attach(5);
left.attach(6);
one.writeMicroseconds(1500);
two.writeMicroseconds(1500);
}

void serialEvent()
{
val=Serial.parseInt();
}

void loop(){
for (pos=1200;pos<=1800;pos+=val)//actuates the motor in cw
{

mid.writeMicroseconds(pos);
left.writeMicroseconds(pos);
}
delay(50);

for(pos=1800;pos>=1200;pos-=val)//actuates the motor in ccw
{
one.writeMicroseconds(pos);
two.writeMicroseconds(pos);
}
delay(50);
}

If you want both motors to move at the same time you must put the servo.writeMicroseconds() for both motors in the same FOR loop.

I'm not sure what exact mathematical relationship you want but something like (pos - 1000) should get one motor to be 90 deg out of phase. Obviously you need to deal with the end cases.

It may be easier to think of the maths using deg. One motor moves to (say) 125deg and the other moves to 125-90 deg (35 deg). I'm not sure what you would wish to happen if the first servo is at an angle less than 90 deg.

...R

I posted some code which allowed one to individually set period and phase of each servo in use. The code was tailored for the AdaFruit servo shield but it shouldn't be hard to adapt it for use with servos directly controlled from the Arduino.

The motion will be linear. It takes a bit more work to generate non-linear motion from a servo.

After purchasing servos for two different hexapods (a big one and a small one), I wired up 32 servos at once to see what they looked like. I thought they looked pretty cool oscillating both in phase and out of phase.