press_down_detect.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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 + 500.0f
  163. && right_max_index < right_min_index && right_max_val > right_min_val + 1000.0f)
  164. {
  165. return 1;
  166. }
  167. if(left_max_index < left_min_index && left_max_val > left_min_val + 500.0f
  168. && right_max_index > right_min_index && right_max_val > right_min_val + 1000.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. }
  319. if(up_count > 5)
  320. {
  321. if(front_mag_left_max - front_mag_left[i] > 3000.f && front_mag_right_max - front_mag_right[i] >3000.f)
  322. {
  323. return 1;
  324. }
  325. }
  326. }
  327. return 0;
  328. }
  329. int stage_class_up_or_down(float *mag_buff, int length)
  330. {
  331. 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)
  332. || ((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))
  333. {
  334. return 1;
  335. }
  336. 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)
  337. || ((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))
  338. {
  339. return -1;
  340. }
  341. return 0;
  342. }
  343. int back_down_by_long_time_big_trend(float* left_mag, float* right_mag, int length)
  344. {
  345. float left_down_sum = 0.0f;
  346. float left_up_sum = 0.0f;
  347. float right_down_sum = 0.0f;
  348. float right_up_sum = 0.0f;
  349. //先寻找到最小值下标
  350. int left_mag_min_index = length - 1;
  351. int right_mag_min_index = length - 1;
  352. float left_mag_min_val = left_mag[length - 1];
  353. float right_mag_min_val = right_mag[length - 1];
  354. int left_mag_max_index = length - 1;
  355. int right_mag_max_index = length - 1;
  356. float left_mag_max_val = left_mag[length - 1];
  357. float right_mag_max_val = right_mag[length - 1];
  358. for(int i = length - 1; i > 0; i--)
  359. {
  360. if(left_mag_min_val > left_mag[i])
  361. {
  362. left_mag_min_val = left_mag[i];
  363. left_mag_min_index = i;
  364. }
  365. if(right_mag_min_val > right_mag[i])
  366. {
  367. right_mag_min_val = right_mag[i];
  368. right_mag_min_index = i;
  369. }
  370. if(left_mag_max_val < left_mag[i])
  371. {
  372. left_mag_max_val = left_mag[i];
  373. left_mag_max_index = i;
  374. }
  375. if(right_mag_max_val < right_mag[i])
  376. {
  377. right_mag_max_val = right_mag[i];
  378. right_mag_max_index = i;
  379. }
  380. }
  381. int index = left_mag_min_index > right_mag_min_index ? left_mag_min_index : right_mag_min_index;
  382. //寻找差异值
  383. float left_diff;
  384. float right_diff;
  385. for(int i = index; i < length - 1; i++)
  386. {
  387. left_diff = left_mag[i] - left_mag[i + 1];
  388. if(left_diff > 0.0f)
  389. {
  390. left_down_sum += left_diff;
  391. }
  392. else
  393. {
  394. left_up_sum -= left_diff;
  395. }
  396. right_diff = right_mag[i] - right_mag[i + 1];
  397. if(right_diff > 0.0f)
  398. {
  399. right_down_sum += right_diff;
  400. }
  401. else
  402. {
  403. right_up_sum -= right_diff;
  404. }
  405. }
  406. if(left_mag_min_val + 2000.f < left_mag[length - 1] && right_mag_min_val + 2000.f < right_mag[length - 1]
  407. && right_down_sum < 0.25f *right_up_sum && left_down_sum < 0.25f * left_up_sum)
  408. //if(left_mag_min_val + 2000.f < left_mag[length - 1] && right_mag_min_val + 2000.f < right_mag[length - 1] )
  409. {
  410. return 1;
  411. }
  412. return 0;
  413. }
  414. int back_down_by_long_time(float* front_mag_left, float* front_mag_right, float* back_mag_left, float* back_mag_right, int length)
  415. {
  416. static float max_front_left_val;
  417. static float max_front_right_val;
  418. static float min_back_left_val;
  419. static float min_back_right_val;
  420. static int up_count;
  421. up_count ++;
  422. static int continue_up_count = 0;
  423. if(up_count == 0)
  424. {
  425. max_front_left_val = front_mag_left[length - 1];
  426. max_front_right_val = front_mag_right[length - 1];
  427. min_back_left_val = back_mag_left[length - 1];
  428. min_back_right_val = back_mag_right[length - 1];
  429. }
  430. // if(stage_class_up_or_down(front_mag_left, 15) == -1 && stage_class_up_or_down(front_mag_right, 15) == -1)
  431. //// if( stage_class_up_or_down(back_mag_left, 15) == 1 && stage_class_up_or_down(back_mag_right, 15) == 1 )
  432. // {
  433. // float tmp = (front_mag_left[0] + front_mag_left[1] + front_mag_left[2]) *0.334f;
  434. //
  435. // max_front_left_val = tmp > max_front_left_val ? tmp : max_front_left_val;
  436. //
  437. // tmp = (front_mag_right[0] + front_mag_right[1] + front_mag_right[2]) *0.334f;
  438. //
  439. // max_front_right_val = tmp > max_front_right_val ? tmp : max_front_left_val;
  440. //
  441. // }
  442. // else
  443. // {
  444. // max_front_left_val = front_mag_left[length - 1];
  445. // max_front_right_val = front_mag_right[length - 1];
  446. //
  447. // }
  448. if(stage_class_up_or_down(back_mag_left, length) == 1 && stage_class_up_or_down(back_mag_right, length) == 1)
  449. {
  450. float tmp = (back_mag_left[0] + back_mag_left[1] + back_mag_left[2]) *0.334f;
  451. min_back_left_val = tmp > min_back_left_val ? min_back_left_val : tmp;
  452. tmp = (back_mag_right[0] + back_mag_right[1] + back_mag_right[2]) *0.334f;
  453. min_back_right_val = tmp > min_back_right_val ? min_back_right_val : tmp;
  454. continue_up_count ++;
  455. }
  456. else
  457. {
  458. min_back_left_val = back_mag_left[length - 1];
  459. min_back_right_val = back_mag_right[length - 1];
  460. continue_up_count = 0;
  461. }
  462. 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)
  463. {
  464. return 1;
  465. }
  466. if(back_mag_left[length - 1] - min_back_left_val > 4000.f && back_mag_right[length - 1] - min_back_right_val > 4000.f)
  467. {
  468. return 1;
  469. }
  470. return 0;
  471. }
  472. #define BIG_WINDOW_SIZE 20
  473. int press_down_detect_new(int index, float front_mag_left, float back_mag_left,
  474. float front_mag_right, float back_mag_right,int left_zupt, int right_zupt,
  475. float left_acc_x,float left_acc_y, float left_acc_z, float right_acc_x, float right_acc_y, float right_acc_z,
  476. int *front_down, int *back_down)
  477. {
  478. static float front_mag_norm_left[PRESS_COUNT_MAX];
  479. static float front_mag_norm_right[PRESS_COUNT_MAX];
  480. static float back_mag_norm_left[PRESS_COUNT_MAX];
  481. static float back_mag_norm_right[PRESS_COUNT_MAX];
  482. static float front_mag_left_big_buff[BIG_WINDOW_SIZE];
  483. static float front_mag_right_big_buff[BIG_WINDOW_SIZE];
  484. static float back_mag_left_big_buff[BIG_WINDOW_SIZE];
  485. static float back_mag_right_big_buff[BIG_WINDOW_SIZE];
  486. static float acc_norm_left[10];
  487. static float acc_norm_right[10];
  488. static float left_front_up_min = 40000.0f;
  489. static float left_back_up_min = 40000.0f;
  490. static float right_front_up_min = 40000.0f;
  491. static float right_back_up_min = 40000.0f;
  492. memcpy(front_mag_norm_left, front_mag_norm_left + 1, 4 * sizeof(float));
  493. memcpy(front_mag_norm_right, front_mag_norm_right + 1, 4 * sizeof(float));
  494. memcpy(front_mag_left_big_buff, front_mag_left_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(float));
  495. memcpy(front_mag_right_big_buff, front_mag_right_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(float));
  496. memcpy(back_mag_norm_left, back_mag_norm_left + 1, 4 * sizeof(float));
  497. memcpy(back_mag_norm_right, back_mag_norm_right + 1, 4 * sizeof(float));
  498. memcpy(acc_norm_left, acc_norm_left + 1, 9 * sizeof(float));
  499. memcpy(acc_norm_right, acc_norm_right + 1, 9 * sizeof(float));
  500. memcpy(back_mag_left_big_buff, back_mag_left_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(float));
  501. memcpy(back_mag_right_big_buff, back_mag_right_big_buff + 1, (BIG_WINDOW_SIZE - 1) * sizeof(float));
  502. front_mag_norm_left[PRESS_COUNT_MAX - 1] = front_mag_left;
  503. front_mag_norm_right[PRESS_COUNT_MAX - 1] = front_mag_right;
  504. front_mag_left_big_buff[(BIG_WINDOW_SIZE - 1)] = front_mag_left;
  505. front_mag_right_big_buff[(BIG_WINDOW_SIZE - 1)] = front_mag_right;
  506. back_mag_norm_left[PRESS_COUNT_MAX - 1] = back_mag_left;
  507. back_mag_norm_right[PRESS_COUNT_MAX - 1] = back_mag_right;
  508. back_mag_left_big_buff[(BIG_WINDOW_SIZE - 1)] = back_mag_left;
  509. back_mag_right_big_buff[(BIG_WINDOW_SIZE - 1)] = back_mag_right;
  510. acc_norm_left[9] = sqrt(left_acc_x * left_acc_x + left_acc_y * left_acc_y + left_acc_z * left_acc_z);
  511. acc_norm_right[9] = sqrt(right_acc_x * right_acc_x + right_acc_y * right_acc_y + right_acc_z * right_acc_z);
  512. detect_up_trend(&left_front_up_min, front_mag_norm_left, PRESS_COUNT_MAX);
  513. detect_up_trend(&right_front_up_min, front_mag_norm_right, PRESS_COUNT_MAX);
  514. detect_up_trend(&left_back_up_min, back_mag_norm_left, PRESS_COUNT_MAX);
  515. detect_up_trend(&right_back_up_min, back_mag_norm_right, PRESS_COUNT_MAX);
  516. if(left_zupt == 0)
  517. {
  518. left_front_up_min = front_mag_left;
  519. left_back_up_min = back_mag_left;
  520. }
  521. if(right_zupt == 0)
  522. {
  523. right_front_up_min = front_mag_right;
  524. right_back_up_min = back_mag_right;
  525. }
  526. //当检测到前脚掌的压力下降同时较快,将不会触发后脚掌下蹲的判断
  527. if(check_dual_front_press_down(front_mag_left_big_buff, front_mag_right_big_buff, BIG_WINDOW_SIZE))
  528. {
  529. left_back_up_min = back_mag_left;
  530. right_back_up_min = back_mag_right;
  531. }
  532. if(front_mag_left - left_front_up_min > 2000.0f && front_mag_right - right_front_up_min > 2000.0f )
  533. {
  534. *front_down = 1;
  535. }
  536. //新增一个长时间判断蹲的,2020/01/08
  537. if(special_stay_by_long_time_down(front_mag_left_big_buff, front_mag_right_big_buff, BIG_WINDOW_SIZE))
  538. {
  539. *front_down = 1;
  540. }
  541. if(back_mag_different_trend(back_mag_left_big_buff, back_mag_right_big_buff, BIG_WINDOW_SIZE))
  542. {
  543. *front_down = 0;
  544. }
  545. if(back_mag_left - left_back_up_min > 2000.0f && back_mag_right - right_back_up_min > 2000.0f )
  546. {
  547. *back_down = 1;
  548. }
  549. if(!station_acc(acc_norm_left, 10) || !station_acc(acc_norm_right, 10))
  550. {
  551. *front_down = 0;
  552. *back_down = 0;
  553. }
  554. // 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))
  555. // {
  556. // *back_down = 1;
  557. // }
  558. //2022/01/17
  559. // if(back_down_by_long_time_big_trend(back_mag_left_big_buff, back_mag_right_big_buff, BIG_WINDOW_SIZE))
  560. //
  561. // {
  562. // *back_down = 1;
  563. // }
  564. return 0;
  565. }
  566. //short press_jump_detect(int16_t *h_pos, int16_t *s_pos)
  567. //{
  568. // static int last_h_z;
  569. // static int last_s_z;
  570. // static int left_up;
  571. // static int right_up;
  572. //
  573. // static int left_up_count;
  574. // static int right_up_count;
  575. //
  576. // if(h_pos[2] - last_h_z > 0 && h_pos[2]> 0)
  577. // {
  578. // left_up = 1;
  579. // }
  580. // else if((h_pos[2] - last_h_z < 0) || h_pos[2] <= 0)
  581. // {
  582. // left_up = -1;
  583. // }
  584. //
  585. // if(left_up == 1)
  586. // {
  587. // left_up_count ++;
  588. // }
  589. // else
  590. // {
  591. // left_up_count = 0;
  592. // }
  593. //
  594. // if(s_pos[2] - last_s_z > 0 && s_pos[2] > 0)
  595. // {
  596. // right_up = 1;
  597. // }
  598. // else if((s_pos[2] - last_s_z < 0) || s_pos[2] <= 0)
  599. // {
  600. // right_up = -1;
  601. // }
  602. //
  603. // if(right_up == 1)
  604. // {
  605. // right_up_count ++;
  606. // }
  607. // else
  608. // {
  609. // right_up_count = 0;
  610. // }
  611. //
  612. // last_h_z = h_pos[2];
  613. // last_s_z = s_pos[2];
  614. //
  615. // if(left_up == 1 && right_up == 1 && right_up_count < 15 && left_up_count < 20
  616. // && right_up_count > 2 && left_up_count > 2)
  617. // {
  618. // return 1;
  619. // }
  620. //
  621. // return 0;
  622. //}
  623. //由于仅靠IMU来探测触底了,高度估计得不太对,所以用加速度来算起跳动作
  624. void max_min_window(float *data, int length, float *max_val , int *max_index, float * min_val, int *min_index)
  625. {
  626. *max_val = data[0];
  627. *max_index = 0;
  628. *min_val = data[0];
  629. *min_index = 0;
  630. for(int i = 0; i < length; i++)
  631. {
  632. if(*max_val < data[i])
  633. {
  634. *max_val = data[i];
  635. *max_index = i;
  636. }
  637. if(*min_val > data[i])
  638. {
  639. *min_val = data[i];
  640. *min_index = i;
  641. }
  642. }
  643. }
  644. 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)
  645. {
  646. static float right_data_z_buff[10];
  647. static float left_data_z_buff[10];
  648. static int last_right_press;
  649. static int last_left_press;
  650. static int left_max_press;
  651. static int right_max_press;
  652. static int wait;
  653. /*
  654. * 存储数据
  655. */
  656. memcpy(right_data_z_buff, right_data_z_buff + 1, 9 * sizeof(float));
  657. memcpy(left_data_z_buff, left_data_z_buff + 1, 9 * sizeof(float));
  658. right_data_z_buff[9] = right_acc_z;
  659. left_data_z_buff[9] = left_acc_z;
  660. if(left_zupt && right_zupt)
  661. {
  662. wait = 13;
  663. }
  664. /*
  665. * 检测压力下降用的逻辑
  666. */
  667. if(last_left_press - left_front_press > 200)
  668. {
  669. if(last_left_press > left_max_press)
  670. {
  671. left_max_press = last_left_press;
  672. }
  673. }
  674. else if(last_left_press - left_front_press < 50)
  675. {
  676. left_max_press = 0;
  677. }
  678. last_left_press = left_front_press;
  679. if(last_right_press - right_front_press > 200)
  680. {
  681. if(last_right_press > right_max_press)
  682. {
  683. right_max_press = last_right_press;
  684. }
  685. }
  686. else if(last_right_press - right_front_press < 50)
  687. {
  688. right_max_press = 0;
  689. }
  690. last_right_press = right_front_press;
  691. /*
  692. * 加速变化估计
  693. */
  694. float max_val_right, min_val_right, max_val_left, min_val_left;
  695. int max_index_right, min_index_right, max_index_left, min_index_left;
  696. max_min_window(right_data_z_buff, 10, &max_val_right , &max_index_right, &min_val_right, &min_index_right);
  697. max_min_window(left_data_z_buff, 10, &max_val_left , &max_index_left, &min_val_left, &min_index_left);
  698. if(wait > 0)
  699. {
  700. wait --;
  701. }
  702. /*
  703. * 1.0f的阈值轻轻跳就可以达到了,现在改为1.3f的阈值
  704. */
  705. if( max_index_right < min_index_right && max_index_left < min_index_left &&
  706. max_val_right - min_val_right > 1.8f && max_val_left - min_val_left > 1.8f
  707. && (wait > 0 && left_zupt == 0 && right_zupt == 0) )
  708. //if(left_max_press - left_front_press > 3000 && right_max_press - right_front_press > 3000)
  709. {
  710. SEGGER_RTT_printf(0, "test\n");
  711. SEGGER_RTT_printf(0," left_max_press - left_front_press= %d , right_max_press - right_front_press = %d\n", left_max_press - left_front_press, right_max_press - right_front_press);
  712. return 1;
  713. }
  714. return 0;
  715. }
  716. int att_jump_detect(float left_pitch, float right_pitch, int left_zupt, int right_zupt)
  717. {
  718. static int num;
  719. static float left_pitch_window[8];
  720. static float right_pitch_window[8];
  721. //用俯仰角判断好了, 现在的IMU方向 下标1为俯仰角
  722. memcpy(left_pitch_window, left_pitch_window + 1, 7 * sizeof(float));
  723. memcpy(right_pitch_window, right_pitch_window + 1, 7 * sizeof(float));
  724. left_pitch_window[7] = left_pitch;
  725. right_pitch_window[7] = right_pitch;
  726. num ++;
  727. if(num < 8)
  728. {
  729. return 0;
  730. }
  731. else
  732. {
  733. num = 8;
  734. }
  735. //要求起跳要同步
  736. static int zupt_count = 0;
  737. if(left_zupt && right_zupt)
  738. {
  739. zupt_count = 21;
  740. }
  741. if(zupt_count > 0)
  742. {
  743. zupt_count --;
  744. }
  745. for(int i = 7; i > 1; i--)
  746. {
  747. if((left_pitch_window[i] > left_pitch_window[i-1] || left_pitch_window[i] > left_pitch_window[i-2])
  748. && (right_pitch_window[i] > right_pitch_window[i-1] || right_pitch_window[i] > right_pitch_window[i-2]))
  749. {
  750. if((left_pitch > left_pitch_window[i - 1] + 0.1f || left_pitch > left_pitch_window[i - 2] + 0.1f )
  751. && (right_pitch > right_pitch_window[i - 1] + 0.1f || right_pitch > right_pitch_window[i - 2] + 0.1f )
  752. && zupt_count > 0 )
  753. {
  754. return 1;
  755. }
  756. }
  757. else
  758. {
  759. return 0;
  760. }
  761. }
  762. return 0;
  763. }
  764. //int little_jump_detect_process(int press, int *press_max, int*press_min, int *last_press)
  765. //{
  766. //
  767. // if(*last_press - press > 200)
  768. // {
  769. // if(*press_max < press)
  770. // {
  771. // *press_max = press;
  772. // }
  773. //
  774. // if(*press_min > press)
  775. // {
  776. // *press_min = press;
  777. // }
  778. // }
  779. // else if(*last_press - press < -200)
  780. // {
  781. // if(*press_max > press)
  782. // {
  783. // *press_max = press;
  784. // }
  785. //
  786. // if(*press_min < press)
  787. // {
  788. // *press_min = press;
  789. // }
  790. // }
  791. //
  792. // *last_press = press;
  793. //
  794. // if(*press_max - press > 3000)
  795. // {
  796. // return 1;
  797. // }
  798. //
  799. // if(*press_max - press < -1000)
  800. // {
  801. // return -1;
  802. // }
  803. //
  804. // return 0;
  805. //}
  806. //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)
  807. //{
  808. // static int left_front_max = 0;
  809. // static int right_front_max = 0;
  810. // static int left_back_max = 0;
  811. // static int right_back_max = 0;
  812. //
  813. // static int left_front_min = 40000;
  814. // static int right_front_min = 40000;
  815. // static int left_back_min = 40000;
  816. // static int right_back_min = 40000;
  817. //
  818. // static int last_left_front_press = 0;
  819. // static int last_right_front_press = 0;
  820. //
  821. // static int last_left_back_press = 0;
  822. // static int last_right_back_press = 0;
  823. // int left_front_down = little_jump_detect_process(left_front_press, &left_front_max, &left_front_min, &last_left_front_press);
  824. //
  825. // int right_front_down = little_jump_detect_process(right_front_press, &right_front_max, &right_front_min, &last_right_front_press);
  826. //
  827. // int left_back_down = little_jump_detect_process(left_back_press, &left_back_max, &left_back_min, &last_left_back_press);
  828. //
  829. // int right_back_down = little_jump_detect_process(right_back_press, &right_back_max, &right_back_min, &last_right_back_press);
  830. //
  831. //// if( left_back_down == -1)
  832. //// {
  833. //// left_front_max = left_front_press;
  834. ////
  835. //// left_front_min = left_front_press;
  836. //// }
  837. ////
  838. //// if( right_back_down == -1)
  839. //// {
  840. //// right_front_max = right_front_press;
  841. ////
  842. //// right_front_min = right_front_press;
  843. //// }
  844. //
  845. // if(left_front_down != 1)
  846. // {
  847. // right_front_max = right_front_press;
  848. //
  849. // right_front_min = right_front_press;
  850. // }
  851. //
  852. // if(right_front_down != 1)
  853. // {
  854. // left_front_max = left_front_press;
  855. //
  856. // left_front_min = left_front_press;
  857. // }
  858. //
  859. // if(left_front_down==1 && right_front_down==1 && left_zupt==0 && right_zupt==0)
  860. // {
  861. // return 1;
  862. // }
  863. //
  864. // if(left_back_down==1 && right_back_down==1 && left_zupt==0 && right_zupt==0)
  865. // {
  866. // return 1;
  867. // }
  868. //
  869. // return 0;
  870. //
  871. //}
  872. //int detect_L_line(float *press_buff, int length)
  873. //{
  874. //
  875. // float max_diff_val = press_buff[0] - press_buff[1];
  876. //
  877. // float temp_diff_val = 0.0f;
  878. //
  879. // int max_index = 0;
  880. //
  881. // for(int i = 1; i < length; i++)
  882. // {
  883. // if(press_buff[i-1] - press_buff[i] > -50.f)
  884. // {
  885. // temp_diff_val += (press_buff[i-1] - press_buff[i]);
  886. // }
  887. // else
  888. // {
  889. //
  890. // temp_diff_val = 0.0f;
  891. // }
  892. //
  893. // if(max_diff_val < temp_diff_val)
  894. // {
  895. // max_diff_val = temp_diff_val;
  896. //
  897. // if(max_diff_val > 4000.f)
  898. // {
  899. // max_index = i;
  900. // }
  901. // }
  902. //
  903. // }
  904. //
  905. // float stable_max_val = press_buff[length - 4];
  906. // float stable_min_val = press_buff[length - 4];
  907. //
  908. // for(int i= length - 4; i< length; i++)
  909. // {
  910. // if(stable_max_val < press_buff[i])
  911. // {
  912. // stable_max_val = press_buff[i];
  913. // }
  914. //
  915. // if(stable_min_val > press_buff[i])
  916. // {
  917. // stable_min_val = press_buff[i];
  918. // }
  919. // }
  920. //
  921. // if( stable_max_val - stable_min_val < 1000.f && max_diff_val > 3000.f
  922. // && max_index > length - 5)
  923. // {
  924. // return 1;
  925. // }
  926. //
  927. // return 0;
  928. //}
  929. //int acc_z_down_trend(float *left_acc_buff, float *right_acc_buff, int length)
  930. //{
  931. // int left_max_index = 0;
  932. // int left_min_index = 0;
  933. //
  934. // int right_max_index = 0;
  935. // int right_min_index = 0;
  936. //
  937. //
  938. // float left_max_val = left_acc_buff[0];
  939. // float left_min_val = left_acc_buff[0];
  940. //
  941. // float right_max_val = right_acc_buff[0];
  942. // float right_min_val = right_acc_buff[0];
  943. //
  944. //
  945. // for(int i = 0; i < length; i++)
  946. // {
  947. // if(left_max_val < left_acc_buff[i])
  948. // {
  949. // left_max_val = left_acc_buff[i];
  950. // left_max_index = i;
  951. // }
  952. //
  953. // if(left_min_val > left_acc_buff[i])
  954. // {
  955. // left_min_val = left_acc_buff[i];
  956. // left_min_index = i;
  957. // }
  958. //
  959. // if(left_max_val < left_acc_buff[i])
  960. // {
  961. // left_max_val = left_acc_buff[i];
  962. // left_max_index = i;
  963. // }
  964. //
  965. // if(right_min_val > right_acc_buff[i])
  966. // {
  967. // right_min_val = right_acc_buff[i];
  968. // right_min_index = i;
  969. // }
  970. //
  971. // if(right_max_val < right_acc_buff[i])
  972. // {
  973. // right_max_val = right_acc_buff[i];
  974. // right_max_index = i;
  975. // }
  976. //
  977. // }
  978. //
  979. // if(right_max_index < right_min_index && right_max_val > right_min_val + 1.5f
  980. // && left_max_index < left_min_index && left_max_val > left_min_val + 1.5f
  981. // && abs(right_max_index - left_max_index < 4) && abs(left_max_index - left_min_index < 4))
  982. // {
  983. // return 1;
  984. // }
  985. //
  986. // return 0;
  987. //
  988. //}
  989. //int acc_little_shake(float *left_acc, float *right_acc, int length)
  990. //{
  991. // float left_max_val = left_acc[0];
  992. // float right_max_val = right_acc[0];
  993. //
  994. // float left_min_val = left_acc[0];
  995. // float right_min_val = right_acc[0];
  996. //
  997. // for(int i = 0; i < length; i++)
  998. // {
  999. // if(left_max_val < left_acc[i])
  1000. // {
  1001. // left_max_val = left_acc[i];
  1002. // }
  1003. //
  1004. // if(left_min_val > left_acc[i])
  1005. // {
  1006. // left_min_val = left_acc[i];
  1007. // }
  1008. //
  1009. // if(right_max_val < left_acc[i])
  1010. // {
  1011. // right_max_val = left_acc[i];
  1012. // }
  1013. //
  1014. // if(right_min_val > left_acc[i])
  1015. // {
  1016. // right_min_val = left_acc[i];
  1017. // }
  1018. //
  1019. // }
  1020. //
  1021. // if(left_max_val - left_min_val > 0.4f && right_max_val - right_min_val > 0.4f)
  1022. // {
  1023. // return 1;
  1024. // }
  1025. //
  1026. // return 0;
  1027. //
  1028. //
  1029. //}
  1030. //int normal_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt,
  1031. // float left_front_press, float right_front_press, float left_back_press, float right_back_press)
  1032. //{
  1033. //// static float right_data_press[15];
  1034. //// static float left_data_press[15];
  1035. //
  1036. // static float right_front_data_press[20];
  1037. // static float left_front_data_press[20];
  1038. //
  1039. // static float left_acc_z_buff[15];
  1040. // static float right_acc_z_buff[15];
  1041. //
  1042. //
  1043. // static int left_wait;
  1044. // static int right_wait;
  1045. // static int wait;
  1046. //
  1047. // static int last_left_zupt;
  1048. // static int last_right_zupt;
  1049. //
  1050. // static int press_jump_count = 0;
  1051. //
  1052. // /*
  1053. // * 存储数据
  1054. // */
  1055. //
  1056. //// memcpy(right_data_press, right_data_press + 1, 14 * sizeof(float));
  1057. ////
  1058. //// memcpy(left_data_press, left_data_press + 1, 14 * sizeof(float));
  1059. //
  1060. // memcpy(right_front_data_press, right_front_data_press + 1, 19 * sizeof(float));
  1061. //
  1062. // memcpy(left_front_data_press, left_front_data_press + 1, 19 * sizeof(float));
  1063. //
  1064. // memcpy(left_acc_z_buff, left_acc_z_buff + 1, 14 * sizeof(float));
  1065. //
  1066. // memcpy(right_acc_z_buff, right_acc_z_buff + 1, 14 * sizeof(float));
  1067. //
  1068. //
  1069. //// right_data_press[14] = right_back_press; left_data_press[14] = left_back_press;
  1070. //
  1071. // right_front_data_press[19] = right_front_press; left_front_data_press[19] = left_front_press;
  1072. //
  1073. // left_acc_z_buff[14] = left_acc_z; right_acc_z_buff[14] = right_acc_z;
  1074. //
  1075. //// if(left_zupt == 0 && last_left_zupt == 1)
  1076. //// {
  1077. //// left_wait = 10;
  1078. //// }
  1079. ////
  1080. //// if(right_zupt == 0 && last_right_zupt == 1)
  1081. //// {
  1082. //// right_wait = 10;
  1083. //// }
  1084. ////
  1085. //
  1086. //// if(left_wait > 0)
  1087. //// {
  1088. //// left_wait --;
  1089. //// }
  1090. ////
  1091. //// if(right_wait > 0)
  1092. //// {
  1093. //// right_wait --;
  1094. //// }
  1095. // if(left_zupt == 1 && right_zupt == 1)
  1096. // {
  1097. // wait = 15;
  1098. // }
  1099. //
  1100. // if(wait > 0)
  1101. // {
  1102. // wait --;
  1103. // }
  1104. //
  1105. // last_left_zupt = left_zupt;
  1106. // last_right_zupt = right_zupt;
  1107. //
  1108. //// 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)
  1109. //// {
  1110. //// return 1;
  1111. //// }
  1112. //
  1113. // 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)
  1114. // {
  1115. // press_jump_count ++;
  1116. // if(press_jump_count > 2)
  1117. // {
  1118. // return 1;
  1119. // }
  1120. // }
  1121. // else
  1122. // {
  1123. // press_jump_count =0;
  1124. // }
  1125. ////
  1126. //// 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)
  1127. //// {
  1128. //// return 1;
  1129. //// }
  1130. //
  1131. // if(acc_z_down_trend(left_acc_z_buff, right_acc_z_buff, 15)&& wait > 0 && wait < 14 && left_zupt == 0 && right_zupt == 0 )
  1132. // {
  1133. // return 1;
  1134. // }
  1135. //
  1136. //
  1137. // return 0;
  1138. //
  1139. //
  1140. //}
  1141. //short press_jump_detect(float left_acc_z, float right_acc_z, int left_zupt, int right_zupt,
  1142. // float left_front_press, float right_front_press, float left_back_press, float right_back_press)
  1143. //{
  1144. //
  1145. //// return little_jump_detect( left_front_press, right_front_press, left_back_press, right_back_press, left_zupt, right_zupt) ||
  1146. //// 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);
  1147. //
  1148. // 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))
  1149. // {
  1150. // return 1;
  1151. // }
  1152. //
  1153. // return 0;
  1154. //
  1155. //}