Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. // Include libraries
  2. #include <Arduino.h>
  3. #include "SoftwareSerial.h"
  4. SoftwareSerial mySerial(23, 24); //rx,tx
  5. #include "math.h"
  6. #include "Adafruit_NeoPixel.h"
  7. #include "Adafruit_PWMServoDriver.h"
  8.  
  9. #define SCENECOUNT 9
  10.  
  11. // Define Motor parameters
  12. #define CHANGETIME 2000
  13. uint8_t PINMOTOR[2];
  14.  
  15. // Define Sensor parameters
  16. #define ECHO 9
  17. #define TRIG 8
  18.  
  19. // Define PWM parameters
  20. #define SERVOCOUNT 4
  21. #define DIST_Z 20
  22. Adafruit_PWMServoDriver pwms[SERVOCOUNT] = {Adafruit_PWMServoDriver(0x40), Adafruit_PWMServoDriver(0x41), Adafruit_PWMServoDriver(0x42), Adafruit_PWMServoDriver(0x43)};
  23.  
  24. // Structure for position variables
  25. struct position {byte x; byte y;};
  26.  
  27. // Structure for servos
  28. struct servoStruct {
  29. int zones[3][2];
  30. // Automatic variables
  31. uint16_t length[3];
  32. };
  33.  
  34. // Structure for scenes
  35. struct scenestruct {
  36. long startTime;
  37. long endTime;
  38. byte protect;
  39. byte pulse;
  40. // Automatic variables
  41. unsigned long time;
  42. float coefX[SERVOCOUNT];
  43. float coefY[SERVOCOUNT];
  44. };
  45.  
  46. /////////////////////////////////////////////
  47. /////////////////////////////////////////////
  48. /////////////////////////////////////////////
  49. // Scene variables
  50. byte id = 0;
  51. bool book;
  52.  
  53. // Servo Directions
  54. position start[SCENECOUNT][SERVOCOUNT] {
  55. {
  56. {10, 10},
  57. {0, 0},
  58. {0, 20},
  59. {20, 20}
  60. },
  61. {},
  62. {},
  63. {},
  64. {},
  65. {},
  66. {},
  67. {},
  68. {}
  69. };
  70.  
  71. position end[SCENECOUNT][SERVOCOUNT] {
  72. {
  73. {0, 0},
  74. {10, 10},
  75. {20, 20},
  76. {0, 0}
  77. },
  78. {},
  79. {},
  80. {},
  81. {},
  82. {},
  83. {},
  84. {},
  85. {}
  86. };
  87.  
  88. byte rad[SCENECOUNT][SERVOCOUNT] = {
  89. {50, 50, 0, 0},
  90. {4, 4, 4, 4},
  91. {},
  92. {},
  93. {},
  94. {},
  95. {},
  96. {},
  97. {}
  98. };
  99.  
  100. byte colors[SCENECOUNT][SERVOCOUNT][3] {
  101. {
  102. {100, 0, 100},
  103. {100, 100, 0},
  104. {0, 100, 100},
  105. {100, 100, 100}
  106. },
  107. {},
  108. {},
  109. {},
  110. {},
  111. {},
  112. {},
  113. {},
  114. {}
  115. };
  116.  
  117.  
  118.  
  119. // Servo structs
  120. servoStruct servos[SERVOCOUNT] {
  121. {},
  122. {{{155, 550}, {200, 515}, {150, 400}}},
  123. {},
  124. {}
  125. };
  126.  
  127. // Scene structs
  128. scenestruct scenes[SCENECOUNT] {
  129. {0, 20, 0b10, 0b1111},
  130. {23, 31, 0b10},
  131.  
  132. {34, 49, 0b10},
  133. {52, 57, 0b10},
  134.  
  135. {60, 62, 0b10},
  136. {65, 69, 0b10},
  137. {72, 75, 0b10},
  138.  
  139. {78, 101, 0b10},
  140. {104, 120, 0b10}
  141. };
  142.  
  143. // Initializate scene structures
  144. void initStructs() {
  145. // Init servos
  146. for (int servo=0; servo < SERVOCOUNT; servo++) {
  147. // INIT PWM
  148. pwms[servo].begin();
  149. pwms[servo].setPWMFreq(50);
  150.  
  151. // INIT VARIABLES
  152. for (byte j; j < 3; j++) {
  153. servos[servo].length[j] = abs(servos[servo].zones[j][0] - servos[servo].zones[j][1]);
  154. }
  155. delay(100);
  156. }
  157.  
  158. // Init scenes
  159. unsigned int time = millis();
  160. for(int scene=0; scene<SCENECOUNT; scene++) {
  161. if (scenes[scene].startTime < 1000) scenes[scene].startTime *= 1000;
  162. if (scenes[scene].endTime < 1000) scenes[scene].endTime *= 1000;
  163. scenes[scene].startTime += time;
  164. scenes[scene].endTime += time;
  165. scenes[scene].time = scenes[scene].endTime - scenes[scene].startTime;
  166.  
  167. for (int servo=0; servo < SERVOCOUNT; servo++) {
  168. scenes[scene].coefX[servo] = (float)abs(start[scene][servo].x - end[scene][servo].x) / scenes[scene].time;
  169. scenes[scene].coefY[servo] = (float)abs(start[scene][servo].y - end[scene][servo].y) / scenes[scene].time;
  170. Serial.println((float)abs(start[scene][servo].x - end[scene][servo].x) / scenes[scene].time);
  171. }
  172. }
  173. }
  174.  
  175. // Function for display the storm
  176. void storm(scenestruct scene) {
  177. // TODO: Storm function
  178. }
  179.  
  180. // Function for circuit frame
  181. void circuit(scenestruct scene) {
  182. // TODO: Circuit function
  183. }
  184.  
  185. void moveServo(byte servo, float x, float y, int zones[3][2]) {
  186. float xAngle = x == 0 ? 90 : atan(x / DIST_Z) * RAD_TO_DEG + 90;
  187. float yAngle = y == 0 ? 90 : atan(y / DIST_Z) * RAD_TO_DEG + 90;
  188.  
  189. Serial.print("Angles:");
  190. Serial.print("\t");
  191. Serial.print(x);
  192. Serial.print("\t");
  193. Serial.print(y);
  194. Serial.print("\t");
  195. Serial.print(xAngle);
  196. Serial.print("\t");
  197. Serial.print(yAngle);
  198. Serial.print("\t");
  199. Serial.print(map(xAngle, 0, 180, zones[0][0], zones[0][1]));
  200. Serial.print("\t");
  201. Serial.print(map(yAngle, 0, 180, zones[1][0], zones[1][1]));
  202. Serial.println("\t");
  203.  
  204. pwms[servo].setPWM(0, 0, map(xAngle, 0, 180, zones[0][0], zones[0][1]));
  205. pwms[servo].setPWM(1, 0, map(yAngle, 0, 180, zones[1][0], zones[1][1]));
  206. pwms[servo].setPWM(2, 0, map(rad[id][servo], 0, 100, zones[2][0], zones[2][1]));
  207. }
  208.  
  209. // Function for color dioids
  210. void colorDiods(byte servo) {
  211. for (byte color=0; color < 3; color++) {
  212. for (byte pin=4; pin < 8; pin++) {
  213. pwms[servo].setPin(pin+color*4, map(colors[id][servo][color], 0, 100, 0, 4095)); // TODO: ^ | v;
  214. }
  215. }
  216. }
  217.  
  218. // Function for move servos
  219. void updatePWMS(scenestruct scene) {
  220. for (int servo=0; servo < SERVOCOUNT; servo++) {
  221. Serial.print("Servo:\t");
  222. Serial.print(servo);
  223. Serial.print("\t");
  224. float x = scene.coefX[servo] * (millis() - scene.startTime) + start[id][servo].x;
  225. float y = scene.coefY[servo] * (millis() - scene.startTime) + start[id][servo].y;
  226. moveServo(servo, x, y, servos[servo].zones);
  227.  
  228. colorDiods(servo);
  229. }
  230. }
  231.  
  232. // Function for change scene
  233. volatile uint8_t motorState[2]; // 0 - off, 1 - start, 2 - go, 3 - stop
  234. uint8_t motorSpeed[2];
  235. uint32_t motorLastTime[2];
  236. void motorUpdate0() {motorState[0] = 3;} // функция обработчик прерывания 0 (второй пин)
  237. void motorUpdate1() {motorState[1] = 3;} // функция обработчик прерывания 1 (третий пин)
  238.  
  239.  
  240. void changeScene() {
  241. for (int i = 0; i < 2; i++) {
  242. if (motorState[i] == 0 || motorState[i] == 2) ; // ничего не делать, если выкл, если едет, читаем датчики ( работает прерывание )
  243. else if (millis() - motorLastTime[i] > CHANGETIME / 255) { // периодически изменяем скорость
  244. motorState[i] == 1 ? motorSpeed[i]++ : motorSpeed[i]--;
  245. motorLastTime[i] = millis();
  246. analogWrite(PINMOTOR[i], motorSpeed[i]);
  247. if (motorSpeed[i] == 255) { // активация прерывания
  248. if (i==0) attachInterrupt(i, motorUpdate0, FALLING);
  249. else attachInterrupt(i, motorUpdate1, FALLING);
  250. motorState[i] = 2;
  251. }
  252. else if (motorSpeed[i] == 0) {
  253. detachInterrupt(i); // отключение прерывания
  254. motorState[i] = 0;
  255. }
  256. }
  257. }
  258. }
  259.  
  260. // Function for check distance from UltraSonic
  261. bool checkDist() {
  262. digitalWrite(TRIG,1);
  263. delayMicroseconds(10);
  264. digitalWrite(TRIG,0);
  265.  
  266. int distance = pulseIn(ECHO, 1, 5000) / 58;
  267. // Serial.println(distance);
  268. return distance > 0;
  269. }
  270. //=======================================================
  271. //====================== MAIN CODE ======================
  272. //=======================================================
  273. void setup() {
  274. delay(3000);
  275. Serial.begin(9600);
  276. Serial.println("Let");
  277. mySerial.begin(38400);
  278.  
  279. pinMode(ECHO, INPUT);
  280. pinMode(TRIG, OUTPUT);
  281.  
  282.  
  283. byte state;
  284. do {
  285. if (millis() % 100 == 0) {
  286. state = mySerial.available() + checkDist()*2;
  287. book = state == 2 ? false : true;
  288. Serial.println(state);
  289. }
  290. } while (!state);
  291. Serial.println(book ? "Book is ok" : "Book is broken");
  292. initStructs();
  293. }
  294.  
  295. unsigned long time = millis();
  296. void loop() {
  297. Serial.print("Update:\t");
  298. Serial.print(id);
  299. Serial.println("\t");
  300.  
  301. if bitRead(scenes[id].protect, 0) storm(scenes[id]);
  302. if bitRead(scenes[id].protect, 1) updatePWMS(scenes[id]);
  303. changeScene();
  304.  
  305. if (millis() - time > 1.2*CHANGETIME && book ? Serial.available():checkDist()) {
  306. id++;
  307. motorState[0] = 1; motorState[1] = 1;
  308. time = millis();
  309. }
  310. Serial.println(" ");
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement