1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| #include <Solution_algorithm.h> #include <init.h>
static float i; static float ax_offset = 0, ay_offset = 0; static float gx_offset = 0, gy_offset = 0; static float yaw_offset = 0; static float mag_offset_x = 0.0, mag_offset_y = 0.0, mag_offset_z = 0.0;
static float rad2deg = 57.29578; static float roll_v = 0, pitch_v = 0, yaw_v = 0; static float gravity = 9.8; static float alpha = 0.5; static float beta = 0.1;
static float now = 0, lasttime = 0, dt = 0;
static float gyro_roll = 0, gyro_pitch = 0, gyro_yaw = 0; static float acc_roll = 0, acc_pitch = 0, acc_yaw = 0; static float k_roll = 0, k_pitch = 0, t_yaw = 0; static float tilt_yaw = 0; static float yaw_buf[WINDOW_SIZE];
static float e_P[2][2] ={{1,0},{0,1}};
static float k_k[2][2] ={{0,0},{0,0}};
static float sliding_window_filter(float *buf, float new_data) { int i; float sum = 0.0;
for(i = (WINDOW_SIZE - 1); i>0; i--) { buf[i - 1] = buf[i]; }
buf[WINDOW_SIZE - 1] = new_data;
for (i = 0; i < WINDOW_SIZE; i++) { sum += buf[i]; }
return sum / WINDOW_SIZE; }
void Caculate_RollPitch_by_Kalman() { now = millis(); dt = (now - lasttime) / 1000.0; lasttime = now;
sensors_event_t a, g, temp; mpu.getEvent(&a, &g, &temp);
roll_v = (g.gyro.x-gx_offset) + ((sin(k_pitch)*sin(k_roll))/cos(k_pitch))*(g.gyro.y-gy_offset) + ((sin(k_pitch)*cos(k_roll))/cos(k_pitch))*g.gyro.z; pitch_v = cos(k_roll)*(g.gyro.y-gy_offset) - sin(k_roll)*g.gyro.z; gyro_roll = k_roll + dt*roll_v; gyro_pitch = k_pitch + dt*pitch_v;
e_P[0][0] = e_P[0][0] + 0.0025; e_P[0][1] = e_P[0][1] + 0; e_P[1][0] = e_P[1][0] + 0; e_P[1][1] = e_P[1][1] + 0.0025;
k_k[0][0] = e_P[0][0]/(e_P[0][0]+0.3); k_k[0][1] = 0; k_k[1][0] = 0; k_k[1][1] = e_P[1][1]/(e_P[1][1]+0.3);
acc_roll = atan((a.acceleration.y - ay_offset) / (a.acceleration.z)) * rad2deg; acc_pitch = -1*atan((a.acceleration.x - ax_offset) / sqrt(sq(a.acceleration.y - ay_offset) + sq(a.acceleration.z))) * rad2deg;
k_roll = gyro_roll + k_k[0][0]*(acc_roll - gyro_roll); k_pitch = gyro_pitch + k_k[1][1]*(acc_pitch - gyro_pitch);
e_P[0][0] = (1 - k_k[0][0])*e_P[0][0]; e_P[0][1] = 0; e_P[1][0] = 0; e_P[1][1] = (1 - k_k[1][1])*e_P[1][1]; }
float caculate_Yaw(float roll,float pitch,bool init_flag) { float rollRad = 0, pitchRad = 0; float xh = 0,yh = 0; sensors_event_t event; mag.getEvent(&event);
if(init_flag) { sensors_event_t a, g, temp; mpu.getEvent(&a, &g, &temp);
rollRad = asin(-(a.acceleration.x/gravity)); pitchRad = atan(a.acceleration.y/a.acceleration.z); } else { rollRad = roll/rad2deg; pitchRad = pitch/rad2deg; }
xh = event.magnetic.x * cos(pitchRad) + event.magnetic.y * sin(pitchRad) * sin(rollRad) + event.magnetic.z * sin(pitchRad) * cos(rollRad); yh = event.magnetic.y * cos(rollRad) - event.magnetic.z * sin(rollRad);
float tiltCompensatedHeading = atan2(yh, xh);
if(tiltCompensatedHeading < 0) tiltCompensatedHeading += 2*PI; if(tiltCompensatedHeading > 2*PI) tiltCompensatedHeading -= 2*PI; float headingDegrees = tiltCompensatedHeading * 180/M_PI;
return headingDegrees; }
void offset_init() { for (i = 1; i <= 2000; i++) { sensors_event_t a, g, m, temp; sensors_event_t event; mpu.getEvent(&a, &g, &temp); mag.getEvent(&m); ax_offset = ax_offset + a.acceleration.x; ay_offset = ay_offset + a.acceleration.y; gx_offset = gx_offset + g.gyro.x; gy_offset = gy_offset + g.gyro.y;
mag_offset_x += m.magnetic.x; mag_offset_y += m.magnetic.y; mag_offset_z += m.magnetic.z; } ax_offset = ax_offset / 2000; ay_offset = ay_offset / 2000; gx_offset = gx_offset / 2000; gy_offset = gy_offset / 2000;
mag_offset_x /= 2000; mag_offset_y /= 2000; mag_offset_z /= 2000;
for(i = 0; i < 100; i++) { yaw_offset += caculate_Yaw(k_roll, k_pitch, true); }
yaw_offset /= 100; }
void get_Euler_angle(float* roll, float* pitch, float* yaw) { Caculate_RollPitch_by_Kalman(); tilt_yaw = caculate_Yaw(k_roll, k_pitch, false); tilt_yaw = tilt_yaw >= yaw_offset ? (tilt_yaw - yaw_offset) : ((360.0 - yaw_offset) + tilt_yaw);
if(tilt_yaw > 180.0) { tilt_yaw -= 360.0; } t_yaw = sliding_window_filter(yaw_buf, tilt_yaw);
*roll = k_roll; *pitch = k_pitch; *yaw = t_yaw; }
|