Hello, I looked around and have found a few forum post about this, but I was having a little difficulty understanding how to implement this into my project. I'm very new at this so I apologize if this is painfully simple.
In my engineering computations class we are programming to have a robot locate a bottle and pick it up (tank drive, moving robot), most other students have the robot moving in a circle until it see the bottle, then moving forward. I noticed that this is fairly inaccurate as the gripper arm gets caught on the bottle cap half the time (y-axis movement).
I'm looking to have my code store the servo motor location for when it first detects the bottle, continue to turn, then store the motor location for when it loses the bottle, find the average between the two and move the robot to that position. Then the robot can move forward blah blah blah. My only problem is that I dont know how to store the motor location in the arduino's volatile memory, or how to write the new position to the robot.
Would anyone be able to help me out with this? Thanks
Here is code
#include "MeMegaPi.h"
//Sensor
MeUltrasonicSensor ultraSensor(PORT_6);
//Pins for reading and writing servo motor location
int pos = 0;
int servoL = PORT1A; //Do not know, value is placeholder
int servoR = PORT2A; //Do not know, value is placeholder
//Motor 1 (Left)
MeMegaPiDCMotor motor1(PORT1A);
MeMegaPiDCMotor motor2(PORT1B);
//Motor 2 (Right)
MeMegaPiDCMotor motor3(PORT2A);
MeMegaPiDCMotor motor4(PORT2B);
//Gantry (Y-axis)
MeMegaPiDCMotor motor5(PORT3A);
MeMegaPiDCMotor motor6(PORT3B);
//Gripper (X-axis)
MeMegaPiDCMotor motor8(PORT4B); //first slot is A second is B
uint8_t motorSpeed = 50;
//Create a servo object
Servo posL;
Servo posR;
void setup()
{
Serial.begin(9600);
posL.attach(servoL);
posR.attach(servoR);
}
void loop()
{
//lifting gripper up so it does not knock into bottle
motor5.run(100);
motor6.run(100);
delay(1600);
motor5.stop();
motor6.stop();
delay(40);
//Turning to locate bottle
while(ultraSensor.distanceCm() > 80)
{
motor1.run(motorSpeed);
motor2.run(motorSpeed);
motor3.run(motorSpeed);
motor4.run(motorSpeed);
}
//Located side one of bottle
if(ultraSensor.distanceCm() < 80 && ultraSensor.distanceCm() > 13)
{
//I want to store the 1st position of both servos here
posL = //Don't know how to save servo location. Will save it to variable PosL1
posR = //Don't know how to save servo location. Will save it to variable PosR1
}
while(ultraSensor.distanceCm() < 80 && ultraSensor.distanceCm() > 13)
{
motor1.run(50);
motor2.run(50);
motor3.run(50);
motor4.run(50);
}
//Sight of bottle waws broken, 1st while loop will not start until whole program has run
//Locating 2nd side of bottle
motor1.stop();
motor2.stop();
motor3.stop();
motor4.stop();
//I want to store the 2st position of both servos here
posL = //Don't know how to save servo location. Will save it to variable PosL2
posR = //Don't know how to save servo location. Will save it to variable PosR2
//Robot centering itself against bottle
pos.write((pos1 + pos2) / 2.0) //Probably not right
//Moving towards the bottle
while(ultraSensor.distanceCm() > 18)
{
motor1.run(motorSpeed);
motor2.run(motorSpeed);
motor3.run(-motorSpeed);
motor4.run(-motorSpeed);
}
//Dropping gripper down onto bottle
motor5.run(-50);
motor6.run(-50);
delay(5000);
motor5.stop();
motor6.stop();
//Closing gripper
motor8.run(25);
delay(5000);
motor8.stop();
//Lifting bottle in triumph
motor5.run(50);
motor6.run(50);
delay(5000);
motor5.stop();
motor6.stop();
//Do a little dance
for(int x = 0; x < 3; x++)
{
motor1.run(-motorSpeed);
motor2.run(-motorSpeed);
motor3.run(-motorSpeed);
motor4.run(-motorSpeed);
delay(1000);
motor1.run(motorSpeed);
motor2.run(motorSpeed);
motor3.run(motorSpeed);
motor4.run(motorSpeed);
delay(1000);
}
//Dropin the mic
motor5.run(-50);
motor6.run(-50);
delay(5000);
motor5.stop();
motor6.stop();
//Opening gripper
motor8.run(-25);
delay(5000);
motor8.stop();
return 0;
}