read.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. #include "read.h"
  2. #include "math.h"
  3. #include "string.h"
  4. #include "timer.h"
  5. typedef struct MeanFilter_s
  6. {
  7. bool init;
  8. //input
  9. float input[SAMPLE_NUM];
  10. uint8_t index;
  11. //sum
  12. float sum;
  13. //output
  14. float output;
  15. } MeanFilter_t;
  16. static MeanFilter_t _mean_filter[SENSOR_NUM] = {0};
  17. // 满额输出688.128mv(128倍增益)
  18. // weight = AD/50KG量程 *128增益 * 2^24 位数 * 5.376mV(3.36V * 1.6mV/V) AD/weight 1g对应AD值136.88
  19. // static uint32_t databuf[SENSOR_NUM][SAMPLE_NUM] = {0};
  20. /*输出范围:0x800000 - 0x7FFFFF(补码) 宽度为 -8388608 ~ 8388607 */
  21. static void Get_All_GrossWeight(void);
  22. static float Get_Weight(void);
  23. static void Processing_Data(void);
  24. static void Filter_Init(void);
  25. // static void Calib_Creep(WEIGHING_PARAM* param, float* buff ,float total_weight);
  26. // static float Get_Compare(WEIGHING_PARAM* param, float total_weight);
  27. // static uint8_t Check_No_Impact(WEIGHING_PARAM* param);
  28. // static void Analog_Value(WEIGHING_PARAM* param, uint8_t num, float* buf);
  29. /*参数列表*/
  30. static struct SENSOR sensor1 =
  31. {
  32. .SCK_GPIO_Port = SCK1_GPIO_Port,
  33. .SCK_Pin = SCK1_Pin,
  34. .DOUT_GPIO_Port = DOUT1_GPIO_Port,
  35. .DOUT_Pin = DOUT1_Pin,
  36. .K.f = 112.000f,
  37. .base_k = 112.0f,
  38. };
  39. static struct SENSOR sensor2 =
  40. {
  41. .SCK_GPIO_Port = SCK2_GPIO_Port,
  42. .SCK_Pin = SCK2_Pin,
  43. .DOUT_GPIO_Port = DOUT2_GPIO_Port,
  44. .DOUT_Pin = DOUT2_Pin,
  45. .K.f = 112.000f,
  46. .base_k = 112.0f,
  47. .Num = 1,
  48. };
  49. static struct SENSOR sensor3 =
  50. {
  51. .SCK_GPIO_Port = SCK3_GPIO_Port,
  52. .SCK_Pin = SCK3_Pin,
  53. .DOUT_GPIO_Port = DOUT3_GPIO_Port,
  54. .DOUT_Pin = DOUT3_Pin,
  55. .K.f = 112.000f,
  56. .base_k = 112.0f,
  57. .Num = 2,
  58. };
  59. static struct SENSOR sensor4 =
  60. {
  61. .SCK_GPIO_Port = SCK4_GPIO_Port,
  62. .SCK_Pin = SCK4_Pin,
  63. .DOUT_GPIO_Port = DOUT4_GPIO_Port,
  64. .DOUT_Pin = DOUT4_Pin,
  65. .K.f = 112.000f,
  66. .base_k = 112.0f,
  67. .Num = 3,
  68. };
  69. static WEIGHING_OPS __ops = {
  70. .get_allgrossweight = Get_All_GrossWeight,
  71. .get_weight = Get_Weight,
  72. .processing_data = Processing_Data,
  73. .filter_init = Filter_Init,
  74. };
  75. static WEIGHING_DEVICE _device = {
  76. .sensor = {&sensor1, &sensor2, &sensor3, &sensor4},
  77. .sensor_num_mask = 4,
  78. .correct_k = 1,
  79. ._ops = &__ops,
  80. };
  81. uint32_t (*read)(struct SENSOR *sensor) = Read_Value;
  82. WEIGHING_DEVICE *Get_Device_Handle(void)
  83. {
  84. return &_device;
  85. }
  86. uint32_t Read_Value(struct SENSOR *sensor)
  87. {
  88. uint32_t Value;
  89. uint8_t CycleIndex;
  90. Value = 0;
  91. // sensor->DOUT_GPIO_Port->BSRR = sensor->DOUT_Pin;
  92. // sensor->SCK_GPIO_Port->BSRR = (uint32_t)sensor->SCK_Pin << 16;
  93. // delay(20);
  94. HAL_GPIO_WritePin(sensor->SCK_GPIO_Port, sensor->SCK_Pin, 0);
  95. uint32_t time = HAL_GetTick();
  96. /*等待HX711准备好数据*/
  97. while (HAL_GPIO_ReadPin(sensor->DOUT_GPIO_Port, sensor->DOUT_Pin))
  98. {
  99. /*大量采集时做了延迟保护*/
  100. if (HAL_GetTick() - time > 100)
  101. {
  102. return 0;
  103. }
  104. }
  105. for (CycleIndex = 0; CycleIndex < 24; ++CycleIndex)
  106. {
  107. // sensor->SCK_GPIO_Port->BSRR = (uint32_t)sensor->SCK_Pin;
  108. HAL_GPIO_WritePin(sensor->SCK_GPIO_Port, sensor->SCK_Pin, 1);
  109. delay_us(5);
  110. Value = Value << 1;
  111. // sensor->SCK_GPIO_Port->BSRR = (uint32_t)sensor->SCK_Pin << 16;
  112. HAL_GPIO_WritePin(sensor->SCK_GPIO_Port, sensor->SCK_Pin, 0);
  113. delay_us(5);
  114. if (HAL_GPIO_ReadPin(sensor->DOUT_GPIO_Port, sensor->DOUT_Pin))
  115. {
  116. Value++;
  117. }
  118. }
  119. /*第25个时钟脉冲选择128倍增益通道*/
  120. // sensor->SCK_GPIO_Port->BSRR = (uint32_t)sensor->SCK_Pin ;
  121. HAL_GPIO_WritePin(sensor->SCK_GPIO_Port, sensor->SCK_Pin, 1);
  122. Value = Value ^ BASE_VALUE; // 异或取原码防止过零
  123. Value = Value & 0xFFFFC0; // 后7位太抖,根据手册取无噪声有效位17位,且0x7F只有1g大小,放弃使用
  124. // sensor->SCK_GPIO_Port->BSRR = (uint32_t)sensor->SCK_Pin << 16;
  125. HAL_GPIO_WritePin(sensor->SCK_GPIO_Port, sensor->SCK_Pin, 0);
  126. return (Value);
  127. }
  128. //============ 均值滤波器 ==================
  129. static void MeanFilterInit( MeanFilter_t *filter, uint8_t coef_size, float def )
  130. {
  131. for(uint8_t i = 0; i < coef_size; i++)
  132. {
  133. filter->input[i] = def;
  134. }
  135. filter->index = 0;
  136. filter->sum = def * coef_size;
  137. filter->output = def;
  138. filter->init = true;
  139. }
  140. static bool wait_front_data(void)
  141. {
  142. static uint8_t wait_count = 0;
  143. wait_count++;
  144. if(wait_count >= SAMPLE_NUM * SENSOR_NUM)
  145. {
  146. wait_count = SAMPLE_NUM * SENSOR_NUM;
  147. return false;
  148. }
  149. return true;
  150. }
  151. /* Computes a MeanFilter_t filter on a sample */
  152. static float meanApply( MeanFilter_t *filter, float input, uint8_t size)
  153. {
  154. filter->sum -= filter->input[filter->index];
  155. filter->sum += input;
  156. filter->output = filter->sum / size;
  157. filter->input[filter->index] = input;
  158. filter->index++;
  159. filter->index = filter->index % size;
  160. return filter->output;
  161. }
  162. static void Filter_Init(void)
  163. {
  164. for(uint8_t i = 0; i < SENSOR_NUM; i++)
  165. {
  166. MeanFilterInit(&_mean_filter[i], SAMPLE_NUM, 0);
  167. }
  168. }
  169. static void old_Filter_Value(uint32_t data_tmp[][SAMPLE_NUM])
  170. {
  171. WEIGHING_DEVICE *device = &_device;
  172. uint8_t sensor_num_c;
  173. uint32_t tmp1 = 0;
  174. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  175. {
  176. if (sensor_num_c == device->sensor_num_mask)
  177. {
  178. continue;
  179. }
  180. for (uint8_t j = 0; j < SAMPLE_NUM - 1; j++)
  181. {
  182. for (uint8_t k = j + 1; k < SAMPLE_NUM; k++)
  183. {
  184. if (data_tmp[sensor_num_c][j] > data_tmp[sensor_num_c][k])
  185. {
  186. tmp1 = data_tmp[sensor_num_c][j];
  187. data_tmp[sensor_num_c][j] = data_tmp[sensor_num_c][k];
  188. data_tmp[sensor_num_c][k] = tmp1;
  189. }
  190. }
  191. }
  192. }
  193. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  194. {
  195. if (sensor_num_c == device->sensor_num_mask)
  196. {
  197. continue;
  198. }
  199. struct SENSOR *sensor = device->sensor[sensor_num_c];
  200. sensor->Raw_Value = 0;
  201. for (uint8_t i = SAMPLE_NUM - 18; i < SAMPLE_NUM - 2; i++)
  202. {
  203. // sensor->Raw_Value += *((uint32_t*)tmp + sensor_num_c * SAMPLE_NUM + i);
  204. sensor->Raw_Value += data_tmp[sensor_num_c][i];
  205. }
  206. // sensor->Raw_Value = *((uint32_t*)tmp + sensor_num_c * SAMPLE_NUM + VALUE_MID);
  207. sensor->Raw_Value = sensor->Raw_Value / (SAMPLE_NUM - 4);
  208. if ((sensor->GrossWeight) < sensor->Raw_Value)
  209. {
  210. sensor->Real_Variation = -(float)(sensor->Raw_Value - sensor->GrossWeight);
  211. }
  212. else if ((sensor->GrossWeight) >= sensor->Raw_Value)
  213. {
  214. sensor->Real_Variation = (float)(sensor->GrossWeight - sensor->Raw_Value);
  215. }
  216. }
  217. }
  218. void Filter_Value(uint32_t* data)
  219. {
  220. WEIGHING_DEVICE *device = &_device;
  221. uint8_t sensor_num_c;
  222. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  223. {
  224. if (sensor_num_c == device->sensor_num_mask)
  225. {
  226. continue;
  227. }
  228. struct SENSOR *sensor = device->sensor[sensor_num_c];
  229. sensor->Raw_Value =
  230. meanApply(&_mean_filter[sensor_num_c] , data[sensor_num_c], SAMPLE_NUM);
  231. if(wait_front_data())
  232. {
  233. sensor->Raw_Value = sensor->GrossWeight;
  234. }
  235. if ((sensor->GrossWeight) < sensor->Raw_Value)
  236. {
  237. sensor->Real_Variation = -(float)(sensor->Raw_Value - sensor->GrossWeight);
  238. }
  239. else if ((sensor->GrossWeight) >= sensor->Raw_Value)
  240. {
  241. sensor->Real_Variation = (float)(sensor->GrossWeight - sensor->Raw_Value);
  242. }
  243. }
  244. }
  245. /*转重量*/
  246. void Convert_Into_Weight(void)
  247. {
  248. WEIGHING_DEVICE *device = &_device;
  249. uint8_t sensor_num_c;
  250. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  251. {
  252. if (sensor_num_c == device->sensor_num_mask)
  253. {
  254. continue;
  255. }
  256. struct SENSOR *sensor = device->sensor[sensor_num_c];
  257. if (sensor->Real_Variation < 0)
  258. {
  259. sensor->Real_Variation = fabsf(sensor->Real_Variation);
  260. }
  261. sensor->Real_Weight = sensor->Real_Variation / sensor->base_k;
  262. // if(sensor->Real_Variation < 0)
  263. // {
  264. // sensor->Real_Weight = 0;
  265. // }
  266. }
  267. }
  268. static void Processing_Data(void)
  269. {
  270. uint8_t sensor_num_c;
  271. uint8_t err_count = 0;
  272. WEIGHING_DEVICE *device = &_device;
  273. // static uint8_t index = 0;
  274. uint32_t data[SENSOR_NUM] = {0};
  275. // // static uint32_t** pdata = NULL;
  276. // if(pdata == NULL)
  277. // {
  278. // pdata = (uint32_t**)malloc(sizeof(uint32_t*) * device->sensor_num);
  279. // for(sensor_num_c = 0; sensor_num_c < device->sensor_num; sensor_num_c++)
  280. // {
  281. // if(sensor_num_c == device->sensor_num_mask)
  282. // {
  283. // continue;
  284. // }
  285. // pdata[sensor_num_c] = (uint32_t*)malloc(sizeof(uint32_t) * SAMPLE_NUM);
  286. // }
  287. // }
  288. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  289. {
  290. if (sensor_num_c == device->sensor_num_mask)
  291. {
  292. continue;
  293. }
  294. data[sensor_num_c] = read(device->sensor[sensor_num_c]);
  295. while (data[sensor_num_c] == 0)
  296. {
  297. data[sensor_num_c] = read(device->sensor[sensor_num_c]);
  298. err_count++;
  299. if (err_count > 10 && device->sensor_num_mask == 4)
  300. {
  301. device->sensor[sensor_num_c]->err_flag = true;
  302. device->sensor_num_mask = sensor_num_c;
  303. err_count = 0;
  304. return;
  305. }
  306. }
  307. }
  308. // index++;
  309. // if (index == SAMPLE_NUM)
  310. {
  311. Filter_Value(data);
  312. Convert_Into_Weight();
  313. // for(sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  314. // {
  315. // // if(sensor_num_c == device->sensor_num_mask)
  316. // // {
  317. // // continue;
  318. // // }
  319. // free(pdata[sensor_num_c]);
  320. // }
  321. // free(pdata);
  322. // pdata = NULL;
  323. // memset(databuf, 0, sizeof(databuf));
  324. // index = 0;
  325. }
  326. }
  327. static float Deal_With_Jitter(WEIGHING_DEVICE *_device, float *buff)
  328. {
  329. WEIGHING_DEVICE *device = _device;
  330. static uint32_t deal_drift = 0;
  331. static float last_total = 0;
  332. static float buff_snapshot[SENSOR_NUM] = {0.0f};
  333. float total = 0;
  334. uint8_t sensor_num_c;
  335. last_total = 0;
  336. /*赋值*/
  337. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  338. {
  339. if (sensor_num_c == device->sensor_num_mask)
  340. {
  341. continue;
  342. }
  343. total += buff[sensor_num_c];
  344. last_total += buff_snapshot[sensor_num_c];
  345. }
  346. if (device->Weight_last != 0)
  347. {
  348. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  349. {
  350. if (sensor_num_c == device->sensor_num_mask)
  351. {
  352. continue;
  353. }
  354. /*限幅消抖*/
  355. if (fabsf(last_total - total) < ALLOW_VALUE)
  356. {
  357. deal_drift++;
  358. if (deal_drift > N)
  359. {
  360. buff[sensor_num_c] = buff_snapshot[sensor_num_c];
  361. }
  362. }
  363. else
  364. {
  365. deal_drift = 0;
  366. }
  367. }
  368. }
  369. // /*清零*/
  370. total = 0;
  371. /*重新赋值*/
  372. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  373. {
  374. if (sensor_num_c == device->sensor_num_mask)
  375. {
  376. continue;
  377. }
  378. if ((buff[sensor_num_c] < MAX_ZERO && buff[sensor_num_c] > MIN_ZERO))
  379. {
  380. buff[sensor_num_c] = 0.f;
  381. }
  382. total += buff[sensor_num_c];
  383. }
  384. memcpy(buff_snapshot, buff, sizeof(buff_snapshot));
  385. return total;
  386. }
  387. static float Get_Weight(void)
  388. {
  389. WEIGHING_DEVICE *device = &_device;
  390. uint8_t sensor_num_c;
  391. // float Total = 0.f;
  392. float allot = 0.f;
  393. float buff[SENSOR_NUM] = {0.f};
  394. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  395. {
  396. if (sensor_num_c == device->sensor_num_mask)
  397. {
  398. continue;
  399. }
  400. buff[sensor_num_c] = device->sensor[sensor_num_c]->Real_Weight;
  401. allot += buff[sensor_num_c];
  402. }
  403. allot = allot / device->correct_k;
  404. /*用于校准时分配偏移量*/
  405. // for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  406. // {
  407. // if (sensor_num_c == device->sensor_num_mask)
  408. // {
  409. // continue;
  410. // }
  411. // device->sensor[sensor_num_c]->Scale = buff[sensor_num_c] / allot;
  412. // // if(device->sensor[sensor_num_c]->Scale <= 0.1f)
  413. // // {
  414. // // device->sensor[sensor_num_c]->Scale = NAN;
  415. // // }
  416. // }
  417. // Total = Deal_With_Jitter(device ,buff);
  418. return allot;
  419. }
  420. // static void Analog_Value(WEIGHING_DEVICE* _device, uint8_t num, float* buf)
  421. // {
  422. // float tmp = 0.f;
  423. // if(num)
  424. // {
  425. // WEIGHING_DEVICE* device = _device;
  426. // for(uint8_t sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  427. // {
  428. // tmp += buf[sensor_num_c];
  429. // }
  430. // buf[num - 1] = tmp / (SENSOR_NUM - 1);
  431. // tmp = 0;
  432. // for(uint8_t sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  433. // {
  434. // tmp += device->sensor[sensor_num_c]->Scale;
  435. // }
  436. // device->sensor[num - 1]->Scale = tmp / SENSOR_NUM;
  437. // Err_Check_And_Set(GOWRONG);
  438. // }
  439. // else if(Check_No_Impact(device) == 0)
  440. // {
  441. // Err_Check_And_Set(0);
  442. // }
  443. // }
  444. static uint32_t Get_GrossWeight(struct SENSOR *sensor)
  445. {
  446. uint32_t GrossWeight = 0;
  447. int i, j, tmp1;
  448. uint32_t sum = 0;
  449. uint32_t tmp[5] = {0};
  450. uint8_t err_count = 0;
  451. /*采集零点*/
  452. for (i = 0; i < 5; ++i)
  453. {
  454. tmp[i] = read(sensor);
  455. if (tmp[i] <= 10)
  456. {
  457. WEIGHING_DEVICE *device = Get_Device_Handle();
  458. err_count++;
  459. if (err_count > 10)
  460. {
  461. if (device->sensor_num_mask != 4)
  462. {
  463. sensor->err_flag = true;
  464. err_count = 0;
  465. return 0.f;
  466. }
  467. device->sensor_num_mask = sensor->Num;
  468. return 0.f;
  469. }
  470. i--;
  471. }
  472. }
  473. for (i = 0; i < 4; ++i)
  474. {
  475. for (j = i + 1; j < 5; ++j)
  476. {
  477. if (tmp[i] > tmp[j])
  478. {
  479. tmp1 = tmp[i];
  480. tmp[i] = tmp[j];
  481. tmp[j] = tmp1;
  482. }
  483. }
  484. }
  485. for (i = 1; i < 4; i++)
  486. {
  487. sum += tmp[i];
  488. }
  489. GrossWeight = sum / 3;
  490. return GrossWeight;
  491. }
  492. static void Get_All_GrossWeight(void)
  493. {
  494. WEIGHING_DEVICE *device = &_device;
  495. uint8_t sensor_num_c;
  496. /*上电后读取数据作为零点*/
  497. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  498. {
  499. if (sensor_num_c == device->sensor_num_mask)
  500. {
  501. continue;
  502. }
  503. read(device->sensor[sensor_num_c]);
  504. }
  505. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  506. {
  507. if (sensor_num_c == device->sensor_num_mask)
  508. {
  509. continue;
  510. }
  511. device->sensor[sensor_num_c]->GrossWeight = Get_GrossWeight(device->sensor[sensor_num_c]);
  512. }
  513. device->check_self_flag = true; // 自检标志位
  514. }
  515. #if 0
  516. static void Calib_Creep(WEIGHING_DEVICE* _device, float* buf, float total_weight)
  517. {
  518. WEIGHING_DEVICE* device = _device;
  519. uint8_t sensor_num_c;
  520. float creep_value = 0.f;
  521. if(HAL_GetTick() - device->creep_time >= HALF_HOUR)
  522. {
  523. device->creep_count++;
  524. device->creep_time = HAL_GetTick();
  525. for(sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  526. {
  527. device->creep_snap += device->sensor[sensor_num_c]->Real_Weight;
  528. }
  529. creep_value = Get_Compare(device, total_weight);
  530. }
  531. if(device->creep_count)
  532. {
  533. for(sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  534. {
  535. buf[sensor_num_c] += creep_value * device->creep_count;
  536. }
  537. // total_weight += creep_value * SENSOR_NUM * device->creep_count;
  538. }
  539. }
  540. static float Get_Compare(WEIGHING_DEVICE* _device, float total_weight)
  541. {
  542. return _device->creep_snap >= total_weight ? CREEP_VALUE : -CREEP_VALUE;
  543. }
  544. #endif