一、模型建立
    根据单摆模型,建立以下模型,如下图:

    arduino两轮平衡车(原理分析含代码) - 图1
    忽略空气阻力的情况下,当物体离开平衡位置后,物体会受到竖直向下的重力和沿着悬挂线向上方向的拉力的合力作用,使得物体在平衡位置做往返运动。该合力又称为恢复力,如下:
    arduino两轮平衡车(原理分析含代码) - 图2

    其中,悬线与竖直方向夹角为θ,与物体运动方向夹角为 π- θ,F方向与物体运动方向相切,指向平衡位置。

    1. 又由极限![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767771919-ee9e57ca-ca53-41d8-89b7-2dc56791e0a4.png#align=left&display=inline&height=35&margin=%5Bobject%20Object%5D&originHeight=35&originWidth=84&size=0&status=done&style=none&width=84),可知,当θ较小时,有
    2. ![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767781017-8261becf-5c87-44b1-97e2-0c9fec6f6c7b.png#align=left&display=inline&height=28&margin=%5Bobject%20Object%5D&originHeight=28&originWidth=195&size=0&status=done&style=none&width=195)
    3. 实际中,物体除了受到回复力F的作用外,还受到空气阻力F阻的作用,从而最终会静止在平衡位置。而运动中空气阻力的大小与物体运动速度大小成正比,该比例由阻尼系数k表示,而阻力方向与运动速度方向相反。此时物体在运动方向上所受合力如下:
    4. ![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767791720-eebe95cc-2c82-4b4f-81d2-75c1a1ea0246.png#align=left&display=inline&height=34&margin=%5Bobject%20Object%5D&originHeight=34&originWidth=210&size=0&status=done&style=none&width=210)
    5. 因此,对于单摆模型,其保持平衡需耀具备以下条件:
    6. 受到与偏移方向相反的回复力的作用<br /> 受到与物体运动方向相反的阻尼力的作用<br />对于两轮平衡车,可以简化为倒立摆模型,如下:<br />![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767806048-37635790-4995-42fc-bfb5-3fb1b0b0431b.png#align=left&display=inline&height=195&margin=%5Bobject%20Object%5D&originHeight=195&originWidth=275&size=0&status=done&style=none&width=275)

    而对于倒立摆模型,其受力如下:
    arduino两轮平衡车(原理分析含代码) - 图3

    如单摆模型,可知物体受到回复力为:

    1. ![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767822791-5b0f8267-14c5-4842-aed3-25d1044aae76.png#align=left&display=inline&height=27&margin=%5Bobject%20Object%5D&originHeight=27&originWidth=182&size=0&status=done&style=none&width=182) <br />但由于此时回复力方向与物体平衡位置方向相反,因此物体反而会加速离开平衡位置。
    2. 为了让物体回到平衡位置,即保持与竖直方向平行,可以让倒立摆向与物体偏离方向相反的方向做加速运动,加速度为a。此时倒立摆受力如图:
    3. ![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767831580-29edd709-dc34-4405-8c17-4f09bffe3d55.png#align=left&display=inline&height=176&margin=%5Bobject%20Object%5D&originHeight=176&originWidth=174&size=0&status=done&style=none&width=174)
    4. 倒立摆将受到惯性力:
    5. ![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767839896-dec4672b-fb2c-4481-84ba-20dec84cd609.png#align=left&display=inline&height=25&margin=%5Bobject%20Object%5D&originHeight=25&originWidth=127&size=0&status=done&style=none&width=127)
    6. 不计空气阻力时,倒立摆运动方向上所受合力为:
    7. ![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767849151-aada5a32-87b7-4e70-a58c-95d3045f82aa.png#align=left&display=inline&height=25&margin=%5Bobject%20Object%5D&originHeight=25&originWidth=170&size=0&status=done&style=none&width=170)
    8. 当偏移角较小时,有
    9. ![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767856754-0bbd4853-fa3a-46a2-bb15-e5e51aae3ee3.png#align=left&display=inline&height=25&margin=%5Bobject%20Object%5D&originHeight=25&originWidth=156&size=0&status=done&style=none&width=156)

    其中,取近似 arduino两轮平衡车(原理分析含代码) - 图4

    1. k1>g时,物体在运动方向上受到与回复力方向相反的力。此时,在空气阻力作用下,物体将在平衡位置往返,最终回到平衡位置。而为了让物体尽快回到平衡位置,可以通过外加阻尼力,其大小与物体偏离平衡位置的速度成正比,比例系数为k2,方向相反,则有:
    2. ![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767872572-635fd115-3d15-478f-98fa-4705769e321c.png#align=left&display=inline&height=30&margin=%5Bobject%20Object%5D&originHeight=30&originWidth=191&size=0&status=done&style=none&width=191)
    3. 因此,可以通过外力![](https://cdn.nlark.com/yuque/0/2021/png/12814627/1615767880741-5860f787-e63f-416a-8150-0ac404f1b2eb.png#align=left&display=inline&height=30&margin=%5Bobject%20Object%5D&originHeight=30&originWidth=128&size=0&status=done&style=none&width=128),来使小车保持平衡。而外力的来源即是通过控制小车车轮的加速度,使得小车获得相应的惯性力。其中k1会使得小车向物体倾倒的相反方向做加速运动,k1过大过小将导致小车直接偏离品平衡位置的方向而倾倒;k2使得小车能够尽快回到平衡位置,k2过大过小时,都会影响小车回到平衡位置的调节时间。

    二、系统分析
    第一部分从建立物理模型的角度出发,通过物理定律推导得到物理等式。
    但光有理论,没有结合实际也会觉得难以下手,因此第二部分结合实践,从系统的角度出发,结合物理等式对系统的控制做进一步分析。
    首先,还是从物理定律出发,得到物理等式。如图,考虑一个可移动小车上的倒立摆模型,其中杆的长度为L,不计杆的质量,杆的末端有一个质量为m的小球,θ(t)该摆偏离垂直位置的角度,g是重力加速度,s(t)是小车相对于某个参考点的位置,a(t)是小车的加速度,x(t)代表由任何扰引起的角加速度。
    arduino两轮平衡车(原理分析含代码) - 图5

    对杆上小球受力分析,有
    arduino两轮平衡车(原理分析含代码) - 图6
    此时,Fθ(t)方向为垂直于杆,沿着倾倒方向。
    根据运动学知识,有
    arduino两轮平衡车(原理分析含代码) - 图7
    注意,角速度要转为线速度

    反馈就是利用一个系统的输出去控制或改变系统的输入。反馈除了能提供一个误差校正的机理而减小系统对扰动以及系统数学模型误差的灵敏度以外,反馈的另一个重要特性在于使一个固有的不稳定系统稳定的能力上。考虑以上倒立摆模型,如果小车保持静止不动的话,倒立摆一定会倒下来。但通过移动小车,让小车根据倒立摆偏离竖直方向的角度来回移动,最终可也让倒立摆达到动态平衡,即与竖直方向角度为0。显然,稳定倒立摆问题也就是设计一个反馈系统。该反馈系统使小车移动以保持倒立摆成垂直状态。

    对于连续时间LTI反馈系统的一般结构可以通过上图表示,此时系统函数为
    arduino两轮平衡车(原理分析含代码) - 图8 假定θ(t)很小,即对于该模型,考虑该模型是在接近垂直位置的时候的动态特性,此时可以做如下近似
    sin⁡[ θ(t) ]≈θ(t) ,cos⁡[ θ(t) ]≈1
    当小车静止时,即a(t)=0。求拉普拉斯变换,可得系统函数为
    arduino两轮平衡车(原理分析含代码) - 图9
    由上式可知,有极点s=g/l 或s=-g/l ,即系统在右半平面有一个极点,意味着该系统是不稳定的。(稳定:系统的单位冲激相应绝对可和。)因此,在不进行反馈控制时,该系统为一个LTI因果不稳定系统。也就是说,在小车静止不动时,任何由x(t)所造成的微小角扰动都将导致偏离垂直方向的角度进一步增大。

    为了让小车以适当的方式移动,以摆在垂直位置,设想采用比例反馈,即 a(t) = Kθ(t)
    同样假定θ(t)很小,使得该系统可以近似满足LTI的特性。
    arduino两轮平衡车(原理分析含代码) - 图10
    观察发现,此时对于系统函数H(s),其在s平面的右半平面至少存在一个极点,因此系统依旧不稳定。考虑加入比例加微分反馈 ,此时该系统函数变为二阶函数,如下
    arduino两轮平衡车(原理分析含代码) - 图11
    可得系统的两个极点 。根据奈奎斯特稳定判断依据arduino两轮平衡车(原理分析含代码) - 图12,要使得系统稳定,则两个极点都必须位于s平面的左半平面,此时有k1>g,k2>0。

    综上所述,平衡车控制核心是一个角度PD控制器,有
    arduino两轮平衡车(原理分析含代码) - 图13
    三、改进
    实际上,由于结构设计或组装上的不合理,小车物理重心总与竖直方向总存在一个夹角(或者说与地面不是保持垂直关系),小车在这一倾角作用下,在水平方向上受到一重力分量而会进行加速运动,最终倾倒而无法保持平衡。因此,需要在原先角度PD控制的基础上,加入速度PI控制,控制小车的速度。至于为什么是速度PI控制?阅读资料,仍然无法完全理解,其中讲到速度计算中存在着噪声影响,如果对该速度信号做微分计算时,会放大噪声的影响。(或者讲到小车为非最小相位系统,速度调节的时间常数要选取比较大??不是很理解,如果有更好的见解,也请多多指教哈)
    速度PI控制如下:
    arduino两轮平衡车(原理分析含代码) - 图14

    总而言之,小车的控制可以归结为两个PID控制,一个为角度PD控制,一个为速度PI控制,为了保证系统的实时显示,两个PID控制必须放在中断程序中执行,而角度PD控制的频率要比速度PI控制的频率高。(具体还不是很清楚??)

    1. #include "Wire.h"
    2. const uint8_t IMUAddress = 0x68; // AD0 is logic low on the PCB
    3. const uint16_t I2C_TIMEOUT = 100; // Used to check for errors in I2C communication
    4. int p;
    5. int receive_x;
    6. uint8_t i2cWrite(uint8_t registerAddress, uint8_t data, bool sendStop) {
    7. return i2cWrite(registerAddress, &data, 1, sendStop); // Returns 0 on success
    8. }
    9. uint8_t i2cWrite(uint8_t registerAddress, uint8_t *data, uint8_t length, bool sendStop) {
    10. Wire.beginTransmission(IMUAddress);
    11. Wire.write(registerAddress);
    12. Wire.write(data, length);
    13. uint8_t rcode = Wire.endTransmission(sendStop); // Returns 0 on success
    14. if (rcode) {
    15. Serial.print(F("i2cWrite failed: "));
    16. Serial.println(rcode);
    17. }
    18. return rcode; // See: http://arduino.cc/en/Reference/WireEndTransmission
    19. }
    20. uint8_t i2cRead(uint8_t registerAddress, uint8_t *data, uint8_t nbytes) {
    21. uint32_t timeOutTimer;
    22. Wire.beginTransmission(IMUAddress);
    23. Wire.write(registerAddress);
    24. uint8_t rcode = Wire.endTransmission(false); // Don't release the bus
    25. if (rcode) {
    26. Serial.print(F("i2cRead failed: "));
    27. Serial.println(rcode);
    28. return rcode; // See: http://arduino.cc/en/Reference/WireEndTransmission
    29. }
    30. Wire.requestFrom(IMUAddress, nbytes, (uint8_t)true); // Send a repeated start and then release the bus after reading
    31. for (uint8_t i = 0; i < nbytes; i++) {
    32. if (Wire.available())
    33. data[i] = Wire.read();
    34. else {
    35. timeOutTimer = micros();
    36. while (((micros() - timeOutTimer) < I2C_TIMEOUT) && !Wire.available());
    37. if (Wire.available())
    38. data[i] = Wire.read();
    39. else {
    40. Serial.println(F("i2cRead timeout"));
    41. return 5; // This error value is not already taken by endTransmission
    42. }
    43. }
    44. }
    45. return 0; // Success
    46. }
    47. /******************************************************/
    48. //2560 pin map 引脚定义好即可,然后改变一下PID的几个值(kp,kd,ksp,ksi)即可,剩下的全部都是固定的程序,
    49. //可能小车会有一点重心不在中点的现象,加一下角度值或者减一点即可
    50. //至于每个MPU6050的误差,自己调节一下即可,不是很难
    51. //调试时先将速度环的ksp,ksi=0,调到基本可以站起来,然后可能会出现倒,或者自动跑起来的时候加上速度环
    52. //这时就会很稳定的站起来,然后用小力气的手推不会倒。
    53. int ENA=9;
    54. int ENB=10;
    55. int IN1=7;
    56. int IN2=4;
    57. int IN3=5;
    58. int IN4=6;
    59. int MAS,MBS;
    60. int KEY = 2;
    61. /* IMU Data */
    62. double accX, accY, accZ;
    63. double gyroX, gyroY, gyroZ;
    64. int16_t tempRaw;
    65. double gyroXangle, gyroYangle; // Angle calculate using the gyro only
    66. double compAngleX, compAngleY; // Calculated angle using a complementary filter
    67. double kalAngleX, kalAngleY; // Calculated angle using a Kalman filter
    68. uint8_t i2cData[14]; // Buffer for I2C data
    69. uint32_t timer;
    70. unsigned long lastTime;
    71. /***************************************/
    72. double P[2][2] = {{ 1, 0 },{ 0, 1 }};
    73. double Pdot[4] ={ 0,0,0,0};
    74. static const double Q_angle=0.001, Q_gyro=0.003, R_angle=0.5,dtt=0.005,C_0 = 1;
    75. double q_bias, angle_err, PCt_0, PCt_1, E, K_0, K_1, t_0, t_1;
    76. double angle,angle_dot,aaxdot,aax;
    77. double position_dot,position_dot_filter,positiono;
    78. /*-------------Encoder---------------*/
    79. #define LF 0
    80. #define RT 1
    81. //The balance PID
    82. float kp,kd,ksp,ksi;
    83. int Lduration,Rduration;
    84. boolean LcoderDir,RcoderDir;
    85. const byte encoder0pinA = 2;
    86. const byte encoder0pinB = 12;//7;
    87. byte encoder0PinALast;
    88. const byte encoder1pinA = 3;
    89. const byte encoder1pinB = 13;//8;
    90. byte encoder1PinALast;
    91. int RotationCoder[2];
    92. int turn_flag=0;
    93. float move_flag=0;
    94. float right_need = 0, left_need = 0;;
    95. int pwm;
    96. int pwm_R,pwm_L;
    97. float range;
    98. float range_error_all;
    99. float wheel_speed;
    100. float last_wheel;
    101. float error_a=0;
    102. void Kalman_Filter(double angle_m,double gyro_m)
    103. {
    104. angle+=(gyro_m-q_bias) * dtt;
    105. Pdot[0]=Q_angle - P[0][1] - P[1][0];
    106. Pdot[1]=- P[1][1];
    107. Pdot[2]=- P[1][1];
    108. Pdot[3]=Q_gyro;
    109. P[0][0] += Pdot[0] * dtt;
    110. P[0][1] += Pdot[1] * dtt;
    111. P[1][0] += Pdot[2] * dtt;
    112. P[1][1] += Pdot[3] * dtt;
    113. angle_err = angle_m - angle;
    114. PCt_0 = C_0 * P[0][0];
    115. PCt_1 = C_0 * P[1][0];
    116. E = R_angle + C_0 * PCt_0;
    117. K_0 = PCt_0 / E;
    118. K_1 = PCt_1 / E;
    119. t_0 = PCt_0;
    120. t_1 = C_0 * P[0][1];
    121. P[0][0] -= K_0 * t_0;
    122. P[0][1] -= K_0 * t_1;
    123. P[1][0] -= K_1 * t_0;
    124. P[1][1] -= K_1 * t_1;
    125. angle+= K_0 * angle_err;
    126. q_bias += K_1 * angle_err;
    127. angle_dot = gyro_m-q_bias;//也许应该用last_angle-angle
    128. }
    129. void setup() {
    130. Wire.begin();
    131. Serial.begin(9600);
    132. pinMode(KEY, INPUT);
    133. pinMode(IN1, OUTPUT);
    134. pinMode(IN2, OUTPUT);
    135. pinMode(IN3, OUTPUT);
    136. pinMode(IN4, OUTPUT);
    137. pinMode(ENA, OUTPUT);
    138. pinMode(ENB, OUTPUT);
    139. TWBR = ((F_CPU / 400000L) - 16) / 2; // Set I2C frequency to 400kHz
    140. i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
    141. i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
    142. i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s
    143. i2cData[3] = 0x00; // Set Accelerometer Full Scale Range to ±2g
    144. while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once
    145. while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode
    146. while (i2cRead(0x75, i2cData, 1));
    147. if (i2cData[0] != 0x68) { // Read "WHO_AM_I" register
    148. Serial.println(F("Error reading sensor"));
    149. while (1);
    150. }
    151. else {
    152. Serial.println(F("success reading sensor"));
    153. }
    154. delay(20); // Wait for sensor to stabilize
    155. while (i2cRead(0x3B, i2cData, 6));
    156. accX = (i2cData[0] << 8) | i2cData[1];
    157. accY = (i2cData[2] << 8) | i2cData[3];
    158. accZ = (i2cData[4] << 8) | i2cData[5];
    159. double roll = atan2(accX, accZ) * RAD_TO_DEG;
    160. EnCoderInit();
    161. timer = micros();
    162. //The balance PID*************************************************************************
    163. kp= 33;//24.80;43
    164. kd= 2.1 ; //9.66;1.4
    165. ksp= 0;//4.14;8.5
    166. ksi= 0;//0.99; 0.55;2.1
    167. }
    168. void EnCoderInit()
    169. {
    170. //pinMode(8,INPUT);/
    171. //pinMode(9,INPUT);
    172. attachInterrupt(LF, LwheelSpeed, RISING);
    173. attachInterrupt(RT, RwheelSpeed, RISING);
    174. }
    175. void pwm_calculate()
    176. {
    177. unsigned long now = millis(); // 当前时间(ms)
    178. int Time = now - lastTime;
    179. int range_error;
    180. range += (Lduration + Rduration) * 0.5;
    181. range *= 0.9;
    182. range_error = Lduration - Rduration;
    183. range_error_all += range_error;
    184. wheel_speed = range - last_wheel;
    185. //wheel_speed = constrain(wheel_speed,-800,800);
    186. //Serial.println(wheel_speed);
    187. last_wheel = range;
    188. pwm = (angle + 0.825) * kp + angle_dot * kd + range * ksp + wheel_speed * ksi;
    189. if(pwm > 255)pwm = 255;
    190. if(pwm < -255)pwm = -255;
    191. if(turn_flag == 0)
    192. {
    193. pwm_R = pwm + range_error_all;
    194. pwm_L = pwm - range_error_all;
    195. }
    196. else if(turn_flag == 1) //左转
    197. {
    198. pwm_R = pwm ; //Direction PID control
    199. pwm_L = pwm + left_need * 68;
    200. range_error_all = 0; //clean
    201. }
    202. else if(turn_flag == 2)
    203. {
    204. pwm_R = pwm + right_need * 68; //Direction PID control
    205. pwm_L = pwm ;
    206. range_error_all = 0; //clean
    207. }
    208. Lduration = 0;//clean
    209. Rduration = 0;
    210. lastTime = now;
    211. }
    212. void PWMD()
    213. {
    214. if(pwm>0)
    215. {
    216. digitalWrite(IN1, HIGH);
    217. digitalWrite(IN2, LOW);
    218. digitalWrite(IN3, LOW);
    219. digitalWrite(IN4, HIGH);
    220. }
    221. else if(pwm<0)
    222. {
    223. digitalWrite(IN1, LOW);
    224. digitalWrite(IN2, HIGH);
    225. digitalWrite(IN3, HIGH);
    226. digitalWrite(IN4, LOW);
    227. }
    228. int PWMr = abs(pwm);
    229. int PWMl = abs(pwm);
    230. analogWrite(ENB, max(PWMl,60)); //PWM调速a==0-255 51
    231. analogWrite(ENA, max(PWMr,60)); //PWM调速a==0-255 54
    232. }
    233. void LwheelSpeed()
    234. {
    235. if(digitalRead(encoder0pinB))
    236. Lduration++;
    237. else Lduration--;
    238. }
    239. void RwheelSpeed()
    240. {
    241. if(digitalRead(encoder1pinB))
    242. Rduration--;
    243. else Rduration++;
    244. }
    245. void control()
    246. {
    247. if(Serial.available()){
    248. int val;
    249. val=Serial.read();
    250. switch(val){
    251. case 'w':
    252. if(move_flag<5)move_flag += 0.5;
    253. else move_flag = 0;
    254. break;
    255. case 's':
    256. if(move_flag<5)move_flag -= 0.5;
    257. else move_flag = 0;
    258. Serial.println("back");
    259. break;
    260. case 'a':
    261. turn_flag = 1;
    262. left_need = 0.6;
    263. Serial.println("zuo");
    264. break;
    265. case 'd':
    266. turn_flag = 2;
    267. right_need = 0.6;
    268. Serial.println("you");
    269. break;
    270. case 't':
    271. move_flag=0;
    272. turn_flag=0;
    273. right_need = left_need = 0;
    274. Serial.println("stop");
    275. break;
    276. default:
    277. break;
    278. }
    279. }
    280. }
    281. //调试时先将速度环的ksp,ksi=0,调到基本可以站起来,然后可能会出现倒,或者自动跑起来的时候加上速度环
    282. //这时就会很稳定的站起来,然后用小力气的手推不会倒。
    283. void change(){
    284. while (Serial.available() > 0) {
    285. receive_x = Serial.parseInt();
    286. char ccc = Serial.read();
    287. if (ccc == 'P') {
    288. kp=receive_x/100.0;
    289. Serial.print("kp:");
    290. Serial.println(kp);
    291. }
    292. else if(ccc == 'D') {
    293. kd=receive_x/100.0;
    294. Serial.print("kd:");
    295. Serial.println(kd);
    296. }
    297. else if(ccc == 'K') {
    298. ksp=receive_x/100.0;
    299. Serial.print("ksp:");
    300. Serial.println(ksp);
    301. }
    302. else if(ccc == 'S') {
    303. ksi=receive_x/100.0;
    304. Serial.print("ksi:");
    305. Serial.println(ksi);
    306. }
    307. //kp= 200; P //24.80;43
    308. //kd= 2.0 ; D //9.66;1.4
    309. //ksp= 0; K //4.14;8.5
    310. //ksi= 0;S //0.99; 0.550,2.1
    311. }
    312. }
    313. void loop()
    314. {
    315. //control();
    316. change();
    317. while (i2cRead(0x3B, i2cData, 14));
    318. accX = ((i2cData[0] << 8) | i2cData[1]);
    319. //accY = ((i2cData[2] << 8) | i2cData[3]);
    320. accZ = ((i2cData[4] << 8) | i2cData[5]);
    321. //gyroX = (i2cData[8] << 8) | i2cData[9];
    322. gyroY = (i2cData[10] << 8) | i2cData[11];
    323. //gyroZ = (i2cData[12] << 8) | i2cData[13];
    324. //Serial.print(accX);Serial.print(" ");
    325. //Serial.print(accZ);Serial.print(" ");
    326. //Serial.println(gyroY);
    327. double dt = (double)(micros() - timer) / 1000000; // Calculate delta time
    328. timer = micros();
    329. double roll = atan2(accX, accZ) * RAD_TO_DEG - move_flag;
    330. //double gyroXrate = gyroX / 131.0; // Convert to deg/s
    331. double gyroYrate = -gyroY / 131.0; // Convert to deg/s
    332. //gyroXangle += gyroXrate * dt; // Calculate gyro angle without any filter
    333. //gyroYangle += gyroYrate * dt;
    334. Kalman_Filter(roll,gyroYrate);
    335. if(abs(angle)<35){
    336. //Serial.println(angle); while (Serial.available() > 0) { // 串口收到字符数大于零。
    337. pwm_calculate();
    338. PWMD();
    339. }
    340. else{
    341. analogWrite(ENB, 0); //PWM调速a==0-255
    342. analogWrite(ENA, 0); //PWM调速a==0-255
    343. }
    344. delay(2);
    345. }