Division R – AEV Programming

 

The following is a list of basic commands along with corresponding descriptions and examples for programming the Ardiuno Nano that controls the AEV. The table is based of of the table found in Exercise 1 OSU Engr 1182 Preliminary Research and Design Lab Manual.

 

Function Call Function Example
celerate(m,p1,p2,t); Accelerates or decelerates motor(s) m from start speed (%) p1 to end speed (%) p2 over a duration of t seconds celerate(1,20,45,2);

Accelerates motor 1 from 20% to 45% power in 2 seconds

motorSpeed(m,p); Initializes motor(s) m at percent power p motorSpeed(2,16);

Sets motor 2 speed to 16% power

goFor(t); Runs the motor(s) at their initialized state for t seconds goFor(5);

Runs the motor(s) at their initialized state for 5 seconds

brake(m); Brakes motor(s) m. Note: This does NOT brake the AEV, just stops the motors from spinning brake(4);

Cuts the power to all motors

reverse(m); Reverses the polarity of motor(s) m reverse(1);

Reverses polarity of motor 1

goToRelativePosition(n); Continues the previous command for n marks from the vehicle’s current position. n can be positive or negative, with positive meaning the vehicle is moving forward, negative meaning the vehicle is moving backward motorSpeed(4,20);
goToRelativePosition(30);
All motors are set to 20% power, and they will continue to run at 20% power until the AEV reaches 30 marks from its current position
goToAbsolutePosition(n); Continues the previous command for n marks relative to the overall starting position of the AEV motorSpeed(4,20);
goToAbsolutePosition(300);
All motors are set to 20% power, and they will continue to run at 20% power until the AEV reaches 300 marks from its starting position

 

Arduino Code

 

pR&D: Exercise 1 (Programming Basics)

 

// accelerate motor from start to 15% in 2.5 seconds.
celerate(1,0,15,2.5);

// Run motor one at a constant speed (15% power) for 1 second.
motorSpeed(1,15);
goFor(1);

// Brake motor one
brake(1);

// accelerate moto two from start to 27% in 4 seconds.
celerate(2,0,27,4);

// Run motor two at a constant speed (27% power) for 2.7 seconds
motorSpeed(2,27);
goFor(2.7);

// Decelerate motor two to 15% power in 1 second.
celerate(2,27,15,1);

// Brake motor two
brake(2);

// Reverse motor 2
reverse(2);

// Accelerate all motors from start to 31% power in 2 seconds
celerate(4,0,31,2);

// Run all motors at a constant speed of 35% power for 1 second
motorSpeed(4,35);
goFor(1);

// Brake motor two but keep motor one running at a constant speed (35% power) for 3 seconds
brake(2);
goFor(3);

// Brake all motors for 1 second
brake(4);
goFor(1);

// Reverse the direction of motor one
reverse(1);

// Accelerate motor one from start to 19% power over 2 seconds
celerate(1,0,19,2);
goFor(2);

/*
* Run motor two at 35% power while simultaneously running motor one at 19% power
* for 2 seconds
*/
motorSpeed(2,35);
motorSpeed(1,19);
goFor(2);

// Run both motors at a constant speed (19% power) for 2 seconds
motorSpeed(4,19);
goFor(2);

// Decelerate both motors to 0% power in 3 seconds
celerate(4,19,0,3);

// Brake all motors
brake(4);

 

pR&D: Exercise 2 (Reflectance Sensors)

 

// Run all motors at a constant speed of 25% power for 2 seconds.
motorSpeed(4, 25);
goFor(2);

// Run all motors at a constant speed of 20% and using the goToAbsolutePosition function travel a total distance of 12 feet (from the starting point)
motorSpeed(4, 20);
goToAbsolutePosition(295);

// Reverse motors
reverse(4);

// Run all motors at a constant speed of 30% power for 1.5 seconds
motorSpeed(4, 30)
goFor(1.5);

// Brake all motors
brake(4);

 

pR&D: Exercise 4 (Data Analysis Tool)

 

//Accelerate all motors from start to 25% in 3 seconds
celerate(4, 0, 25, 3);

//Run all motors at a constant speed (25% power) for 1 second
motorSpeed(4, 25);
goFor(1);

//Run all motors at 20% power for 2 seconds
motorSpeed(4, 20);
goFor(2);

//Reverse all motors
reverse(4);

//Run all motors at a constant speed (25% power) for 2 second
motorSpeed(4, 25);
goFor(1);

//Brake all motors.
brake(4);

 

aR&D: Coasting vs. Power Braking

Coasting

 

/*--------------- VARIABLE DECLARATIONS ---------------*/

const double inchesToMarks = 2.05128;______________________// 2.05128 marks in an inch
const double feetToInches = 12.0;__________________________// 12 inches in a foot
const double feetToMarks = feetToInches * inchesToMarks;___// converts feet to marks

/*--------------------- MAIN CODE ---------------------*/

// START OFF GOING FORWARD
reverse(4);

// accelerate all motors from 0 to 25% power in 2 seconds
celerate(4, 0, 25, 2);

// run all motors at 25% power to 10 feet from start
motorSpeed(4, 25);
goToAbsolutePosition(10 * feetToMarks);

// stop all motors
brake(4);

 

aR&D: Coasting vs. Power Braking

Power Breaking

 

/*--------------- VARIABLE DECLARATIONS ---------------*/

const double inchesToMarks = 2.05128;______________________// 2.05128 marks in an inch
const double feetToInches = 12.0;__________________________// 12 inches in a foot
const double feetToMarks = feetToInches * inchesToMarks;___// converts feet to marks

/*--------------------- MAIN CODE ---------------------*/

// START OFF GOING FORWARD
reverse(4);

// accelerate all motors from 0 to 25% power in 2 seconds
celerate(4, 0, 25, 2);

// run all motors at 25% power to 12 feet from start
motorSpeed(4, 25);
goToAbsolutePosition(12 * feetToMarks);

// reverse all motors
reverse(4);

// run all motors at 25% power for 2 seconds (backwards)
motorSpeed(4, 25);
goFor(2);

 

aR&D: Braking Using a Servo and Physical Break

 

/*--------------- VARIABLE DECLARATIONS ---------------*/

const double inchesToMarks = 2.05128;______________________// 2.05128 marks in an inch
const double feetToInches = 12.0;__________________________// 12 inches in a foot
const double feetToMarks = feetToInches * inchesToMarks;___// converts feet to marks

const int braked = 165;_____// rotates servo to 165 degrees (180 pointing straight down) to brake
const int notBraked = 150;__// rotates servo to 150 degrees (180 pointing straight down) to release brake

/*--------------------- MAIN CODE ---------------------*/

// set servo to start position
rotateServo(notBraked);

// START OFF GOING FORWARD
reverse(4);

// accelerate all motors from 0 to 25% power in 2 seconds
celerate(4, 0, 25, 2);

// run all motors at 25% power to 10 feet from start
motorSpeed(4, 25);
goToAbsolutePosition(10 * feetToMarks);

// stop all motors
brake(4);

// brake using servo
servoBrake();

/*--------------- FUNCTION DECLARATIONS ---------------*/

void servoBrake() {
__rotateServo(braked);_____// go to braking position
__delay(3000);_____________// hold down brake for 3 seconds (3000 ms)
__rotateServo(notBraked);__// release from braking position
}

 

Final Performance Test: Wait Until Reached Mark Function

 
The following code will pause execution of AEV commands until it has reached a certain mark on the track. This is usefull for accomplishing tasks like waiting until the AEV has reached the gate to break instead of relying on inconsistent timing functions

__void waitUntilReachedMark(double numMarks) {
____// if moving forward
____if (getVehicleDirection() == 1) {
______// wait until AEV reaches numMarks
______while (getVehiclePostion() < numMarks) { ______}
____} else { // else, if moving backward
______// wait until AEV reaches numMarks
______while (getVehiclePostion() > numMarks) {
______}
____}
__}