Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <SD.h>
  3.  
  4. #include <AFMotor.h>
  5. #include "SR04.h"
  6.  
  7. //Ustawienia karty SD
  8. File myFile;
  9. const int8_t DISABLE_CHIP_SELECT = 10;
  10.  
  11. int pinCS = 53; // Pin komunikacji z karta SD
  12.  
  13. //zmienne dla predkosciomierza
  14. const byte motor_A = 20; //Motor1 interrupt pin
  15. const byte motor_B = 21; //Motor2 interrupt pin
  16.  
  17. volatile int counter_A = 0; //puls counter variables
  18. volatile int counter_B = 0;
  19.  
  20. float diskslots = 20.00; // encoders slots
  21.  
  22. float average_speed;
  23. const float wheeldiameter = 65.10; // wheel diameter (mm)
  24.  
  25. //Zmienne dla millis
  26. unsigned long startMillis;
  27. unsigned long currentMillis;
  28. unsigned long speedMillis;
  29. const unsigned long period = 100;
  30.  
  31. bool timeToWrite = false;
  32.  
  33. // This is a structure to hold data.
  34. struct robotLog
  35. {
  36. uint8_t Direction; // 0 - prosto 1 - skret
  37. };
  38.  
  39. typedef struct robotLog RobotLog;
  40.  
  41. // Now create the buffer. The size can be changed
  42. const unsigned int numOfLogsToBuffer = 10;
  43. RobotLog sdBuffer[numOfLogsToBuffer];
  44.  
  45. // index to indicate the current position in the buffer.
  46. unsigned int logBufferIndex = 0;
  47.  
  48.  
  49. //Ustawienie sensorow
  50. int LEFT_SENSOR_TRIG = A2;
  51. int LEFT_SENSOR_ECHO = A3;
  52. int FRONT_SENSOR_TRIG = A0;
  53. int FRONT_SENSOR_ECHO = A1;
  54.  
  55. SR04 sr04_left = SR04(LEFT_SENSOR_ECHO, LEFT_SENSOR_TRIG);
  56. SR04 sr04_front = SR04(FRONT_SENSOR_ECHO, FRONT_SENSOR_TRIG);
  57.  
  58. //Ustawienie silnikow
  59. AF_DCMotor motor1(1, MOTOR12_1KHZ);
  60. AF_DCMotor motor2(3, MOTOR34_1KHZ);
  61.  
  62. int NO_OBSTACLE = 0;
  63. int OBSTACLE = 1;
  64.  
  65. //Zmienne PID
  66. const float Kp_wall = 8; // stala uzyta do skretu
  67. const float Ki_wall = 0.003; // stala calki uzyta do poprawy bledu
  68. const float Kd_wall = 3; // stala rozniczki uzyta do poprawy predkosci poprawy bledu
  69.  
  70. const float Kp_front = 30; // stala uzyta do skretu
  71. const float Ki_front = 0; // stala calki uzyta do poprawy bledu
  72. const float Kd_front = 15; // stala rozniczki uzyta do poprawy predkosci poprawy bledu
  73.  
  74. const int offset = 13; // docelowa odleglosc
  75. const int max_front_distance = 20; // maksymalna odleglosc z przodu
  76.  
  77. int Tp = 128; // docelowa predkosc - target power
  78.  
  79. float error_wall = 0;
  80. float lastError_wall = 0;
  81. int correction_wall = 0;
  82. float p_wall = 0; //proporcja
  83. float i_wall = 0; //calka
  84. float d_wall = 0; //rozniczka
  85.  
  86. int Wall = 0;
  87. int Front = 0;
  88.  
  89. float error_front = 0;
  90. float lastError_front = 0;
  91. float correction_front = 0;
  92. float p_front = 0; //proporcja
  93. float i_front = 0; //calka
  94. float d_front = 0; //rozniczka
  95.  
  96. int leftMotorSpeed = 0;
  97. int rightMotorSpeed = 0;
  98.  
  99. int robot_status = 0; // 0 - jazda prosto; 1 - skret w lewo; 2 - przeszkoda
  100. int front_status = 0;
  101.  
  102. //0 - true; 1 - false
  103. int rightTurnBegin = 0;
  104. int leftTurnBegin = 0;
  105. int straightLineBegin = 0;
  106.  
  107. int speedSet = 0;
  108.  
  109. void setup() {
  110. Serial.begin(9600);
  111. //Ustawienie czytnika kart SD
  112. pinMode(53, OUTPUT);
  113.  
  114. if (!SD.begin(pinCS))
  115. {
  116. Serial.println("SD card initialization failed");
  117. return;
  118. } else
  119. {
  120. Serial.println("SD card ready");
  121. }
  122.  
  123. SD.remove("DaneMapy.txt");
  124. myFile = SD.open("DaneMapy.txt", O_CREAT | O_APPEND | O_WRITE);
  125. myFile.close();
  126. }
  127.  
  128. void loop() {
  129. currentMillis = millis();
  130.  
  131. timerMillis();
  132. if (timeToWrite == true) {
  133. if (leftMotorSpeed != 0 && rightMotorSpeed != 0) {
  134. storeData();
  135. }
  136. } else {
  137. //starts robot
  138. checking_status();
  139. }
  140. }
  141.  
  142. void checking_status() {
  143. front_status = GET_FRONT_STATUS();
  144.  
  145. if (front_status == OBSTACLE) {
  146.  
  147. if (leftTurnBegin == 0) {
  148. zero_wall();
  149. }
  150. followWall_front();
  151.  
  152. // Zmieniam flage skretu na false do nastepnego czasu az natrafi na przeszkode
  153. rightTurnBegin = 1;
  154. straightLineBegin = 0;
  155.  
  156. }
  157. if (front_status != OBSTACLE) {
  158. followWall();
  159. }
  160. }
  161.  
  162. void followWall() {
  163. //Kiedy robot zaczyna jechac prosto (dopoki nie ma przeszkody) zmieniam flage na true
  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. //jezeli error_wall jest mniejszy od 10, oznacza to ze robot jedzie obok sciany poprawnie
  174. if (error_wall < 10) {
  175.  
  176. if (straightLineBegin == 0) {
  177. zero_wall();
  178. }
  179.  
  180. if (correction_wall > 127 && correction_wall > 0) {
  181. correction_wall = 127;
  182. }
  183.  
  184. if (correction_wall < -127 && correction_wall < 0) {
  185. correction_wall = -127;
  186. }
  187.  
  188. rightMotorSpeed = Tp + correction_wall;
  189. leftMotorSpeed = Tp - correction_wall;
  190. robot_status = 0;
  191. leftTurnBegin = 0;
  192. straightLineBegin = 1;
  193. } else {
  194.  
  195. //jezeli error_wall jest wiekszy od 10, oznacza to ze robot musi skrecic w lewo
  196.  
  197. //zeruje wartosci na poczatku skretu w lewo
  198. if (leftTurnBegin == 0) {
  199. zero_wall();
  200. zero_front();
  201. }
  202.  
  203. //PID do skretu w lewo
  204. int speed = 2.5 * error_wall + 7 * 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. robot_status = 1;
  217.  
  218. leftTurnBegin = 1;
  219. straightLineBegin = 0;
  220. }
  221.  
  222. motor1.setSpeed(rightMotorSpeed);
  223. motor1.run(FORWARD);
  224. motor2.setSpeed(leftMotorSpeed);
  225. motor2.run(FORWARD);
  226.  
  227. lastError_wall = error_wall;
  228. zero_front();
  229. }
  230.  
  231. void zero_wall(void) {
  232. i_wall = 0;
  233. d_wall = 0;
  234. lastError_wall = 0;
  235. }
  236.  
  237. void zero_front(void) {
  238. i_front = 0;
  239. d_front = 0;
  240. lastError_front = 0;
  241. }
  242.  
  243. void followWall_front()
  244. {
  245. //tu umiec zczytywanie z sensora
  246.  
  247. Front = sr04_front.Distance();
  248. error_front = (Front - max_front_distance);
  249. i_front = (i_front + error_front);
  250. d_front = (error_front - lastError_front);
  251.  
  252. correction_front = Kp_front * error_front + Ki_front * i_front + Kd_front * d_front;
  253.  
  254.  
  255. if (correction_front > 127 && correction_front > 0) {
  256. correction_front = 127;
  257. }
  258.  
  259. if (correction_front < -127 && correction_front < 0) {
  260. correction_front = -127;
  261. }
  262.  
  263. rightMotorSpeed = Tp + correction_front;
  264. leftMotorSpeed = Tp - correction_front;
  265. robot_status = 2;
  266.  
  267. motor1.setSpeed(rightMotorSpeed);
  268. motor1.run(FORWARD);
  269. motor2.setSpeed(leftMotorSpeed);
  270. motor2.run(FORWARD);
  271.  
  272. lastError_front = error_front;
  273. }
  274.  
  275. void timerMillis() {
  276. if (currentMillis - startMillis >= period) {
  277. timeToWrite = true;
  278. startMillis = currentMillis;
  279. }
  280. }
  281.  
  282. void storeData() {
  283. sdBuffer[logBufferIndex].Direction = robot_status;
  284.  
  285. logBufferIndex++;
  286. if (logBufferIndex == 10) {
  287. motor1.setSpeed(0);
  288. motor1.run(FORWARD);
  289. motor2.setSpeed(0);
  290. motor2.run(FORWARD);
  291. myFile = SD.open("DaneMapy.txt", O_CREAT | O_APPEND | O_WRITE);
  292. for (int i = 0; i < 10; i++) {
  293. if (myFile) {
  294. myFile.println(sdBuffer[i].Direction);
  295. }
  296. }
  297. myFile.close();
  298. logBufferIndex = 0;
  299. }
  300. timeToWrite = false;
  301. }
  302.  
  303. int mediana(int n, int x[]) {
  304. int temp;
  305. int i, j;
  306. for (i = 0; i < n - 1; i++) {
  307. for (j = i + 1; j < n; j++) {
  308. if (x[j] < x[i]) {
  309. temp = x[i];
  310. x[i] = x[j];
  311. x[j] = temp;
  312. }
  313. }
  314. }
  315. return x[n / 2];
  316. }
  317. int GET_LEFT_STATUS(void) {
  318. if (sr04_left.Distance() < offset)
  319. return OBSTACLE;
  320. else
  321. return NO_OBSTACLE;
  322. }
  323.  
  324. int GET_FRONT_STATUS(void) {
  325. if (sr04_front.Distance() < max_front_distance)
  326. return OBSTACLE;
  327. else
  328. return NO_OBSTACLE;
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement