Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. #include <PID_v1.h>
  2. #include <LMotorController.h>
  3. #include "I2Cdev.h"
  4. #include "MPU6050_6Axis_MotionApps20.h"
  5. #include <SoftwareSerial.h>
  6. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  7. #include "Wire.h"
  8. #endif
  9. #define MIN_ABS_SPEED 30
  10. MPU6050 mpu;
  11. //*************************************** Ajustes ***************************************
  12. double MotorVelocidadIzq = 1; //double MotorVelocidadIzq = 0.3;
  13. double MotorVelocidadDer = 1; //double MotorVelocidadDer = 0.3;
  14. double PuntoEquilibrio = 180.8;
  15. SoftwareSerial Serial_2 (4, 3);//SoftwareSerial Serial_2 (1, 0);
  16. //-----------------Control de Motores
  17. int ENA = 5;
  18. int IN1 = 6;
  19. int IN2 = 7;
  20. int IN3 = 9;
  21. int IN4 = 8;
  22. int ENB = 10;
  23. //------------------Los Valors de PID cambian con cada diseño
  24. double Kp = 60; //double Kp = 60;
  25. double Kd = 0; //double Kd = 2.2;
  26. double Ki = 0; //double Ki = 270;
  27.  
  28. //***************************************************************************************
  29. int estado = 'g'; // inicia detenido
  30. // MPU control/status vars
  31. bool dmpReady = false; // set true if DMP init was successful
  32. uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
  33. uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
  34. uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
  35. uint16_t fifoCount; // count of all bytes currently in FIFO
  36. uint8_t fifoBuffer[64]; // FIFO storage buffer
  37.  
  38. // orientation/motion vars
  39. Quaternion q; // [w, x, y, z] quaternion container
  40. VectorFloat gravity; // [x, y, z] gravity vector
  41. float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
  42.  
  43. //PID
  44.  
  45. double originalSetpoint = PuntoEquilibrio; //double originalSetpoint = 172.50;
  46.  
  47. double setpoint = originalSetpoint;
  48. double movingAngleOffset = 0.1;
  49. double input, output;
  50.  
  51.  
  52. PID pid(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);
  53.  
  54. double motorSpeedFactorLeft = MotorVelocidadIzq; //double motorSpeedFactorLeft = 0.6;
  55. double motorSpeedFactorRight = MotorVelocidadDer; //double motorSpeedFactorRight = 0.5;
  56.  
  57.  
  58. LMotorController motorController(ENA, IN1, IN2, ENB, IN3, IN4, motorSpeedFactorLeft, motorSpeedFactorRight);
  59.  
  60. volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
  61. void dmpDataReady()
  62. {
  63. mpuInterrupt = true;
  64. }
  65.  
  66.  
  67. void setup()
  68. {
  69. Serial_2.begin(9600); // inicia el puerto serial para comunicacion con el Bluetooth
  70. //Serial.begin(9600);
  71. // join I2C bus (I2Cdev library doesn't do this automatically)
  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. mpu.initialize();
  80.  
  81. devStatus = mpu.dmpInitialize();
  82.  
  83. // supply your own gyro offsets here, scaled for min sensitivity
  84. mpu.setXGyroOffset(220);
  85. mpu.setYGyroOffset(76);
  86. mpu.setZGyroOffset(-85);
  87. mpu.setZAccelOffset(1788); // 1688 factory default for my test chip
  88.  
  89. // make sure it worked (returns 0 if so)
  90. if (devStatus == 0)
  91. {
  92. // turn on the DMP, now that it's ready
  93. mpu.setDMPEnabled(true);
  94.  
  95. // enable Arduino interrupt detection
  96. attachInterrupt(0, dmpDataReady, RISING);
  97. mpuIntStatus = mpu.getIntStatus();
  98.  
  99. // set our DMP Ready flag so the main loop() function knows it's okay to use it
  100. dmpReady = true;
  101.  
  102. // get expected DMP packet size for later comparison
  103. packetSize = mpu.dmpGetFIFOPacketSize();
  104.  
  105. //setup PID
  106. pid.SetMode(AUTOMATIC);
  107. pid.SetSampleTime(10);
  108. pid.SetOutputLimits(-255, 255);
  109. }
  110. else
  111. {
  112. // ERROR!
  113. // 1 = initial memory load failed
  114. // 2 = DMP configuration updates failed
  115. // (if it's going to break, usually the code will be 1)
  116. Serial.print(F("DMP Initialization failed (code "));
  117. Serial.print(devStatus);
  118. Serial.println(F(")"));
  119. }
  120. }
  121.  
  122. void loop()
  123. {
  124.  
  125. //********************************************** Control Blu ************************
  126. if(Serial_2.available()>0){ // lee el bluetooth y almacena en estado
  127. estado = Serial_2.read();
  128. }
  129. if(estado=='a'){ // Boton desplazar al Frente
  130. setpoint = (setpoint + 0.5);
  131. //Serial.println(originalSetpoint);
  132. estado = 'g';
  133. }
  134. if(estado=='b'){ // Boton IZQ
  135.  
  136. estado = 'g';
  137. }
  138. if(estado=='c'){ // Boton Parar
  139. setpoint = PuntoEquilibrio;
  140.  
  141. estado = 'g';
  142. }
  143. if(estado=='d'){ // Boton DER
  144.  
  145. estado = 'g';
  146. }
  147.  
  148. if(estado=='e'){ // Boton Reversa
  149. setpoint = (setpoint - 0.5);
  150. estado = 'g';
  151. }
  152.  
  153. //********************************************** Fin Control Blu ************************
  154. // if programming failed, don't try to do anything
  155. if (!dmpReady) return;
  156.  
  157. // wait for MPU interrupt or extra packet(s) available
  158. while (!mpuInterrupt && fifoCount < packetSize)
  159. {
  160. //no mpu data - performing PID calculations and output to motors
  161. pid.Compute();
  162. motorController.move(output, MIN_ABS_SPEED);
  163.  
  164. }
  165.  
  166. // reset interrupt flag and get INT_STATUS byte
  167. mpuInterrupt = false;
  168. mpuIntStatus = mpu.getIntStatus();
  169.  
  170. // get current FIFO count
  171. fifoCount = mpu.getFIFOCount();
  172.  
  173. // check for overflow (this should never happen unless our code is too inefficient)
  174. if ((mpuIntStatus & 0x10) || fifoCount == 1024)
  175. {
  176. // reset so we can continue cleanly
  177. mpu.resetFIFO();
  178. Serial.println(F("FIFO overflow!"));
  179.  
  180. // otherwise, check for DMP data ready interrupt (this should happen frequently)
  181. }
  182. else if (mpuIntStatus & 0x02)
  183. {
  184. // wait for correct available data length, should be a VERY short wait
  185. while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
  186.  
  187. // read a packet from FIFO
  188. mpu.getFIFOBytes(fifoBuffer, packetSize);
  189.  
  190. // track FIFO count here in case there is > 1 packet available
  191. // (this lets us immediately read more without waiting for an interrupt)
  192. fifoCount -= packetSize;
  193.  
  194. mpu.dmpGetQuaternion(&q, fifoBuffer);
  195. mpu.dmpGetGravity(&gravity, &q);
  196. mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  197. input = ypr[1] * 180/M_PI + 180;
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement