AppFuncLib.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * @Author: chenjie
  3. * @Date: 2022-06-06
  4. * @LastEditTime: 2022-11-10
  5. * @LastEditors: chenjie
  6. * @Description:
  7. * @FilePath: \S32K146_4G\code\app\lib\AppFuncLib.c
  8. * Copyright (c) 2022 by chenjie, All Rights Reserved.
  9. */
  10. #include "AppFuncLib.h"
  11. #include "AppGlobalVar.h"
  12. #include "Hal_Fls.h"
  13. #include "AppTaskUart1.h"
  14. /**
  15. * @brief : 获取故障码函数,从故障数组中获取故障码,并将之前的故障码向前移动
  16. * @param {UINT16} *ErrorArray
  17. * @param {UINT8} Errorlen
  18. * @return {*}
  19. */
  20. static uint8 bcc_chk_fota(uint8 *data, uint8 length);
  21. static uint8 Fota_crc_chk(uint8 *data, uint8 length);
  22. uint16 GetErrorNum(uint16 *ErrorArray, uint8 Errorlen)
  23. {
  24. uint16 OutNum;
  25. OutNum = *(ErrorArray);
  26. for (uint8 i = 0; i < Errorlen - 1; i++)
  27. {
  28. *(ErrorArray + i) = *(ErrorArray + i + 1);
  29. if (*(ErrorArray + i + 1) == 0)
  30. break;
  31. }
  32. return OutNum;
  33. }
  34. uint8 PutErrorNum(uint16 *ErrorArray, uint8 Errorlen, uint16 ErrorNum)
  35. {
  36. for (uint8 i = 0; i < Errorlen; i++)
  37. {
  38. if (*(ErrorArray + i) == 0)
  39. {
  40. *(ErrorArray + i) = ErrorNum;
  41. return 0;
  42. }
  43. else
  44. {
  45. if (*(ErrorArray + i) == ErrorNum)
  46. {
  47. return 1;
  48. }
  49. else
  50. {
  51. continue;
  52. }
  53. }
  54. }
  55. return 2;
  56. }
  57. uint16 ATstrdel(char *str)
  58. {
  59. char *p = str;
  60. bool flag = false;
  61. while (*str)
  62. {
  63. if (*str > 0x20)
  64. {
  65. *(p) = *str;
  66. p = p + 1;
  67. flag = false;
  68. }
  69. else
  70. {
  71. if (!flag)
  72. {
  73. *(p) = ',';
  74. p = p + 1;
  75. flag = true;
  76. }
  77. }
  78. str++;
  79. }
  80. *p = '\0';
  81. return 0;
  82. }
  83. uint16 mstrlen(const char *s)
  84. {
  85. uint16 out = 0;
  86. const char *ss = s;
  87. while (*ss)
  88. ss++;
  89. out = (ss - s);
  90. return out;
  91. }
  92. int mstrncmp(const char *s1, const char *s2, int n)
  93. {
  94. const unsigned char *c1 = (const unsigned char *)s1;
  95. const unsigned char *c2 = (const unsigned char *)s2;
  96. unsigned char ch;
  97. int d = 0;
  98. while (n--)
  99. {
  100. d = (int)(ch = *c1++) - (int)*c2++;
  101. if (d || !ch)
  102. break;
  103. }
  104. return d;
  105. }
  106. unsigned char HexToChar(unsigned char bHex)
  107. {
  108. if ((bHex >= 0) && (bHex <= 9))
  109. bHex += 0x30;
  110. else if ((bHex >= 10) && (bHex <= 15)) // 大写字母
  111. bHex += 0x37;
  112. else
  113. bHex = 0xff;
  114. return bHex;
  115. }
  116. unsigned char CharToHex(unsigned char bChar)
  117. {
  118. if ((bChar >= 0x30) && (bChar <= 0x39))
  119. bChar -= 0x30;
  120. else if ((bChar >= 0x41) && (bChar <= 0x46)) // 大写字母
  121. bChar -= 0x37;
  122. else if ((bChar >= 0x61) && (bChar <= 0x66)) // 小写字母
  123. bChar -= 0x57;
  124. else
  125. bChar = 0xff;
  126. return bChar;
  127. }
  128. uint8 AtStrCompare(const char *a, const char *b)
  129. {
  130. uint8 out = 1;
  131. while (1)
  132. {
  133. if (*a == '\0' || *b == '\0') // 判断其中是否有字符串结束
  134. {
  135. if (strlen(a) == strlen(b))
  136. {
  137. out = 1;
  138. break;
  139. }
  140. else
  141. {
  142. out = 0;
  143. break;
  144. }
  145. }
  146. else
  147. {
  148. if (*a != *b)
  149. {
  150. out = 0;
  151. break;
  152. }
  153. else if (*a == '=' && *b == '=')
  154. {
  155. out = 1;
  156. break;
  157. }
  158. }
  159. a++;
  160. b++;
  161. }
  162. return out;
  163. }
  164. unsigned short CRC16_Modbus(unsigned char *pdata, int len)
  165. {
  166. unsigned short crc = 0xFFFF;
  167. int i, j;
  168. for (j = 0; j < len; j++)
  169. {
  170. crc = crc ^ pdata[j];
  171. for (i = 0; i < 8; i++)
  172. {
  173. if ((crc & 0x0001) > 0)
  174. {
  175. crc = crc >> 1;
  176. crc = crc ^ 0xa001;
  177. }
  178. else
  179. crc = crc >> 1;
  180. }
  181. }
  182. return crc;
  183. }
  184. char *Myitoa(int value, char *result, int base)
  185. {
  186. // check that the base if valid
  187. if (base < 2 || base > 36)
  188. {
  189. *result = '\0';
  190. return result;
  191. }
  192. char *ptr = result, *ptr1 = result, tmp_char;
  193. int tmp_value;
  194. do
  195. {
  196. tmp_value = value;
  197. value /= base;
  198. *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
  199. } while (value);
  200. // Apply negative sign
  201. if (tmp_value < 0)
  202. *ptr++ = '-';
  203. *ptr-- = '\0';
  204. while (ptr1 < ptr)
  205. {
  206. tmp_char = *ptr;
  207. *ptr-- = *ptr1;
  208. *ptr1++ = tmp_char;
  209. }
  210. return result;
  211. }
  212. /************************************************************************
  213. * @brief 整数转字符串
  214. * @param[in] num 整数
  215. * @param[out] buf 字符串
  216. * @return 返回字符串长度
  217. ************************************************************************/
  218. inline int _itoa(int num, char buf[32])
  219. {
  220. return _i2a(num, buf, 10);
  221. }
  222. /************************************************************************
  223. * @brief 整数转字符串
  224. * @param[in] num 整数
  225. * @param[out] buf 字符串
  226. * @param[in] radix 进位制整数
  227. * @return 返回字符串长度
  228. ************************************************************************/
  229. int _i2a(int num, char buf[32], int radix)
  230. {
  231. static const char s[] = "0123456789abcdef";
  232. int n = num, R = radix;
  233. char *dst = buf;
  234. if (n < 0)
  235. {
  236. *dst++ = '-';
  237. n = -n;
  238. }
  239. if (n < 10)
  240. {
  241. *dst++ = s[n];
  242. *dst = 0;
  243. }
  244. else
  245. {
  246. char tmp[32], *p = tmp;
  247. while (n)
  248. {
  249. *p++ = s[n % R];
  250. n /= R;
  251. }
  252. while (--p != tmp)
  253. *dst++ = *p;
  254. *dst++ = *tmp;
  255. *dst = 0;
  256. }
  257. return dst - buf;
  258. }
  259. /************************************************************************
  260. * @brief 浮点数转字符串
  261. * @param[in] val 浮点数
  262. * @param[out] buf 字符串
  263. * @param[in] eps 精度(小数位)
  264. * @return 返回字符串长度
  265. ************************************************************************/
  266. int _ftoa(double val, char buf[32], int eps)
  267. {
  268. double f = val;
  269. char *p = buf;
  270. if (val < 0)
  271. {
  272. *p++ = '-';
  273. f = -f;
  274. }
  275. int n = f;
  276. int len = _itoa(n, p);
  277. return len + __ftoa(f - n, p + len, eps);
  278. }
  279. /************************************************************************
  280. * @brief 浮点数转字符串:范围(-1, 1)
  281. * @param[in] val 浮点数
  282. * @param[out] buf 字符串
  283. * @param[in] eps 精度(小数位)
  284. * @return 返回字符串长度
  285. ************************************************************************/
  286. int __ftoa(double val, char buf[32], int eps)
  287. {
  288. double f = val;
  289. char *p = buf;
  290. static const char s[] = "0123456789";
  291. if (f < 0)
  292. {
  293. *p++ = '-';
  294. f = -f;
  295. }
  296. *p++ = '.';
  297. for (int i = eps + 1, n; --i; ++p, f -= n)
  298. *p = s[n = f *= 10.0];
  299. *p = 0;
  300. return p - buf;
  301. }
  302. /************************************************************************
  303. * @brief 替换sprintf
  304. * @ref 可变长参数列表误区与陷阱——va_arg不可接受的类型
  305. * http://www.cppblog.com/ownwaterloo/archive/2009/04/21/80655.aspx
  306. ************************************************************************/
  307. int _sprintf(char *dst, const char *format, ...)
  308. {
  309. char *s = dst;
  310. const char *f = format;
  311. va_list ap, another;
  312. va_start(ap, format);
  313. va_copy(another, ap);
  314. while (*f)
  315. {
  316. int n = 1;
  317. if ('%' != *f)
  318. {
  319. *s = *f;
  320. }
  321. else
  322. {
  323. ++f;
  324. switch (*f)
  325. {
  326. case 's': // 字符串
  327. {
  328. const char *p = va_arg(ap, char *);
  329. n = strlen(p);
  330. memcpy(s, p, n);
  331. }
  332. break;
  333. case 'd':
  334. case 'u': // 整数
  335. {
  336. char buf[32];
  337. n = _itoa(va_arg(ap, int), buf);
  338. memcpy(s, buf, n);
  339. }
  340. break;
  341. case 'f': // 浮点数
  342. {
  343. char buf[32];
  344. n = _ftoa(va_arg(ap, double), buf, 6);
  345. memcpy(s, buf, n);
  346. }
  347. break;
  348. case 'x': // 16进制数
  349. {
  350. char buf[32];
  351. n = _i2a(va_arg(ap, int), buf, 16);
  352. memcpy(s, buf, n);
  353. }
  354. break;
  355. case 'c': // 字符
  356. {
  357. *s = va_arg(ap, int);
  358. }
  359. break;
  360. case '%': // 百分号
  361. {
  362. *s = '%';
  363. }
  364. break;
  365. default:
  366. {
  367. va_end(ap);
  368. int x = vsprintf(dst, format, another);
  369. va_end(another);
  370. return x;
  371. }
  372. break;
  373. }
  374. }
  375. ++f;
  376. s += n;
  377. }
  378. *s = 0;
  379. va_end(ap);
  380. return s - dst;
  381. }
  382. uint8 bcc_chk(uint8 *data, uint16 length)
  383. {
  384. uint8 bcc_chk_return = 0x00;
  385. uint16 count = 0;
  386. while (count < length)
  387. {
  388. bcc_chk_return ^= data[count];
  389. count++;
  390. }
  391. return bcc_chk_return;
  392. }
  393. uint16 crc_chk(uint8 *data, uint8 length)
  394. {
  395. uint8 j;
  396. uint16 reg_crc = 0xFFFF;
  397. while (length--)
  398. {
  399. reg_crc ^= *data++;
  400. for (j = 0; j < 8; j++)
  401. {
  402. if (reg_crc & 0x01)
  403. {
  404. reg_crc = (reg_crc >> 1) ^ 0xA001;
  405. }
  406. else
  407. {
  408. reg_crc = reg_crc >> 1;
  409. }
  410. }
  411. }
  412. return reg_crc;
  413. }
  414. /***************************************************************************************************/
  415. #define PI 3.14159265358979
  416. //-------------------uint16数组取最大-------------------------------------
  417. uint16_T ArrMax(uint16_T *Data, uint16_T n)
  418. {
  419. uint16_T i;
  420. uint16_T DataMax;
  421. DataMax = Data[0];
  422. for (i = 0; i < n; i++)
  423. {
  424. if (DataMax < Data[i])
  425. {
  426. DataMax = Data[i];
  427. }
  428. }
  429. return DataMax;
  430. }
  431. //-----------------uint16数组取最小-------------------------------------
  432. uint16_T ArrMin(uint16_T *Data, uint16_T n)
  433. {
  434. uint16_T i;
  435. uint16_T DataMin;
  436. DataMin = Data[0];
  437. for (i = 0; i < n; i++)
  438. {
  439. if (DataMin > Data[i])
  440. {
  441. DataMin = Data[i];
  442. }
  443. }
  444. return DataMin;
  445. }
  446. //
  447. /////////////////////////// int16数组求均值////////////////////////////////////
  448. int16_T ArrMean(int16_T *Data, uint16_T n)
  449. {
  450. uint16_T i;
  451. int32_T Sum = 0;
  452. int16_T DataMean;
  453. for (i = 0; i < n; i++)
  454. {
  455. Sum = Sum + Data[i];
  456. }
  457. DataMean = (int16_T)Sum / n;
  458. return DataMean;
  459. }
  460. //-----------------real_T 限幅函数
  461. real_T Saturation_r(real_T in, real_T LowLim, real_T UpLim)
  462. {
  463. real_T out;
  464. out = in > LowLim ? in : LowLim;
  465. out = out > UpLim ? UpLim : out;
  466. return out;
  467. }
  468. //-----------------uint16 限幅函数
  469. uint16_T Saturation_u(uint16_T in, uint16_T LowLim, uint16_T UpLim)
  470. {
  471. uint16_T out;
  472. out = in > LowLim ? in : LowLim;
  473. out = out > UpLim ? UpLim : out;
  474. return out;
  475. }
  476. // ---------------------滤波控制变动速率--------------------------------------
  477. uint16_T DataFilt(uint16_T in, uint16_T *out, uint16_T Lim)
  478. {
  479. int16_T delt;
  480. delt = (int16_T)(in - *out);
  481. if (delt > Lim)
  482. {
  483. *out = *out + (delt > Lim ? Lim : delt);
  484. }
  485. if (delt < -Lim)
  486. {
  487. *out = *out + (delt < -Lim ? -Lim : delt);
  488. }
  489. if (delt <= Lim && delt >= -Lim)
  490. {
  491. *out = in;
  492. }
  493. return *out;
  494. }
  495. // ---------------------滤波控制变动速率--------------------------------------
  496. uint8_T DataFilt8(uint8_T in, uint8_T *out, uint8_T Lim)
  497. {
  498. int8_T delt;
  499. delt = (int8_T)(in - *out);
  500. if (delt > Lim)
  501. {
  502. *out = *out + (delt > Lim ? Lim : delt);
  503. }
  504. if (delt < -Lim)
  505. {
  506. *out = *out + (delt < -Lim ? -Lim : delt);
  507. }
  508. if (delt <= Lim && delt >= -Lim)
  509. {
  510. *out = in;
  511. }
  512. return *out;
  513. }
  514. //-------------------uint16 to uint16的一维查表--------------------------------------
  515. uint16_T look1_u16tu16(uint16_T u0, const uint16_T *bp0, const uint16_T *table, uint16_T MaxLen)
  516. {
  517. uint32_T bpIdx = 0;
  518. uint32_T iLeft = 0;
  519. uint32_T iRght = 0;
  520. uint16_T y = 0;
  521. uint32_T yL_0d0 = 0;
  522. uint32_T yR_0d0 = 0;
  523. uint32_T maxIndex = MaxLen - 1;
  524. if (u0 <= bp0[0U])
  525. {
  526. iLeft = 0U;
  527. iRght = 0U;
  528. }
  529. else if (u0 < bp0[maxIndex])
  530. {
  531. //????????u0??λ??
  532. bpIdx = maxIndex >> 1U;
  533. iLeft = 0U;
  534. iRght = maxIndex;
  535. while ((iRght - iLeft) > 1)
  536. {
  537. if (u0 < bp0[bpIdx])
  538. {
  539. iRght = bpIdx;
  540. }
  541. else
  542. {
  543. iLeft = bpIdx;
  544. }
  545. bpIdx = (iRght + iLeft) >> 1U;
  546. }
  547. }
  548. else
  549. {
  550. iLeft = maxIndex;
  551. iRght = maxIndex;
  552. }
  553. //???λ??????????
  554. if (iLeft != iRght)
  555. {
  556. //??????
  557. yR_0d0 = table[iLeft + 1U];
  558. yL_0d0 = table[iLeft];
  559. if (yR_0d0 >= yL_0d0)
  560. {
  561. y = (uint16_T)(((uint32_T)(u0 - bp0[iLeft]) * (yR_0d0 - yL_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]) + yL_0d0);
  562. }
  563. else
  564. {
  565. y = (uint16_T)((yL_0d0 - ((uint32_T)(u0 - bp0[iLeft]) * (yL_0d0 - yR_0d0)) / (bp0[iLeft + 1] - bp0[iLeft])));
  566. }
  567. }
  568. else
  569. {
  570. y = (uint16_T)table[iLeft];
  571. }
  572. return y;
  573. }
  574. //-------------------int16 to uint16的一维查表--------------------------------------
  575. uint16_T look1_i16tu16(int16_T u0, const int16_T *bp0, const uint16_T *table, uint16_T MaxLen)
  576. {
  577. uint32_T bpIdx = 0;
  578. uint32_T iLeft = 0;
  579. uint32_T iRght = 0;
  580. uint16_T y = 0;
  581. uint32_T yL_0d0 = 0;
  582. uint32_T yR_0d0 = 0;
  583. uint32_T maxIndex = MaxLen - 1;
  584. if (u0 <= bp0[0U])
  585. {
  586. iLeft = 0U;
  587. iRght = 0U;
  588. }
  589. else if (u0 < bp0[maxIndex])
  590. {
  591. //????????u0??λ??
  592. bpIdx = maxIndex >> 1U;
  593. iLeft = 0U;
  594. iRght = maxIndex;
  595. while ((iRght - iLeft) > 1)
  596. {
  597. if (u0 < bp0[bpIdx])
  598. {
  599. iRght = bpIdx;
  600. }
  601. else
  602. {
  603. iLeft = bpIdx;
  604. }
  605. bpIdx = (iRght + iLeft) >> 1U;
  606. }
  607. }
  608. else
  609. {
  610. iLeft = maxIndex;
  611. iRght = maxIndex;
  612. }
  613. //???λ??????????
  614. if (iLeft != iRght)
  615. {
  616. //??????
  617. yR_0d0 = table[iLeft + 1U];
  618. yL_0d0 = table[iLeft];
  619. if (yR_0d0 >= yL_0d0)
  620. {
  621. y = (uint16_T)(((uint32_T)(u0 - bp0[iLeft]) * (yR_0d0 - yL_0d0)) / (bp0[iLeft + 1] - bp0[iLeft]) + yL_0d0);
  622. }
  623. else
  624. {
  625. y = (uint16_T)((yL_0d0 - ((uint32_T)(u0 - bp0[iLeft]) * (yL_0d0 - yR_0d0)) / (bp0[iLeft + 1] - bp0[iLeft])));
  626. }
  627. }
  628. else
  629. {
  630. y = (uint16_T)table[iLeft];
  631. }
  632. return y;
  633. }
  634. // uint16 二维查表
  635. uint16_T look2_u16u16tu16(uint16_T x, uint16_T y, const uint16_T xTable[], const uint16_T yTable[], const uint16_T zTable[], uint16_T xLen, uint16_T yLen)
  636. {
  637. uint16_T xMaxIndex;
  638. uint16_T yMaxIndex;
  639. uint16_T xIndexLeft;
  640. uint16_T xIndexRight;
  641. uint16_T yIndexLeft;
  642. uint16_T yIndexRight;
  643. uint16_T xValueLeft;
  644. uint16_T xValueRight;
  645. uint16_T yValueLeft;
  646. uint16_T yValueRight;
  647. uint16_T bpIdx;
  648. uint32_T zIndex1;
  649. uint32_T zIndex2;
  650. uint32_T zValueMid_xLeft;
  651. uint32_T zValueMid_xRight;
  652. uint16_T z;
  653. xMaxIndex = xLen - 1;
  654. yMaxIndex = yLen - 1;
  655. if (x <= xTable[0U])
  656. {
  657. xIndexLeft = 0U;
  658. xIndexRight = 0U;
  659. }
  660. else if (x >= xTable[xMaxIndex])
  661. {
  662. xIndexLeft = xMaxIndex;
  663. xIndexRight = xMaxIndex;
  664. }
  665. else
  666. {
  667. bpIdx = xMaxIndex >> 1U;
  668. xIndexLeft = 0;
  669. xIndexRight = xMaxIndex;
  670. while (xIndexRight - xIndexLeft > 1U)
  671. {
  672. if (x < xTable[bpIdx])
  673. {
  674. xIndexRight = bpIdx;
  675. }
  676. else
  677. {
  678. xIndexLeft = bpIdx;
  679. }
  680. bpIdx = (xIndexRight + xIndexLeft) >> 1U;
  681. }
  682. }
  683. xValueLeft = xTable[xIndexLeft];
  684. xValueRight = xTable[xIndexRight];
  685. if (y <= yTable[0U])
  686. {
  687. yIndexLeft = 0U;
  688. yIndexRight = 0U;
  689. }
  690. else if (y >= yTable[yMaxIndex])
  691. {
  692. yIndexLeft = yMaxIndex;
  693. yIndexRight = yMaxIndex;
  694. }
  695. else
  696. {
  697. bpIdx = yMaxIndex >> 1U;
  698. yIndexLeft = 0;
  699. yIndexRight = yMaxIndex;
  700. while (yIndexRight - yIndexLeft > 1U)
  701. {
  702. if (y < yTable[bpIdx])
  703. {
  704. yIndexRight = bpIdx;
  705. }
  706. else
  707. {
  708. yIndexLeft = bpIdx;
  709. }
  710. bpIdx = (yIndexRight + yIndexLeft) >> 1U;
  711. }
  712. }
  713. yValueLeft = yTable[yIndexLeft];
  714. yValueRight = yTable[yIndexRight];
  715. zIndex1 = yIndexLeft * xLen + xIndexLeft;
  716. zIndex2 = yIndexRight * xLen + xIndexLeft;
  717. if (yIndexLeft != yIndexRight)
  718. {
  719. if (zTable[zIndex2] > zTable[zIndex1])
  720. {
  721. zValueMid_xLeft = (uint16_T)(((uint32_T)(y - yValueLeft) * (zTable[zIndex2] - zTable[zIndex1])) / (yValueRight - yValueLeft) + zTable[zIndex1]);
  722. }
  723. else
  724. {
  725. zValueMid_xLeft = (uint16_T)(zTable[zIndex1] - ((uint32_T)(y - yValueLeft) * (zTable[zIndex1] - zTable[zIndex2])) / (yValueRight - yValueLeft));
  726. }
  727. if (zTable[zIndex2 + 1] > zTable[zIndex1 + 1])
  728. {
  729. zValueMid_xRight = (uint16_T)(((uint32_T)(y - yValueLeft) * (zTable[zIndex2 + 1] - zTable[zIndex1 + 1])) / (yValueRight - yValueLeft) + zTable[zIndex1 + 1]);
  730. }
  731. else
  732. {
  733. zValueMid_xRight = (uint16_T)(zTable[zIndex1 + 1] - ((uint32_T)(y - yValueLeft) * (zTable[zIndex1 + 1] - zTable[zIndex2 + 1])) / (yValueRight - yValueLeft));
  734. }
  735. }
  736. else
  737. {
  738. zValueMid_xLeft = (uint16_T)zTable[zIndex1];
  739. zValueMid_xRight = (uint16_T)zTable[zIndex1 + 1];
  740. }
  741. if (xIndexLeft != xIndexRight)
  742. {
  743. if (zValueMid_xLeft < zValueMid_xRight)
  744. {
  745. z = (uint16_T)(((uint32_T)(x - xValueLeft) * (zValueMid_xRight - zValueMid_xLeft)) / ((uint16_T)(xValueRight - xValueLeft)) + zValueMid_xLeft);
  746. }
  747. else
  748. {
  749. z = (uint16_T)(zValueMid_xLeft - ((uint32_T)(x - xValueLeft) * (zValueMid_xLeft - zValueMid_xRight)) / ((uint16_T)(xValueRight - xValueLeft)));
  750. }
  751. }
  752. else
  753. {
  754. z = (uint16_T)(zValueMid_xLeft);
  755. }
  756. return z;
  757. }
  758. //-----------------------------------------------诊断大于阈值
  759. boolean_T DiagThrSystem1(boolean_T Enable, boolean_T precondition, uint16_T Input, uint16_T fltThr, uint16_T recThr, uint8_T fltNumThr, uint8_T recNumThr, uint8_T *fltNum, uint8_T *recNum, boolean_T *fitFlg)
  760. {
  761. if (Enable && precondition && Input > fltThr)
  762. {
  763. *fltNum = (*fltNum + 1) > 200 ? 200 : (*fltNum + 1);
  764. }
  765. else
  766. {
  767. *fltNum = 0;
  768. }
  769. if (Enable && precondition && Input < recThr)
  770. {
  771. *recNum = (*recNum + 1) > 200 ? 200 : (*recNum + 1);
  772. }
  773. else
  774. {
  775. *recNum = 0;
  776. }
  777. if ((*fltNum > fltNumThr || (*fitFlg && *recNum < recNumThr)) && precondition)
  778. {
  779. *fitFlg = true;
  780. }
  781. else
  782. {
  783. *fitFlg = false;
  784. }
  785. return *fitFlg;
  786. }
  787. //================================诊断小于阈值===================================
  788. boolean_T DiagThrSystem2(boolean_T Enable, boolean_T precondition, uint16_T Input, uint16_T fltThr, uint16_T recThr, uint8_T fltNumThr, uint8_T recNumThr, uint8_T *fltNum, uint8_T *recNum, boolean_T *fitFlg)
  789. {
  790. if (Enable && precondition && Input < fltThr)
  791. {
  792. *fltNum = (*fltNum + 1) > 200 ? 200 : (*fltNum + 1);
  793. }
  794. else
  795. {
  796. *fltNum = 0;
  797. }
  798. if (Enable && precondition && Input > recThr)
  799. {
  800. *recNum = (*recNum + 1) > 200 ? 200 : (*recNum + 1);
  801. }
  802. else
  803. {
  804. *recNum = 0;
  805. }
  806. if ((*fltNum > fltNumThr || (*fitFlg && *recNum < recNumThr)) && precondition)
  807. {
  808. *fitFlg = true;
  809. }
  810. else
  811. {
  812. *fitFlg = false;
  813. }
  814. return *fitFlg;
  815. }
  816. //---------------------------判断条件成立次数
  817. boolean_T JudgeTimeSystem(boolean_T Enable, boolean_T Input, uint16_T *N, uint16_T Thr)
  818. {
  819. boolean_T Flg = false;
  820. if (Input && Enable)
  821. {
  822. *N = (*N + 1) > 20000 ? 20000 : (*N + 1);
  823. }
  824. else
  825. {
  826. *N = 0;
  827. }
  828. if (*N > Thr && Enable)
  829. {
  830. Flg = true;
  831. }
  832. else
  833. {
  834. Flg = false;
  835. }
  836. return Flg;
  837. }
  838. void fft(real_T *S, uint16_T N, real_T freq, real_T *returnFreq, real_T *returnP) // N为偶数
  839. {
  840. creal_T X[SIZE_FFT];
  841. creal_T W[SIZE_FFT];
  842. real_T Y1[SIZE_FFT];
  843. real_T Y2[SIZE_FFT / 2 + 1];
  844. real_T F[SIZE_FFT / 2 + 1];
  845. uint16_T i = 0;
  846. uint16_T j = 0;
  847. uint16_T k = 0;
  848. uint16_T l = 0;
  849. creal_T up;
  850. creal_T down;
  851. creal_T product;
  852. // 变换
  853. for (uint16_t i = 0; i < N; i++)
  854. {
  855. X[i].re = S[i];
  856. X[i].im = 0;
  857. }
  858. RaderReverse(X, N);
  859. for (i = 0; i < N; i++)
  860. {
  861. W[i].re = cos(2 * PI / N * i);
  862. W[i].im = -sin(2 * PI / N * i);
  863. }
  864. for (i = 0; i < log(N) / log(2); i++)
  865. {
  866. l = 1 << i;
  867. for (j = 0; j < N; j += 2 * l)
  868. {
  869. for (k = 0; k < l; k++)
  870. {
  871. cmul(X[j + k + l], W[N * k / 2 / l], &product);
  872. cadd(X[j + k], product, &up);
  873. csub(X[j + k], product, &down);
  874. X[j + k] = up;
  875. X[j + k + l] = down;
  876. }
  877. }
  878. }
  879. // 计算幅值
  880. for (i = 0; i < N; i++)
  881. {
  882. Y1[i] = sqrt((X[i].im) * (X[i].im) + (X[i].re) * (X[i].re)) / N;
  883. }
  884. Y2[0] = Y1[0];
  885. Y2[N / 2] = Y1[N / 2];
  886. for (i = 1; i < N / 2; i++)
  887. {
  888. Y2[i] = 2 * Y1[i];
  889. }
  890. // 计算频率
  891. for (i = 0; i < N / 2 + 1; i++)
  892. {
  893. F[i] = freq * i / N;
  894. }
  895. // 从大到小 排序
  896. real_T temp;
  897. uint16_T temp_idx;
  898. uint16_T idx[5];
  899. for (i = 0; i < N / 2 + 1; i++)
  900. {
  901. idx[i] = i;
  902. }
  903. for (j = 0; j < N / 2 + 1; j++) // 比较n-1轮
  904. {
  905. for (k = 0; k < N / 2 - j; k++) // 每轮比较n-1-i次,
  906. {
  907. if (Y2[k] < Y2[k + 1]) // 从大到小
  908. {
  909. temp = Y2[k];
  910. Y2[k] = Y2[k + 1];
  911. Y2[k + 1] = temp;
  912. temp_idx = idx[k];
  913. idx[k] = idx[k + 1];
  914. idx[k + 1] = temp_idx;
  915. }
  916. }
  917. }
  918. // 输出前5个
  919. for (i = 0; i < 5; i++)
  920. {
  921. returnFreq[i] = F[idx[i]];
  922. returnP[i] = Y2[i];
  923. }
  924. }
  925. //
  926. void RaderReverse(creal_T *X, uint16_T N)
  927. {
  928. uint16_T i;
  929. uint16_T j;
  930. uint16_T k;
  931. creal_T temp;
  932. j = N / 2;
  933. for (i = 1; i < N - 1; i++)
  934. {
  935. if (i < j)
  936. {
  937. temp = X[j];
  938. X[j] = X[i];
  939. X[i] = temp;
  940. }
  941. k = N / 2;
  942. while (k <= j)
  943. {
  944. j = j - k;
  945. k = k / 2;
  946. }
  947. j = j + k;
  948. }
  949. }
  950. void cmul(creal_T a, creal_T b, creal_T *c)
  951. {
  952. c->re = a.re * b.re - a.im * b.im;
  953. c->im = a.re * b.im + a.im * b.re;
  954. }
  955. void cadd(creal_T a, creal_T b, creal_T *c)
  956. {
  957. c->re = a.re + b.re;
  958. c->im = a.im + b.im;
  959. }
  960. void csub(creal_T a, creal_T b, creal_T *c)
  961. {
  962. c->re = a.re - b.re;
  963. c->im = a.im - b.im;
  964. }