drv_calibration.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #include <math.h>
  2. #include "stdio.h"
  3. #include "ble_comm.h"
  4. #include "hal_flash.h"
  5. #include "hal_imu.h"
  6. #include "system.h"
  7. #include "bsp_time.h"
  8. #include "drv_calibration.h"
  9. #include "app_charge.h"
  10. #include "hal_ble_client.h"
  11. #include "hal_led.h"
  12. char calibration_printfbuf[256];
  13. static float invSampleFreq = 0.010f; //采样率(Hz)
  14. #define ERROR_PIN_ON nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_ENABLE);
  15. #define ERROR_PIN_OFF nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_DISABLE);
  16. //匿名四轴上位机api
  17. void send_ANO(unsigned char fun, unsigned char* p, int len){
  18. unsigned char buf[256];
  19. int L = 0;
  20. unsigned char ver = 0;
  21. buf[L] = 0xAA;
  22. ver += buf[L++];
  23. buf[L] = 0x05;
  24. ver += buf[L++];
  25. buf[L] = 0xAF;
  26. ver += buf[L++];
  27. buf[L] = fun;
  28. ver += buf[L++];
  29. buf[L] = len;
  30. ver += buf[L++];
  31. for (int i = 0; i < len; i++)
  32. {
  33. buf[L] = p[i];
  34. ver += buf[L++];
  35. }
  36. buf[L++] = ver;
  37. // extern void send_bytes_client(unsigned char* bytes, int len);
  38. send_bytes_client(buf, L);
  39. // SEGGER_RTT_Write(0,buf, L);
  40. // ESB_SendBuff(buf,L);
  41. }
  42. void send_ANO_Quaternion(float* Q){
  43. unsigned char buf[256];
  44. unsigned char L = 0;
  45. int quat[4];
  46. quat[0] = Q[0] * 10000;
  47. quat[1] = Q[1] * 10000;
  48. quat[2] = Q[2] * 10000;
  49. quat[3] = Q[3] * 10000;
  50. buf[L++] = (unsigned char)(quat[0] >> 24);
  51. buf[L++] = (unsigned char)(quat[0] >> 16);
  52. buf[L++] = (unsigned char)(quat[0] >> 8);
  53. buf[L++] = (unsigned char)(quat[0] >> 0);
  54. buf[L++] = (unsigned char)(quat[1] >> 24);
  55. buf[L++] = (unsigned char)(quat[1] >> 16);
  56. buf[L++] = (unsigned char)(quat[1] >> 8);
  57. buf[L++] = (unsigned char)(quat[1] >> 0);
  58. buf[L++] = (unsigned char)(quat[2] >> 24);
  59. buf[L++] = (unsigned char)(quat[2] >> 16);
  60. buf[L++] = (unsigned char)(quat[2] >> 8);
  61. buf[L++] = (unsigned char)(quat[2] >> 0);
  62. buf[L++] = (unsigned char)(quat[3] >> 24);
  63. buf[L++] = (unsigned char)(quat[3] >> 16);
  64. buf[L++] = (unsigned char)(quat[3] >> 8);
  65. buf[L++] = (unsigned char)(quat[3] >> 0);
  66. buf[L++] = 0;
  67. send_ANO(0x03, buf, L);
  68. }
  69. void send_ANO_STATUS(float _roll, float _pitch, float _yaw, float _posx, float _posy, float _posz){
  70. unsigned char buf[256];
  71. unsigned char L = 0;
  72. short roll = _roll * 100;
  73. short pitch = _pitch * 100;
  74. short yaw = _yaw * 100;
  75. short posx = _posx * 100;
  76. short posy = _posy * 100;
  77. short posz = _posz * 100;
  78. buf[L++] = (unsigned char)(roll >> 8);
  79. buf[L++] = (unsigned char)(roll >> 0);
  80. buf[L++] = (unsigned char)(pitch >> 8);
  81. buf[L++] = (unsigned char)(pitch >> 0);
  82. buf[L++] = (unsigned char)(yaw >> 8);
  83. buf[L++] = (unsigned char)(yaw >> 0);
  84. buf[L++] = (unsigned char)(posx >> 8);
  85. buf[L++] = (unsigned char)(posx >> 0);
  86. buf[L++] = (unsigned char)(posy >> 8);
  87. buf[L++] = (unsigned char)(posy >> 0);
  88. buf[L++] = (unsigned char)(posz >> 8);
  89. buf[L++] = (unsigned char)(posz >> 0);
  90. buf[L++] = 0;
  91. send_ANO(0x01, buf, L);
  92. }
  93. void send_ANO_SENSER(short gx, short gy, short gz, short ax, short ay, short az, short mx, short my, short mz){
  94. unsigned char buf[256];
  95. unsigned char L = 0;
  96. buf[L++] = (unsigned char)(ax >> 8);
  97. buf[L++] = (unsigned char)(ax >> 0);
  98. buf[L++] = (unsigned char)(ay >> 8);
  99. buf[L++] = (unsigned char)(ay >> 0);
  100. buf[L++] = (unsigned char)(az >> 8);
  101. buf[L++] = (unsigned char)(az >> 0);
  102. buf[L++] = (unsigned char)(gx >> 8);
  103. buf[L++] = (unsigned char)(gx >> 0);
  104. buf[L++] = (unsigned char)(gy >> 8);
  105. buf[L++] = (unsigned char)(gy >> 0);
  106. buf[L++] = (unsigned char)(gz >> 8);
  107. buf[L++] = (unsigned char)(gz >> 0);
  108. buf[L++] = (unsigned char)(mx >> 8);
  109. buf[L++] = (unsigned char)(mx >> 0);
  110. buf[L++] = (unsigned char)(my >> 8);
  111. buf[L++] = (unsigned char)(my >> 0);
  112. buf[L++] = (unsigned char)(mz >> 8);
  113. buf[L++] = (unsigned char)(mz >> 0);
  114. send_ANO(0x02, buf, L);
  115. }
  116. //LDLT分解法解线性方程组,和LDLTBKSB_6一起用
  117. char LDLTDCMP_6(int n, float (*a)[6]){
  118. int k;
  119. int m;
  120. int i;
  121. for (k = 0; k < n; k++){
  122. for (m = 0; m < k; m++){
  123. a[k][k] = a[k][k] - a[m][k] * a[k][m];
  124. }
  125. if (a[k][k] == 0){
  126. return 1;//error
  127. }
  128. for(i = k + 1; i < n; i++){
  129. for (m = 0; m < k; m++){
  130. a[k][i] = a[k][i] - a[m][i] * a[k][m];
  131. }
  132. a[i][k] = a[k][i] / a[k][k];
  133. }
  134. }
  135. return 0;
  136. }
  137. //LDLT分解法解线性方程组,和LDLTDCMP_6一起用
  138. void LDLTBKSB_6(int n, float (*a)[6], float* b)
  139. {
  140. int k;
  141. int i;
  142. for (i = 0; i < n; i++){
  143. for (k = 0; k < i; k++){
  144. b[i] = b[i] - a[i][k] * b[k];
  145. }
  146. }
  147. for (i = n - 1; i >= 0; i--){
  148. b[i] = b[i] / a[i][i];
  149. for (k = i + 1; k < n; k++){
  150. b[i] = b[i] - a[k][i] * b[k];
  151. }
  152. }
  153. }
  154. float Acc_static_calibration_b[6] = {0.0f};
  155. float Acc_static_calibration_D[6][6] = {0.0f};
  156. float Acc_static_calibration_out[6] = {1.000718f, 1.001100f, 0.988632f, 0.012943f, 0.006423f, 0.0034f}; //V1.3板
  157. void Ellipsoidfit_six_pram_update(float X, float Y, float Z, float* b, float (*D)[6]){
  158. float coft[6] = {0.0f};
  159. int r, j;
  160. coft[0] = X * X;
  161. coft[3] = 2.0f * X;
  162. coft[1] = Y * Y;
  163. coft[4] = 2.0f * Y;
  164. coft[2] = Z * Z;
  165. coft[5] = 2.0f * Z;
  166. for (j = 0; j < 6; j++){
  167. b[j] += coft[j];
  168. }
  169. for (r = 0; r < 6; r++){
  170. for (j = 0; j <= r; j++){
  171. D[r][j] += coft[r] * coft[j];
  172. }
  173. }
  174. for (r = 0; r < 6; r++){
  175. for (j = 5; j > r; j--){
  176. D[r][j] = D[j][r];
  177. }
  178. }
  179. }
  180. void Ellipsoidfit_six_pram_Solution(float* b, float (*D)[6], float* out)
  181. {
  182. float temp = 0;
  183. //解线性方程组,求解超定方程最小二乘解
  184. LDLTDCMP_6(6, D);
  185. LDLTBKSB_6(6, D, b);
  186. temp = b[1] * b[2] * b[3] * b[3] + b[0] * b[2] * b[4] * b[4] + b[0] * b[1] * b[5] * b[5];
  187. temp = 1.0f - (temp / (temp + (b[0] * b[1] * b[2])));
  188. out[0] = sqrtf(temp * b[0]);
  189. out[1] = sqrtf(temp * b[1]);
  190. out[2] = sqrtf(temp * b[2]);
  191. out[3] = b[3] * temp / out[0];
  192. out[4] = b[4] * temp / out[1];
  193. out[5] = b[5] * temp / out[2];
  194. }
  195. static float Acc_before[3];
  196. static float accmod=0,accmodbef=0;
  197. static char cun=0;
  198. static unsigned int timer_cli=0;
  199. //需要200ms执行一次,开始信号检测,检测到返回1,否则返回0
  200. char begin_REC(float *acccli)
  201. {
  202. float gyrmod = 0;
  203. gyrmod = (acccli[0]*Acc_before[0]) + (acccli[1]*Acc_before[1]) + (acccli[2]*Acc_before[2]);
  204. accmod = sqrtf(acccli[0] * acccli[0] + acccli[1] * acccli[1] + acccli[2] * acccli[2]);
  205. gyrmod = gyrmod/(accmod * accmodbef);
  206. gyrmod = acosf(gyrmod)*3.14f;
  207. Acc_before[0]=acccli[0];Acc_before[1]=acccli[1];Acc_before[2]=acccli[2];
  208. accmodbef=accmod;
  209. Mahony_PRINT("gyrmod:%f,timer:%d cun:%d %f\n",gyrmod,TIME_GetTicks()-timer_cli,cun,accmodbef);
  210. timer_cli=TIME_GetTicks();
  211. if((gyrmod > 0.25f)&&(gyrmod < 0.75f))
  212. {
  213. cun++;
  214. }else
  215. {
  216. // if(cun>=3)cun--;else
  217. cun=0;
  218. }
  219. if(cun>=10){
  220. cun=0;
  221. return 1;
  222. }
  223. else return 0;
  224. }
  225. static uint8_t ImuCal_state = ImuCal_init;
  226. char Acc_static_calibration(float* Acc_in, float* Acc_out, const float* gyr)
  227. {
  228. static int temp = 0;
  229. static int overtime = 0;
  230. static int caiyingcun = 0;
  231. switch (ImuCal_state){
  232. case ImuCal_init:{//未校准状态
  233. if(begin_REC(Acc_in)){
  234. ImuCal_state = ImuCal_GetData;
  235. caiyingcun = 0;
  236. overtime = 0;
  237. for (int i = 0; i < 6; i++){
  238. Acc_static_calibration_b[i] = 0.0f;
  239. Acc_static_calibration_D[i][0] = 0.0f;
  240. Acc_static_calibration_D[i][1] = 0.0f;
  241. Acc_static_calibration_D[i][2] = 0.0f;
  242. Acc_static_calibration_D[i][3] = 0.0f;
  243. Acc_static_calibration_D[i][4] = 0.0f;
  244. Acc_static_calibration_D[i][5] = 0.0f;
  245. }
  246. ERROR_PIN_ON
  247. Mahony_PRINT("Acc_static_calibration -> start \r\n");
  248. temp = 0;
  249. }
  250. }
  251. break;
  252. case ImuCal_GetData:{//采集校准数据状态
  253. float gyrmod = sqrtf(gyr[0] * gyr[0] + gyr[1] * gyr[1] + gyr[2] * gyr[2]);
  254. if (gyrmod < 18.27f){ //静止检测
  255. //采样数据
  256. if(((Acc_in[0]<2.0f)||(Acc_in[0]>-2.0f))&&((Acc_in[1]<2.0f)||(Acc_in[1]>-2.0f))&&((Acc_in[2]<2.0f)||(Acc_in[2]>-2.0f))){
  257. Ellipsoidfit_six_pram_update(Acc_in[0], Acc_in[1], Acc_in[2], Acc_static_calibration_b, Acc_static_calibration_D);
  258. caiyingcun++;
  259. send_ANO_SENSER(gyr[0] * 100, gyr[1] * 100, gyr[2] * 100, Acc_in[0] * 100, Acc_in[1] * 100, Acc_in[2] * 100, 0, 0, 0);
  260. }
  261. }
  262. //检测结束条件
  263. if ((gyrmod > 25.0f) && (gyrmod < 60.0f) && (caiyingcun > 20)){
  264. temp = temp + 1;
  265. }
  266. else{
  267. if(temp>50)temp--;else temp = 0;
  268. }
  269. if (temp * invSampleFreq > 2.0f){
  270. Ellipsoidfit_six_pram_Solution(Acc_static_calibration_b, Acc_static_calibration_D, Acc_static_calibration_out);
  271. ImuCal_state = ImuCal_Analyze;
  272. Mahony_PRINT("ImuCal_GetData");
  273. }
  274. else
  275. {
  276. // Mahony_PRINT("ImuCal_GetData temp %d",temp);
  277. }
  278. overtime++;
  279. if (overtime * invSampleFreq > 300.0f){
  280. ImuCal_state = ImuCal_error;
  281. Mahony_PRINT("ImuCal overtime ImuCal_error");
  282. }
  283. }
  284. break;
  285. case ImuCal_Analyze:{//校准数据处理状态
  286. float gyrmod = sqrtf(gyr[0] * gyr[0] + gyr[1] * gyr[1] + gyr[2] * gyr[2]);
  287. if (gyrmod < 18.27f){ //静止检测
  288. if (((Acc_static_calibration_out[0] > 0.85f) && (Acc_static_calibration_out[0] < 1.15f)) &&
  289. ((Acc_static_calibration_out[1] > 0.85f) && (Acc_static_calibration_out[1] < 1.15f)) &&
  290. ((Acc_static_calibration_out[2] > 0.85f) && (Acc_static_calibration_out[2] < 1.15f))){
  291. // Mahony_PRINT("out[%f,%f,%f,%f,%f,%f] \r\n", Acc_static_calibration_out[0], Acc_static_calibration_out[1], Acc_static_calibration_out[2], Acc_static_calibration_out[3], Acc_static_calibration_out[4], Acc_static_calibration_out[5]);
  292. Mahony_PRINT("ImuCal_state ImuCal_finish");
  293. temp = 0;
  294. ImuCal_state = ImuCal_finish;
  295. for(int a = 0; a < 6; a++){
  296. mBackup.cal[a] = Acc_static_calibration_out[a];
  297. }
  298. }
  299. else{
  300. ImuCal_state = ImuCal_quiet;
  301. }
  302. overtime = 0;
  303. }
  304. }
  305. break;
  306. // case ImuCal_finish:{//已经校准完状态
  307. // Mahony_PRINT("ImuCal_state ImuCal_init");
  308. // ImuCal_state = 0;
  309. // }
  310. // break;
  311. // case ImuCal_error:{//校准过程中断退出状态
  312. // Mahony_PRINT("ImuCal_state ImuCal_init");
  313. // ImuCal_state = 0;
  314. // Mahony_PRINT("ImuCal_state ImuCal_init");
  315. // }
  316. // break;
  317. // case ImuCal_quiet:{//校准过程中断退出状态
  318. // ImuCal_state = 0;
  319. // }
  320. // break;
  321. }
  322. return ImuCal_state;
  323. }
  324. void Mahony_imu_lbs(short* Acc_in, short* Gyr_in, short* Mag_in, float* Acc, float* Gyr, float* Mag)
  325. {
  326. float ACC_LBS = 32768.0f / 16.0f;
  327. float GYR_LBS = 32768.0f / 2000.0f;
  328. Acc[0] = Acc_in[0] / ACC_LBS;
  329. Acc[1] = Acc_in[1] / ACC_LBS;
  330. Acc[2] = Acc_in[2] / ACC_LBS;
  331. Gyr[0] = Gyr_in[0] / GYR_LBS;
  332. Gyr[1] = Gyr_in[1] / GYR_LBS;
  333. Gyr[2] = Gyr_in[2] / GYR_LBS;
  334. Mag[0] = Mag_in[0] / 1.0f;
  335. Mag[1] = Mag_in[1] / 1.0f;
  336. Mag[2] = Mag_in[2] / 1.0f;
  337. }
  338. static float acc[3], gyr[3], mag[3];
  339. void ImuCalibration_pcs(short* Acc, short* Gyr, short* Mag)
  340. {
  341. Mahony_imu_lbs(Acc, Gyr, Mag, acc, gyr, mag); //转换IMU数据量程,加速度单位转换为G,陀螺仪单位转换为度每秒,磁力计不需要转换单位,后面会做归一化处理
  342. Acc_static_calibration(acc, NULL, gyr);
  343. }
  344. uint8_t ImuCalibration_GetState(void)
  345. {
  346. return ImuCal_state;
  347. }
  348. void ImuCalibration_SetState(uint8_t _state)
  349. {
  350. ImuCal_state = _state;
  351. }