Guest User

Untitled

a guest
Jan 14th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.32 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 "TimerOne.h"
  8. #include <IRremote.h>
  9.  
  10. //Ustawienia karty SD
  11. File myFile;
  12. const int8_t DISABLE_CHIP_SELECT = 10;
  13.  
  14. int pinCS = 53; // Pin to communicate with SD card
  15.  
  16. //Ustawienie timera
  17. unsigned long startTime;
  18. unsigned long followWallTime;
  19. unsigned long followWall_frontTime;
  20.  
  21. //Ustawienie predkosciomierza
  22. const byte motor_A = 20; //Motor1 interrupt pin
  23. const byte motor_B = 21; //Motor2 interrupt pin
  24.  
  25. volatile int counter_A = 0; //puls counter variables
  26. volatile int counter_B = 0;
  27.  
  28. float diskslots = 20.00; // encoders slots
  29.  
  30. const float wheeldiameter = 65.10; // wheel diameter (mm)
  31.  
  32. //Ustawienie sensorow
  33. int LEFT_SENSOR_TRIG = A2;
  34. int LEFT_SENSOR_ECHO = A3;
  35. int FRONT_SENSOR_TRIG = A0;
  36. int FRONT_SENSOR_ECHO = A1;
  37.  
  38. SR04 sr04_left = SR04(LEFT_SENSOR_ECHO, LEFT_SENSOR_TRIG);
  39. SR04 sr04_front = SR04(FRONT_SENSOR_ECHO, FRONT_SENSOR_TRIG);
  40.  
  41. //Ustawienie pilota
  42. int RECV_PIN = 22;
  43.  
  44. IRrecv irrecv(RECV_PIN);
  45. decode_results results;
  46.  
  47. //Ustawienie silnikow
  48. AF_DCMotor motor1(1, MOTOR12_1KHZ);
  49. AF_DCMotor motor2(3, MOTOR34_1KHZ);
  50.  
  51. int NO_OBSTACLE = 0;
  52. int OBSTACLE = 1;
  53.  
  54. //Zmienne PID
  55. const float Kp_wall = 16;
  56. const float Ki_wall = 0.003;
  57. const float Kd_wall = 11;
  58.  
  59. const float Kp_front = 30;
  60. const float Ki_front = 0;
  61. const float Kd_front = 15;
  62.  
  63. const int offset = 12; // offset distance
  64. const int max_front_distance = 16; // max front distance
  65.  
  66. int Tp = 128; // target power
  67.  
  68. float error_wall = 0;
  69. float lastError_wall = 0;
  70. int correction_wall = 0;
  71. float p_wall = 0;
  72. float i_wall = 0;
  73. float d_wall = 0;
  74.  
  75. int Wall = 0;
  76. int Front = 0;
  77.  
  78. float error_front = 0;
  79. float lastError_front = 0;
  80. float correction_front = 0;
  81. float p_front = 0;
  82. float i_front = 0;
  83. float d_front = 0;
  84.  
  85. int leftMotorSpeed = 0;
  86. int rightMotorSpeed = 0;
  87.  
  88. int front_status = 0;
  89.  
  90. //int rangeFrontObstacle = 0;
  91. //int rangeWallObstacle = 0;
  92. //int rangeFront = 0;
  93. //int rangeWall = 0;
  94. //int speedSet = 0;
  95.  
  96. //0 - true; 1 - false
  97. int rightTurnBegin = 0;
  98. int leftTurnBegin = 0;
  99. int straightLineBegin = 0;
  100.  
  101. int speedSet = 0;
  102.  
  103.  
  104.  
  105. void setup() {
  106.  
  107. Serial.begin(9600);
  108.  
  109. //SD setting
  110. pinMode(53, OUTPUT);
  111. delay(5000);
  112.  
  113. //Ustawienie timera
  114. Timer1.initialize(1000000); // setting timer to 1 sec
  115. //zwieksz counter1 gdy pin predkosciomieza jest na Hig
  116. attachInterrupt(digitalPinToInterrupt (motor_A), ISR_count_A, RISING);
  117. //zwieksz counter2 gdy pin predkosciomieza jest na High
  118. attachInterrupt(digitalPinToInterrupt (motor_B), ISR_count_B, RISING);
  119. Timer1.attachInterrupt(ISR_timerone);
  120.  
  121. if (SD.begin(SS))
  122. {
  123. Serial.println("SD card is ready to use.");
  124. } else
  125. {
  126. Serial.println("SD card initialization failed");
  127. return;
  128. }
  129.  
  130. myFile = SD.open("DaneMapy.txt", FILE_WRITE);
  131.  
  132. if(myFile){
  133. myFile.print("Lewy silnik cm/s, Prawy silnik cm/s");
  134. }
  135. myFile.close();
  136. }
  137.  
  138. void loop() {
  139.  
  140. // put your main code here, to run repeatedly:
  141. beginning:
  142.  
  143. //scheck obstacle in front
  144. front_status = GET_FRONT_STATUS();
  145.  
  146. if (front_status == OBSTACLE) {
  147.  
  148. if (leftTurnBegin == 0) {
  149. zero_wall();
  150. }
  151. followWall_front();
  152.  
  153. rightTurnBegin = 1;
  154. straightLineBegin = 0;
  155.  
  156. goto beginning;
  157. }
  158.  
  159. followWall();
  160.  
  161. }
  162.  
  163. void followWall() {
  164. rightTurnBegin = 0;
  165.  
  166. Wall = sr04_left.Distance();
  167. error_wall = Wall - offset;
  168. i_wall = (i_wall + error_wall);
  169. d_wall = (error_wall - lastError_wall);
  170.  
  171. correction_wall = Kp_wall * error_wall + Ki_wall * i_wall + Kd_wall * d_wall;
  172.  
  173. //if error less than 10, robot drives correctly
  174.  
  175. if (error_wall < 10) {
  176.  
  177. if (straightLineBegin == 0) {
  178. zero_wall();
  179. }
  180.  
  181. if (correction_wall > 127 && correction_wall > 0) {
  182. correction_wall = 127;
  183. }
  184.  
  185. if (correction_wall < -127 && correction_wall < 0) {
  186. correction_wall = -127;
  187. }
  188.  
  189. rightMotorSpeed = Tp + correction_wall;
  190. leftMotorSpeed = Tp - correction_wall;
  191.  
  192. leftTurnBegin = 0;
  193. straightLineBegin = 1;
  194. } else {
  195.  
  196. //if error greater than 10, robot has to turn right
  197.  
  198. if (leftTurnBegin == 0) {
  199. zero_wall();
  200. zero_front();
  201. }
  202.  
  203. //PID for left turn
  204. int speed = 2.5 * error_wall + 8 * d_wall;
  205.  
  206. if (speed > 127 && speed > 0) {
  207. speed = 127;
  208. }
  209.  
  210. if (speed < -127 && speed < 0) {
  211. speed = -127;
  212. }
  213.  
  214. rightMotorSpeed = Tp + (speed);
  215. leftMotorSpeed = Tp - (speed);
  216.  
  217. leftTurnBegin = 1;
  218. straightLineBegin = 0;
  219. }
  220.  
  221. motor1.setSpeed(rightMotorSpeed);
  222. motor1.run(FORWARD);
  223. motor2.setSpeed(leftMotorSpeed);
  224. motor2.run(FORWARD);
  225.  
  226. lastError_wall = error_wall;
  227. zero_front();
  228. }
  229.  
  230. void zero_wall(void) {
  231. i_wall = 0;
  232. d_wall = 0;
  233. lastError_wall = 0;
  234. }
  235.  
  236. void zero_front(void) {
  237. i_front = 0;
  238. d_front = 0;
  239. lastError_front = 0;
  240. }
  241.  
  242. void followWall_front()
  243. {
  244.  
  245. Front = sr04_front.Distance();
  246. error_front = (Front - max_front_distance);
  247. i_front = (i_front + error_front);
  248. d_front = (error_front - lastError_front);
  249.  
  250. correction_front = Kp_front * error_front + Ki_front * i_front + Kd_front * d_front;
  251.  
  252.  
  253. if (correction_front > 160 && correction_front > 0) {
  254. correction_front = 160;
  255. }
  256.  
  257. if (correction_front < -160 && correction_front < 0) {
  258. correction_front = -160;
  259. }
  260.  
  261. rightMotorSpeed = 95 + correction_front;
  262. leftMotorSpeed = 95 - correction_front;
  263.  
  264. motor1.setSpeed(rightMotorSpeed);
  265. motor1.run(FORWARD);
  266. motor2.setSpeed(leftMotorSpeed);
  267. motor2.run(FORWARD);
  268.  
  269. lastError_front = error_front;
  270. }
  271.  
  272.  
  273. //Rpm to cm/s
  274. int RMPtoCmS(float RPM){
  275. int result;
  276. float radius = (wheeldiameter / 2);
  277. float metersPerSecond = (radius * 0.001) * RPM * 0.10472; //speed m/s
  278. float cmPerSec = metersPerSecond * 100; // m/s to cm/s
  279.  
  280. float f_result = cmPerSec;
  281. result = (int) f_result;
  282.  
  283. return result;
  284. }
  285.  
  286. //(Interupt Service Routines ISR)
  287.  
  288. void ISR_count_A(){
  289. counter_A++;
  290. }
  291.  
  292. void ISR_count_B(){
  293. counter_B++;
  294. }
  295.  
  296. //TimerOne ISR
  297. void ISR_timerone(){
  298. Timer1.detachInterrupt(); //Stop timer
  299.  
  300. Serial.print("Motor1 Speed: ");
  301. float rotation_A = (counter_A / diskslots) * 60.00; //(RPM) for motor1
  302. Serial.print(RMPtoCmS(rotation_A));
  303. Serial.print(" Cm/s - ");
  304. counter_A = 0;
  305.  
  306. Serial.print("Motor2 Speed: ");
  307. float rotation_B = (counter_B / diskslots) * 60.00; //(RPM) for motor2
  308. Serial.print(RMPtoCmS(rotation_B));
  309. Serial.println(" Cm/s");
  310. counter_B = 0;
  311.  
  312. float average_speed = (RMPtoCmS(rotation_A) + RMPtoCmS(rotation_B)) /2;
  313.  
  314. myFile = SD.open("DaneMapy.txt", FILE_WRITE);
  315. if (myFile) {
  316. Serial.println("Writing to file...");
  317. myFile.print("Motor1: ");
  318. myFile.print(RMPtoCmS(rotation_A));
  319. myFile.print("Motor2: ");
  320. myFile.print(RMPtoCmS(rotation_B));
  321. myFile.print(" average_speed: ");
  322. myFile.print(average_speed);
  323. myFile.close();
  324. // delay(500);
  325. Serial.println("Done.");
  326. }
  327.  
  328. Timer1.attachInterrupt(ISR_timerone);
  329. }
  330.  
  331. int GET_LEFT_STATUS(void) {
  332. if (sr04_left.Distance() < offset)
  333. return OBSTACLE;
  334. else
  335. return NO_OBSTACLE;
  336. }
  337.  
  338. int GET_FRONT_STATUS(void) {
  339. if (sr04_front.Distance() < max_front_distance)
  340. return OBSTACLE;
  341. else
  342. return NO_OBSTACLE;
  343. }
Add Comment
Please, Sign In to add comment