Division R AEV Code

Basic Functions

Below is a table that explains the functions that are used within our code.

Function Call Function Example
celerate(m,p1,p2,t); Accelerates or decelerates motor (m) from initial power (p1) to final power (p2) in amount of (t) seconds celerate(1,0,15,2.5);

Accelerates motor one from start to 15% power in 2.5 seconds

motorSpeed(m,p); Starts motor (m) at speed (p) motorSpeed(1,15);

Runs motor one at a constant speed at 15% power

goFor(t); Runs motor at motor speed for (t) seconds goFor(1);

command goes for 1 second

brake(m); Brakes motor (m) brake(1);

Brakes motor one

reverse(m); Reverses direction of motor (m) reverse(2);

Reverses the direction of motor 2

 

Programming Basics

The code below was used to test the basic functions of our code, and ensure that each function worked the way it was supposed to with the motors running individually and in unison.

// Run motor one at a constant speed (23% power) for 2.5 second.

motorSpeed(1,23);

goFor(2.5);

//1. Accelerate motor one from start to 15% power in 2.5 seconds.

      celerate(1,0,15,2.5);

// 2. Run motor one at a constant speed (15% power) for 1 second.

      motorSpeed(1,15);

      goFor(1);

// 3.  Brake motor one.

      brake(1);

goFor(4);

// 4.  Accelerate motor two from start to 27% power in 4 seconds.

       celerate(2,0,27,4);

// 5.  Run motor two at a constant speed (27% power) for 2.7 seconds.

       motorSpeed(2,27);

       goFor(2.7);

// 6.  Decelerate motor two to 15% power in 1 second.

       celerate(2,27,15,1);

// 7.  Brake motor two.

       brake(2);

// 8.  Reverse the direction of only motor 2.

       reverse(2);

// 9.  Accelerate all motors from start to 31% power in 2 seconds.

       celerate(4,0,31,2);

// 10. Run all motors at a constant speed of 35% power for 1 second.

       motorSpeed(4,35);

       goFor(1);

 //11. Brake motor two but keep motor one running at a constant speed (35% power) for 3 seconds.

       brake(2);

       motorSpeed(1,35);

       goFor(3);

// 12. Brake all motors for 1 second.

       brake(4);

       goFor(1);

// 13. Reverse the direction of motor one.

       reverse(1);

// 14. Accelerate motor one from start to 19% power over 2 seconds.

       celerate(1,0,19,2);

 //15. 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);

 //16. Run both motors at a constant speed (19% power) for 2 seconds.

       motorSpeed(4,19);

       goFor(2);

// 17. Decelerate both motors to 0% power in 3 seconds.

       celerate(4,19,0,3);

 //18. Brake all motors.

       brake(4);

 

 Reflectance Sensor Test Code

This code was used to ensure that our reflectance sensors performed correctly.

// 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). You will actually

// rotate the wheel by hand (since you are on the desktop tracks) until the wheel has

// completed the required distance and the motors stop.

motorSpeed(4, 20);

goToAbsolutePosition(1476);

// 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);

 

DAT Code

This code was used in conjunction with the Design Analysis Tool to see how the AEV used energy, using different intervals of speed and time.

// 1.  Accelerate all motors from start to 25% in 3 seconds.

celerate(4,0,25,3);

// 2.  Run all motors at a constant speed (25% power) for 1 second.

motorSpeed(4,25);

goFor(1);

// 3.  Run all motors at 20% power for 2 seconds.

motorSpeed(4,20);

goFor(2);

// 4.  Reverse all motors.

reverse(4);

// 5.  Run all motors at a constant speed (25% power) for 2 seconds.

motorSpeed(4,25);

goFor(2);

// 6.  Brake all motors.

brake(4);

goFor(4);

 

Coasting Code:

//1. Run all motors at a constant speed (27% power) for 2 seconds.

motorSpeed(4,27);

goFor(2);

 

Power Braking Code:

//1. Run all motors at a constant speed (27% power) for 2 seconds.

motorSpeed(4,27);

goFor(2);

//2. Brake for 2 seconds.

brake(4);

goFor(2);

 

Servo Testing Code:

Speed 1:

//1. Run all motors at a constant speed (27% power) for 4 seconds then brake all.

motorSpeed(4,27);

goFor(4);

brake(4);

//2. Rotate Servo 45 degrees then return to 0.

rotateServo(45);

goFor(3);

rotateServo(0);

Speed 2:

//1. Run all motors at a constant speed (35% power) for 4 seconds then brake all.

motorSpeed(4,35);

goFor(4);

brake(4);

//2. Rotate Servo 45 degrees then return to 0.

rotateServo(45);

goFor(3);

rotateServo(0);

 

Performance Test 1 Code:

   reverse(4);

   motorSpeed(4,27);

    goToRelativePosition(-293);

    brake(4);

   rotateServo(50);

   goFor(7);

   motorSpeed(4,27);

   goFor(3);

   brake(4);

   rotateServo(50);

   goFor(1);

   rotateServo(0);   

   goFor(1);

 

Performance Test 2 Code:

  //takes AEV to gate, pauses for 7

   reverse(4);

   motorSpeed(4,27);

    goToRelativePosition(-285);

    brake(4);

   rotateServo(50);

   goFor(7);

  //releases servo, continues down track, pauses before load

   rotateServo(0);

   motorSpeed(4,27);

   goToRelativePosition(-260);

   brake(4);

   rotateServo(50);

   goFor(1);

 //AEV attaches to load

   rotateServo(0);

 motorSpeed(4, 20);

   goToRelativePosition(-28);

   brake(4);

   rotateServo(50);

   goFor(5);

   rotateServo(0);

   reverse(4);

   motorSpeed(4, 41);

   goFor(6);

   brake(4);

   rotateServo(50);

   goFor(1);

   rotateServo(0);

Final Performance Test 1:

//takes AEV to gate, pauses for 7
   reverse(4);
   motorSpeed(4,45);
    goToRelativePosition(-275,2,45);
    brake(4);
   rotateServo(55);
   goFor(8);

  //releases servo, continues down track, pauses before load
   rotateServo(0);
   motorSpeed(4,26);
   goToRelativePosition(-280,2,26);
   brake(4);
   rotateServo(55);
   goFor(1.5);

 //AEV attaches to load
   rotateServo(0);
   motorSpeed(4,20);
   goToRelativePosition(-30,1,20);
   brake(4);
   rotateServo(55);
   goFor(5);

 //AEV goes back to gate
   rotateServo(0);
   reverse(4);
   motorSpeed(4,60);
   goToRelativePosition(268,2,60);
   brake(4);
   rotateServo(57);
   goFor(8);
   rotateServo(0);

 //AEV goes back to start
   motorSpeed(4,60);
   goToRelativePosition(61,2,60);

 //AEV attaches to load
   motorSpeed(4,0);
   goToRelativePosition(255,0,0);
   brake(4);
   rotateServo(55);
   goFor(5);
   rotateServo(0);

 

Final Performance Test 2, Go To Relative Position change):

//**************************************************************************

// (6) Go to Relative Position

void goToRelativePosition(int numOfMarks, int DesiredSpeed, int startpower)

 

 // goToRelativePosition(numOfMarks, Speed, startpower);

 

 // Continues the previous statement until the vehicle has traveled

 // “numOfMarks” from the current position.

 // Example Call on Routine:

 // motorSpeed(1,23);

 // goToRelativePosition(130,5,20);

 // Input:

 // numOfMarks: Number of marks to travel from current position.

 // Speed = the speed in marks per 60ms you want the AEV to travel at.

 // startpower = power you started the motor off on

 // Output: none

 // Compute end mark to travel to.

 int endMark = encoderPos + numOfMarks;

 //int endMark = numOfMarks + encoderTotal;

 Serial.println(endMark,DEC);

 // If the end mark is greater than the current position

 if(endMark > encoderPos)

do

int GoGojuice = GetSpeed(); // get the current speed

  if (GoGojuice < DesiredSpeed) // if not fast enough, go faster by 1%

    motorSpeed(4,startpower+1);

    startpower = startpower+1;

  else if (GoGojuice > DesiredSpeed) // if too fast, slow down by 1%

    motorSpeed(4,startpower-1);

    startpower = startpower-1;

  if (startpower > 100)

      startpower = 100;

 // end check, prevent AEV from accidentally going past 0% power

  else if (startpower <= 2)  

       startpower = 4;

  // Every loop call recordData

  recordData(minTimeLapse);

  Serial.println(encoderPos,DEC);

while(encoderPos < endMark);

 else // Else, if the the end mark is less than the current position

do

int GoGojuice = GetSpeed();

  // if not fast enough, go faster by 1%

  if (GoGojuice < DesiredSpeed)

    motorSpeed(4,startpower+1);

    startpower = startpower+1;

  else if (GoGojuice > DesiredSpeed) // if too fast, slow down by 1%

    motorSpeed(4,startpower-1);

    startpower = startpower-1;

  if (startpower > 100)

      startpower = 100;

  else if (startpower <= 2)

    startpower = 4;

  // Every loop call recordData

  recordData(minTimeLapse);

  Serial.println(encoderPos,DEC);

while(encoderPos > endMark);

   

Final Performance Test 3, GetSpeed function):

// (11) Get current Speed

int GetSpeed()

 

 // GetSpeed();

 // Gets Current vehicle speed in marks/60ms

 // Input: none

 // Output: Current Vehicle Speed in marks per 60 ms

 //————————————————————————-

 int Marksi = encoderTotal; // Getting total marks at start of command

 delay(60);              // Waiting 60 milliseconds (the amount of time to record a mark position)

 int Marksf = encoderTotal; // Setting total marks at end of command

 int Speedreturn = (Marksf – Marksi); // Change in marks over 60 milliseconds

 return Speedreturn;