Guest User

PID_Robot

a guest
Jan 7th, 2019
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3.  
  4. #include <AFMotor.h>
  5. //#include <NewPing.h>
  6. #include "SR04.h"
  7. #include <IRremote.h>
  8.  
  9. //SD module settings
  10. File myFile;
  11. const int8_t DISABLE_CHIP_SELECT = 10;
  12.  
  13. int pinCS = 53; // Pin komunikacji z karta SD
  14.  
  15. //Sensors settings
  16. int LEFT_SENSOR_TRIG = A2;
  17. int LEFT_SENSOR_ECHO = A3;
  18. int FRONT_SENSOR_TRIG = A0;
  19. int FRONT_SENSOR_ECHO = A1;
  20.  
  21. SR04 sr04_left = SR04(LEFT_SENSOR_ECHO, LEFT_SENSOR_TRIG);
  22. SR04 sr04_front = SR04(FRONT_SENSOR_ECHO, FRONT_SENSOR_TRIG);
  23.  
  24. //Remote controll settings
  25. int RECV_PIN = 40;
  26.  
  27. IRrecv irrecv(RECV_PIN);
  28. decode_results results;
  29.  
  30. //DC motors settings
  31. AF_DCMotor motor1(1, MOTOR12_1KHZ);
  32. AF_DCMotor motor2(3, MOTOR34_1KHZ);
  33.  
  34. int NO_OBSTACLE = 0;
  35. int OBSTACLE = 1;
  36.  
  37. //PID
  38. const float Kp_wall = 20;
  39. const float Ki_wall = 0.003;
  40. const float Kd_wall = 9;
  41.  
  42. const float Kp_front = 30;
  43. const float Ki_front = 0;
  44. const float Kd_front = 15;
  45.  
  46. const int offset = 12; // target distance
  47. const int max_front_distance = 16; // max front distanc
  48.  
  49. int Tp = 128; // target power
  50.  
  51. float error_wall = 0;
  52. float lastError_wall = 0;
  53. int correction_wall = 0;
  54. float p_wall = 0;
  55. float i_wall = 0;
  56. float d_wall = 0;
  57.  
  58. int Wall = 0;
  59. int Front = 0;
  60.  
  61. float error_front = 0;
  62. float lastError_front = 0;
  63. float correction_front = 0;
  64. float p_front = 0;
  65. float i_front = 0;
  66. float d_front = 0;
  67.  
  68. int leftMotorSpeed = 0;
  69. int rightMotorSpeed = 0;
  70.  
  71. int front_status = 0;
  72.  
  73. //int rangeFrontObstacle = 0;
  74. //int rangeWallObstacle = 0;
  75. //int rangeFront = 0;
  76. //int rangeWall = 0;
  77. //int speedSet = 0;
  78.  
  79. //0 - true; 1 - false
  80. int rightTurnBegin = 0;
  81. int leftTurnBegin = 0;
  82. int straightLineBegin = 0;
  83.  
  84. int speedSet = 0;
  85.  
  86.  
  87.  
  88. void setup() {
  89. pinMode(RECV_PIN, INPUT);
  90. irrecv.enableIRIn();
  91.  
  92. Serial.begin(9600);
  93. pinMode(pinCS, OUTPUT);
  94. delay(5000);
  95.  
  96. if (SD.begin())
  97. {
  98. Serial.println("SD card is ready to use.");
  99. } else
  100. {
  101. Serial.println("SD card initialization failed");
  102. return;
  103. }
  104.  
  105. }
  106.  
  107. void loop() {
  108.  
  109. beginning:
  110.  
  111. myFile = SD.open("DaneMapy.txt", FILE_WRITE);
  112.  
  113. if (irrecv.decode(&results))
  114. {
  115. switch (results.value)
  116. {
  117. case 0xFF22DD: //vol-
  118. Serial.print("falling back");
  119. fall_back();
  120. break;
  121.  
  122. case 0xFF02FD: //vol+
  123. drive_forward();
  124. break;
  125.  
  126. case 0xFFC23D: //play/pause
  127. motor_stop();
  128. break;
  129. }
  130. irrecv.resume();
  131. }
  132. //checks if there is obstacle in front
  133. front_status = GET_FRONT_STATUS();
  134.  
  135. if (front_status == OBSTACLE) {
  136.  
  137. if (leftTurnBegin == 0) {
  138. zero_wall();
  139. }
  140. followWall_front();
  141.  
  142. //changing turn flag until next obstacle
  143. rightTurnBegin = 1;
  144. straightLineBegin = 0;
  145.  
  146. goto beginning;
  147. }
  148.  
  149. followWall();
  150. }
  151.  
  152. void followWall() {
  153.  
  154. //When robot drives straight (until there is no obstacle) changing flag to true
  155.  
  156. rightTurnBegin = 0;
  157.  
  158. Wall = sr04_left.Distance();
  159. error_wall = Wall - offset;
  160. i_wall = (i_wall + error_wall);
  161. d_wall = (error_wall - lastError_wall);
  162.  
  163. correction_wall = Kp_wall * error_wall + Ki_wall * i_wall + Kd_wall * d_wall;
  164.  
  165.  
  166. //if error is less then 10, it means that the robot follow the wall correctly
  167. if (error_wall < 10) {
  168.  
  169. if (straightLineBegin == 0) {
  170. zero_wall();
  171. }
  172.  
  173. if (correction_wall > 127 && correction_wall > 0) {
  174. correction_wall = 127;
  175. }
  176.  
  177. if (correction_wall < -127 && correction_wall < 0) {
  178. correction_wall = -127;
  179. }
  180.  
  181. rightMotorSpeed = Tp + correction_wall;
  182. leftMotorSpeed = Tp - correction_wall;
  183.  
  184.  
  185. leftTurnBegin = 0;
  186. straightLineBegin = 1;
  187. } else {
  188.  
  189. //if error if greater then 10, robot has to turn right
  190.  
  191. if (leftTurnBegin == 0) {
  192. zero_wall();
  193. zero_front();
  194. }
  195.  
  196. //PID left turn
  197. int speed = 2.5 * error_wall + 8 * d_wall;
  198.  
  199. if (speed > 127 && speed > 0) {
  200. speed = 127;
  201. }
  202.  
  203. if (speed < -127 && speed < 0) {
  204. speed = -127;
  205. }
  206.  
  207. rightMotorSpeed = Tp + (speed);
  208. leftMotorSpeed = Tp - (speed);
  209.  
  210. leftTurnBegin = 1;
  211. straightLineBegin = 0;
  212. }
  213.  
  214. motor1.setSpeed(rightMotorSpeed);
  215. motor1.run(FORWARD);
  216. motor2.setSpeed(leftMotorSpeed);
  217. motor2.run(FORWARD);
  218.  
  219. lastError_wall = error_wall;
  220. zero_front();
  221. }
  222.  
  223. void zero_wall(void) {
  224. i_wall = 0;
  225. d_wall = 0;
  226. lastError_wall = 0;
  227. }
  228.  
  229. void zero_front(void) {
  230. i_front = 0;
  231. d_front = 0;
  232. lastError_front = 0;
  233. }
  234.  
  235. void followWall_front()
  236. {
  237.  
  238. Front = sr04_front.Distance();
  239. error_front = (Front - max_front_distance);
  240. i_front = (i_front + error_front);
  241. d_front = (error_front - lastError_front);
  242.  
  243. correction_front = Kp_front * error_front + Ki_front * i_front + Kd_front * d_front;
  244.  
  245.  
  246. if (correction_front > 127 && correction_front > 0) {
  247. correction_front = 127;
  248. }
  249.  
  250. if (correction_front < -127 && correction_front < 0) {
  251. correction_front = -127;
  252. }
  253.  
  254. rightMotorSpeed = Tp + correction_front;
  255. leftMotorSpeed = Tp - correction_front;
  256.  
  257. motor1.setSpeed(rightMotorSpeed);
  258. motor1.run(FORWARD);
  259. motor2.setSpeed(leftMotorSpeed);
  260. motor2.run(FORWARD);
  261.  
  262. lastError_front = error_front;
  263. }
  264.  
  265. void drive_forward() {
  266. motor1.setSpeed(128);
  267. motor2.setSpeed(128);
  268. motor1.run(FORWARD);
  269. motor2.run(FORWARD);
  270. delay(400);
  271. }
  272. //zatrzymuje robota na 5 sekund
  273. void motor_stop() {
  274. motor1.run(RELEASE);
  275. motor2.run(RELEASE);
  276. delay(5000);
  277. }
  278.  
  279. //wycofanie gdy robot wjedzie w przeszkode
  280. void fall_back() {
  281. motor1.setSpeed(128);
  282. motor2.setSpeed(128);
  283. motor1.run(BACKWARD);
  284. motor2.run(BACKWARD);
  285. delay(500);
  286. motor1.run(BACKWARD);
  287. motor2.run(FORWARD);
  288. delay(400);
  289. Serial.print("falling back");
  290. Serial.println();
  291. }
  292.  
  293.  
  294. int GET_LEFT_STATUS(void) {
  295. if (sr04_left.Distance() < offset)
  296. return OBSTACLE;
  297. else
  298. return NO_OBSTACLE;
  299. }
  300.  
  301. int GET_FRONT_STATUS(void) {
  302. if (sr04_front.Distance() < max_front_distance)
  303. return OBSTACLE;
  304. else
  305. return NO_OBSTACLE;
  306. }
Advertisement
Add Comment
Please, Sign In to add comment