Advertisement
hockeygoalie5

Arduino

Mar 24th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.76 KB | None | 0 0
  1. // EDIT THE NUMERATOR TO HOW MANY MARKS YOU READ IN ONE
  2. // REVOLUTION (USE THE SENSOR TEST):
  3. float marksPerInch = 7 / 3.9; // 1 rev = 7 marks / 3.9 in diameter
  4. // IF LEFT IS NEGATIVE, SET TO FALSE
  5. bool motorsForward = true; // are the motors currently set to spin forward?
  6. int cruiseSpeed = 50; // how fast the motors should go normally
  7. int uphillSpeed = 75; // speed to get up a hill
  8. int manueverSpeed = 20; // speed when correcting (set to minimum)
  9.  
  10. void myCode()
  11. {
  12.   //----------------------------------------------------------------------------------------
  13.   // myCode();
  14.   // Note:
  15.   // (1) After running your AEV do not turn the AEV off, connect the AEV to a computer, or
  16.   //     push the reset button on the Arduino. There is a 13 second processing period. In
  17.   //     post processing, data is stored and battery recuperation takes place.
  18.   // (2) Time, current, voltage, total marks, position traveled are recorded approximately
  19.   //     every 60 milliseconds. This may vary depending on the vehicles operational tasks.
  20.   //     It takes approximately 35-40 milliseconds for each recording. Thus when programming,
  21.   //     code complexity may not be beneficial.
  22.   // (3) Always comment your code. Debugging will be quicker and easier to do and will
  23.   //     especially aid the instructional team in helping you.
  24.   //----------------------------------------------------------------------------------------
  25.   // WE HAVE TO MANUALLY PROGRAM GOING UP/DOWN THE HILL DUE TO THE
  26.   // SPEED WE NEED. IF IT ISN'T NECESSARY, THEN USE JUST
  27.   // goToPosition().
  28.   // IF LEFT IS NEGATIVE, NEGATE ALL OF THE POSITIONS
  29.   reverse(2); // one of our motors faces the opposite direction
  30.   // go up first hill
  31.   motorSpeed(4, uphillSpeed - 10); // start slow
  32.   goToRelativePosition(3 * 12 * marksPerInch); // then, halfway up
  33.   motorSpeed(4, uphillSpeed); // go full speed
  34.   goToRelativePosition(3 * 12 * marksPerInch); // brake at the top
  35.   // end go up first hill
  36.   goToPosition(8 * 12, cruiseSpeed); // go to the top of the incline
  37.   // attach other cart
  38.   goFor(4); // wait four seconds
  39.   // go down first hill
  40.   // IF LEFT IS NEGATIVE ADD A ! (i.e., !motorsForward)
  41.   if(motorsForward) { // if the function didn't leave us going right
  42.     reverse(4); // change direction
  43.     motorsForward = !motorsForward; // ditto
  44.   }
  45.   motorSpeed(4, cruiseSpeed); // start going down the hill
  46.   goToRelativePosition(-2 * 12 * marksPerInch); // half way down
  47.   reverse(4); // reverse direction
  48.   motorSpeed(4, cruiseSpeed); // thrust opposite to motion to stay slow
  49.   goToRelativePosition(-4 * 12 * marksPerInch); // at the bottom
  50.   reverse(4); // reverse again
  51.   // end go down first hill
  52.   goToPosition(-2 * 12, cruiseSpeed); // go to the second stop at 20%
  53.   goFor(4); // wait four seconds
  54.   goToPosition(-11 * 12, cruiseSpeed); // go to the third stop
  55.   goFor(4); // wait four seconds
  56.   // go up second hill
  57.   motorSpeed(4, cruiseSpeed); // move to the bottom of the hill
  58.   goToRelativePosition(-1 * 12 * marksPerInch); // ditto
  59.   motorSpeed(4, uphillSpeed - 10); // start slow
  60.   goToRelativePosition(-3 * 12 * marksPerInch); // then, halfway up
  61.   motorSpeed(4, uphillSpeed); // go full blast
  62.   goToRelativePosition(-3 * 12 * marksPerInch); // brake at the top
  63.   // end go up second hill
  64.   goToPosition(-20 * 12, cruiseSpeed); // go to the top of the second incline
  65.   goFor(4); // wait four seconds
  66.   // go down second hill
  67.   // IF LEFT IS NEGATIVE REMOVE THE ! (i.e., motorsForward)
  68.   if(!motorsForward) { // if the function didn't leave us facing the right way
  69.     reverse(4); // change direction
  70.     motorsForward = !motorsForward; // ditto
  71.   }
  72.   motorSpeed(4, cruiseSpeed); // start going down the hill
  73.   goToRelativePosition(2 * 12 * marksPerInch); // half way down
  74.   reverse(4); // reverse direction
  75.   motorSpeed(4, cruiseSpeed); // thrust opposite to motion to stay slow
  76.   goToRelativePosition(4 * 12 * marksPerInch); // at the bottom
  77.   reverse(4); // reverse again
  78.   // end go down second hill
  79.   goToPosition(-12 * 12, cruiseSpeed); // go to the fifth stop
  80.   // detach other cart
  81.   goFor(4); // wait four seconds
  82.   goToPosition(0, 40); // go to the maintenance stop
  83. }
  84.  
  85. // Accepts an absolute position in inches and a motor speed.
  86. // Automatically handles motor direction, breaking
  87. // and self-correction.
  88. void goToPosition(int targetPosition, int mSpeed) {
  89.   while(true) {
  90.     float curPosIn = getVehiclePostion() / marksPerInch; // convert vehicle position to inches
  91.     if(curPosIn < (targetPosition - 3)) { // If we're behind the target position with a 6 inch tolerance
  92.       if(!motorsForward) { // and the motors are set to backwards
  93.         reverse(4); // reverse the motors
  94.         motorsForward = !motorsForward; // and update our variable
  95.       }
  96.       motorSpeed(4, mSpeed); // then travel at the set speed
  97.     } else if(curPosIn > (targetPosition + 3)) { // If we're in front of the target position with a 6 inch tolerance
  98.       if(motorsForward) { // and the motors are set to forward
  99.         reverse(4); // reverse the motors
  100.         motorsForward = !motorsForward; // and update our variable
  101.       }
  102.       motorSpeed(4, mSpeed); // then travel at the set speed
  103.     } else {
  104.       brake(4); // we're within tolerence, time to brake
  105.       goFor(1.5); // wait for half a second in case we're drifting still
  106.       curPosIn = getVehiclePostion() / marksPerInch; // get our position after that time
  107.       if(curPosIn > (targetPosition - 3) & curPosIn < (targetPosition + 3)) {
  108.         break; // if we're still within tolerance, we're done here
  109.       } else {
  110.         mSpeed = manueverSpeed; // if we've drifted too much, we're going
  111.         // to have to do this all over again, but lets set the
  112.         // speed down to reduce overshooting the target.
  113.       }
  114.     }
  115.   }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement