123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- #include "special_motion.h"
- #include "motion_config.h"
- #include "app_game.h"
- int down_send_count = 0;
- int is_down = 0;
- int jump_send_count = 0;
- int is_jump = 0;
- int down_res = 0;
- int jump_res = 0;
- int16_t left_time;
- uint16_t left_press;
- int32_t left_pos[3];
- int16_t left_att[3];
- int16_t left_acc[3];
- int16_t left_zupt;
- int16_t right_time;
- uint16_t right_press;
- int32_t right_pos[3];
- int16_t right_att[3];
- int16_t right_acc[3];
- int16_t right_zupt;
- uint8_t rssi;
- uint8_t right_rssi;
- uint8_t left_rssi;
- uint16_t right_front_mag;
- uint16_t left_front_mag;
- uint16_t right_back_mag;
- uint16_t left_back_mag;
- uint32_t right_step;
- uint32_t left_step;
- void decode_shoes_data_right(uint8_t* buff,uint8_t right_len)
- {
- right_pos[0] = ((int32_t)buff[0] << 24) | ((int32_t)buff[1] << 16) | ((int32_t)buff[2] << 8) | ((int32_t)buff[3] << 0);
- right_pos[1] = ((int32_t)buff[4] << 24) | ((int32_t)buff[5] << 16) | ((int32_t)buff[6] << 8) | ((int32_t)buff[7] << 0);
- right_pos[2] = ((int32_t)buff[8] << 24) | ((int32_t)buff[9] << 16) | ((int32_t)buff[10] << 8) | ((int32_t)buff[11] << 0);
-
- right_att[0] = ((int16_t)buff[12] << 8) | ((int16_t)buff[13] << 0);
- right_att[1] = ((int16_t)buff[14] << 8) | ((int16_t)buff[15] << 0);
- right_att[2] = ((int16_t)buff[16] << 8) | ((int16_t)buff[17] << 0);
-
- right_front_mag = ((uint16_t)buff[18] << 8) | ((uint16_t)buff[19] << 0);
-
- right_back_mag = ((uint16_t)buff[20] << 8) | ((uint16_t)buff[21] << 0);
-
- right_zupt = ((uint8_t)buff[22]);
- right_rssi = (uint8_t) buff[23];
-
- right_time = ((uint16_t)buff[24] << 8) | ((uint16_t)buff[25] << 0);
-
- right_acc[0] = ((int16_t)buff[26] << 8) | ((int16_t)buff[27] << 0);
- right_acc[1] = ((int16_t)buff[28] << 8) | ((int16_t)buff[29] << 0);
- right_acc[2] = ((int16_t)buff[30] << 8) | ((int16_t)buff[31] << 0);
-
- right_step = ((uint32_t)buff[32] << 24) | ((uint32_t)buff[33] << 16) | ((uint32_t)buff[34] << 8) | ((uint32_t)buff[35] << 0);
- }
- void decode_shoes_data_left(uint8_t* buff,uint8_t left_len)
- {
- left_pos[0] = ((int32_t)buff[0] << 24) | ((int32_t)buff[1] << 16) | ((int32_t)buff[2] << 8) | ((int32_t)buff[3] << 0);
- left_pos[1] = ((int32_t)buff[4] << 24) | ((int32_t)buff[5] << 16) | ((int32_t)buff[6] << 8) | ((int32_t)buff[7] << 0);
- left_pos[2] = ((int32_t)buff[8] << 24) | ((int32_t)buff[9] << 16) | ((int32_t)buff[10] << 8) | ((int32_t)buff[11] << 0);
-
- left_att[0] = ((int16_t)buff[12] << 8) | ((int16_t)buff[13] << 0);
- left_att[1] = ((int16_t)buff[14] << 8) | ((int16_t)buff[15] << 0);
- left_att[2] = ((int16_t)buff[16] << 8) | ((int16_t)buff[17] << 0);
-
- left_front_mag = ((uint16_t)buff[18] << 8) | ((uint16_t)buff[19] << 0);
-
- left_back_mag = ((uint16_t)buff[20] << 8) | ((uint16_t)buff[21] << 0);
-
- left_zupt = ((uint8_t)buff[22]);
- left_rssi = (uint8_t) buff[23];
-
- left_time = ((uint16_t)buff[24] << 8) | ((uint16_t)buff[25] << 0);
-
- left_acc[0] = ((int16_t)buff[26] << 8) | ((int16_t)buff[27] << 0);
- left_acc[1] = ((int16_t)buff[28] << 8) | ((int16_t)buff[29] << 0);
- left_acc[2] = ((int16_t)buff[30] << 8) | ((int16_t)buff[31] << 0);
-
- left_step = ((uint32_t)buff[32] << 24) | ((uint32_t)buff[33] << 16) | ((uint32_t)buff[34] << 8) | ((uint32_t)buff[35] << 0);
-
- }
- uint8_t buf[82];
- uint8_t L=0;
- void inter_package_data()
- {
-
- static uint8_t package_time;
-
- L=0;
-
- buf[L++] = (uint8_t)(right_pos[0]>>24);
- buf[L++] = (uint8_t)(right_pos[0]>>16);
- buf[L++] = (uint8_t)(right_pos[0]>>8);
- buf[L++] = (uint8_t)(right_pos[0]>>0);
-
- buf[L++] = (uint8_t)(right_pos[1]>>24);
- buf[L++] = (uint8_t)(right_pos[1]>>16);
- buf[L++] = (uint8_t)(right_pos[1]>>8);
- buf[L++] = (uint8_t)(right_pos[1]>>0);
-
- buf[L++] = (uint8_t)(right_pos[2]>>24);
- buf[L++] = (uint8_t)(right_pos[2]>>16);
- buf[L++] = (uint8_t)(right_pos[2]>>8);
- buf[L++] = (uint8_t)(right_pos[2]>>0);
-
-
- buf[L++] = (uint8_t)(left_pos[0]>>24);
- buf[L++] = (uint8_t)(left_pos[0]>>16);
- buf[L++] = (uint8_t)(left_pos[0]>>8);
- buf[L++] = (uint8_t)(left_pos[0]>>0);
-
- buf[L++] = (uint8_t)(left_pos[1]>>24);
- buf[L++] = (uint8_t)(left_pos[1]>>16);
- buf[L++] = (uint8_t)(left_pos[1]>>8);
- buf[L++] = (uint8_t)(left_pos[1]>>0);
-
- buf[L++] = (uint8_t)(left_pos[2]>>24);
- buf[L++] = (uint8_t)(left_pos[2]>>16);
- buf[L++] = (uint8_t)(left_pos[2]>>8);
- buf[L++] = (uint8_t)(left_pos[2]>>0);
-
- buf[L++] = (uint8_t)(right_att[0]>>8);
- buf[L++] = (uint8_t)(right_att[0]>>0);
-
- buf[L++] = (uint8_t)(right_att[1]>>8);
- buf[L++] = (uint8_t)(right_att[1]>>0);
-
- buf[L++] = (uint8_t)(right_att[2]>>8);
- buf[L++] = (uint8_t)(right_att[2]>>0);
-
- buf[L++] = (uint8_t)(left_att[0]>>8);
- buf[L++] = (uint8_t)(left_att[0]>>0);
-
- buf[L++] = (uint8_t)(left_att[1]>>8);
- buf[L++] = (uint8_t)(left_att[1]>>0);
-
- buf[L++] = (uint8_t)(left_att[2]>>8);
- buf[L++] = (uint8_t)(left_att[2]>>0);
-
- buf[L++] = (uint8_t)(right_acc[0]>>8);
- buf[L++] = (uint8_t)(right_acc[0]>>0);
-
- buf[L++] = (uint8_t)(right_acc[1]>>8);
- buf[L++] = (uint8_t)(right_acc[1]>>0);
-
- buf[L++] = (uint8_t)(right_acc[2]>>8);
- buf[L++] = (uint8_t)(right_acc[2]>>0);
-
- buf[L++] = (uint8_t)(left_acc[0]>>8);
- buf[L++] = (uint8_t)(left_acc[0]>>0);
-
- buf[L++] = (uint8_t)(left_acc[1]>>8);
- buf[L++] = (uint8_t)(left_acc[1]>>0);
-
- buf[L++] = (uint8_t)(left_acc[2]>>8);
- buf[L++] = (uint8_t)(left_acc[2]>>0);
-
- int is_girl = 0;
-
- if(GIRL_SHOES)
- is_girl = 1;
-
-
- buf[L++] = (uint8_t)(is_girl * 16 + right_zupt * 8 + left_zupt * 4 + down_res * 2 + jump_res * 1);
-
- buf[L++] = (uint8_t)(left_rssi >> 0);
-
- buf[L++] = (uint8_t)(package_time);
-
- buf[L++] = (uint8_t)(right_front_mag>>8);
- buf[L++] = (uint8_t)(right_front_mag>>0);
-
- buf[L++] = (uint8_t)(right_back_mag>>8);
- buf[L++] = (uint8_t)(right_back_mag>>0);
-
- buf[L++] = (uint8_t)(left_front_mag>>8);
- buf[L++] = (uint8_t)(left_front_mag>>0);
-
- buf[L++] = (uint8_t)(left_back_mag>>8);
- buf[L++] = (uint8_t)(left_back_mag>>0);
-
- buf[L++] = (uint8_t)(right_step>>24);
- buf[L++] = (uint8_t)(right_step>>16);
- buf[L++] = (uint8_t)(right_step>>8);
- buf[L++] = (uint8_t)(right_step>>0);
-
- buf[L++] = (uint8_t)(left_step>>24);
- buf[L++] = (uint8_t)(left_step>>16);
- buf[L++] = (uint8_t)(left_step>>8);
- buf[L++] = (uint8_t)(left_step>>0);
-
- package_time ++;
- }
- //滑动滤波--均值滤波
- uint16_t cal_mean_val(uint16_t *filter_window, int length)
- {
- uint32_t filter_window_sum = 0;
-
- for(int i = 0; i < length; i++)
- {
- filter_window_sum += filter_window[i];
- }
-
- return filter_window_sum/length;
- }
- void special_motion(int time_stamp, uint16_t front_mag_left,
- uint16_t back_mag_left, uint16_t front_mag_right, uint16_t back_mag_right,
- int16_t h_pos[3], int16_t s_pos[3], int16_t *down, int16_t *jump)
- {
- static int32_t last_left_pos[3];
- static int32_t last_right_pos[3];
-
- static int16_t left_pos_offset[3];
- static int16_t right_pos_offset[3];
-
- static int time_count = 0;
-
- static uint16_t front_mag_left_filter_window[5];
- static uint16_t back_mag_left_filter_window[5];
-
- static uint16_t front_mag_right_filter_window[5];
- static uint16_t back_mag_right_filter_window[5];
-
- static float front_mag_left_norm;
-
- static float back_mag_left_norm;
-
- static float front_mag_right_norm;
-
- static float back_mag_right_norm;
-
- //PU鞋垫数据通常都带有小抖动,现在需要滤波, 让数据更加好看
-
- memcpy(front_mag_left_filter_window, front_mag_left_filter_window + 1, 4 * sizeof(uint16_t));
- memcpy(back_mag_left_filter_window, back_mag_left_filter_window + 1, 4 * sizeof(uint16_t));
-
- memcpy(front_mag_right_filter_window, front_mag_right_filter_window + 1, 4 * sizeof(uint16_t));
- memcpy(back_mag_right_filter_window, back_mag_right_filter_window + 1, 4 * sizeof(uint16_t));
-
- front_mag_left_filter_window[4] = front_mag_left;
- back_mag_left_filter_window[4] = back_mag_left;
-
- front_mag_right_filter_window[4] = front_mag_right;
- back_mag_right_filter_window[4] = back_mag_right;
-
- if(time_count > 4)
- {
- left_front_mag = cal_mean_val(front_mag_left_filter_window, 5);
-
- left_back_mag = cal_mean_val(back_mag_left_filter_window, 5);
-
- right_front_mag = cal_mean_val(front_mag_right_filter_window, 5);
-
- right_back_mag = cal_mean_val(back_mag_right_filter_window, 5);
- }
- front_mag_left_norm = left_front_mag;
-
- back_mag_left_norm = left_back_mag;
-
- front_mag_right_norm = right_front_mag;
-
- back_mag_right_norm = right_back_mag;
-
-
- time_count ++;
-
-
- float left_acc_z = left_acc[2]/2048.f;
-
- float right_acc_z = right_acc[2]/2048.f;
-
- /*
- 处理蹲的动作
- */
- int front_down_tag = 0;
- int back_down_tag = 0;
- press_down_detect_new(time_stamp, front_mag_left_norm, back_mag_left_norm,
- front_mag_right_norm, back_mag_right_norm,left_zupt,right_zupt,
- left_acc[0]/2048.f, left_acc[1]/2048.f, left_acc[2]/2048.f, right_acc[0]/2048.f, right_acc[1]/2048.f, right_acc[2]/2048.f,
- &front_down_tag, &back_down_tag);
- // is_down = front_down_tag + back_down_tag * 2;
-
- if (front_down_tag || back_down_tag)
- //if (front_down_tag)
- {
- /*
- gpio_mt_run(300);
- */
- down_send_count = 30;
- }
-
- if(down_send_count > 0)
- {
- is_down = 1;
- down_send_count --;
- }
- else
- {
- is_down = 0;
- }
-
- /*
- 处理跳的动作
- */
-
- if(left_zupt)
- {
- memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
- }
-
- if(right_zupt)
- {
- memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
- }
-
- for(int i = 0; i < 3; i++)
- {
- left_pos_offset[i] = left_pos[i] - last_left_pos[i];
- right_pos_offset[i] = right_pos[i] - last_right_pos[i];
- }
-
-
- short jump_tag = 0;
-
- int normal_jump_tag = att_jump_detect(left_att[1] / 10000.f, right_att[1] / 10000.f, left_zupt, right_zupt);
-
- int unnormal_jump_tag = press_jump_detect( left_acc_z, right_acc_z, left_zupt, right_zupt, (int) front_mag_left_norm, (int) front_mag_right_norm);
-
- if(unnormal_jump_tag == 1 || normal_jump_tag == 1)
- {
- jump_tag = 1;
- }
- //jump_tag = press_jump_detect(left_acc_z, right_acc_z, left_zupt, right_zupt, front_mag_left_norm, front_mag_right_norm, back_mag_left_norm, back_mag_right_norm);
-
- if (jump_tag)
- {
- /*
- gpio_mt_run(300);
- */
- jump_send_count = 30;
- }
-
- //连续发20次蹲,终端再处理
- if(jump_send_count > 0)
- {
- is_jump = 1;
- jump_send_count --;
- }
- else
- {
- is_jump = 0;
- }
-
-
-
- down_res = is_down;
-
- jump_res = is_jump;
-
- }
- //uint8_t buf[64];
- //uint8_t L=0;
- void imu_package_to_pack(uint8_t* left_buff, uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
- {
- static uint8_t package_time;
-
- int index = 0;
- int left_index = 0;
- int right_index = 0;
- while(left_index < left_len)
- {
- buf[index ++] = left_buff[left_index ++];
- }
- while(right_index < right_len)
- {
- buf[index ++] = right_buff[right_index ++];
- }
-
- buf[index ++] = (uint8_t) is_down;
- buf[index ++] = (uint8_t) is_jump;
- buf[index ++] = (uint8_t) (package_time ++);
-
- L = index;
- }
- void integrate_left_right(uint8_t* left_buff,uint8_t left_len, uint8_t* right_buff, uint8_t right_len)
- {
-
- #if GAMEDATA_TO_RAWDATA
- if(gamedata_to_rawdata){
- imu_package_to_pack(left_buff, left_len, right_buff, right_len);
- return;
- }
- #endif
-
-
- static uint16_t time_stamp;
-
- static int16_t left_pos_offset[3];
- static int16_t right_pos_offset[3];
- static int32_t last_left_pos[3];
- static int32_t last_right_pos[3];
-
- //将双脚的数据读取出来
-
- decode_shoes_data_right(right_buff, right_len);
- decode_shoes_data_left(left_buff, left_len);
-
-
- //先处理跳蹲再发过去
- int16_t down = 0;
- int16_t jump = 0;
-
- //获取位置的偏移量再判断跳
- if(left_zupt == 1)
- {
- memcpy(last_left_pos, left_pos, 3 * sizeof(int32_t));
- }
- if(right_zupt == 1)
- {
- memcpy(last_right_pos, right_pos, 3 * sizeof(int32_t));
- }
-
- for(int i = 0; i < 3; i++)
- {
- left_pos_offset[i] = left_pos[i] - last_left_pos[i];
- right_pos_offset[i] = right_pos[i] - last_right_pos[i];
- }
-
-
- special_motion(time_stamp, left_front_mag, left_back_mag, right_front_mag, right_back_mag,
- left_pos_offset, right_pos_offset , &down, &jump);
-
- // SEGGER_RTT_printf(0,"left_step: %d, right_step: %d, ======= \n", left_step, right_step);
-
- inter_package_data();
-
-
- }
- //获取包裹的接口
- void get_game_package(uint8_t* buff_address, uint8_t * buff_len)
- {
- memcpy(buff_address, buf, L);
-
- // SEGGER_RTT_printf(0,"L: %d, =======+++++++++ \n", L);
- *buff_len = L;
-
- #if GAMEDATA_TO_RAWDATA
- if(gamedata_to_rawdata){
- if(L <= 0x34){
- *buff_len = 0x34;
- }
- }
- #endif
-
- }
|