weight_read.c 16 KB

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