Advertisement
Guest User

MEGA_Sketch

a guest
Jan 18th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 KB | None | 0 0
  1. #include <NewPing.h>
  2. #include <MFRC522.h>
  3. #include <EngineControl.h>
  4. #include "I2Cdev.h"
  5. #include "MPU6050_6Axis_MotionApps20.h"
  6. #include "Wire.h"
  7. #include <avr/wdt.h> // library for default watchdog functions
  8. #include <avr/interrupt.h> // library for interrupts handling
  9. #include <avr/sleep.h> // library for sleep
  10. #include <avr/power.h>
  11.  
  12. MPU6050 mpu;
  13. int MPUOffsets[6] = { -363, -480, 4803, 171, 19, -21};
  14. #define LED_PIN 13
  15. volatile bool mpuInterrupt = false;
  16. void dmpDataReady() {
  17. mpuInterrupt = true;
  18. }
  19.  
  20. #define interruptPin 3
  21. int giro = 0;
  22. int FifoAlive = 0;
  23. int IsAlive = -20;
  24. uint8_t mpuIntStatus;
  25. uint8_t devStatus;
  26. uint16_t packetSize;
  27. uint16_t fifoCount;
  28. uint8_t fifoBuffer[128];
  29. Quaternion q;
  30. VectorInt16 aa;
  31. VectorInt16 aaReal;
  32. VectorInt16 aaWorld;
  33. VectorFloat gravity;
  34. float euler[3];
  35. float ypr[3];
  36. float Yaw, Pitch, Roll;
  37. void MPU6050Connect() {
  38. static int MPUInitCntr = 0;
  39. mpu.initialize(); // same
  40. devStatus = mpu.dmpInitialize();// same
  41. if (devStatus != 0) {
  42. char * StatStr[5] { "No Error", "initial memory load failed", "DMP configuration updates failed", "3", "4"};
  43. MPUInitCntr++;
  44. Serial.print(F("MPU connection Try #"));
  45. Serial.println(MPUInitCntr);
  46. Serial.print(F("DMP Initialization failed (code "));
  47. Serial.print(StatStr[devStatus]);
  48. Serial.println(F(")"));
  49. if (MPUInitCntr >= 10) return; //only try 10 times
  50. delay(1000);
  51. MPU6050Connect(); // Lets try again
  52. return;
  53. }
  54. mpu.setXAccelOffset(MPUOffsets[0]);
  55. mpu.setYAccelOffset(MPUOffsets[1]);
  56. mpu.setZAccelOffset(MPUOffsets[2]);
  57. mpu.setXGyroOffset(MPUOffsets[3]);
  58. mpu.setYGyroOffset(MPUOffsets[4]);
  59. mpu.setZGyroOffset(MPUOffsets[5]);
  60. Serial.println(F("Enabling DMP..."));
  61. mpu.setDMPEnabled(true);
  62. Serial.println(F("Enabling interrupt detection (Arduino external interrupt pin 2 on the Uno)..."));
  63. attachInterrupt(digitalPinToInterrupt(2), dmpDataReady, FALLING); //pin 2 on the Uno
  64. mpuIntStatus = mpu.getIntStatus();
  65. packetSize = mpu.dmpGetFIFOPacketSize();
  66. delay(1000); // Let it Stabalize
  67. mpu.resetFIFO(); // Clear fifo buffer
  68. mpu.getIntStatus();
  69. mpuInterrupt = false; // wait for next interrupt
  70. }
  71. void i2cSetup() {
  72. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  73. Wire.begin();
  74. TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
  75. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  76. Fastwire::setup(400, true);
  77. #endif
  78. }
  79.  
  80.  
  81.  
  82. const EnginePins FRONT_LEFT_ENGINE_PINS = {7, 30, 31};
  83. const EnginePins FRONT_RIGHT_ENGINE_PINS = {6, 32, 33};
  84. const SensorPins SENSOR_PINS = {A15, A14, A13, A12, A11};
  85.  
  86. MFRC522 mfrc522(53, 5);
  87. EngineControl engineControl(255, 200);
  88. NewPing sonar(22, 23);
  89.  
  90. String message = "";
  91. String initialTag = "";
  92. int rfidIndex = 0;
  93. boolean leavedBase = false;
  94. const int BUZZER = 4;
  95. int timer = millis();
  96.  
  97. void setup() {
  98. Serial.begin(115200);
  99. Serial1.begin(9600);
  100. while (!Serial);
  101. i2cSetup();
  102. Serial.println(F("Alive"));
  103. MPU6050Connect();
  104. pinMode(interruptPin, INPUT_PULLUP);
  105. Serial.print("Nivel interrupcao: ");Serial.println(digitalRead(interruptPin));
  106. pinMode(BUZZER, OUTPUT);
  107. digitalWrite(BUZZER, HIGH);
  108. pinMode(LED_PIN, OUTPUT);
  109. digitalWrite(LED_PIN, HIGH);
  110. while(timer <=5000) {
  111. Serial1.read();
  112. timer = millis();
  113. }
  114. Going_To_Sleep();
  115.  
  116.  
  117. SPI.begin();
  118. mfrc522.PCD_Init();
  119. mfrc522.PCD_AntennaOn();
  120. mfrc522.PCD_SetAntennaGain(0x07 << 4);
  121. engineControl.setFrontLeftPins(FRONT_LEFT_ENGINE_PINS);
  122. engineControl.setFrontRightPins(FRONT_RIGHT_ENGINE_PINS);
  123. engineControl.setSensorPins(SENSOR_PINS);
  124. Serial.println("Setup completo");
  125. }
  126.  
  127. void loopEngine() {
  128. float distance = sonar.ping_cm();
  129. if (distance < 15 && distance > 0) {
  130. engineControl.parar();
  131. } else {
  132. engineControl.loop();
  133. }
  134. }
  135.  
  136. void Going_To_Sleep() {
  137. Serial.println("Dormindo");
  138. digitalWrite(LED_PIN, LOW);
  139. sleep_enable();
  140. attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, LOW);
  141. set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  142. delay(1000);
  143. sleep_cpu();
  144. Serial.println("Acordei");
  145. Serial.print("Nivel interrupcao: "); Serial.println(digitalRead(interruptPin));
  146. digitalWrite(LED_PIN, HIGH);
  147. }
  148.  
  149. void wakeUp() {
  150. Serial.println("Interrupção feita");
  151. sleep_disable();
  152. detachInterrupt(digitalPinToInterrupt(interruptPin));
  153. }
  154.  
  155. void loop() {
  156.  
  157. digitalWrite(BUZZER, HIGH);
  158. String currentDirection;
  159.  
  160. if (message == "") {
  161. if (Serial1.available()) {
  162. message = Serial1.readString();
  163. Serial.println(message);
  164. digitalWrite(BUZZER, LOW);
  165. delay(200);
  166. digitalWrite(BUZZER, HIGH);
  167. }
  168. return;
  169. }
  170.  
  171. if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
  172. if (leavedBase == true) {
  173. loopEngine();
  174. }
  175. return;
  176. }
  177.  
  178. Serial.println("rfid encontrado");
  179.  
  180. String rfidUid = "";
  181. for (byte i = 0; i < mfrc522.uid.size; i++) {
  182. rfidUid += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
  183. rfidUid += String(mfrc522.uid.uidByte[i], HEX);
  184. }
  185.  
  186. rfidUid.toLowerCase();
  187. rfidUid.trim();
  188. Serial.println(rfidUid);
  189.  
  190. if (initialTag == rfidUid){
  191. return;
  192. }
  193.  
  194. initialTag = rfidUid;
  195.  
  196. int incomingRfidIndex = message.indexOf(rfidUid);
  197.  
  198. if (incomingRfidIndex == rfidIndex) {
  199. Serial1.print(rfidUid);
  200. currentDirection = message.substring(rfidIndex + 9, rfidIndex + 10);
  201. rfidIndex += 11;
  202. }
  203.  
  204. Serial.print("nova direção: ");
  205.  
  206. Serial.println(currentDirection);
  207.  
  208.  
  209. static long QTimer = millis();
  210. if ((long)( millis() - QTimer ) >= 100) {
  211. QTimer = millis();
  212. Serial.print(F("\t Yaw")); Serial.print(Yaw);
  213. Serial.println();
  214. }
  215.  
  216. Serial.println(message);
  217. if (currentDirection == "b") {
  218. engineControl.impulso();
  219. loopEngine();
  220. leavedBase = true;
  221. } else if (currentDirection == "d") {
  222. engineControl.parar();
  223. Serial.println("Parou");
  224. Serial.println(Yaw);
  225. delay(1000);
  226. while (Yaw >= giro-90) {
  227. int count = 1;
  228. Serial.println("Entrou while");
  229. engineControl.direita();
  230. Serial.println(Yaw);
  231. if (count == 1) {
  232. Serial.println("Contou 1");
  233. if (mpuInterrupt) {
  234. GetDMP();
  235. }
  236. }
  237. static long QTimer = millis();
  238. if ((long)( millis() - QTimer ) >= 10) {
  239. QTimer = millis();
  240. Serial.print(F("\t Yaw")); Serial.print(Yaw);
  241. Serial.println();
  242. }
  243. count++;
  244. Serial.print("Contou: ");
  245. Serial.println(count);
  246. }
  247. Serial.println("Saiu while");
  248. giro = giro-90;
  249.  
  250. engineControl.parar();
  251. delay(500);
  252.  
  253. loopEngine();
  254. } else if (currentDirection == "e") {
  255. engineControl.parar();
  256. Serial.println("Parou");
  257. Serial.println(Yaw);
  258. delay(1000);
  259. while (Yaw <= giro+90) {
  260. int count = 1;
  261. Serial.println("Entrou while");
  262. engineControl.esquerda();
  263. Serial.println(Yaw);
  264. if (count == 1) {
  265. Serial.println("Contou 1");
  266. if (mpuInterrupt) {
  267. GetDMP();
  268. }
  269. }
  270. static long QTimer = millis();
  271. if ((long)( millis() - QTimer ) >= 10) {
  272. QTimer = millis();
  273. Serial.print(F("\t Yaw")); Serial.print(Yaw);
  274. Serial.println();
  275. }
  276. count++;
  277. Serial.print("Contou: ");
  278. Serial.println(count);
  279. }
  280. Serial.println("Saiu while");
  281. giro = giro+90;
  282.  
  283. engineControl.parar();
  284. delay(500);
  285. loopEngine();
  286. } else if (currentDirection == "f") {
  287. loopEngine();
  288. digitalWrite(BUZZER, LOW);
  289. delay(150);
  290. digitalWrite(BUZZER, HIGH);
  291. delay(150);
  292. digitalWrite(BUZZER, LOW);
  293. delay(150);
  294. digitalWrite(BUZZER, HIGH);
  295. } else if (currentDirection == "t") {
  296. engineControl.tras();
  297. } else if (currentDirection == "p") {
  298. engineControl.parar();
  299. Serial.println("ligando buzzer");
  300. digitalWrite(BUZZER, LOW);
  301. delay(150);
  302. digitalWrite(BUZZER, HIGH);
  303. message = "";
  304. rfidIndex = 0;
  305. leavedBase = false;
  306. Going_To_Sleep();
  307. }
  308. }
  309.  
  310. void GetDMP() {
  311. mpuInterrupt = false;
  312. FifoAlive = 1;
  313. fifoCount = mpu.getFIFOCount();
  314. uint16_t MaxPackets = 50;
  315. if ((fifoCount % packetSize) || (fifoCount > (packetSize * MaxPackets)) || (fifoCount < packetSize)) {
  316. digitalWrite(LED_PIN, LOW);
  317. Serial.println(F("Reset FIFO"));
  318. if (fifoCount % packetSize) Serial.print(F("\t Packet corruption"));
  319. Serial.print(F("\tfifoCount ")); Serial.print(fifoCount);
  320. Serial.print(F("\tpacketSize ")); Serial.print(packetSize);
  321. mpuIntStatus = mpu.getIntStatus();
  322. Serial.print(F("\tMPU Int Status ")); Serial.print(mpuIntStatus , BIN);
  323. if (mpuIntStatus & B10000) {
  324. Serial.print(F("\tFIFO buffer overflow interrupt "));
  325. }
  326. if (mpuIntStatus & B1000) {
  327. Serial.print(F("\tSlave I2c Device Status Int "));
  328. }
  329. if (mpuIntStatus & B1) {
  330. Serial.print(F("\tData Ready interrupt "));
  331. }
  332. Serial.println();
  333. mpu.resetFIFO();
  334. mpu.getIntStatus();
  335. } else {
  336. while (fifoCount >= packetSize) {
  337. if (fifoCount < packetSize) break;
  338. mpu.getFIFOBytes(fifoBuffer, packetSize);
  339. fifoCount -= packetSize;
  340. }
  341. MPUMath();
  342. digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  343. if (fifoCount > 0) mpu.resetFIFO();
  344. }
  345. }
  346. void MPUMath() {
  347. mpu.dmpGetQuaternion(&q, fifoBuffer);
  348. mpu.dmpGetGravity(&gravity, &q);
  349. mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  350. Yaw = (ypr[0] * 180 / M_PI);
  351. Pitch = (ypr[1] * 180 / M_PI);
  352. Roll = (ypr[2] * 180 / M_PI);
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement