gihanchanaka

Untitled

Aug 6th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*ALAbot
  3.  
  4. Edit history
  5. 2016-08-06-11:26 gihanchanaka :
  6.   Added sonar return time methods
  7.   Added sonar micro second to centemeter conversion method
  8.   @Dushan, complete the above funtions
  9.  
  10. 2016-08-06-12-20 dushan:
  11.   completed sonar distance
  12.  
  13. 2016-08-06-12:25 gihanchanaka:
  14.   Added whichSideShouldTheRobotGo()
  15.  
  16. 2016-08-06-15:04 gihanchanaka:
  17.     completed robotTurnRight(),robotTurnLeft().robotGoForward() functions
  18.  
  19. 2016-08-06-15:09 gihanchanaka:
  20.   debugging
  21. */
  22.  
  23. //The following constants are used to define the pins used for sonar
  24. const int sonarCommonPingPin;
  25. const int sonarForwardTriggerPin;
  26. const int sonarLeftTriggerPin;
  27. const int sonarRightTriggerPin;
  28.  
  29.  
  30. //The following constants are used to define the interegers returned in the direction choosing method
  31. const int goRight=1;
  32. const int goForward=2;
  33. const int goLeft=2;
  34.  
  35.  
  36. //This is used to maintain the gap and also decide when to turn
  37. const int gapBetweenRobotAndWall;
  38.  
  39. //These data is used to configure the motor pins
  40. //motorOne RIGHT side mortor
  41. //motorTwo LEFT side mortor
  42. const int motorOneDirection;
  43. const int motorTwoDirection;
  44. const int motorOneBrake;
  45. const int motorTwoBrake;
  46. const int motorOneSpeed;
  47. const int motorTwoSpeed;
  48.  
  49. //This data stores the time needed for the motors to turn the robot by 90 degrees
  50. const int microSecondsForATurn;
  51. const int percentageSpeedInTurning;
  52.  
  53. int sonarReturnTimeMicroSecondsForward(){
  54.  
  55.   // establish variables for duration of the ping,
  56.   // and the distance result in inches and centimeters:
  57.   long duration, forwardcm;
  58.   // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  59.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  60.    pinMode(sonarForwardTriggerPin, OUTPUT);
  61.    digitalWrite(sonarForwardTriggerPin, LOW);
  62.    delayMicroseconds(2);
  63.    digitalWrite(sonarForwardTriggerPin, HIGH);
  64.    delayMicroseconds(5);
  65.    digitalWrite(sonarForwardTriggerPin, LOW);
  66.  
  67.    pinMode(sonarCommonPingPin, INPUT);
  68.    duration = pulseIn(sonarCommonPingPin, HIGH);
  69.  
  70.    // convert the time into a distance
  71.    forwardcm = sonarMilliSecondsToCentiMeters(duration);
  72.    return forwardcm ;
  73.  
  74. }
  75.  
  76. int sonarReturnTimeMicroSecondsLeft(){
  77.  
  78.   // establish variables for duration of the ping,
  79.   // and the distance result in inches and centimeters:
  80.   long duration, leftcm;
  81.   // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  82.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  83.    pinMode(sonarLeftTriggerPin, OUTPUT);
  84.    digitalWrite(sonarLeftTriggerPin, LOW);
  85.    delayMicroseconds(2);
  86.    digitalWrite(sonarLeftTriggerPin, HIGH);
  87.    delayMicroseconds(5);
  88.    digitalWrite(sonarLeftTriggerPin, LOW);
  89.  
  90.    pinMode(sonarCommonPingPin, INPUT);
  91.    duration = pulseIn(sonarCommonPingPin, HIGH);
  92.  
  93.    // convert the time into a distance
  94.    leftcm = sonarMilliSecondsToCentiMeters(duration);
  95.    return leftcm ;
  96.  
  97. }
  98.  
  99. int sonarReturnTimeMicroSecondsRight(){
  100.  
  101.   // establish variables for duration of the ping,
  102.   // and the distance result in inches and centimeters:
  103.   long duration, rightcm;
  104.   // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  105.   // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  106.    pinMode(sonarRightTriggerPin, OUTPUT);
  107.    digitalWrite(sonarRightTriggerPin, LOW);
  108.    delayMicroseconds(2);
  109.    digitalWrite(sonarRightTriggerPin, HIGH);
  110.    delayMicroseconds(5);
  111.    digitalWrite(sonarRightTriggerPin, LOW);
  112.  
  113.    pinMode(sonarCommonPingPin, INPUT);
  114.    duration = pulseIn(sonarCommonPingPin, HIGH);
  115.  
  116.    // convert the time into a distance
  117.    rightcm = sonarMilliSecondsToCentiMeters(duration);
  118.    return rightcm ;
  119.  
  120.  
  121.  
  122.  
  123. }
  124.  
  125. int sonarMilliSecondsToCentiMeters(int microSeconds){
  126.  
  127.    // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  128.    // The ping travels out and back, so to find the distance of the
  129.    // object we take half of the distance travelled.
  130.    return microSeconds / 29 / 2;
  131.    
  132. }
  133.  
  134.  
  135. int whichSideShouldTheRobotGo(){
  136.   int distanceRight=sonarMilliSecondsToCentiMeters(sonarReturnTimeMicroSecondsRight());
  137.   int distanceForward=sonarMilliSecondsToCentiMeters(sonarReturnTimeMicroSecondsForward());
  138.   int distanceLeft=sonarMilliSecondsToCentiMeters(sonarReturnTimeMicroSecondsLeft());
  139.  
  140.   if(distanceRight>gapBetweenRobotAndWall){
  141.     robotTurnRight();
  142.   }
  143.   else{
  144.     if(distanceForward>gapBetweenRobotAndWall){
  145.       robotGoForward(100);
  146.     }
  147.     else{
  148.       robotTurnLeft();
  149.     }
  150.   }
  151.  
  152. }
  153.  
  154. void setPinModeForMotorPins(){
  155.   //This pinMode should go inside the setup;
  156.   pinMode(motorOneDirection,OUTPUT);
  157.   pinMode(motorOneBrake,OUTPUT);
  158.   pinMode(motorOneSpeed,OUTPUT);
  159.   pinMode(motorTwoDirection,OUTPUT);
  160.   pinMode(motorTwoBrake,OUTPUT);
  161.   pinMode(motorTwoSpeed,OUTPUT);  
  162. }
  163.  
  164. void robotGoForward(int speedPercentage){
  165.   setPinModeForMotorPins();
  166.  
  167.   digitalWrite(motorOneDirecton,HIGH);
  168.   digitalWrite(motorTwoDirecton,HIGH);
  169.   digitalWrite(motorOneBrake,LOW);
  170.   digitalWrite(motorTwoBrake,LOW);
  171.  
  172.   analogWrite(motorOneSpeed,(255*speedPercentage)/100);
  173.   analogWrite(motorTwoSpeed,(255*speedPercentage)/100);
  174.  
  175. }
  176.  
  177. void robotTurnRight(){
  178.   setPinModeForMotorPins();
  179.  
  180.   digitalWrite(motorOneDirecton,LOW);
  181.   digitalWrite(motorTwoDirecton,HIGH);
  182.   digitalWrite(motorOneBrake,LOW);
  183.   digitalWrite(motorTwoBrake,HIGH);
  184.  
  185.   analogWrite(motorOneSpeed,(255*0)/100);
  186.   analogWrite(motorTwoSpeed,(255*percentageSpeedInTurning)/100);
  187.   delay(microSecondsForATurn);  
  188. }
  189.  
  190. void robotTurnLeft(){
  191.   setPinModeForMotorPins();
  192.  
  193.   digitalWrite(motorOneDirecton,HIGH);
  194.   digitalWrite(motorTwoDirecton,LOW);
  195.   digitalWrite(motorOneBrake,HIGH);
  196.   digitalWrite(motorTwoBrake,LOW);
  197.  
  198.   analogWrite(motorOneSpeed,(255*percentageSpeedInTurning)/100);
  199.   analogWrite(motorTwoSpeed,(255*0)/100);
  200.   delay(microSecondsForATurn);  
  201. }
Add Comment
Please, Sign In to add comment