Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.86 KB | None | 0 0
  1. //the right back motor will be controlled by the motor A pins on the motor driver 1
  2. //I'm tired of commenting I basically changed every pin in this section.
  3. //This section was originally written for two motors by sparkfun and Jaeyoung made it work for four motors then I changed the pins. //john beck
  4.  
  5.  
  6.  
  7. //this section contains the motor driver facing away from the sensor.
  8. const int PWMA_1 = 11; //speed control pin on the motor driver for the right motor
  9. const int AIN1_1 = 13; //control pin 1 on the motor driver for the right motor //shift register pin
  10. const int AIN2_1 = 12; //control pin 2 on the motor driver for the right motor //shift register pin
  11.  
  12. //this section contains the motor driver facing away from the sensor.
  13. //the left motor will be controlled by the motor B pins on the motor driver 1
  14. const int PWMB_1 = 10; //speed control pin on the motor driver for the left motor
  15. const int BIN1_1 = 7; //control pin 1 on the motor driver for the left motor //shift register pin
  16. const int BIN2_1 = 8; //control pin 2 on the motor driver for the left motor //shift register pin
  17.  
  18.  
  19.  
  20.  
  21. const int trigPin = 6; //connects to the trigger pin on the distance sensor
  22. const int echoPin = 5; //connects to the echo pin on the distance sensor
  23.  
  24. float sensor_distance = 0; //stores the distance measured by the distance sensor
  25.  
  26. // Pin definitions:
  27. // The 74HC595 uses a type of serial connection called SPI
  28. // (Serial Peripheral Interface) that requires three pins:
  29.  
  30. int datapin = 8; //changed pin number //john beck
  31. int clockpin = 13; //changed pin number //john beck
  32. int latchpin = 12; //changed pin number //john beck
  33.  
  34. // We'll also declare a global variable for the data we're
  35. // sending to the shift register:
  36.  
  37. byte data = 0;
  38.  
  39. const int driveTime = 20; //this is the number of milliseconds that it takes the robot to drive 1 inch
  40. //it is set so that if you tell the robot to drive forward 25 units, the robot drives about 25 inches
  41.  
  42. const int turnTime = 8; //this is the number of milliseconds that it takes to turn the robot 1 degree
  43. //it is set so that if you tell the robot to turn right 90 units, the robot turns about 90 degrees
  44.  
  45. //Note: these numbers will vary a little bit based on how you mount your motors, the friction of the
  46. //surface that your driving on, and fluctuations in the power to the motors.
  47. //You can change the driveTime and turnTime to make them more accurate
  48.  
  49. String botDirection; //the direction that the robot will drive in (this change which direction the two motors spin in)
  50. String distance_string; //the distance to travel in each direction
  51.  
  52. float distance=600.0;
  53.  
  54. //These are the floats for calibrating each wheel speed. They are custom variables //John beck
  55. float left_adjustment=1, right_adjustment=1;
  56.  
  57. //these encode forwards back left and right //john beck
  58. char forwards=0, left=1, back=2, right=3;
  59.  
  60. //------------------FUNCTIONS-------------------------------
  61.  
  62.  
  63.  
  64. //These motor_specific functions were written for two motors by sparkfun. Jaeyoung rewrote it to work on four motors. I have tweaked them with constant speed adjustements for each wheel so that the thing goes
  65. //straight. Jaeyoung originally did her own speed adjustments but since I had to re-glue the wheels it needed to eb calibrated again. I changed all of the digitalWrite instnaces to digitalWrite (John Beck).
  66.  
  67. /********************************************************************************/
  68. void rightMotor(int motorSpeed) //function for driving the right motor
  69. {
  70. if (motorSpeed > 0) //if the motor should drive forward (positive speed)
  71. {
  72. digitalWrite(AIN1_1, HIGH); //set pin 1 to high
  73. digitalWrite(AIN2_1, LOW); //set pin 2 to low
  74. }
  75. else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
  76. {
  77. digitalWrite(AIN1_1, LOW); //set pin 1 to low
  78. digitalWrite(AIN2_1, HIGH); //set pin 2 to high
  79. }
  80. else //if the motor should stop
  81. {
  82. digitalWrite(AIN1_1, LOW); //set pin 1 to low
  83. digitalWrite(AIN2_1, LOW); //set pin 2 to low
  84. }
  85. analogWrite(PWMA_1, abs(motorSpeed * right_adjustment)); //now that the motor direction is set, drive it at the entered speed
  86. }
  87.  
  88. /********************************************************************************/
  89. void leftMotor(int motorSpeed) //function for driving the left motor
  90. {
  91. if (motorSpeed > 0) //if the motor should drive forward (positive speed)
  92. {
  93. digitalWrite(BIN1_1, HIGH); //set pin 1 to high
  94. digitalWrite(BIN2_1, LOW); //set pin 2 to low
  95. }
  96. else if (motorSpeed < 0) //if the motor should drive backward (negative speed)
  97. {
  98. digitalWrite(BIN1_1, LOW); //set pin 1 to low
  99. digitalWrite(BIN2_1, HIGH); //set pin 2 to high
  100. }
  101. else //if the motor should stop
  102. {
  103. digitalWrite(BIN1_1, LOW); //set pin 1 to low
  104. digitalWrite(BIN2_1, LOW); //set pin 2 to low
  105. }
  106. analogWrite(PWMB_1, abs(motorSpeed * left_adjustment)); //now that the motor direction is set, drive it at the entered speed
  107. }
  108.  
  109.  
  110.  
  111.  
  112. //RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
  113. float getDistance()
  114. {
  115. float echoTime; //variable to store the time it takes for a ping to bounce off an object
  116. float calculatedDistance; //variable to store the distance calculated from the echo time
  117.  
  118. //send out an ultrasonic pulse that's 10ms long
  119. digitalWrite(trigPin, HIGH);
  120. delayMicroseconds(10);
  121. digitalWrite(trigPin, LOW);
  122.  
  123. echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the
  124. //pulse to bounce back to the sensor
  125.  
  126. calculatedDistance = echoTime / 148.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)
  127.  
  128. return calculatedDistance; //send back the distance that was calculated
  129. }
  130.  
  131. //function by john beck
  132. void all_stop(){
  133. //Each of these four lines was written by sparkfun.
  134. rightMotor(0);
  135. leftMotor(0);
  136. }
  137.  
  138. //This function is stolen from the loop of the sparkfun. The original loop only included two motors and jaeyoung added four. I have put it into a movement function
  139. //I have also improved the design by reducing the number of lines of code necessary.
  140. void move(char botDirection, int movement_distance){
  141.  
  142. if(botDirection == forwards) //if the entered direction is forward
  143. {
  144. rightMotor(200); //drive the right wheel forward
  145. leftMotor(200); //drive the left wheel forward
  146.  
  147. delay(driveTime * movement_distance); //drive the motors long enough travel the entered distance
  148. rightMotor(0); //turn the right motor off
  149. leftMotor(0); //turn the left motor off
  150.  
  151. }
  152. else if(botDirection == back) //if the entered direction is backward
  153. {
  154. rightMotor(-200); //drive the right wheel forward
  155. leftMotor(-200); //drive the left wheel forward
  156.  
  157. delay(driveTime * movement_distance); //drive the motors long enough travel the entered distance
  158. rightMotor(0); //turn the right motor off
  159. leftMotor(0); //turn the left motor off
  160.  
  161. }
  162. else if(botDirection == left) //if the entered direction is right
  163. {
  164. rightMotor(-200); //drive the right wheel forward
  165. leftMotor(255); //drive the left wheel forward
  166.  
  167. delay(turnTime * movement_distance); //drive the motors long enough turn the entered distance
  168. rightMotor(0); //turn the right motor off
  169. leftMotor(0); //turn the left motor off
  170.  
  171. }
  172. else if(botDirection == right) //if the entered direction is left
  173. {
  174. rightMotor(255); //drive the right wheel forward
  175. leftMotor(-200); //drive the left wheel forward
  176.  
  177. delay(turnTime * movement_distance); //drive the motors long enough turn the entered distance
  178. rightMotor(0); //turn the right motor off
  179. leftMotor(0); //turn the left motor off
  180.  
  181. }
  182.  
  183.  
  184. /*
  185. //botDirection = Serial.readStringUntil(' '); //read the characters in the command until you reach the first space
  186. // distance = Serial.readStringUntil(' '); //read the characters in the command until you reach the second space
  187.  
  188. //print the command that was just received in the serial monitor
  189. //Serial.println(movement_direction);
  190. // Serial.println(movement_distance);
  191.  
  192. if(movement_direction==forwards) //if the entered direction is forward
  193. {
  194. rightMotor(200); //drive the right wheel forward
  195. leftMotor(200); //drive the left wheel forward
  196.  
  197. delay(driveTime * movement_distance); //drive the motors long enough turn the entered distance
  198.  
  199. }
  200. else if(movement_direction==back) //if the entered direction is backward
  201. {
  202. rightMotor(-200); //drive the right wheel forward
  203. leftMotor(-200); //drive the left wheel forward
  204.  
  205. delay(driveTime * movement_distance); //drive the motors long enough turn the entered distance
  206. }
  207. else if(movement_direction==right) //if the entered direction is right
  208. {
  209. rightMotor(-200); //drive the right wheel forward
  210. leftMotor(255); //drive the left wheel forward
  211.  
  212. delay(turnTime * movement_distance); //drive the motors long enough turn the entered distance
  213. }
  214. else if(movement_direction==left) //if the entered direction is left
  215. {
  216. rightMotor(255); //drive the right wheel forward
  217. leftMotor(-200); //drive the left wheel forward
  218.  
  219. delay(turnTime * movement_distance); //drive the motors long enough turn the entered distance
  220. }
  221.  
  222. all_stop();
  223. */
  224.  
  225. }
  226.  
  227.  
  228. /********************************************************************************/
  229. void setup()
  230. {
  231.  
  232. //I bairly ain't write nothin in the dang ol setup its mostly not my code yo. Don't be hatin.
  233.  
  234. //set the motor control pins as outputs
  235. pinMode(PWMA_1, OUTPUT);
  236. pinMode(AIN1_1, OUTPUT);
  237. pinMode(AIN2_1, OUTPUT);
  238.  
  239. pinMode(PWMB_1, OUTPUT);
  240. pinMode(BIN1_1, OUTPUT);
  241. pinMode(BIN2_1, OUTPUT);
  242.  
  243.  
  244. //sensor setup
  245. pinMode(trigPin, OUTPUT); //the trigger pin will output pulses of electricity
  246. pinMode(echoPin, INPUT); //the echo pin will measure the duration of pulses coming back from the distance sensor
  247.  
  248. // Set the three SPI pins to be outputs:
  249. //shift register setup
  250. pinMode(datapin, OUTPUT);
  251. pinMode(clockpin, OUTPUT);
  252. pinMode(latchpin, OUTPUT);
  253.  
  254. Serial.begin(9600); //begin serial communication with the computer
  255.  
  256. //prompt the user to enter a command
  257. /*Serial.println("Enter a direction followed by a distance.");
  258. Serial.println("f = forward, b = backward, r = turn right, l = turn left");
  259. Serial.println("Example command: f 50");*/
  260.  
  261. //for now the code is inside of setup because it occurs once. Make sure to erase this when moving to loop
  262. //move(forwards, 150);
  263. //move(forwards, 200);
  264. //rightMotor(200); //drive the right wheel forward
  265. //leftMotor(-200); //drive the left wheel forward
  266. //leftMotor(-200); //drive the right wheel forward
  267.  
  268.  
  269. // move(right,145);
  270.  
  271.  
  272. }
  273.  
  274.  
  275. int tries=0;
  276. /********************************************************************************/
  277. void loop()
  278. {
  279.  
  280. float distance2=getDistance();
  281. float difference=distance-distance2;
  282. if(difference < 0){
  283. difference=difference * -1;
  284. }
  285. if(difference < 20){
  286. tries++;
  287. }
  288. if(tries==100){
  289. move(back,30);
  290. tries=0;
  291. }
  292. distance=distance2;
  293. unsigned long le_time=millis();
  294. while(distance > 7.0 && millis() < (le_time + 8000)){
  295. leftMotor(200);
  296. rightMotor(200);
  297. distance=getDistance();
  298. Serial.println(distance);
  299. }
  300. Serial.println("out");
  301. all_stop();
  302. delay(200);
  303. move(back,30);
  304. move(right,270);
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement