Final square trajectroy code
This commit is contained in:
parent
198bef2aa0
commit
47c9f5f57a
|
|
@ -12,45 +12,52 @@ double threshold= 10;
|
||||||
int Squarex[]={0,25,25,0};
|
int Squarex[]={0,25,25,0};
|
||||||
int Squarey[]={0,0,25,25};
|
int Squarey[]={0,0,25,25};
|
||||||
bool tmp=0;
|
bool tmp=0;
|
||||||
double angle =0;
|
double angle =0.0;
|
||||||
double angle2=0;
|
double angleDifference=0.0;
|
||||||
|
double targetAngle = 90.0;
|
||||||
|
|
||||||
MeGyro gyro;
|
MeGyro gyro;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{Serial.begin(9600);
|
{
|
||||||
|
Serial.begin(9600);
|
||||||
gyro.begin();
|
gyro.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
if(tmp == 0){
|
if(tmp == 0){ /* if-loop with flag to make the robot go 25 cm forward */
|
||||||
motorSpeed =100;
|
motorSpeed = 100;
|
||||||
motor1.run(-motorSpeed); /* value: between -255 and 255. */
|
motor1.run(-motorSpeed); /* value: between -255 and 255. */
|
||||||
motor2.run(motorSpeed); /* value: between -255 and 255. */
|
motor2.run(motorSpeed); /* value: between -255 and 255. */
|
||||||
delay(2000);
|
delay(2000);
|
||||||
motor1.stop();
|
motor1.stop();
|
||||||
motor2.stop();
|
motor2.stop();
|
||||||
tmp = 1;
|
gyro.update();
|
||||||
|
angle = gyro.getAngleZ(); /* Sense the initial position of the robot before turning */
|
||||||
|
tmp = 1; /* update flag */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tmp == 1) {
|
if (tmp == 1) { /* if loop for rotation after translation */
|
||||||
gyro.update();
|
|
||||||
|
|
||||||
Serial.read();
|
|
||||||
angle = 90 - gyro.getAngleZ();
|
|
||||||
Serial.println(gyro.getAngleZ());
|
|
||||||
|
|
||||||
motor1.run(2.8*angle); /* value: between -255 and 255. */
|
|
||||||
motor2.run(2.8*angle); /* value: between -255 and 255. */
|
|
||||||
|
|
||||||
if (2.8*angle <=100){
|
|
||||||
|
|
||||||
motor1.run(100); /* value: between -255 and 255. */
|
gyro.update();
|
||||||
motor2.run(100);}
|
angleDifference = gyro.getAngleZ() - angle; /* Calculate how much the robot turned */
|
||||||
|
|
||||||
if(abs(2.8*angle) <= 35){
|
if(angleDifference < targetAngle){
|
||||||
tmp =0;}
|
if (angleDifference < 0){
|
||||||
}
|
motorSpeed = 2.83*(targetAngle);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
motorSpeed = 2.83*(targetAngle-angleDifference);
|
||||||
|
}
|
||||||
|
motor1.run(motorSpeed);
|
||||||
|
motor2.run(motorSpeed);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(angleDifference >= targetAngle){ /* if robot reaches 90°, stop rotation and update flag*/
|
||||||
|
motor1.stop();
|
||||||
|
motor2.stop();
|
||||||
|
tmp = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue