heelstrike.c
Go to the documentation of this file.00001
00007 #include <includes.h>
00008
00009
00010 static const int HS_IDLESWING = 1, HS_DATACOLLECT = 2, HS_LOOKING4HS = 3, HS_IDLESTANCE = 4, HS_LOOKING4HUP = 5;
00011 static int hs_state = HS_LOOKING4HS;
00012
00013
00014 static int hs_cmax = 0;
00015 static fixed hs_exp_fiter_coeff, hs_threshold = 0, hs_confidence =0;
00016 static INT_VOID_F hs_get_left = intvoid;
00017 static INT_VOID_F hs_get_right = intvoid;
00018
00019
00020 static int hs_heelstrike = 0;
00021
00022
00023 static fixed raw_sum = 0, filtered_sum = 0, prev_filtered_sum = 0;
00024 static fixed raw_abs_dev =0, filtered_abs_dev = 0, prev_filtered_abs_dev = 0;
00025 static int hs_counter = 0;
00026 static fixed hs_foot_up_threshold = 0;
00027
00028
00029 void hs_init(float exp_filter_coeff, int confidence, int threshold, int cmax, INT_VOID_F get_right, INT_VOID_F get_left){
00030
00031 hs_exp_fiter_coeff = float_to_fixed(exp_filter_coeff);
00032 hs_confidence = int_to_fixed(confidence);
00033 hs_threshold = int_to_fixed(threshold);
00034 hs_cmax = cmax;
00035 hs_get_left = get_left;
00036 hs_get_right = get_right;
00037 }
00038
00039
00040 void hs_update(void){
00041
00042 switch (hs_state){
00043 case HS_IDLESWING: break;
00044 case HS_DATACOLLECT: hs_calc_filtered_data(); break;
00045 case HS_LOOKING4HS: hs_look4hs(); break;
00046 case HS_IDLESTANCE: break;
00047 case HS_LOOKING4HUP: hs_look4hup(); break;
00048 }
00049 }
00050
00051
00052 void hs_set_state(float state){
00053
00054 int next_state = (int)state;
00055 mcu_led_green_blink(10);
00056
00057 if (next_state != hs_state){
00058 switch (next_state){
00059 case HS_IDLESWING:
00060 break;
00061 case HS_DATACOLLECT:
00062 raw_sum = int_to_fixed(hs_get_right() + hs_get_left());
00063 prev_filtered_sum = raw_sum;
00064 break;
00065 case HS_LOOKING4HS:
00066 break;
00067 case HS_IDLESTANCE:
00068 break;
00069 case HS_LOOKING4HUP:
00070 hs_foot_up_threshold = raw_sum;
00071 break;
00072 }
00073 hs_state = next_state;
00074 }
00075
00076 }
00077
00078
00079 void hs_calc_filtered_data(void){
00080
00081 raw_sum = int_to_fixed(hs_get_right() + hs_get_left());
00082 filtered_sum = raw_sum + fixed_mult(hs_exp_fiter_coeff, (prev_filtered_sum - raw_sum)) ;
00083 raw_abs_dev = fixed_abs(raw_sum - filtered_sum);
00084 filtered_abs_dev = raw_abs_dev + fixed_mult(hs_exp_fiter_coeff, (prev_filtered_abs_dev - raw_abs_dev)) ;
00085 }
00086
00087 void hs_look4hs(void){
00088
00089 if (hs_heelstrike == 0){
00090 raw_sum = int_to_fixed(hs_get_right() + hs_get_left());
00091 if((raw_sum > filtered_sum+ fixed_mult(hs_confidence , filtered_abs_dev)) && (raw_sum > filtered_sum + hs_threshold) ){
00092 hs_counter++;
00093 if(hs_counter >= hs_cmax){ hs_heelstrike = 1;}
00094 }else{
00095 hs_counter = 0 ;
00096 hs_calc_filtered_data();
00097 }
00098 }
00099 }
00100
00101 void hs_look4hup(void){
00102
00103 if (hs_heelstrike == 1){
00104 raw_sum = int_to_fixed(hs_get_right() + hs_get_left());
00105 if(raw_sum < hs_foot_up_threshold ) {hs_heelstrike = 0;}
00106 }
00107
00109 }
00110
00111 int hs_get(void){
00112 return hs_heelstrike;
00113 }
00114
00115 float hs_get_float(void){
00116 if(hs_heelstrike){return 1.0;}
00117 return 0.0;
00118 }