press_down_detect - 副本.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. #include "press_down_detect.h"
  2. #define down_thresh 2000.0f
  3. int back_jump_stage = 0;
  4. int cancel_down = 0;
  5. int virus_flag = 0;
  6. float real_front_min_left = 50000.f;
  7. float real_front_min_right = 50000.f;
  8. void detect_up_trend(float *mag_up_min, float *mag_window, int window_size)
  9. {
  10. //1、寻找最大值
  11. float max_val = mag_window[0];
  12. for(int i = 1; i < window_size; i++)
  13. {
  14. if(max_val < mag_window[i])
  15. {
  16. max_val = mag_window[i];
  17. }
  18. }
  19. if(max_val - mag_window[0] > 100.0f)
  20. {
  21. if(mag_window[0] < *mag_up_min)
  22. {
  23. *mag_up_min = mag_window[0];
  24. }
  25. }
  26. else
  27. {
  28. *mag_up_min = 40000.f;
  29. }
  30. }
  31. void avoid_down_apeear_dual_foot_run(float *mag_window,float *another_mag_window, int window_size, float *trend_min, float *trend_max, float *down_max, float *down_min)
  32. {
  33. float max_val = mag_window[0];
  34. for(int i = 1; i < window_size; i++)
  35. {
  36. if(max_val < mag_window[i])
  37. {
  38. max_val = mag_window[i];
  39. }
  40. }
  41. if(max_val - mag_window[0] > 200.0f)
  42. {
  43. if(*trend_min > mag_window[0])
  44. {
  45. *trend_min = mag_window[0];
  46. }
  47. if( *trend_max < max_val)
  48. {
  49. *trend_max = max_val;
  50. }
  51. *down_max = another_mag_window[window_size - 1];
  52. *down_min = another_mag_window[window_size - 1];
  53. }
  54. else if(mag_window[window_size - 1] - max_val < -200.0f)
  55. {
  56. if(mag_window[window_size - 1] < *trend_max)
  57. {
  58. *trend_max = mag_window[window_size - 1];
  59. }
  60. if(max_val > *trend_min)
  61. {
  62. *trend_min = max_val;
  63. }
  64. if(*down_max < another_mag_window[window_size - 1])
  65. {
  66. *down_max = another_mag_window[window_size - 1];
  67. }
  68. if(*down_min > another_mag_window[window_size - 1])
  69. {
  70. *down_min = another_mag_window[window_size - 1];
  71. }
  72. }
  73. }
  74. int station_acc(float* acc_buff, int buff_size)
  75. {
  76. float max_val = acc_buff[0];
  77. float min_val = acc_buff[0];
  78. for(int i = 1; i < buff_size; i++)
  79. {
  80. if(max_val < acc_buff[i] )
  81. {
  82. max_val = acc_buff[i];
  83. }
  84. if(min_val > acc_buff[i] )
  85. {
  86. min_val = acc_buff[i];
  87. }
  88. }
  89. if(max_val - min_val < 0.3f)
  90. {
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. int check_dual_front_press_down(float *left_mag_window, float *right_mag_window, int length)
  96. {
  97. float left_max_val = left_mag_window[0];
  98. float right_max_val = right_mag_window[0];
  99. float left_min_val = left_mag_window[0];
  100. float right_min_val = right_mag_window[0];
  101. int left_max_index = 0; int right_max_index = 0;
  102. int left_min_index = 0; int right_min_index = 0;
  103. for(int i = 1; i < length; i++)
  104. {
  105. if(left_max_val < left_mag_window[i])
  106. {
  107. left_max_val = left_mag_window[i];
  108. left_max_index = i;
  109. }
  110. if(right_max_val < right_mag_window[i])
  111. {
  112. right_max_val = right_mag_window[i];
  113. right_max_index = i;
  114. }
  115. if(left_min_val > left_mag_window[i])
  116. {
  117. left_min_val = left_mag_window[i];
  118. left_min_index = i;
  119. }
  120. if(right_min_val > right_mag_window[i])
  121. {
  122. right_min_val = right_mag_window[i];
  123. right_min_index = i;
  124. }
  125. }
  126. if(right_min_index > right_max_index && left_min_index > left_max_index
  127. && left_max_val > left_min_val + 2000.0f && right_max_val > right_min_val + 2000.0f)
  128. {
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. int back_mag_different_trend(float *left_back_buff, float * right_back_buff, int length)
  134. {
  135. int left_max_index = 0; int right_max_index = 0;
  136. int left_min_index = 0; int right_min_index = 0;
  137. float left_max_val = left_back_buff[0]; float right_max_val = right_back_buff[0];
  138. float left_min_val = left_back_buff[0]; float right_min_val = right_back_buff[0];
  139. for(int i = 0; i < length; i++)
  140. {
  141. if(left_max_val < left_back_buff[i])
  142. {
  143. left_max_val = left_back_buff[i];
  144. left_max_index = i;
  145. }
  146. if(left_min_val > left_back_buff[i])
  147. {
  148. left_min_val = left_back_buff[i];
  149. left_min_index = i;
  150. }
  151. if(right_max_val < right_back_buff[i])
  152. {
  153. right_max_val = right_back_buff[i];
  154. right_max_index = i;
  155. }
  156. if(right_min_val > right_back_buff[i])
  157. {
  158. right_min_val = right_back_buff[i];
  159. right_min_index = i;
  160. }
  161. }
  162. if(left_max_index > left_min_index && left_max_val > left_min_val + 2000.0f
  163. && right_max_index < right_min_index && right_max_val > right_min_val + 2000.0f)
  164. {
  165. return 1;
  166. }
  167. if(left_max_index < left_min_index && left_max_val > left_min_val + 2000.0f
  168. && right_max_index > right_min_index && right_max_val > right_min_val + 2000.0f)
  169. {
  170. return 1;
  171. }
  172. return 0;
  173. }
  174. //快速排序
  175. void quick_sork(float arr[], int start, int end)
  176. {
  177. if(start < end)
  178. {
  179. return;
  180. }
  181. int i = start;
  182. int j = end;
  183. float baseval = arr[start];
  184. while(i < j)
  185. {
  186. while(i < j && arr[j] >= baseval)
  187. {
  188. j--;
  189. }
  190. if(i < j)
  191. {
  192. arr[i] = arr[j];
  193. i++;
  194. }
  195. while(i < j && arr[i] < baseval)
  196. {
  197. i++;
  198. }
  199. if(i < j)
  200. {
  201. arr[j] = arr[i];
  202. j--;
  203. }
  204. }
  205. arr[i] = baseval;
  206. quick_sork(arr, start, i-1);
  207. quick_sork(arr, i+1, end);
  208. }
  209. float mid_val(float *mag_buff)
  210. {
  211. static float src[10];
  212. memcpy(src, mag_buff, 10 * sizeof(float));
  213. //快速排序
  214. quick_sork(mag_buff, 0 , 9);
  215. return 0.5f *(mag_buff[4] + mag_buff[5]);
  216. }
  217. int special_stay_by_back_mag_down(float *left_back_mag, float *right_back_mag, int left_zupt, int right_zupt)
  218. {
  219. static float last_left_back_mag_val = 0.0f;
  220. static float last_right_back_mag_val = 0.0f;
  221. static float max_left_back;
  222. static float min_left_back;
  223. static float max_right_back;
  224. static float min_right_back;
  225. float cur_left_back_mag_val = mid_val(left_back_mag);
  226. float cur_right_back_mag_val = mid_val(right_back_mag);
  227. if(last_left_back_mag_val < cur_left_back_mag_val - 0.00001f)
  228. {
  229. max_left_back = cur_left_back_mag_val;
  230. min_left_back = cur_left_back_mag_val;
  231. max_right_back = cur_right_back_mag_val;
  232. min_right_back = cur_right_back_mag_val;
  233. }
  234. else
  235. {
  236. min_left_back = cur_left_back_mag_val;
  237. }
  238. if(last_right_back_mag_val < cur_right_back_mag_val - 0.00001f)
  239. {
  240. max_left_back = cur_left_back_mag_val;
  241. min_left_back = cur_left_back_mag_val;
  242. max_right_back = cur_right_back_mag_val;
  243. min_right_back = cur_right_back_mag_val;
  244. }
  245. else
  246. {
  247. min_right_back = cur_right_back_mag_val;
  248. }
  249. last_left_back_mag_val = cur_left_back_mag_val;
  250. last_right_back_mag_val = cur_right_back_mag_val;
  251. if(max_left_back > min_left_back +2000.f && max_right_back > min_right_back +2000.f
  252. && left_zupt && right_zupt)
  253. {
  254. return 1;
  255. }
  256. else
  257. {
  258. return 0;
  259. }
  260. }
  261. int special_stay_by_long_time_down_process(float *front_mag_left_buff, float *front_mag_right_buff, float *back_mag_left_buff, float *back_mag_right_buff)
  262. {
  263. int left_index = 9;
  264. int right_index = 9;
  265. /*
  266. * 寻找第一个递增的点
  267. */
  268. for(left_index = 9; left_index > 0 ; left_index --)
  269. {
  270. if(front_mag_left_buff[left_index] < front_mag_left_buff[left_index - 1])
  271. {
  272. break;
  273. }
  274. }
  275. for(right_index = 9; right_index > 0 ; right_index --)
  276. {
  277. if(front_mag_right_buff[right_index] < front_mag_right_buff[right_index - 1])
  278. {
  279. break;
  280. }
  281. }
  282. float right_max_front_val = front_mag_right_buff[0];
  283. float right_min_front_val = front_mag_right_buff[0];
  284. for(int i = 0; i < 10; i++)
  285. {
  286. right_max_front_val = right_max_front_val > front_mag_right_buff[i] ? right_max_front_val : front_mag_right_buff[i];
  287. right_min_front_val = right_min_front_val < front_mag_right_buff[i] ? right_min_front_val : front_mag_right_buff[i];
  288. }
  289. /*
  290. * 有些特殊的点,左脚前掌往下压,但是右脚前掌没啥变化, 只靠后脚掌数据递降来判断
  291. */
  292. if(left_index < 6 && front_mag_left_buff[9] - front_mag_left_buff[left_index] > 2000.0f
  293. && right_max_front_val - right_min_front_val < 500.f && right_min_front_val > 15000.f
  294. && back_mag_right_buff[left_index] - back_mag_right_buff[9] > 2000.f)
  295. {
  296. return 1;
  297. }
  298. /*
  299. * 左脚前掌往下压,右脚前掌往下压
  300. */
  301. if(left_index < 6 && front_mag_left_buff[9] - front_mag_left_buff[left_index] > 2000.0f
  302. && right_index < 6 && front_mag_right_buff[9] - front_mag_right_buff[right_index] > 2000.0f)
  303. {
  304. return 1;
  305. }
  306. return 0;
  307. }
  308. int special_stay_by_long_time_down(float* front_mag_left, float* front_mag_right, int length)
  309. {
  310. float front_mag_left_max = front_mag_left[length - 1];
  311. float front_mag_right_max = front_mag_right[length - 1];
  312. int up_count = 0;
  313. for(int i = length - 1; i > 1; i--)
  314. {
  315. if(front_mag_left[i] - front_mag_left[i-1] > 10.f && front_mag_right[i] - front_mag_right[i-1] > 10.f)
  316. {
  317. up_count ++;
  318. if (front_mag_left[i] > front_mag_left_max)
  319. {
  320. front_mag_left_max = front_mag_left[i];
  321. }
  322. if (front_mag_right[i] > front_mag_right_max)
  323. {
  324. front_mag_right_max = front_mag_right[i];
  325. }
  326. }
  327. if(up_count > 7 && front_mag_left_max - front_mag_left[i] > 2000.f && front_mag_right_max - front_mag_right[i] >2000.f
  328. && front_mag_left_max - front_mag_left[length - 1] < 1000.f && front_mag_right_max - front_mag_right[length - 1] < 1000.f)
  329. {
  330. return 1;
  331. }
  332. }
  333. return 0;
  334. }
  335. int stage_class_up_or_down(float *mag_buff, int length)
  336. {
  337. if(((mag_buff[length - 10] + mag_buff[length - 9] + mag_buff[length - 8])*0.334f + 50.f < (mag_buff[length - 6] + mag_buff[length - 5] + mag_buff[length - 4])*0.334f)
  338. || ((mag_buff[length - 10] + mag_buff[length - 9] + mag_buff[length - 8])*0.334f + 50.f < (mag_buff[length - 3] + mag_buff[length - 2] + mag_buff[length - 1])*0.334f))
  339. {
  340. return 1;
  341. }
  342. if(((mag_buff[length - 10] + mag_buff[length - 9] + mag_buff[length - 8])*0.334f + 100.f > (mag_buff[length - 6] + mag_buff[length - 5] + mag_buff[length - 4])*0.334f)
  343. || ((mag_buff[length - 10] + mag_buff[length - 9] + mag_buff[length - 8])*0.334f + 100.f > (mag_buff[length - 3] + mag_buff[length - 2] + mag_buff[length - 1])*0.334f))
  344. {
  345. return -1;
  346. }
  347. return 0;
  348. }
  349. int back_down_by_long_time_big_trend(float* left_mag, float* right_mag, int length)
  350. {
  351. float left_down_sum = 0.0f;
  352. float left_up_sum = 0.0f;
  353. float right_down_sum = 0.0f;
  354. float right_up_sum = 0.0f;
  355. //先寻找到最小值下标
  356. int left_mag_min_index = length - 1;
  357. int right_mag_min_index = length - 1;
  358. float left_mag_min_val = left_mag[length - 1];
  359. float right_mag_min_val = right_mag[length - 1];
  360. int left_mag_max_index = length - 1;
  361. int right_mag_max_index = length - 1;
  362. float left_mag_max_val = left_mag[length - 1];
  363. float right_mag_max_val = right_mag[length - 1];
  364. for(int i = length - 1; i > 0; i--)
  365. {
  366. if(left_mag_min_val > left_mag[i])
  367. {
  368. left_mag_min_val = left_mag[i];
  369. left_mag_min_index = i;
  370. }
  371. if(right_mag_min_val > right_mag[i])
  372. {
  373. right_mag_min_val = right_mag[i];
  374. right_mag_min_index = i;
  375. }
  376. if(left_mag_max_val < left_mag[i])
  377. {
  378. left_mag_max_val = left_mag[i];
  379. left_mag_max_index = i;
  380. }
  381. if(right_mag_max_val < right_mag[i])
  382. {
  383. right_mag_max_val = right_mag[i];
  384. right_mag_max_index = i;
  385. }
  386. }
  387. int index = left_mag_min_index > right_mag_min_index ? left_mag_min_index : right_mag_min_index;
  388. //寻找差异值
  389. float left_diff;
  390. float right_diff;
  391. for(int i = index; i < length - 1; i++)
  392. {
  393. left_diff = left_mag[i] - left_mag[i + 1];
  394. if(left_diff > 0.0f)
  395. {
  396. left_down_sum += left_diff;
  397. }
  398. else
  399. {
  400. left_up_sum -= left_diff;
  401. }
  402. right_diff = right_mag[i] - right_mag[i + 1];
  403. if(right_diff > 0.0f)
  404. {
  405. right_down_sum += right_diff;
  406. }
  407. else
  408. {
  409. right_up_sum -= right_diff;
  410. }
  411. }
  412. if(left_mag_min_val + 2000.f < left_mag[length - 1] && right_mag_min_val + 2000.f < right_mag[length - 1]
  413. && right_down_sum < 0.25f *right_up_sum && left_down_sum < 0.25f * left_up_sum)
  414. //if(left_mag_min_val + 2000.f < left_mag[length - 1] && right_mag_min_val + 2000.f < right_mag[length - 1] )
  415. {
  416. return 1;
  417. }
  418. return 0;
  419. }
  420. int back_down_by_long_time(float* front_mag_left, float* front_mag_right, float* back_mag_left, float* back_mag_right, int length)
  421. {
  422. static float max_front_left_val;
  423. static float max_front_right_val;
  424. static float min_back_left_val;
  425. static float min_back_right_val;
  426. static int up_count;
  427. up_count ++;
  428. static int continue_up_count = 0;
  429. if(up_count == 0)
  430. {
  431. max_front_left_val = front_mag_left[length - 1];
  432. max_front_right_val = front_mag_right[length - 1];
  433. min_back_left_val = back_mag_left[length - 1];
  434. min_back_right_val = back_mag_right[length - 1];
  435. }
  436. // if(stage_class_up_or_down(front_mag_left, 15) == -1 && stage_class_up_or_down(front_mag_right, 15) == -1)
  437. //// if( stage_class_up_or_down(back_mag_left, 15) == 1 && stage_class_up_or_down(back_mag_right, 15) == 1 )
  438. // {
  439. // float tmp = (front_mag_left[0] + front_mag_left[1] + front_mag_left[2]) *0.334f;
  440. //
  441. // max_front_left_val = tmp > max_front_left_val ? tmp : max_front_left_val;
  442. //
  443. // tmp = (front_mag_right[0] + front_mag_right[1] + front_mag_right[2]) *0.334f;
  444. //
  445. // max_front_right_val = tmp > max_front_right_val ? tmp : max_front_left_val;
  446. //
  447. // }
  448. // else
  449. // {
  450. // max_front_left_val = front_mag_left[length - 1];
  451. // max_front_right_val = front_mag_right[length - 1];
  452. //
  453. // }
  454. if(stage_class_up_or_down(back_mag_left, length) == 1 && stage_class_up_or_down(back_mag_right, length) == 1)
  455. {
  456. float tmp = (back_mag_left[0] + back_mag_left[1] + back_mag_left[2]) *0.334f;
  457. min_back_left_val = tmp > min_back_left_val ? min_back_left_val : tmp;
  458. tmp = (back_mag_right[0] + back_mag_right[1] + back_mag_right[2]) *0.334f;
  459. min_back_right_val = tmp > min_back_right_val ? min_back_right_val : tmp;
  460. continue_up_count ++;
  461. }
  462. else
  463. {
  464. min_back_left_val = back_mag_left[length - 1];
  465. min_back_right_val = back_mag_right[length - 1];
  466. continue_up_count = 0;
  467. }
  468. if(back_mag_left[length - 1] - min_back_left_val > 2000.f && back_mag_right[length - 1] - min_back_right_val > 2000.f && continue_up_count > 10)
  469. {
  470. return 1;
  471. }
  472. if(back_mag_left[length - 1] - min_back_left_val > 4000.f && back_mag_right[length - 1] - min_back_right_val > 4000.f)
  473. {
  474. return 1;
  475. }
  476. return 0;
  477. }
  478. #define BIG_WINDOW_SIZE 20
  479. int press_down_detect_new(int index, float front_mag_left, float back_mag_left,
  480. float front_mag_right, float back_mag_right,int left_zupt, int right_zupt,
  481. float left_acc_x,float left_acc_y, float left_acc_z, float right_acc_x, float right_acc_y, float right_acc_z,
  482. int *front_down, int *back_down)
  483. {
  484. static float front_mag_norm_left[PRESS_COUNT_MAX];
  485. static float front_mag_norm_right[PRESS_COUNT_MAX];
  486. static float back_mag_norm_left[PRESS_COUNT_MAX];
  487. static float back_mag_norm_right[PRESS_COUNT_MAX];
  488. static float front_mag_left_big_buff[BIG_WINDOW_SIZE];
  489. static float front_mag_right_big_buff[BIG_WINDOW_SIZE];
  490. static float back_mag_left_big_buff[BIG_WINDOW_SIZE];
  491. static float back_mag_right_big_buff[BIG_WINDOW_SIZE];
  492. static float acc_norm_left[10];
  493. static float acc_norm_right[10];
  494. static float left_front_up_min = 40000.0f;
  495. static float left_back_up_min = 40000.0f;
  496. static float right_front_up_min = 40000.0f;
  497. static float right_back_up_min = 40000.0f;
  498. memcpy(front_mag_norm_left, front_mag_norm_left + 1, 4 * sizeof(float));
  499. memcpy(front_mag_norm_right, front_mag_norm_right + 1, 4 * sizeof(float));
  500. memcpy(front_mag_left_big_buff, front_mag_left_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(float));
  501. memcpy(front_mag_right_big_buff, front_mag_right_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(float));
  502. memcpy(back_mag_norm_left, back_mag_norm_left + 1, 4 * sizeof(float));
  503. memcpy(back_mag_norm_right, back_mag_norm_right + 1, 4 * sizeof(float));
  504. memcpy(acc_norm_left, acc_norm_left + 1, 9 * sizeof(float));
  505. memcpy(acc_norm_right, acc_norm_right + 1, 9 * sizeof(float));
  506. memcpy(back_mag_left_big_buff, back_mag_left_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(float));
  507. memcpy(back_mag_right_big_buff, back_mag_right_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(float));
  508. front_mag_norm_left[PRESS_COUNT_MAX - 1] = front_mag_left;
  509. front_mag_norm_right[PRESS_COUNT_MAX - 1] = front_mag_right;
  510. front_mag_left_big_buff[(BIG_WINDOW_SIZE - 1)] = front_mag_left;
  511. front_mag_right_big_buff[(BIG_WINDOW_SIZE - 1)] = front_mag_right;
  512. back_mag_norm_left[PRESS_COUNT_MAX - 1] = back_mag_left;
  513. back_mag_norm_right[PRESS_COUNT_MAX - 1] = back_mag_right;
  514. back_mag_left_big_buff[(BIG_WINDOW_SIZE - 1)] = back_mag_left;
  515. back_mag_right_big_buff[(BIG_WINDOW_SIZE - 1)] = back_mag_right;
  516. acc_norm_left[9] = sqrt(left_acc_x * left_acc_x + left_acc_y * left_acc_y + left_acc_z * left_acc_z);
  517. acc_norm_right[9] = sqrt(right_acc_x * right_acc_x + right_acc_y * right_acc_y + right_acc_z * right_acc_z);
  518. detect_up_trend(&left_front_up_min, front_mag_norm_left, PRESS_COUNT_MAX);
  519. detect_up_trend(&right_front_up_min, front_mag_norm_right, PRESS_COUNT_MAX);
  520. detect_up_trend(&left_back_up_min, back_mag_norm_left, PRESS_COUNT_MAX);
  521. detect_up_trend(&right_back_up_min, back_mag_norm_right, PRESS_COUNT_MAX);
  522. if(left_zupt == 0)
  523. {
  524. left_front_up_min = front_mag_left;
  525. left_back_up_min = back_mag_left;
  526. }
  527. if(right_zupt == 0)
  528. {
  529. right_front_up_min = front_mag_right;
  530. right_back_up_min = back_mag_right;
  531. }
  532. //当检测到前脚掌的压力下降同时较快,将不会触发后脚掌下蹲的判断
  533. if(check_dual_front_press_down(front_mag_left_big_buff, front_mag_right_big_buff, BIG_WINDOW_SIZE))
  534. {
  535. left_back_up_min = back_mag_left;
  536. right_back_up_min = back_mag_right;
  537. }
  538. if(front_mag_left - left_front_up_min > 3000.f && front_mag_right - right_front_up_min > 3000.f)
  539. {
  540. *front_down = 1;
  541. }
  542. if(back_mag_different_trend(back_mag_left_big_buff, back_mag_right_big_buff, BIG_WINDOW_SIZE))
  543. {
  544. *front_down = 0;
  545. }
  546. //新增一个长时间判断蹲的,2020/01/08
  547. if (special_stay_by_long_time_down(front_mag_left_big_buff, front_mag_right_big_buff, BIG_WINDOW_SIZE))
  548. {
  549. *front_down = 1;
  550. }
  551. if(back_mag_left - left_back_up_min > 2000.0f && back_mag_right - right_back_up_min > 2000.0f )
  552. {
  553. *back_down = 1;
  554. }
  555. if(!station_acc(acc_norm_left, 10) || !station_acc(acc_norm_right, 10))
  556. {
  557. *front_down = 0;
  558. *back_down = 0;
  559. }
  560. // if(back_down_by_long_time(front_mag_left_big_buff, front_mag_right_big_buff, back_mag_left_big_buff, back_mag_right_big_buff, BIG_WINDOW_SIZE))
  561. // {
  562. // *back_down = 1;
  563. // }
  564. //2022/01/17
  565. /* if(back_down_by_long_time_big_trend(back_mag_left_big_buff, back_mag_right_big_buff, BIG_WINDOW_SIZE))
  566. {
  567. *back_down = 1;
  568. }*/
  569. if (special_stay_by_long_time_down(back_mag_left_big_buff, back_mag_right_big_buff, BIG_WINDOW_SIZE))
  570. {
  571. *back_down = 1;
  572. }
  573. return 0;
  574. }
  575. short pos_jump_detect(int *h_pos, int *s_pos, int left_zupt, int right_zupt)
  576. {
  577. static int last_h_z;
  578. static int last_s_z;
  579. static int left_up;
  580. static int right_up;
  581. static int left_up_count;
  582. static int right_up_count;
  583. static int left_zupt_count;
  584. static int right_zupt_count;
  585. if(left_zupt)
  586. {
  587. left_zupt_count = 20;
  588. }
  589. else
  590. {
  591. left_zupt_count --;
  592. }
  593. if(right_zupt)
  594. {
  595. right_zupt_count = 20;
  596. }
  597. else
  598. {
  599. right_zupt_count --;
  600. }
  601. if(h_pos[2] - last_h_z > 0 && h_pos[2]> 0)
  602. {
  603. left_up = 1;
  604. }
  605. else if((h_pos[2] - last_h_z < 0) || h_pos[2] <= 0)
  606. {
  607. left_up = -1;
  608. }
  609. if(left_up == 1)
  610. {
  611. left_up_count ++;
  612. }
  613. else
  614. {
  615. left_up_count = 0;
  616. }
  617. if(s_pos[2] - last_s_z > 0 && s_pos[2] > 0)
  618. {
  619. right_up = 1;
  620. }
  621. else if((s_pos[2] - last_s_z < 0) || s_pos[2] <= 0)
  622. {
  623. right_up = -1;
  624. }
  625. if(right_up == 1)
  626. {
  627. right_up_count ++;
  628. }
  629. else
  630. {
  631. right_up_count = 0;
  632. }
  633. last_h_z = h_pos[2];
  634. last_s_z = s_pos[2];
  635. if(left_up == 1 && right_up == 1 && right_up_count < 15 && left_up_count < 15
  636. && right_zupt_count > 0 && left_zupt_count > 0 &&
  637. ((right_up_count > 2 && left_up_count > 2) ||
  638. (right_up_count > 0 && left_up_count > 0 && h_pos[2] >1 && s_pos[2] > 1)))
  639. {
  640. return 1;
  641. }
  642. return 0;
  643. }
  644. //由于仅靠IMU来探测触底了,高度估计得不太对,所以用加速度来算起跳动作
  645. void max_min_window(float *data, int length, float *max_val , int *max_index, float * min_val, int *min_index)
  646. {
  647. *max_val = data[0];
  648. *max_index = 0;
  649. *min_val = data[0];
  650. *min_index = 0;
  651. for(int i = 0; i < length; i++)
  652. {
  653. if(*max_val < data[i])
  654. {
  655. *max_val = data[i];
  656. *max_index = i;
  657. }
  658. if(*min_val > data[i])
  659. {
  660. *min_val = data[i];
  661. *min_index = i;
  662. }
  663. }
  664. }
  665. short press_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt, int left_front_press, int right_front_press)
  666. {
  667. static float right_data_z_buff[10];
  668. static float left_data_z_buff[10];
  669. static int last_right_press;
  670. static int last_left_press;
  671. static int left_max_press;
  672. static int right_max_press;
  673. static int wait;
  674. /*
  675. * 存储数据
  676. */
  677. memcpy(right_data_z_buff, right_data_z_buff + 1, 9 * sizeof(float));
  678. memcpy(left_data_z_buff, left_data_z_buff + 1, 9 * sizeof(float));
  679. right_data_z_buff[9] = right_acc_z;
  680. left_data_z_buff[9] = left_acc_z;
  681. if (left_zupt && right_zupt)
  682. {
  683. wait = 20;
  684. }
  685. /*
  686. * 检测压力下降用的逻辑
  687. */
  688. if (last_left_press - left_front_press > 200)
  689. {
  690. if (last_left_press > left_max_press)
  691. {
  692. left_max_press = last_left_press;
  693. }
  694. }
  695. else if (last_left_press - left_front_press < 50)
  696. {
  697. left_max_press = 0;
  698. }
  699. last_left_press = left_front_press;
  700. if (last_right_press - right_front_press > 200)
  701. {
  702. if (last_right_press > right_max_press)
  703. {
  704. right_max_press = last_right_press;
  705. }
  706. }
  707. else if (last_right_press - right_front_press < 50)
  708. {
  709. right_max_press = 0;
  710. }
  711. last_right_press = right_front_press;
  712. /*
  713. * 加速变化估计
  714. */
  715. float max_val_right, min_val_right, max_val_left, min_val_left;
  716. int max_index_right, min_index_right, max_index_left, min_index_left;
  717. max_min_window(right_data_z_buff, 10, &max_val_right, &max_index_right, &min_val_right, &min_index_right);
  718. max_min_window(left_data_z_buff, 10, &max_val_left, &max_index_left, &min_val_left, &min_index_left);
  719. if (wait > 0)
  720. {
  721. wait--;
  722. }
  723. /*
  724. * 1.0f的阈值轻轻跳就可以达到了,现在改为1.3f的阈值
  725. */
  726. if (max_index_right < min_index_right && max_index_left < min_index_left
  727. && max_val_right - min_val_right > 1.0f && min_val_right < 0.f && max_val_left-min_val_left > 1.0f && min_val_left < 0.f
  728. && (wait > 0 && left_zupt == 0 && right_zupt == 0))
  729. //if(left_max_press - left_front_press > 3000 && right_max_press - right_front_press > 3000)
  730. {
  731. return 1;
  732. }
  733. return 0;
  734. }
  735. int att_jump_detect(float left_pitch, float right_pitch, int left_zupt, int right_zupt)
  736. {
  737. static int num;
  738. static float left_pitch_window[8];
  739. static float right_pitch_window[8];
  740. //用俯仰角判断好了, 现在的IMU方向 下标1为俯仰角
  741. memcpy(left_pitch_window, left_pitch_window + 1, 7 * sizeof(float));
  742. memcpy(right_pitch_window, right_pitch_window + 1, 7 * sizeof(float));
  743. left_pitch_window[7] = left_pitch;
  744. right_pitch_window[7] = right_pitch;
  745. num ++;
  746. if(num < 8)
  747. {
  748. return 0;
  749. }
  750. else
  751. {
  752. num = 8;
  753. }
  754. //要求起跳要同步
  755. static int zupt_count = 0;
  756. if(left_zupt && right_zupt)
  757. {
  758. zupt_count = 21;
  759. }
  760. if(zupt_count > 0)
  761. {
  762. zupt_count --;
  763. }
  764. for(int i = 7; i > 1; i--)
  765. {
  766. if((left_pitch_window[i] > left_pitch_window[i-1] || left_pitch_window[i] > left_pitch_window[i-2])
  767. && (right_pitch_window[i] > right_pitch_window[i-1] || right_pitch_window[i] > right_pitch_window[i-2]))
  768. {
  769. if((left_pitch > left_pitch_window[i - 1] + 0.1f || left_pitch > left_pitch_window[i - 2] + 0.1f )
  770. && (right_pitch > right_pitch_window[i - 1] + 0.1f || right_pitch > right_pitch_window[i - 2] + 0.1f )
  771. && zupt_count > 0 )
  772. {
  773. return 1;
  774. }
  775. }
  776. else
  777. {
  778. return 0;
  779. }
  780. }
  781. return 0;
  782. }
  783. //int little_jump_detect_process(int press, int *press_max, int*press_min, int *last_press)
  784. //{
  785. //
  786. // if(*last_press - press > 200)
  787. // {
  788. // if(*press_max < press)
  789. // {
  790. // *press_max = press;
  791. // }
  792. //
  793. // if(*press_min > press)
  794. // {
  795. // *press_min = press;
  796. // }
  797. // }
  798. // else if(*last_press - press < -200)
  799. // {
  800. // if(*press_max > press)
  801. // {
  802. // *press_max = press;
  803. // }
  804. //
  805. // if(*press_min < press)
  806. // {
  807. // *press_min = press;
  808. // }
  809. // }
  810. //
  811. // *last_press = press;
  812. //
  813. // if(*press_max - press > 3000)
  814. // {
  815. // return 1;
  816. // }
  817. //
  818. // if(*press_max - press < -1000)
  819. // {
  820. // return -1;
  821. // }
  822. //
  823. // return 0;
  824. //}
  825. //int little_jump_detect( int left_front_press, int right_front_press, int left_back_press, int right_back_press, int left_zupt, int right_zupt)
  826. //{
  827. // static int left_front_max = 0;
  828. // static int right_front_max = 0;
  829. // static int left_back_max = 0;
  830. // static int right_back_max = 0;
  831. //
  832. // static int left_front_min = 40000;
  833. // static int right_front_min = 40000;
  834. // static int left_back_min = 40000;
  835. // static int right_back_min = 40000;
  836. //
  837. // static int last_left_front_press = 0;
  838. // static int last_right_front_press = 0;
  839. //
  840. // static int last_left_back_press = 0;
  841. // static int last_right_back_press = 0;
  842. // int left_front_down = little_jump_detect_process(left_front_press, &left_front_max, &left_front_min, &last_left_front_press);
  843. //
  844. // int right_front_down = little_jump_detect_process(right_front_press, &right_front_max, &right_front_min, &last_right_front_press);
  845. //
  846. // int left_back_down = little_jump_detect_process(left_back_press, &left_back_max, &left_back_min, &last_left_back_press);
  847. //
  848. // int right_back_down = little_jump_detect_process(right_back_press, &right_back_max, &right_back_min, &last_right_back_press);
  849. //
  850. //// if( left_back_down == -1)
  851. //// {
  852. //// left_front_max = left_front_press;
  853. ////
  854. //// left_front_min = left_front_press;
  855. //// }
  856. ////
  857. //// if( right_back_down == -1)
  858. //// {
  859. //// right_front_max = right_front_press;
  860. ////
  861. //// right_front_min = right_front_press;
  862. //// }
  863. //
  864. // if(left_front_down != 1)
  865. // {
  866. // right_front_max = right_front_press;
  867. //
  868. // right_front_min = right_front_press;
  869. // }
  870. //
  871. // if(right_front_down != 1)
  872. // {
  873. // left_front_max = left_front_press;
  874. //
  875. // left_front_min = left_front_press;
  876. // }
  877. //
  878. // if(left_front_down==1 && right_front_down==1 && left_zupt==0 && right_zupt==0)
  879. // {
  880. // return 1;
  881. // }
  882. //
  883. // if(left_back_down==1 && right_back_down==1 && left_zupt==0 && right_zupt==0)
  884. // {
  885. // return 1;
  886. // }
  887. //
  888. // return 0;
  889. //
  890. //}
  891. //int detect_L_line(float *press_buff, int length)
  892. //{
  893. //
  894. // float max_diff_val = press_buff[0] - press_buff[1];
  895. //
  896. // float temp_diff_val = 0.0f;
  897. //
  898. // int max_index = 0;
  899. //
  900. // for(int i = 1; i < length; i++)
  901. // {
  902. // if(press_buff[i-1] - press_buff[i] > -50.f)
  903. // {
  904. // temp_diff_val += (press_buff[i-1] - press_buff[i]);
  905. // }
  906. // else
  907. // {
  908. //
  909. // temp_diff_val = 0.0f;
  910. // }
  911. //
  912. // if(max_diff_val < temp_diff_val)
  913. // {
  914. // max_diff_val = temp_diff_val;
  915. //
  916. // if(max_diff_val > 4000.f)
  917. // {
  918. // max_index = i;
  919. // }
  920. // }
  921. //
  922. // }
  923. //
  924. // float stable_max_val = press_buff[length - 4];
  925. // float stable_min_val = press_buff[length - 4];
  926. //
  927. // for(int i= length - 4; i< length; i++)
  928. // {
  929. // if(stable_max_val < press_buff[i])
  930. // {
  931. // stable_max_val = press_buff[i];
  932. // }
  933. //
  934. // if(stable_min_val > press_buff[i])
  935. // {
  936. // stable_min_val = press_buff[i];
  937. // }
  938. // }
  939. //
  940. // if( stable_max_val - stable_min_val < 1000.f && max_diff_val > 3000.f
  941. // && max_index > length - 5)
  942. // {
  943. // return 1;
  944. // }
  945. //
  946. // return 0;
  947. //}
  948. //int acc_z_down_trend(float *left_acc_buff, float *right_acc_buff, int length)
  949. //{
  950. // int left_max_index = 0;
  951. // int left_min_index = 0;
  952. //
  953. // int right_max_index = 0;
  954. // int right_min_index = 0;
  955. //
  956. //
  957. // float left_max_val = left_acc_buff[0];
  958. // float left_min_val = left_acc_buff[0];
  959. //
  960. // float right_max_val = right_acc_buff[0];
  961. // float right_min_val = right_acc_buff[0];
  962. //
  963. //
  964. // for(int i = 0; i < length; i++)
  965. // {
  966. // if(left_max_val < left_acc_buff[i])
  967. // {
  968. // left_max_val = left_acc_buff[i];
  969. // left_max_index = i;
  970. // }
  971. //
  972. // if(left_min_val > left_acc_buff[i])
  973. // {
  974. // left_min_val = left_acc_buff[i];
  975. // left_min_index = i;
  976. // }
  977. //
  978. // if(left_max_val < left_acc_buff[i])
  979. // {
  980. // left_max_val = left_acc_buff[i];
  981. // left_max_index = i;
  982. // }
  983. //
  984. // if(right_min_val > right_acc_buff[i])
  985. // {
  986. // right_min_val = right_acc_buff[i];
  987. // right_min_index = i;
  988. // }
  989. //
  990. // if(right_max_val < right_acc_buff[i])
  991. // {
  992. // right_max_val = right_acc_buff[i];
  993. // right_max_index = i;
  994. // }
  995. //
  996. // }
  997. //
  998. // if(right_max_index < right_min_index && right_max_val > right_min_val + 1.5f
  999. // && left_max_index < left_min_index && left_max_val > left_min_val + 1.5f
  1000. // && abs(right_max_index - left_max_index < 4) && abs(left_max_index - left_min_index < 4))
  1001. // {
  1002. // return 1;
  1003. // }
  1004. //
  1005. // return 0;
  1006. //
  1007. //}
  1008. //int acc_little_shake(float *left_acc, float *right_acc, int length)
  1009. //{
  1010. // float left_max_val = left_acc[0];
  1011. // float right_max_val = right_acc[0];
  1012. //
  1013. // float left_min_val = left_acc[0];
  1014. // float right_min_val = right_acc[0];
  1015. //
  1016. // for(int i = 0; i < length; i++)
  1017. // {
  1018. // if(left_max_val < left_acc[i])
  1019. // {
  1020. // left_max_val = left_acc[i];
  1021. // }
  1022. //
  1023. // if(left_min_val > left_acc[i])
  1024. // {
  1025. // left_min_val = left_acc[i];
  1026. // }
  1027. //
  1028. // if(right_max_val < left_acc[i])
  1029. // {
  1030. // right_max_val = left_acc[i];
  1031. // }
  1032. //
  1033. // if(right_min_val > left_acc[i])
  1034. // {
  1035. // right_min_val = left_acc[i];
  1036. // }
  1037. //
  1038. // }
  1039. //
  1040. // if(left_max_val - left_min_val > 0.4f && right_max_val - right_min_val > 0.4f)
  1041. // {
  1042. // return 1;
  1043. // }
  1044. //
  1045. // return 0;
  1046. //
  1047. //
  1048. //}
  1049. //int normal_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt,
  1050. // float left_front_press, float right_front_press, float left_back_press, float right_back_press)
  1051. //{
  1052. //// static float right_data_press[15];
  1053. //// static float left_data_press[15];
  1054. //
  1055. // static float right_front_data_press[20];
  1056. // static float left_front_data_press[20];
  1057. //
  1058. // static float left_acc_z_buff[15];
  1059. // static float right_acc_z_buff[15];
  1060. //
  1061. //
  1062. // static int left_wait;
  1063. // static int right_wait;
  1064. // static int wait;
  1065. //
  1066. // static int last_left_zupt;
  1067. // static int last_right_zupt;
  1068. //
  1069. // static int press_jump_count = 0;
  1070. //
  1071. // /*
  1072. // * 存储数据
  1073. // */
  1074. //
  1075. //// memcpy(right_data_press, right_data_press + 1, 14 * sizeof(float));
  1076. ////
  1077. //// memcpy(left_data_press, left_data_press + 1, 14 * sizeof(float));
  1078. //
  1079. // memcpy(right_front_data_press, right_front_data_press + 1, 19 * sizeof(float));
  1080. //
  1081. // memcpy(left_front_data_press, left_front_data_press + 1, 19 * sizeof(float));
  1082. //
  1083. // memcpy(left_acc_z_buff, left_acc_z_buff + 1, 14 * sizeof(float));
  1084. //
  1085. // memcpy(right_acc_z_buff, right_acc_z_buff + 1, 14 * sizeof(float));
  1086. //
  1087. //
  1088. //// right_data_press[14] = right_back_press; left_data_press[14] = left_back_press;
  1089. //
  1090. // right_front_data_press[19] = right_front_press; left_front_data_press[19] = left_front_press;
  1091. //
  1092. // left_acc_z_buff[14] = left_acc_z; right_acc_z_buff[14] = right_acc_z;
  1093. //
  1094. //// if(left_zupt == 0 && last_left_zupt == 1)
  1095. //// {
  1096. //// left_wait = 10;
  1097. //// }
  1098. ////
  1099. //// if(right_zupt == 0 && last_right_zupt == 1)
  1100. //// {
  1101. //// right_wait = 10;
  1102. //// }
  1103. ////
  1104. //
  1105. //// if(left_wait > 0)
  1106. //// {
  1107. //// left_wait --;
  1108. //// }
  1109. ////
  1110. //// if(right_wait > 0)
  1111. //// {
  1112. //// right_wait --;
  1113. //// }
  1114. // if(left_zupt == 1 && right_zupt == 1)
  1115. // {
  1116. // wait = 15;
  1117. // }
  1118. //
  1119. // if(wait > 0)
  1120. // {
  1121. // wait --;
  1122. // }
  1123. //
  1124. // last_left_zupt = left_zupt;
  1125. // last_right_zupt = right_zupt;
  1126. //
  1127. //// if(detect_L_line(right_data_press, 15) && detect_L_line(left_data_press, 15) && wait > 0 && wait < 12 && left_zupt == 0 && right_zupt == 0)
  1128. //// {
  1129. //// return 1;
  1130. //// }
  1131. //
  1132. // if(detect_L_line(right_front_data_press, 20) && detect_L_line(left_front_data_press, 20) && wait > 0 && wait < 14 && left_zupt == 0 && right_zupt == 0)
  1133. // {
  1134. // press_jump_count ++;
  1135. // if(press_jump_count > 2)
  1136. // {
  1137. // return 1;
  1138. // }
  1139. // }
  1140. // else
  1141. // {
  1142. // press_jump_count =0;
  1143. // }
  1144. ////
  1145. //// if(little_up_jump_by_four_mag(right_data_press+7, left_data_press+7, right_front_data_press+7, left_front_data_press+7, 8) && wait > 0 && wait < 12)
  1146. //// {
  1147. //// return 1;
  1148. //// }
  1149. //
  1150. // if(acc_z_down_trend(left_acc_z_buff, right_acc_z_buff, 15)&& wait > 0 && wait < 14 && left_zupt == 0 && right_zupt == 0 )
  1151. // {
  1152. // return 1;
  1153. // }
  1154. //
  1155. //
  1156. // return 0;
  1157. //
  1158. //
  1159. //}
  1160. //short press_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt,
  1161. // float left_front_press, float right_front_press, float left_back_press, float right_back_press)
  1162. //{
  1163. //
  1164. //// return little_jump_detect( left_front_press, right_front_press, left_back_press, right_back_press, left_zupt, right_zupt) ||
  1165. //// normal_jump_detect( left_acc_z, right_acc_z, left_zupt, right_zupt, left_front_press, right_front_press, left_back_press, right_back_press);
  1166. //
  1167. // if(normal_jump_detect( left_acc_z, right_acc_z, left_zupt, right_zupt, left_front_press, right_front_press, left_back_press, right_back_press))
  1168. // {
  1169. // return 1;
  1170. // }
  1171. //
  1172. // return 0;
  1173. //
  1174. //}