Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 KB | None | 0 0
  1. //
  2. // TRUCKBOT
  3. //
  4.  
  5. #include <Servo.h>
  6.  
  7. // SN754410 Quad Half-H Driver
  8. int motorPin1= 3;      // 1A
  9. int motorPin2 = 5;     // 2A
  10. int steeringPin1 = 9;  // 4A
  11. int steeringPin2 = 10; // 3A
  12. // Rangefinder Servo
  13. int servopin = 12;
  14. int ServoDirection = 1;
  15. int ServoPos = 0;
  16.  
  17. long mapArray[RANGEFINGER_FOV];  // stores measure from each degree in the 90 degree FOV
  18. long optionArray[MAX_PATH_OPTIONS];
  19.  
  20. bool ready = FALSE;              // wait until ready to move
  21.  
  22. // constants
  23. const int SERVO_START_POS = 45;
  24. const int SERVODELAY = 2;
  25. const int RANGEFINGER_FOV = 90;
  26. const int MAX_PATH_OPTIONS = 5;  // for now, hard/soft right/left and forward.
  27. const int CRUISE_SPEED = 150;    // pwm motor speed, 150/255
  28.  
  29. const int LEFT = -1;
  30. const int FORWARD = 0;
  31. const int RIGHT = 1;
  32. const int REVERSE = 1;
  33. const int STOP = -1;
  34. const int HARD = 250;
  35. const int SOFT = 150;
  36.  
  37. void setup()
  38. {
  39.   memset(mapArray, 0, sizeof(mapArray)) // initialize mapArray to 0
  40.   Serial.begin(96000);
  41.   myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
  42.   pinMode(motorPin1, OUTPUT);
  43.   pinMode(motorPin2, OUTPUT);
  44.   pinMode(steeringPin1, OUTPUT);
  45.   pinMode(steeringPin2, OUTPUT);
  46.   myservo.write(SERVO_START_POS);
  47. }
  48.  
  49. void loop()
  50. {
  51.   buildMapArray(pos);         // populate mapArray[pos] with distance from rangefinder
  52.   if(ServoPos == 45 || [ServoPos == 135)   // servo sweep 45-135 = 90degree FOV
  53.     {
  54.       ServoDirection *= -1;   // servo return sweep
  55.       shrinkArray();          // shrink Maparray to OptionArray[MAX_PATH_OPTIONS]
  56.       calcNextAction();
  57.      }    
  58.   while(ready){
  59.     move();}   
  60.   ServoPos = ServoPos + ServoDirection;
  61.   myservo.write(pos);        
  62.   delay(SERVODELAY);  
  63. }
  64.  
  65. void calcNextAction(){
  66.  
  67. int dLongest = 0;  // variable to record index of longest distance in mapArry
  68. int dShortest = 0; // variable to record index of shortest distance in mapArry
  69.  
  70.   for(i=0;i<=MAX_PATH_OPTIONS;i++)
  71.   {
  72.     // Determine longest/shortest distance indexs
  73.     if (mapArray[dLongest] < mapArray[i]){dLongest = i;}
  74.     if (mapArray[dShortest] > mapArray[i]){dShortest = i;}    
  75.   }
  76.   switch(i){
  77.     case 0: move(FORWARD, CRUISE_SPEED*.4, LEFT, HARD);    
  78.     case 1: move(FORWARD, CRUISE_SPEED*.2, LEFT, SOFT);
  79.     case 2: move(FORWARD, CRUISE_SPEED, FORWARD, HARD);
  80.     case 3: move(FORWARD, CRUISE_SPEED*.2, RIGHT, SOFT);
  81.     case 4: move(FORWARD, CRUISE_SPEED*.4, RIGHT, HARD);
  82.   }
  83. }
  84.  
  85. void move(int vMotor, byte sMotor, int vSteering, byte sSteering){
  86.   switch (vMotor){
  87.   case 0:
  88.     digitalWrite(motorPin1, LOW);
  89.     analogWrite(motorPin2, sMotor);
  90.     break;
  91.   case -1:
  92.     digitalWrite(motorPin1, LOW);
  93.     digitalWrite(motorPin2, LOW);
  94.     break;
  95.   case 1:
  96.     analogWrite(motorPin1, sMotor);
  97.     digitalWrite(motorPin2, LOW);
  98.     break;
  99.   }
  100.   switch (vSteering){
  101.   case -1:
  102.     analogWrite(steeringPin1, sSteering);
  103.     digitalWrite(steeringPin2, LOW);
  104.     break;
  105.   case 0:
  106.     digitalWrite(steeringPin1, LOW);
  107.     analogWrite(steeringPin2, LOW );
  108.     break;
  109.   case 1:
  110.     digitalWrite(steeringPin1, LOW);
  111.     digitalWrite(steeringPin2, sSteering);  
  112.     break;
  113.   }
  114. }
  115.  
  116. void shrinkArray(){
  117. int i = 0;
  118. int n = 0;
  119. int count = 0;
  120. int divisor = RANGEFINGER_FOV / MAX_PATH_OPTIONS;  
  121.  
  122.   for(i=0;i<=MAX_PATH_OPTIONS;i++){
  123.     for(n=0;n<=divisor;n++){OptionArray[i]+=mapArray[count+n];}
  124.     OptionArray[i] =/ divisor;
  125.     count += divisor;
  126.     }
  127.   }
  128. }
  129. void buildMap(){
  130. long duration, inches, cm;  
  131.   pinMode(pingPin, OUTPUT);      // RangeFinder is triggered by a HIGH pulse of 2 or more microseconds.
  132.   digitalWrite(pingPin, LOW);    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  133.   delayMicroseconds(2);
  134.   digitalWrite(pingPin, HIGH);
  135.   delayMicroseconds(5);
  136.   digitalWrite(pingPin, LOW);
  137.   pinMode(pingPin, INPUT);
  138.   duration = pulseIn(pingPin, HIGH);
  139.   inches = microsecondsToInches(duration);
  140.   cm = microsecondsToCentimeters(duration);
  141.   mapArray[ServoPos] = cm;       // record measurement in mapArray
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement