© Fort Street High School Robotics
for each action within the spill
reset counters
do the action
record the action speed and rotation counts
store this data somewhere
drop the can
for each action taken so far, other than can drop
start from the latest action
reset counters
do the action, in reverse
struct
.
// Place this before your main function
struct ActionData {
int leftMotorSpeed;
int rightMotorSpeed;
int leftMotorCount;
int rightMotorCount;
};
// Place this at the top within the main function
struct ActionData allActions[10]; // Create an array of ActionData
int currentAction = 0; // Keep track of where we are
...
// For each action within the spill, record the data
nMotorEncoder[leftMotor] = 0;
nMotorEncoder[rightMotor] = 0;
// Rotate around until we find something on the sonar
move(10, -10);
while (SensorValue[Sonar] < 10) {
// finding...
}
// Store this data
allActions[currentAction].leftMotorSpeed = 10;
allActions[currentAction].rightMotorSpeed = -10;
allActions[currentAction].leftMotorCount = nMotorEncoder[leftMotor];
allActions[currentAction].rightMotorCount = nMotorEncoder[rightMotor];
currentAction = currentAction + 1;
// Next action can now start!
...
allActions
, and that the latest action was number currentAction - 1
(because we increment after the action is completed). So:currentAction - 1
currentAction == -1
Let's take a look at this in code:
currentAction = currentAction - 1;
while (currentAction >= 0) {
// Reset counters
nMotorEncoder[leftMotor] = 0;
nMotorEncoder[rightMotor] = 0;
// Get the data out of the struct
int leftSpeed = allActions[currentAction].leftMotorSpeed;
int rightSpeed = allActions[currentAction].rightMotorSpeed;
int leftCount = allActions[currentAction].leftMotorCount;
int rightCount = allActions[currentAction].rightMotorCount;
// Inverse these actions
// We can just keep track of the absolute values of the counts
// Because otherwise it gets messy with negative values.
leftSpeed = -leftSpeed;
rightSpeed = -rightSpeed;
leftCount = abs(leftCount);
rightCount = abs(rightCount);
// Do the action
move(leftSpeed, rightSpeed);
while (leftCount > abs(nMotorEncoder[leftMotor]) || rightCount > abs(nMotorEncoder[rightMotor]))) {
// moving...
}
move(0, 0);
// We should be back to where we were before this action.
currentAction = currentAction - 1;
// Subtract so that we can take the previous action.
}