Advertisement
safwan092

Untitled

Apr 17th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.18 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Servo.h>
  3. #include <LiquidCrystal_I2C.h>
  4.  
  5. LiquidCrystal_I2C lcd(0x27, 16, 2);
  6. Servo right_prop;
  7. Servo left_prop;
  8.  
  9. /*MPU-6050 gives you 16 bits data so you have to create some 16int constants
  10. to store the data for accelerations and gyro*/
  11.  
  12. int16_t Acc_rawX, Acc_rawY, Acc_rawZ, Gyr_rawX, Gyr_rawY, Gyr_rawZ;
  13.  
  14.  
  15. float Acceleration_angle[2];
  16. float Gyro_angle[2];
  17. float Total_angle[2];
  18.  
  19.  
  20.  
  21.  
  22. float elapsedTime, time, timePrev;
  23. int i;
  24. float rad_to_deg = 180 / 3.141592654;
  25.  
  26. float PID, pwmLeft, pwmRight, error, previous_error;
  27. float pid_p = 0;
  28. float pid_i = 0;
  29. float pid_d = 0;
  30. /////////////////PID CONSTANTS/////////////////
  31. double kp = 3.55; //3.55
  32. double ki = 0.005; //0.003
  33. double kd = 2.05; //2.05
  34. ///////////////////////////////////////////////
  35.  
  36. double throttle = 1300; //initial value of throttle to the motors
  37. float desired_angle = 0; //This is the angle in which we whant the
  38. //balance to stay steady
  39.  
  40.  
  41. void setup() {
  42. lcd.init();
  43. lcd.init();
  44. lcd.backlight();
  45. lcd.setCursor(0, 0);
  46. lcd.print("Hello, world!");
  47. Wire.begin(); //begin the wire comunication
  48. Wire.beginTransmission(0x68);
  49. Wire.write(0x6B);
  50. Wire.write(0);
  51. Wire.endTransmission(true);
  52. Serial.begin(250000);
  53. right_prop.attach(3); //attatch the right motor to pin 3
  54. left_prop.attach(5); //attatch the left motor to pin 5
  55.  
  56. time = millis(); //Start counting time in milliseconds
  57. /*In order to start up the ESCs we have to send a min value
  58. of PWM to them before connecting the battery. Otherwise
  59. the ESCs won't start up or enter in the configure mode.
  60. The min value is 1000us and max is 2000us, REMEMBER!*/
  61. left_prop.writeMicroseconds(1000);
  62. right_prop.writeMicroseconds(1000);
  63. delay(7000); /*Give some delay, 7s, to have time to connect
  64. the propellers and let everything start up*/
  65. }//end of setup void
  66.  
  67. void loop() {
  68.  
  69. /////////////////////////////I M U/////////////////////////////////////
  70. timePrev = time; // the previous time is stored before the actual time read
  71. time = millis(); // actual time read
  72. elapsedTime = (time - timePrev) / 1000;
  73.  
  74. /*The tiemStep is the time that elapsed since the previous loop.
  75. This is the value that we will use in the formulas as "elapsedTime"
  76. in seconds. We work in ms so we haveto divide the value by 1000
  77. to obtain seconds*/
  78.  
  79. /*Reed the values that the accelerometre gives.
  80. We know that the slave adress for this IMU is 0x68 in
  81. hexadecimal. For that in the RequestFrom and the
  82. begin functions we have to put this value.*/
  83.  
  84. Wire.beginTransmission(0x68);
  85. Wire.write(0x3B); //Ask for the 0x3B register- correspond to AcX
  86. Wire.endTransmission(false);
  87. Wire.requestFrom(0x68, 6, true);
  88.  
  89. /*We have asked for the 0x3B register. The IMU will send a brust of register.
  90. The amount of register to read is specify in the requestFrom function.
  91. In this case we request 6 registers. Each value of acceleration is made out of
  92. two 8bits registers, low values and high values. For that we request the 6 of them
  93. and just make then sum of each pair. For that we shift to the left the high values
  94. register (<<) and make an or (|) operation to add the low values.*/
  95.  
  96. Acc_rawX = Wire.read() << 8 | Wire.read(); //each value needs two registres
  97. Acc_rawY = Wire.read() << 8 | Wire.read();
  98. Acc_rawZ = Wire.read() << 8 | Wire.read();
  99.  
  100.  
  101. /*///This is the part where you need to calculate the angles using Euler equations///*/
  102.  
  103. /* - Now, to obtain the values of acceleration in "g" units we first have to divide the raw
  104. values that we have just read by 16384.0 because that is the value that the MPU6050
  105. datasheet gives us.*/
  106. /* - Next we have to calculate the radian to degree value by dividing 180º by the PI number
  107. which is 3.141592654 and store this value in the rad_to_deg variable. In order to not have
  108. to calculate this value in each loop we have done that just once before the setup void.
  109. */
  110.  
  111. /* Now we can apply the Euler formula. The atan will calculate the arctangent. The
  112. pow(a,b) will elevate the a value to the b power. And finnaly sqrt function
  113. will calculate the rooth square.*/
  114. /*---X---*/
  115. Acceleration_angle[0] = atan((Acc_rawY / 16384.0) / sqrt(pow((Acc_rawX / 16384.0), 2) + pow((Acc_rawZ / 16384.0), 2))) * rad_to_deg;
  116. /*---Y---*/
  117. Acceleration_angle[1] = atan(-1 * (Acc_rawX / 16384.0) / sqrt(pow((Acc_rawY / 16384.0), 2) + pow((Acc_rawZ / 16384.0), 2))) * rad_to_deg;
  118.  
  119.  
  120.  
  121. /*Now we read the Gyro data in the same way as the Acc data. The adress for the
  122. gyro data starts at 0x43. We can see this adresses if we look at the register map
  123. of the MPU6050. In this case we request just 4 values. W don¡t want the gyro for
  124. the Z axis (YAW).*/
  125.  
  126. Wire.beginTransmission(0x68);
  127. Wire.write(0x43); //Gyro data first adress
  128. Wire.endTransmission(false);
  129. Wire.requestFrom(0x68, 4, true); //Just 4 registers
  130.  
  131. Gyr_rawX = Wire.read() << 8 | Wire.read(); //Once again we shif and sum
  132. Gyr_rawY = Wire.read() << 8 | Wire.read();
  133.  
  134. /*Now in order to obtain the gyro data in degrees/seconda we have to divide first
  135. the raw value by 131 because that's the value that the datasheet gives us*/
  136.  
  137. /*---X---*/
  138. Gyro_angle[0] = Gyr_rawX / 131.0;
  139. /*---Y---*/
  140. Gyro_angle[1] = Gyr_rawY / 131.0;
  141.  
  142. /*Now in order to obtain degrees we have to multiply the degree/seconds
  143. value by the elapsedTime.*/
  144. /*Finnaly we can apply the final filter where we add the acceleration
  145. part that afects the angles and ofcourse multiply by 0.98 */
  146.  
  147. /*---X axis angle---*/
  148. Total_angle[0] = 0.98 * (Total_angle[0] + Gyro_angle[0] * elapsedTime) + 0.02 * Acceleration_angle[0];
  149. /*---Y axis angle---*/
  150. Total_angle[1] = 0.98 * (Total_angle[1] + Gyro_angle[1] * elapsedTime) + 0.02 * Acceleration_angle[1];
  151.  
  152. /*Now we have our angles in degree and values from -10º0 to 100º aprox*/
  153. //Serial.println(Total_angle[1]);
  154. lcd.clear();
  155. delay(400);
  156. lcd.setCursor(0, 0);
  157. lcd.print("X: ");
  158. lcd.print(Total_angle[0]);
  159. lcd.print(" Y: ");
  160. lcd.print(Total_angle[1]);
  161. lcd.setCursor(0, 1);
  162. lcd.print("X: ");
  163. lcd.print(Gyro_angle[0]);
  164. lcd.print(" Y: ");
  165. lcd.print(Gyro_angle[1]);
  166. delay(400);
  167. /*///////////////////////////P I D///////////////////////////////////*/
  168. /*Remember that for the balance we will use just one axis. I've choose the x angle
  169. to implement the PID with. That means that the x axis of the IMU has to be paralel to
  170. the balance*/
  171.  
  172. /*First calculate the error between the desired angle and
  173. the real measured angle*/
  174. error = Total_angle[1] - desired_angle;
  175.  
  176. /*Next the proportional value of the PID is just a proportional constant
  177. multiplied by the error*/
  178.  
  179. pid_p = kp * error;
  180.  
  181. /*The integral part should only act if we are close to the
  182. desired position but we want to fine tune the error. That's
  183. why I've made a if operation for an error between -2 and 2 degree.
  184. To integrate we just sum the previous integral value with the
  185. error multiplied by the integral constant. This will integrate (increase)
  186. the value each loop till we reach the 0 point*/
  187. if (-3 < error < 3)
  188. {
  189. pid_i = pid_i + (ki * error);
  190. }
  191.  
  192. /*The last part is the derivate. The derivate acts upon the speed of the error.
  193. As we know the speed is the amount of error that produced in a certain amount of
  194. time divided by that time. For taht we will use a variable called previous_error.
  195. We substract that value from the actual error and divide all by the elapsed time.
  196. Finnaly we multiply the result by the derivate constant*/
  197.  
  198. pid_d = kd * ((error - previous_error) / elapsedTime);
  199.  
  200. /*The final PID values is the sum of each of this 3 parts*/
  201. PID = pid_p + pid_i + pid_d;
  202.  
  203. /*We know taht the min value of PWM signal is 1000us and the max is 2000. So that
  204. tells us that the PID value can/s oscilate more than -1000 and 1000 because when we
  205. have a value of 2000us the maximum value taht we could sybstract is 1000 and when
  206. we have a value of 1000us for the PWM sihnal, the maximum value that we could add is 1000
  207. to reach the maximum 2000us*/
  208. if (PID < -1000)
  209. {
  210. PID = -1000;
  211. }
  212. if (PID > 1000)
  213. {
  214. PID = 1000;
  215. }
  216.  
  217. /*Finnaly we calculate the PWM width. We sum the desired throttle and the PID value*/
  218. pwmLeft = throttle + PID;
  219. pwmRight = throttle - PID;
  220.  
  221.  
  222. /*Once again we map the PWM values to be sure that we won't pass the min
  223. and max values. Yes, we've already maped the PID values. But for example, for
  224. throttle value of 1300, if we sum the max PID value we would have 2300us and
  225. that will mess up the ESC.*/
  226. //Right
  227. if (pwmRight < 1000)
  228. {
  229. pwmRight = 1000;
  230. }
  231. if (pwmRight > 2000)
  232. {
  233. pwmRight = 2000;
  234. }
  235. //Left
  236. if (pwmLeft < 1000)
  237. {
  238. pwmLeft = 1000;
  239. }
  240. if (pwmLeft > 2000)
  241. {
  242. pwmLeft = 2000;
  243. }
  244.  
  245. /*Finnaly using the servo function we create the PWM pulses with the calculated
  246. width for each pulse*/
  247. left_prop.writeMicroseconds(pwmLeft);
  248. right_prop.writeMicroseconds(pwmRight);
  249. previous_error = error; //Remember to store the previous error.
  250.  
  251. }//end of loop void
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement