Thank you Kurte, I appreciate you looking at it. But I must ask for further help haha.
So I ran a test of servo RA and it worked, so I finished adding in the calculation segments for the other servos. But now, no matter what angle I put in for my function the servos always go the same direction and ALL the way until they collide with the frame. This happens every time and I can’t seem to find whats wrong. Could you (or anyone with the time and a kind heart) take a look and tell me if they see something?
Also, I know that my servos are centered on my robot because I took care of that while
//This code was written by Ensign Justin Shelton, USN.
//Designed to control groups of servos in sequence.
// 6/14/2014
#include <Servo.h>
Servo Servo_RH; //Right hip
Servo Servo_RK; //Right knee
Servo Servo_RA; //Right ankle
Servo Servo_LH; //Left hip
Servo Servo_LK; //Left knee
Servo Servo_LA; //Left ankle
int RHLastPosition = -1; //Right hip last position
int RKLastPosition = -1; //Right knee last position
int RALastPosition = -1; //Right ankle last position
int LHLastPosition = -1; //Left hip last position
int LKLastPosition = -1; //Left knee last position
int LALastPosition = -1; //Left ankle last position
bool runOnce=false;
int LA = 0; //new position of the Left ankle
int LK = 0; //new position of the Left knee
int LH = 0; //new position of the Left hip
int RA = 0; //new position of the Right ankle
int RK = 0; //new position of the Right knee
int RH = 0; //new position of the Right hip
//This is a function that tells each servo to move to a new positio(an angle)
//int TimeChange indicates how long the servo has to get to it's new position.
//Example:
// ServoTimedMove(45, 45, 45, 45, 45, 45, 200);
// The would make the left ankle move to 45 in 200ms, then the Left knee to 45 in 200ms, and so on.
void ServoTimedMove(int RA, int RK, int RH, int LH, int LK, int LA, int TimeChange)
{
{
//Of course, we cannot do a timed move for a servo if we don't know it's position....
//so this means that for each servo, we need to be give the servo some differences in last
//position and the new position. We start with the right ankle.
if ((TimeChange != 0) && (RALastPosition != 1))
{
int DeltaRA = RA - RALastPosition; //The total degree change for RA is the new position(RA) minus RAlastposition
int CycleTime = 20;
int CountOfCycles = TimeChange / 20; //this is because 20ms is the natural recycle time for a servo.
int RADeltaPerCycle = DeltaRA/CountOfCycles;
if (RADeltaPerCycle == 0) //This is for when the changes per cycle is equal to zero
{
// If there isn't enough change to do it, then....
RADeltaPerCycle = (DeltaRA > 0)?1: -1; //this assumes there is a change per cycle.
CountOfCycles = abs(DeltaRA); //As you can guess, this takes the absolute value of the change.
CycleTime = TimeChange/CountOfCycles;
}
int RA = RALastPosition;
while (CountOfCycles--)
{
RALastPosition += RADeltaPerCycle;
Servo_RA.write(RA);
delay(CycleTime);
}
Servo_RA.write(RA);
RALastPosition = RA;
}
}
//This is for the Right knee
{
if ((TimeChange != 0) && (RKLastPosition != 1))
{
int DeltaRK = RK - RKLastPosition; //The total degree change for RA is the new position(RA) minus RAlastposition
int CycleTime = 20;
int CountOfCycles = TimeChange / 20; //this is because 20ms is the natural recycle time for a servo.
int RKDeltaPerCycle = DeltaRK/CountOfCycles;
if (RKDeltaPerCycle == 0) //This is for when the changes per cycle is equal to zero
{
// If there isn't enough change to do it, then....
RKDeltaPerCycle = (DeltaRK > 0)?1: -1; //this assumes there is a change per cycle.
CountOfCycles = abs(DeltaRK); //As you can guess, this takes the absolute value of the change.
CycleTime = TimeChange/CountOfCycles;
}
int RK = RKLastPosition;
while (CountOfCycles--)
{
RKLastPosition += RKDeltaPerCycle;
Servo_RK.write(RK);
delay(CycleTime);
}
Servo_RK.write(RK);
RKLastPosition = RK;
}
}
//This is for the Right hip
{
if ((TimeChange != 0) && (RHLastPosition != 1))
{
int DeltaRH = RH - RHLastPosition; //The total degree change for RA is the new position(RA) minus RAlastposition
int CycleTime = 20;
int CountOfCycles = TimeChange / 20; //this is because 20ms is the natural recycle time for a servo.
int RHDeltaPerCycle = DeltaRH/CountOfCycles;
if (RHDeltaPerCycle == 0) //This is for when the changes per cycle is equal to zero
{
// If there isn't enough change to do it, then....
RHDeltaPerCycle = (DeltaRH > 0)?1: -1; //this assumes there is a change per cycle.
CountOfCycles = abs(DeltaRH); //As you can guess, this takes the absolute value of the change.
CycleTime = TimeChange/CountOfCycles;
}
int RH = RHLastPosition;
while (CountOfCycles--)
{
RHLastPosition += RHDeltaPerCycle;
Servo_RH.write(RH);
delay(CycleTime);
}
Servo_RH.write(RH);
RHLastPosition = RH;
}
}
//This is for the Left hip
{
if ((TimeChange != 0) && (LHLastPosition != 1))
{
int DeltaLH = LH - LHLastPosition; //The total degree change for RA is the new position(RA) minus RAlastposition
int CycleTime = 20;
int CountOfCycles = TimeChange / 20; //this is because 20ms is the natural recycle time for a servo.
int LHDeltaPerCycle = DeltaLH/CountOfCycles;
if (LHDeltaPerCycle == 0) //This is for when the changes per cycle is equal to zero
{
// If there isn't enough change to do it, then....
LHDeltaPerCycle = (DeltaLH > 0)?1: -1; //this assumes there is a change per cycle.
CountOfCycles = abs(DeltaLH); //As you can guess, this takes the absolute value of the change.
CycleTime = TimeChange/CountOfCycles;
}
int LH = LHLastPosition;
while (CountOfCycles--)
{
LHLastPosition += LHDeltaPerCycle;
Servo_LH.write(LH);
delay(CycleTime);
}
Servo_LH.write(LH);
LHLastPosition = LH;
}
}
//This is for the Left knee
{
if ((TimeChange != 0) && (LKLastPosition != 1))
{
int DeltaLK = LK - LKLastPosition; //The total degree change for RA is the new position(RA) minus RAlastposition
int CycleTime = 20;
int CountOfCycles = TimeChange / 20; //this is because 20ms is the natural recycle time for a servo.
int LKDeltaPerCycle = DeltaLK/CountOfCycles;
if (LKDeltaPerCycle == 0) //This is for when the changes per cycle is equal to zero
{
// If there isn't enough change to do it, then....
LKDeltaPerCycle = (DeltaLK > 0)?1: -1; //this assumes there is a change per cycle.
CountOfCycles = abs(DeltaLK); //As you can guess, this takes the absolute value of the change.
CycleTime = TimeChange/CountOfCycles;
}
int LK = LKLastPosition;
while (CountOfCycles--)
{
LKLastPosition += LKDeltaPerCycle;
Servo_LK.write(LK);
delay(CycleTime);
}
Servo_LK.write(LK);
LHLastPosition = LK;
}
}
//This is for the Left ankle
{
if ((TimeChange != 0) && (LALastPosition != 1))
{
int DeltaLA = LA - LALastPosition; //The total degree change for RA is the new position(RA) minus RAlastposition
int CycleTime = 20;
int CountOfCycles = TimeChange / 20; //this is because 20ms is the natural recycle time for a servo.
int LADeltaPerCycle = DeltaLA/CountOfCycles;
if (LADeltaPerCycle == 0) //This is for when the changes per cycle is equal to zero
{
// If there isn't enough change to do it, then....
LADeltaPerCycle = (DeltaLA > 0)?1: -1; //this assumes there is a change per cycle.
CountOfCycles = abs(DeltaLA); //As you can guess, this takes the absolute value of the change.
CycleTime = TimeChange/CountOfCycles;
}
int LA = LALastPosition;
while (CountOfCycles--)
{
LALastPosition += LADeltaPerCycle;
Servo_LA.write(LA);
delay(CycleTime);
}
Servo_LA.write(LA);
LALastPosition = LA;
}
}
}
//This is a test of the program
void Test()
{
//ServoTimedMove( RA, RK, RH, LH, LK, LK, TIME)
ServoTimedMove( 45, 45, 45, 45, 45, 45, 500);
//So, theoretically if all goes well all the motors will whip to 90 degrees in 300ms each.
}
void DetachServos()
{
Servo_LH.detach();
Servo_LK.detach();
Servo_LA.detach();
Servo_RH.detach();
Servo_RK.detach();
Servo_RA.detach();
}
void setup()
{
Servo_LH.attach(4);
Servo_LK.attach(3);
Servo_LA.attach(2);
Servo_RH.attach(12);
Servo_RK.attach(11);
Servo_RA.attach(10);
}
void loop()
{
if (!runOnce)
{
Test();
delay(1000);
DetachServos();
runOnce=1; //Set true so the program doesn't run again
}
}