From 3acae8ae103ed362f1e11283a9fc7f4ecbb70d7b Mon Sep 17 00:00:00 2001 From: Candas1 Date: Sun, 3 Jan 2021 00:09:39 +0100 Subject: [PATCH 01/14] Function for setting, getting, incrementing,initializing and dumping parameters --- Inc/util.h | 40 +++++++++++++++ Src/util.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++++- platformio.ini | 2 +- 3 files changed, 173 insertions(+), 2 deletions(-) diff --git a/Inc/util.h b/Inc/util.h index 714ef86..0519dc1 100644 --- a/Inc/util.h +++ b/Inc/util.h @@ -127,5 +127,45 @@ typedef struct { } MultipleTap; void multipleTapDet(int16_t u, uint32_t timeNow, MultipleTap *x); +#define SIZEP(x) ((char*)(&(x) + 1) - (char*)&(x)) + + +enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT}; +#define typename(x) _Generic((x), \ + uint8_t: UINT8_T, \ + uint16_t: UINT16_T, \ + uint32_t: UINT32_T, \ + int8_t: INT8_T, \ + int16_t: INT16_T, \ + int32_t: INT32_T, \ + int: INT, \ + float: FLOAT) + +#define PARAM_SIZE(param) sizeof(param) / sizeof(parameter_entry) +#define ADD_PARAM(value_var,value_var2) &value_var,&value_var2,typename(value_var) + +uint8_t setValue(uint8_t index, int32_t newValue); +uint8_t initValue(uint8_t index); +uint32_t getValue(uint8_t index); +uint8_t incrValue(uint8_t index); +uint8_t saveValue(uint8_t index); +void dumpValues(); + +typedef struct parameter_entry_struct parameter_entry; +struct parameter_entry_struct { + const char *name; + void *value; + void *value2; + const uint8_t type; + const int32_t addr; + const int32_t init; + const int32_t min; + const int32_t max; + const uint8_t div; + const uint8_t fix; + void (*callback_function)(); + const char *help; +}; + #endif diff --git a/Src/util.c b/Src/util.c index 8f7b2f2..ff4eb5d 100644 --- a/Src/util.c +++ b/Src/util.c @@ -231,8 +231,139 @@ static uint8_t standstillAcv = 0; #endif #endif +enum parameters {PCTRL_MOD_REQ, + PCTRL_TYP_SEL, + PI_MOT_MAX, + PN_MOT_MAX, + PFIELD_WEAK_ENA, + PFIELD_WEAK_HI, + PFIELD_WEAK_LO, + PFIELD_WEAK_MAX}; + +parameter_entry params[] = { + //Name ,Value ptr ,EEPRM Addr ,Init ,Min ,Max ,Div ,Fix ,Callback Function ,Help text + {"CTRL_MOD_REQ" ,ADD_PARAM(ctrlModReqRaw,ctrlModReqRaw) ,0 ,CTRL_MOD_REQ ,0 ,3 ,0 ,0 ,NULL ,"Ctrl mode [0] Open [1] voltage [2] Speed [3] Torque"}, + {"CTRL_TYP_SEL" ,ADD_PARAM(rtP_Left.z_ctrlTypSel,rtP_Right.z_ctrlTypSel) ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,NULL ,"Ctrl type [0] Commutation [1] Sinusoidal [2] FOC"}, + {"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max,rtP_Right.i_max) ,1 ,I_MOT_MAX ,0 ,32000 ,A2BIT_CONV ,4 ,NULL ,"Maximum phase current [A]"}, + {"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max,rtP_Right.n_max) ,2 ,N_MOT_MAX ,0 ,32000 ,0 ,4 ,NULL ,"Maximum motor [RPM]"}, + {"FIELD_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna,rtP_Right.b_fieldWeakEna) ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,NULL ,"Enable field weakening"}, + {"FIELD_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi,rtP_Right.r_fieldWeakHi) ,0 ,FIELD_WEAK_HI ,0 ,24000 ,0 ,4 ,Input_Lim_Init ,"Field weak high [RPM]"}, + {"FIELD_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo,rtP_Right.r_fieldWeakLo) ,0 ,FIELD_WEAK_LO ,0 ,16000 ,0 ,4 ,Input_Lim_Init ,"Field weak low [RPM)"}, + {"FIEL_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax,rtP_Left.id_fieldWeakMax),0 ,FIELD_WEAK_MAX ,0 ,3200 ,A2BIT_CONV ,4 ,NULL ,"Field weak max current [A]"}, +}; + +uint8_t setValue(uint8_t index, int32_t newValue) { + int32_t value = newValue; + // Check divider + if(params[index].div){ + value *= params[index].div; + } + // Check Shift + if (params[index].fix){ + value <<= params[index].fix; + } + + if (value >= params[index].min && value <= params[index].max){ + if (*(int32_t*)params[index].value != value){ + // if value is different, beep and assign value + beepShort(8); + switch (params[index].type){ + case UINT8_T: + *(uint8_t*)params[index].value2 = *(uint8_t*)params[index].value = value; + break; + case UINT16_T: + *(uint16_t*)params[index].value2 = *(uint16_t*)params[index].value = value; + break; + case UINT32_T: + *(uint32_t*)params[index].value2 = *(uint32_t*)params[index].value = value; + break; + case INT8_T: + *(int8_t*)params[index].value2 = *(int8_t*)params[index].value = value; + break; + case INT16_T: + *(int16_t*)params[index].value2 = *(int16_t*)params[index].value = value; + break; + case INT32_T: + *(int32_t*)params[index].value2 = *(int32_t*)params[index].value = value; + break; + } + } + // Run callback function if assigned + if (params[index].callback_function) (*params[index].callback_function)(); + return 1; + }else{ + return 0; + } +} + +uint8_t initValue(uint8_t index) { + return setValue(index,(int32_t) params[index].init); +} + +uint32_t getValue(uint8_t index) { + int32_t value; + switch (params[index].type){ + case UINT8_T: + value = *(uint8_t*)params[index].value; + break; + case UINT16_T: + value = *(uint16_t*)params[index].value; + break; + case UINT32_T: + value = *(uint32_t*)params[index].value; + break; + case INT8_T: + value = *(int8_t*)params[index].value; + break; + case INT16_T: + value = *(int16_t*)params[index].value; + break; + case INT32_T: + value = *(int32_t*)params[index].value; + break; + default: + value = 0; + } + + if(params[index].div){ + value /= params[index].div; + } + if(params[index].fix){ + value >>= params[index].fix; + } + return value; +} + +void dumpValues(){ + printf("* "); + for(int index=0;index 0; len--, userCommand++) { if (*userCommand != '\n' && *userCommand != '\r') { // Do not accept 'new line' and 'carriage return' commands - printf("Command = %c\r\n", *userCommand); + //printf("Command = %c\r\n", *userCommand); // handle_input(*userCommand); // -> Create this function to handle the user commands } } diff --git a/platformio.ini b/platformio.ini index d7612db..fe5c48d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ src_dir = Src ; ; Choose one or all variants get built ; -;default_envs = VARIANT_ADC ; Variant for control via ADC input +default_envs = VARIANT_ADC ; Variant for control via ADC input ;default_envs = VARIANT_USART ; Variant for Serial control via USART3 input ;default_envs = VARIANT_NUNCHUK ; Variant for Nunchuk controlled vehicle build ;default_envs = VARIANT_PPM ; Variant for RC-Remotes with PPM-Sum signal From 0f6132a71ed75445d30b5ac62600a94f1e6fb8bb Mon Sep 17 00:00:00 2001 From: Candas1 Date: Sun, 3 Jan 2021 01:58:42 +0100 Subject: [PATCH 02/14] Renamed functions --- Inc/util.h | 7 ++-- Src/util.c | 115 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 71 insertions(+), 51 deletions(-) diff --git a/Inc/util.h b/Inc/util.h index 0519dc1..b1f096d 100644 --- a/Inc/util.h +++ b/Inc/util.h @@ -153,10 +153,11 @@ void dumpValues(); typedef struct parameter_entry_struct parameter_entry; struct parameter_entry_struct { + const uint8_t parameter_type; const char *name; - void *value; - void *value2; - const uint8_t type; + void *valueL; + void *valueR; + const uint8_t variable_type; const int32_t addr; const int32_t init; const int32_t min; diff --git a/Src/util.c b/Src/util.c index ff4eb5d..07374c5 100644 --- a/Src/util.c +++ b/Src/util.c @@ -231,6 +231,7 @@ static uint8_t standstillAcv = 0; #endif #endif +enum paramTypes {PARAMETER,VARIABLE}; enum parameters {PCTRL_MOD_REQ, PCTRL_TYP_SEL, PI_MOT_MAX, @@ -238,53 +239,65 @@ enum parameters {PCTRL_MOD_REQ, PFIELD_WEAK_ENA, PFIELD_WEAK_HI, PFIELD_WEAK_LO, - PFIELD_WEAK_MAX}; + PFIELD_WEAK_MAX, + PPHASE_ADV_MAX, + VI_DC_LINK, + VSPEED_AVG}; parameter_entry params[] = { - //Name ,Value ptr ,EEPRM Addr ,Init ,Min ,Max ,Div ,Fix ,Callback Function ,Help text - {"CTRL_MOD_REQ" ,ADD_PARAM(ctrlModReqRaw,ctrlModReqRaw) ,0 ,CTRL_MOD_REQ ,0 ,3 ,0 ,0 ,NULL ,"Ctrl mode [0] Open [1] voltage [2] Speed [3] Torque"}, - {"CTRL_TYP_SEL" ,ADD_PARAM(rtP_Left.z_ctrlTypSel,rtP_Right.z_ctrlTypSel) ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,NULL ,"Ctrl type [0] Commutation [1] Sinusoidal [2] FOC"}, - {"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max,rtP_Right.i_max) ,1 ,I_MOT_MAX ,0 ,32000 ,A2BIT_CONV ,4 ,NULL ,"Maximum phase current [A]"}, - {"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max,rtP_Right.n_max) ,2 ,N_MOT_MAX ,0 ,32000 ,0 ,4 ,NULL ,"Maximum motor [RPM]"}, - {"FIELD_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna,rtP_Right.b_fieldWeakEna) ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,NULL ,"Enable field weakening"}, - {"FIELD_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi,rtP_Right.r_fieldWeakHi) ,0 ,FIELD_WEAK_HI ,0 ,24000 ,0 ,4 ,Input_Lim_Init ,"Field weak high [RPM]"}, - {"FIELD_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo,rtP_Right.r_fieldWeakLo) ,0 ,FIELD_WEAK_LO ,0 ,16000 ,0 ,4 ,Input_Lim_Init ,"Field weak low [RPM)"}, - {"FIEL_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax,rtP_Left.id_fieldWeakMax),0 ,FIELD_WEAK_MAX ,0 ,3200 ,A2BIT_CONV ,4 ,NULL ,"Field weak max current [A]"}, + // Type ,Name ,Value ptr ,EEPRM Addr ,Init ,Min ,Max ,Div ,Fix ,Callback Function ,Help text + {PARAMETER ,"CTRL_MOD_REQ" ,ADD_PARAM(ctrlModReqRaw,ctrlModReqRaw) ,0 ,CTRL_MOD_REQ ,0 ,3 ,0 ,0 ,NULL ,"Ctrl mode [0] Open [1] voltage [2] Speed [3] Torque"}, + {PARAMETER ,"CTRL_TYP_SEL" ,ADD_PARAM(rtP_Left.z_ctrlTypSel,rtP_Right.z_ctrlTypSel) ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,NULL ,"Ctrl type [0] Commutation [1] Sinusoidal [2] FOC"}, + {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max,rtP_Right.i_max) ,1 ,I_MOT_MAX ,0 ,40 ,A2BIT_CONV ,4 ,NULL ,"Maximum phase current [A]"}, + {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max,rtP_Right.n_max) ,2 ,N_MOT_MAX ,0 ,2000 ,0 ,4 ,NULL ,"Maximum motor [RPM]"}, + {PARAMETER ,"FIELD_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna,rtP_Right.b_fieldWeakEna) ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,NULL ,"Enable field weakening"}, + {PARAMETER ,"FIELD_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi,rtP_Right.r_fieldWeakHi) ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,4 ,Input_Lim_Init ,"Field weak high [RPM]"}, + {PARAMETER ,"FIELD_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo,rtP_Right.r_fieldWeakLo) ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,4 ,Input_Lim_Init ,"Field weak low [RPM)"}, + {PARAMETER ,"FIEL_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax,rtP_Right.id_fieldWeakMax),0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,4 ,NULL ,"Field weak max current [A](only for FOC)"}, + {PARAMETER ,"PHASE_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax,rtP_Right.a_phaAdvMax) ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,4 ,NULL ,"Maximum Phase Advance angle [Deg](only for SIN)"}, + {VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink,rtU_Right.i_DCLink) ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,NULL ,"DC Link current [A]"}, + {VARIABLE ,"SPEED_AVG" ,ADD_PARAM(speedAvg,speedAvg) ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Motor Speed Average [RPM]"}, + }; -uint8_t setValue(uint8_t index, int32_t newValue) { +uint8_t setParam(uint8_t index, int32_t newValue) { + // Only Parameters can be set + if (params[index].parameter_type == VARIABLE) return 1; + int32_t value = newValue; - // Check divider - if(params[index].div){ - value *= params[index].div; - } - // Check Shift - if (params[index].fix){ - value <<= params[index].fix; - } - + // check mean and max before conversion to internal values if (value >= params[index].min && value <= params[index].max){ - if (*(int32_t*)params[index].value != value){ - // if value is different, beep and assign value + + // Check divider + if(params[index].div){ + value *= params[index].div; + } + // Check Shift + if (params[index].fix){ + value <<= params[index].fix; + } + + if (*(int32_t*)params[index].valueL != value){ + // if value is different, beep and assign new value beepShort(8); - switch (params[index].type){ + switch (params[index].variable_type){ case UINT8_T: - *(uint8_t*)params[index].value2 = *(uint8_t*)params[index].value = value; + *(uint8_t*)params[index].valueL = *(uint8_t*)params[index].valueR = value; break; case UINT16_T: - *(uint16_t*)params[index].value2 = *(uint16_t*)params[index].value = value; + *(uint16_t*)params[index].valueL = *(uint16_t*)params[index].valueR = value; break; case UINT32_T: - *(uint32_t*)params[index].value2 = *(uint32_t*)params[index].value = value; + *(uint32_t*)params[index].valueL = *(uint32_t*)params[index].valueR = value; break; case INT8_T: - *(int8_t*)params[index].value2 = *(int8_t*)params[index].value = value; + *(int8_t*)params[index].valueL = *(int8_t*)params[index].valueR = value; break; case INT16_T: - *(int16_t*)params[index].value2 = *(int16_t*)params[index].value = value; + *(int16_t*)params[index].valueL = *(int16_t*)params[index].valueR = value; break; case INT32_T: - *(int32_t*)params[index].value2 = *(int32_t*)params[index].value = value; + *(int32_t*)params[index].valueL = *(int32_t*)params[index].valueR = value; break; } } @@ -296,30 +309,30 @@ uint8_t setValue(uint8_t index, int32_t newValue) { } } -uint8_t initValue(uint8_t index) { - return setValue(index,(int32_t) params[index].init); +uint8_t initParam(uint8_t index) { + return setParam(index,(int32_t) params[index].init); } -uint32_t getValue(uint8_t index) { +uint32_t getParam(uint8_t index) { int32_t value; - switch (params[index].type){ + switch (params[index].variable_type){ case UINT8_T: - value = *(uint8_t*)params[index].value; + value = *(uint8_t*)params[index].valueL + *(uint8_t*)params[index].valueR /2; break; case UINT16_T: - value = *(uint16_t*)params[index].value; + value = *(uint16_t*)params[index].valueL + *(uint16_t*)params[index].valueR /2; break; case UINT32_T: - value = *(uint32_t*)params[index].value; + value = *(uint32_t*)params[index].valueL + *(uint32_t*)params[index].valueR /2; break; case INT8_T: - value = *(int8_t*)params[index].value; + value = *(int8_t*)params[index].valueL + *(int8_t*)params[index].valueR /2; break; case INT16_T: - value = *(int16_t*)params[index].value; + value = *(int16_t*)params[index].valueL + *(int16_t*)params[index].valueR /2; break; case INT32_T: - value = *(int32_t*)params[index].value; + value = *(int32_t*)params[index].valueL + *(int32_t*)params[index].valueR /2; break; default: value = 0; @@ -334,24 +347,30 @@ uint32_t getValue(uint8_t index) { return value; } -void dumpValues(){ - printf("* "); +void dumpParamValues(){ + printf("*"); for(int index=0;index Date: Sun, 3 Jan 2021 02:12:33 +0100 Subject: [PATCH 03/14] Fix --- Inc/util.h | 4 +++- Src/util.c | 23 ++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Inc/util.h b/Inc/util.h index b1f096d..9e9e2ff 100644 --- a/Inc/util.h +++ b/Inc/util.h @@ -149,7 +149,9 @@ uint8_t initValue(uint8_t index); uint32_t getValue(uint8_t index); uint8_t incrValue(uint8_t index); uint8_t saveValue(uint8_t index); -void dumpValues(); +void saveAllParams(); +void dumpParamValues(); +void dumpParameters(); typedef struct parameter_entry_struct parameter_entry; struct parameter_entry_struct { diff --git a/Src/util.c b/Src/util.c index 07374c5..6cf2220 100644 --- a/Src/util.c +++ b/Src/util.c @@ -262,7 +262,7 @@ parameter_entry params[] = { uint8_t setParam(uint8_t index, int32_t newValue) { // Only Parameters can be set - if (params[index].parameter_type == VARIABLE) return 1; + if (params[index].parameter_type == VARIABLE) return 0; int32_t value = newValue; // check mean and max before conversion to internal values @@ -310,6 +310,8 @@ uint8_t setParam(uint8_t index, int32_t newValue) { } uint8_t initParam(uint8_t index) { + // Only Parameters can be initialized + if (params[index].parameter_type == VARIABLE) return 0; return setParam(index,(int32_t) params[index].init); } @@ -362,6 +364,9 @@ void dumpParameters(){ } uint8_t incrParam(uint8_t index) { + // Only Parameters can be set + if (params[index].parameter_type == VARIABLE) return 0; + uint32_t value = getParam(index); if (value < params[index].max){ return setParam(index,value + 1); @@ -371,16 +376,28 @@ uint8_t incrParam(uint8_t index) { } uint8_t saveParam(uint8_t index) { - uint32_t value = getValue(index); + // Only Parameters can be saved to EEPROM + if (params[index].parameter_type == VARIABLE) return 0; + if (params[index].addr){ HAL_FLASH_Unlock(); - EE_WriteVariable(VirtAddVarTab[params[index].addr] , (uint16_t)value); + EE_WriteVariable(VirtAddVarTab[params[index].addr] , (uint16_t)getValue(index)); HAL_FLASH_Lock(); return 1; } return 0; } +void saveAllParams() { + HAL_FLASH_Unlock(); + for(int index=0;index Date: Sun, 3 Jan 2021 12:19:16 +0100 Subject: [PATCH 04/14] Improvements --- Inc/util.h | 24 +++--- Src/util.c | 216 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 150 insertions(+), 90 deletions(-) diff --git a/Inc/util.h b/Inc/util.h index 9e9e2ff..8018584 100644 --- a/Inc/util.h +++ b/Inc/util.h @@ -142,24 +142,24 @@ enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT}; float: FLOAT) #define PARAM_SIZE(param) sizeof(param) / sizeof(parameter_entry) -#define ADD_PARAM(value_var,value_var2) &value_var,&value_var2,typename(value_var) +#define ADD_PARAM(var) typename(var),&var -uint8_t setValue(uint8_t index, int32_t newValue); -uint8_t initValue(uint8_t index); -uint32_t getValue(uint8_t index); -uint8_t incrValue(uint8_t index); -uint8_t saveValue(uint8_t index); -void saveAllParams(); -void dumpParamValues(); -void dumpParameters(); +uint8_t setParamVal(uint8_t index, int32_t newValue); +uint8_t initParamVal(uint8_t index); +uint32_t getParamVal(uint8_t index); +uint8_t incrParamVal(uint8_t index); +uint8_t saveParamVal(uint8_t index); +void saveAllParamVal(); +void dumpParamVal(); +void dumpParamDef(); typedef struct parameter_entry_struct parameter_entry; struct parameter_entry_struct { - const uint8_t parameter_type; + const uint8_t type; const char *name; - void *valueL; + const uint8_t datatype; + void *valueL; void *valueR; - const uint8_t variable_type; const int32_t addr; const int32_t init; const int32_t min; diff --git a/Src/util.c b/Src/util.c index 6cf2220..3c0482c 100644 --- a/Src/util.c +++ b/Src/util.c @@ -232,6 +232,7 @@ static uint8_t standstillAcv = 0; #endif enum paramTypes {PARAMETER,VARIABLE}; +// Keywords to match with param index enum parameters {PCTRL_MOD_REQ, PCTRL_TYP_SEL, PI_MOT_MAX, @@ -245,59 +246,68 @@ enum parameters {PCTRL_MOD_REQ, VSPEED_AVG}; parameter_entry params[] = { - // Type ,Name ,Value ptr ,EEPRM Addr ,Init ,Min ,Max ,Div ,Fix ,Callback Function ,Help text - {PARAMETER ,"CTRL_MOD_REQ" ,ADD_PARAM(ctrlModReqRaw,ctrlModReqRaw) ,0 ,CTRL_MOD_REQ ,0 ,3 ,0 ,0 ,NULL ,"Ctrl mode [0] Open [1] voltage [2] Speed [3] Torque"}, - {PARAMETER ,"CTRL_TYP_SEL" ,ADD_PARAM(rtP_Left.z_ctrlTypSel,rtP_Right.z_ctrlTypSel) ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,NULL ,"Ctrl type [0] Commutation [1] Sinusoidal [2] FOC"}, - {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max,rtP_Right.i_max) ,1 ,I_MOT_MAX ,0 ,40 ,A2BIT_CONV ,4 ,NULL ,"Maximum phase current [A]"}, - {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max,rtP_Right.n_max) ,2 ,N_MOT_MAX ,0 ,2000 ,0 ,4 ,NULL ,"Maximum motor [RPM]"}, - {PARAMETER ,"FIELD_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna,rtP_Right.b_fieldWeakEna) ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,NULL ,"Enable field weakening"}, - {PARAMETER ,"FIELD_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi,rtP_Right.r_fieldWeakHi) ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,4 ,Input_Lim_Init ,"Field weak high [RPM]"}, - {PARAMETER ,"FIELD_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo,rtP_Right.r_fieldWeakLo) ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,4 ,Input_Lim_Init ,"Field weak low [RPM)"}, - {PARAMETER ,"FIEL_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax,rtP_Right.id_fieldWeakMax),0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,4 ,NULL ,"Field weak max current [A](only for FOC)"}, - {PARAMETER ,"PHASE_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax,rtP_Right.a_phaAdvMax) ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,4 ,NULL ,"Maximum Phase Advance angle [Deg](only for SIN)"}, - {VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink,rtU_Right.i_DCLink) ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,NULL ,"DC Link current [A]"}, - {VARIABLE ,"SPEED_AVG" ,ADD_PARAM(speedAvg,speedAvg) ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Motor Speed Average [RPM]"}, - + // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Fix ,Callback Function ,Help text + {PARAMETER ,"CTRL_MOD_REQ" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,1 ,3 ,0 ,0 ,NULL ,"Ctrl mode [1] voltage [2] Speed [3] Torque"}, + {PARAMETER ,"CTRL_TYP_SEL" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,NULL ,"Ctrl type [0] Commutation [1] Sinusoidal [2] FOC"}, + {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,0 ,40 ,A2BIT_CONV ,4 ,NULL ,"Maximum phase current [A]"}, + {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,0 ,2000 ,0 ,4 ,NULL ,"Maximum motor [RPM]"}, + {PARAMETER ,"FIELD_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,NULL ,"Enable field weakening"}, + {PARAMETER ,"FIELD_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,4 ,Input_Lim_Init ,"Field weak high [RPM]"}, + {PARAMETER ,"FIELD_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,4 ,Input_Lim_Init ,"Field weak low [RPM)"}, + {PARAMETER ,"FIEL_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,4 ,NULL ,"Field weak max current [A](only for FOC)"}, + {PARAMETER ,"PHASE_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,4 ,NULL ,"Maximum Phase Advance angle [Deg](only for SIN)"}, + {VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink) ,&rtU_Right.i_DCLink ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,NULL ,"DC Link current [A]"}, + {VARIABLE ,"SPEED_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Motor Speed Average [RPM]"}, + {VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,4 ,NULL ,"Rate"}, + {VARIABLE ,"SPEED_COEFFICIENT" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,4 ,NULL ,"Speed Coefficient"}, + {VARIABLE ,"STEER_COEFFICIENT" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,4 ,NULL ,"Steer Coefficient"}, }; -uint8_t setParam(uint8_t index, int32_t newValue) { +uint8_t setParamVal(uint8_t index, int32_t newValue) { // Only Parameters can be set - if (params[index].parameter_type == VARIABLE) return 0; + if (params[index].type == VARIABLE) return 0; int32_t value = newValue; // check mean and max before conversion to internal values if (value >= params[index].min && value <= params[index].max){ - // Check divider + // Multiply to translate to internal format if(params[index].div){ value *= params[index].div; } - // Check Shift + + // Shift to translate to internal format if (params[index].fix){ value <<= params[index].fix; } if (*(int32_t*)params[index].valueL != value){ - // if value is different, beep and assign new value + // if value is different, beep, cast and assign new value beepShort(8); - switch (params[index].variable_type){ + switch (params[index].datatype){ case UINT8_T: - *(uint8_t*)params[index].valueL = *(uint8_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(uint8_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(uint8_t*)params[index].valueR = value; break; case UINT16_T: - *(uint16_t*)params[index].valueL = *(uint16_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(uint16_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(uint16_t*)params[index].valueR = value; break; case UINT32_T: - *(uint32_t*)params[index].valueL = *(uint32_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(uint32_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(uint32_t*)params[index].valueR = value; break; case INT8_T: - *(int8_t*)params[index].valueL = *(int8_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(int8_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(int8_t*)params[index].valueR = value; break; case INT16_T: - *(int16_t*)params[index].valueL = *(int16_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(int16_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(int16_t*)params[index].valueR = value; break; case INT32_T: - *(int32_t*)params[index].valueL = *(int32_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(int32_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(int32_t*)params[index].valueR = value; break; } } @@ -309,90 +319,140 @@ uint8_t setParam(uint8_t index, int32_t newValue) { } } -uint8_t initParam(uint8_t index) { - // Only Parameters can be initialized - if (params[index].parameter_type == VARIABLE) return 0; - return setParam(index,(int32_t) params[index].init); +uint32_t getParamVal(uint8_t index) { + int32_t value = 0; + + int countVar = 0; + if (params[index].valueL != NULL) countVar++; + if (params[index].valueR != NULL) countVar++; + + if (countVar > 0){ + // Read Left and Right values and calculate average + // If left and right have to be summed up, DIV field could be adapted to multiply by 2 + // Cast to parameter datatype + switch (params[index].datatype){ + case UINT8_T: + if (params[index].valueL != NULL) value += *(uint8_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(uint8_t*)params[index].valueR; + break; + case UINT16_T: + if (params[index].valueL != NULL) value += *(uint16_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(uint16_t*)params[index].valueR; + break; + case UINT32_T: + if (params[index].valueL != NULL) value += *(uint32_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(uint32_t*)params[index].valueR; + break; + case INT8_T: + if (params[index].valueL != NULL) value += *(int8_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(int8_t*)params[index].valueR; + break; + case INT16_T: + if (params[index].valueL != NULL) value += *(int16_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(int16_t*)params[index].valueR; + break; + case INT32_T: + if (params[index].valueL != NULL) value += *(int32_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(int32_t*)params[index].valueR; + break; + default: + value = 0; + } + + // Divide by number of values provided for the parameter + value /= countVar; + + // Divide to translate to external format + if(params[index].div){ + value /= params[index].div; + } + + // Shift to translate to external format + if(params[index].fix){ + value >>= params[index].fix; + } + + return value; + }else{ + // No variable was provided, return init value that might contain a macro + return params[index].init; + } + + return 0; + } -uint32_t getParam(uint8_t index) { - int32_t value; - switch (params[index].variable_type){ - case UINT8_T: - value = *(uint8_t*)params[index].valueL + *(uint8_t*)params[index].valueR /2; - break; - case UINT16_T: - value = *(uint16_t*)params[index].valueL + *(uint16_t*)params[index].valueR /2; - break; - case UINT32_T: - value = *(uint32_t*)params[index].valueL + *(uint32_t*)params[index].valueR /2; - break; - case INT8_T: - value = *(int8_t*)params[index].valueL + *(int8_t*)params[index].valueR /2; - break; - case INT16_T: - value = *(int16_t*)params[index].valueL + *(int16_t*)params[index].valueR /2; - break; - case INT32_T: - value = *(int32_t*)params[index].valueL + *(int32_t*)params[index].valueR /2; - break; - default: - value = 0; - } - - if(params[index].div){ - value /= params[index].div; - } - if(params[index].fix){ - value >>= params[index].fix; - } - return value; -} - -void dumpParamValues(){ +void dumpParamVal(){ printf("*"); for(int index=0;index Date: Sun, 3 Jan 2021 12:20:52 +0100 Subject: [PATCH 05/14] FIx --- Src/util.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Src/util.c b/Src/util.c index 3c0482c..61914aa 100644 --- a/Src/util.c +++ b/Src/util.c @@ -232,6 +232,7 @@ static uint8_t standstillAcv = 0; #endif enum paramTypes {PARAMETER,VARIABLE}; + // Keywords to match with param index enum parameters {PCTRL_MOD_REQ, PCTRL_TYP_SEL, @@ -243,7 +244,11 @@ enum parameters {PCTRL_MOD_REQ, PFIELD_WEAK_MAX, PPHASE_ADV_MAX, VI_DC_LINK, - VSPEED_AVG}; + VSPEED_AVG, + VRATE, + VSPEED_COEFFICIENT, + VSTEER_COEFFICIENT, + }; parameter_entry params[] = { // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Fix ,Callback Function ,Help text From 7a6dc017a624d84b171e9fb14ac2dcb36c120dcb Mon Sep 17 00:00:00 2001 From: Candas1 Date: Tue, 5 Jan 2021 12:40:30 +0100 Subject: [PATCH 06/14] Improvements --- Inc/comms.h | 100 +++++++++++ Inc/config.h | 1 + Inc/util.h | 43 ----- Src/comms.c | 458 +++++++++++++++++++++++++++++++++++++++++++++++++ Src/main.c | 25 +-- Src/util.c | 250 ++------------------------- platformio.ini | 2 +- 7 files changed, 587 insertions(+), 292 deletions(-) create mode 100644 Inc/comms.h create mode 100644 Src/comms.c diff --git a/Inc/comms.h b/Inc/comms.h new file mode 100644 index 0000000..0de28dc --- /dev/null +++ b/Inc/comms.h @@ -0,0 +1,100 @@ +/** + * This file is part of the hoverboard-firmware-hack project. + * + * Copyright (C) 2020-2021 Emanuel FERU + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +// Define to prevent recursive inclusion +#ifndef COMMS_H +#define COMMS_H + +#include "stm32f1xx_hal.h" + + +enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT}; +#define typename(x) _Generic((x), \ + uint8_t: UINT8_T, \ + uint16_t: UINT16_T, \ + uint32_t: UINT32_T, \ + int8_t: INT8_T, \ + int16_t: INT16_T, \ + int32_t: INT32_T, \ + int: INT, \ + float: FLOAT) + +#define PARAM_SIZE(param) sizeof(param) / sizeof(parameter_entry) +#define COMMAND_SIZE(command) sizeof(command) / sizeof(command_entry) + +#define SIZEP(x) ((char*)(&(x) + 1) - (char*)&(x)) +#define ADD_PARAM(var) typename(var),&var + + +int32_t ExtToInt(uint8_t index,int32_t value); +int8_t setParamValInt(uint8_t index, int32_t newValue); +int8_t setParamValExt(uint8_t index, int32_t newValue); +int32_t IntToExt(uint8_t index,int32_t value); +int32_t getParamValInt(uint8_t index); +int32_t getParamValExt(uint8_t index); + +int8_t initParamVal(uint8_t index); +int8_t incrParamVal(uint8_t index); + +int8_t saveParamVal(uint8_t index); +int16_t getParamInitInt(uint8_t index); +int32_t getParamInitExt(uint8_t index); +int8_t printCommandHelp(uint8_t index); +int8_t printParamHelp(uint8_t index); +int8_t printAllParamHelp(); +int8_t printParamVal(); +int8_t printParamDef(uint8_t index); +int8_t printAllParamDef(); +int8_t watchParamVal(uint8_t index); + +int8_t findCommand(uint8_t *userCommand, uint32_t len); +int8_t findParam(uint8_t *userCommand, uint32_t len); +void handle_input(uint8_t *userCommand, uint32_t len); + + +typedef struct command_entry_struct command_entry; +struct command_entry_struct { + const char *name; + int8_t (*callback_function0)(); + int8_t (*callback_function1)(uint8_t index); + int8_t (*callback_function2)(uint8_t index,int32_t value); + const char *help; +}; + +typedef struct parameter_entry_struct parameter_entry; +struct parameter_entry_struct { + const uint8_t type; + const char *name; + const uint8_t datatype; + void *valueL; + void *valueR; + const int32_t addr; + const int32_t init; + const int32_t min; + const int32_t max; + const uint8_t div; + const uint8_t mul; + const uint8_t fix; + void (*callback_function)(); + uint8_t watch; + const char *help; +}; + + +#endif \ No newline at end of file diff --git a/Inc/config.h b/Inc/config.h index 45b2a9d..0a7ace9 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -291,6 +291,7 @@ #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! #endif + #define DEBUG_SERIAL_PROTOCOL // #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2! // #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3! #endif diff --git a/Inc/util.h b/Inc/util.h index 8018584..714ef86 100644 --- a/Inc/util.h +++ b/Inc/util.h @@ -127,48 +127,5 @@ typedef struct { } MultipleTap; void multipleTapDet(int16_t u, uint32_t timeNow, MultipleTap *x); -#define SIZEP(x) ((char*)(&(x) + 1) - (char*)&(x)) - - -enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT}; -#define typename(x) _Generic((x), \ - uint8_t: UINT8_T, \ - uint16_t: UINT16_T, \ - uint32_t: UINT32_T, \ - int8_t: INT8_T, \ - int16_t: INT16_T, \ - int32_t: INT32_T, \ - int: INT, \ - float: FLOAT) - -#define PARAM_SIZE(param) sizeof(param) / sizeof(parameter_entry) -#define ADD_PARAM(var) typename(var),&var - -uint8_t setParamVal(uint8_t index, int32_t newValue); -uint8_t initParamVal(uint8_t index); -uint32_t getParamVal(uint8_t index); -uint8_t incrParamVal(uint8_t index); -uint8_t saveParamVal(uint8_t index); -void saveAllParamVal(); -void dumpParamVal(); -void dumpParamDef(); - -typedef struct parameter_entry_struct parameter_entry; -struct parameter_entry_struct { - const uint8_t type; - const char *name; - const uint8_t datatype; - void *valueL; - void *valueR; - const int32_t addr; - const int32_t init; - const int32_t min; - const int32_t max; - const uint8_t div; - const uint8_t fix; - void (*callback_function)(); - const char *help; -}; - #endif diff --git a/Src/comms.c b/Src/comms.c new file mode 100644 index 0000000..0fc1f4d --- /dev/null +++ b/Src/comms.c @@ -0,0 +1,458 @@ +/** + * This file is part of the hoverboard-firmware-hack project. + * + * Copyright (C) 2020-2021 Emanuel FERU + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + + +// Includes +#include +#include "stm32f1xx_hal.h" +#include "config.h" +#include "defines.h" +#include "eeprom.h" +#include "BLDC_controller.h" +#include "util.h" +#include "comms.h" + +#if defined(DEBUG_SERIAL_PROTOCOL) + +extern ExtY rtY_Left; /* External outputs */ +extern ExtU rtU_Left; /* External inputs */ +extern P rtP_Left; + +extern ExtY rtY_Right; /* External outputs */ +extern ExtU rtU_Right; /* External inputs */ +extern P rtP_Right; + +extern uint16_t VirtAddVarTab[NB_OF_VAR]; +extern int16_t speedAvg; // average measured speed +extern int16_t speedAvgAbs; // average measured speed in absolute +extern uint8_t ctrlModReqRaw; + +// Function0 - Function with 0 parameter +// Function1 - Function with 1 parameter (e.g. GET PARAM) +// Function2 - Function with 2 parameter (e.g. SET PARAM XXXX) +command_entry commands[] = { + // Name Function0 Function1 Function2 Help + {"GET" ,printAllParamDef ,printParamDef ,NULL ,"Get Parameter/Variable Values"}, + {"SET" ,NULL ,NULL ,setParamValExt ,"Set Parameter with Value"}, + {"INIT" ,NULL ,initParamVal ,NULL ,"Init Parameter with Value from EEPROM or CONFIG.H"}, + {"SAVE" ,NULL ,saveParamVal ,NULL ,"Save Parameter Value to EEPROM"}, + {"HELP" ,printAllParamHelp ,printParamHelp ,NULL ,"Show Help"}, + {"WATCH" ,NULL ,watchParamVal ,NULL ,"Enable/Disable Watch for Parameter/Variable"}, +}; + +enum paramTypes {PARAMETER,VARIABLE}; +// Keywords to match with param index +enum parameters {PCTRL_MOD_REQ, + PCTRL_TYP_SEL, + PI_MOT_MAX, + PN_MOT_MAX, + PFIELD_WEAK_ENA, + PFIELD_WEAK_HI, + PFIELD_WEAK_LO, + PFIELD_WEAK_MAX, + PPHASE_ADV_MAX, + VI_DC_LINK, + VSPEED_AVG, + VRATE, + VSPEED_COEF, + VSTEER_COEF, + }; + +parameter_entry params[] = { + // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Watch ,Help text + {PARAMETER ,"CTRL_MOD_REQ" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,1 ,3 ,0 ,0 ,0 ,NULL ,0 ,"Ctrl mode [1] voltage [2] Speed [3] Torque"}, + {PARAMETER ,"CTRL_TYP_SEL" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,0 ,NULL ,0 ,"Ctrl type [0] Commutation [1] Sinusoidal [2] FOC"}, + {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,1 ,40 ,A2BIT_CONV ,0 ,4 ,NULL ,0 ,"Maximum phase current [A]"}, + {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,10 ,2000 ,0 ,0 ,4 ,NULL ,0 ,"Maximum motor [RPM]"}, + {PARAMETER ,"FIELD_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,0 ,NULL ,0 ,"Enable field weakening"}, + {PARAMETER ,"FIELD_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,0 ,4 ,Input_Lim_Init ,0 ,"Field weak high [RPM]"}, + {PARAMETER ,"FIELD_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,0 ,4 ,Input_Lim_Init ,0 ,"Field weak low [RPM)"}, + {PARAMETER ,"FIELD_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,0 ,4 ,NULL ,0 ,"Field weak max current [A](only for FOC)"}, + {PARAMETER ,"PHASE_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,0 ,4 ,NULL ,0 ,"Maximum Phase Advance angle [Deg](only for SIN)"}, + {VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink) ,&rtU_Right.i_DCLink ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,0 ,NULL ,0 ,"DC Link current [A]"}, + {VARIABLE ,"SPEED_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Motor Measured Average Speed [RPM]"}, + {VARIABLE ,"SPEEDL" ,ADD_PARAM(rtY_Left.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Left Motor Measured Speed [RPM]"}, + {VARIABLE ,"SPEEDR" ,ADD_PARAM(rtY_Right.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Right Motor Measured Speed [RPM]"}, + {VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,0 ,4 ,NULL ,0 ,"Rate *10"}, + {VARIABLE ,"SPEED_COEF" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,0 ,"Speed Coefficient *10"}, + {VARIABLE ,"STEER_COEF" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,0 ,"Steer Coefficient *10"}, + //{VARIABLE ,"BATV" ,ADD_PARAM(adc_buffer.batt1) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Battery voltage [V]*100"}, + //{VARIABLE ,"TEMP" ,ADD_PARAM(board_temp_deg_c) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Temperature [°C]*10"}, + +}; + +// Translate from External format to Internal Format +int32_t ExtToInt(uint8_t index,int32_t value){ + // Multiply to translate to internal format + if(params[index].div) value *= params[index].div; + // Shift to translate to internal format + if (params[index].fix) value <<= params[index].fix; + // Divide for small number + if(params[index].mul)value /= params[index].mul; + return value; +} + +// Set Param with Value from external format +int8_t setParamValExt(uint8_t index, int32_t value) { + // Only Parameters can be set + if (params[index].type == VARIABLE){ + printf("! A variable cannot be SET\r\n"); + return 0; + } + + // check min and max before conversion to internal values + if (IN_RANGE(value,params[index].min,params[index].max)){ + return setParamValInt(index,ExtToInt(index,value)); + }else{ + printf("! Value %li not in range [min:%li max:%li]\r\n",value,params[index].min,params[index].max); + return 0; + } +} + +// Set Param with value from internal format +int8_t setParamValInt(uint8_t index, int32_t newValue) { + int32_t value = newValue; + if (*(int32_t*)params[index].valueL != value){ + // if value is different, beep, cast and assign new value + switch (params[index].datatype){ + case UINT8_T: + if (params[index].valueL != NULL) *(uint8_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(uint8_t*)params[index].valueR = value; + break; + case UINT16_T: + if (params[index].valueL != NULL) *(uint16_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(uint16_t*)params[index].valueR = value; + break; + case UINT32_T: + if (params[index].valueL != NULL) *(uint32_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(uint32_t*)params[index].valueR = value; + break; + case INT8_T: + if (params[index].valueL != NULL) *(int8_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(int8_t*)params[index].valueR = value; + break; + case INT16_T: + if (params[index].valueL != NULL) *(int16_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(int16_t*)params[index].valueR = value; + break; + case INT32_T: + if (params[index].valueL != NULL) *(int32_t*)params[index].valueL = value; + if (params[index].valueR != NULL) *(int32_t*)params[index].valueR = value; + break; + } + } + + // Run callback function if assigned + if (params[index].callback_function) (*params[index].callback_function)(); + return 1; +} + +// Get Parameter Internal value and translate to external +int32_t getParamValExt(uint8_t index) { + return IntToExt(index,getParamValInt(index)); +} + +// Get Parameter Internal Value +int32_t getParamValInt(uint8_t index) { + int32_t value = 0; + + int countVar = 0; + if (params[index].valueL != NULL) countVar++; + if (params[index].valueR != NULL) countVar++; + + if (countVar > 0){ + // Read Left and Right values and calculate average + // If left and right have to be summed up, DIV field could be adapted to multiply by 2 + // Cast to parameter datatype + switch (params[index].datatype){ + case UINT8_T: + if (params[index].valueL != NULL) value += *(uint8_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(uint8_t*)params[index].valueR; + break; + case UINT16_T: + if (params[index].valueL != NULL) value += *(uint16_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(uint16_t*)params[index].valueR; + break; + case UINT32_T: + if (params[index].valueL != NULL) value += *(uint32_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(uint32_t*)params[index].valueR; + break; + case INT8_T: + if (params[index].valueL != NULL) value += *(int8_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(int8_t*)params[index].valueR; + break; + case INT16_T: + if (params[index].valueL != NULL) value += *(int16_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(int16_t*)params[index].valueR; + break; + case INT32_T: + if (params[index].valueL != NULL) value += *(int32_t*)params[index].valueL; + if (params[index].valueR != NULL) value += *(int32_t*)params[index].valueR; + break; + default: + value = 0; + } + + // Divide by number of values provided for the parameter + value /= countVar; + }else{ + // No variable was provided, return init value that might contain a macro + value = params[index].init; + } + + return value; +} + + +// Set watch flag for parameter +int8_t watchParamVal(uint8_t index){ + params[index].watch = (params[index].watch==0); + return 1; +} + +// Print value for all parameters with watch flag +int8_t printParamVal(){ + int count = 0; + for(int i=0;i>= params[index].fix; + return value; +} + +int32_t getParamInitExt(uint8_t index) { + int32_t value = 0; + value = IntToExt(index,getParamInitInt(index)); + return value; +} + +// Get Parameter value with EEprom data if address is avalaible, init value otherwise +int16_t getParamInitInt(uint8_t index) { + if (params[index].addr){ + // if EEPROM address is specified, init from EEPROM address + uint16_t readEEPROMVal; + HAL_FLASH_Unlock(); + EE_ReadVariable(VirtAddVarTab[params[index].addr] , &readEEPROMVal); + HAL_FLASH_Lock(); + return readEEPROMVal; + }else{ + // Initialize from param array + return params[index].init; + } +} + + +// initialize Parameter value with EEprom data if address is avalaible, init value otherwise +int8_t initParamVal(uint8_t index) { + // Only Parameters can be loaded from EEPROM + if (params[index].type == VARIABLE) return 0; + + return setParamValInt(index,(int32_t) getParamInitInt(index)); +} + +// Find parameter in params array and return index +int8_t findParam(uint8_t *userCommand, uint32_t len){ + for(int index=0;indexMAX_int16_T){printf("! Value not in range\r\n");return;} + } + + if (count == 0){ + printf("! Value required\r\n"); + return; + } + + // Apply sign + value*= sign; + + if (commands[cindex].callback_function2 != NULL){ + // This function needs an additional parameter + ret = (*commands[cindex].callback_function2)(pindex,value); + if (ret==1){printf("OK\r\n");} + } + +} + +#endif \ No newline at end of file diff --git a/Src/main.c b/Src/main.c index 3f8c1d4..2ff55d1 100644 --- a/Src/main.c +++ b/Src/main.c @@ -29,6 +29,7 @@ #include "util.h" #include "BLDC_controller.h" /* BLDC's header file */ #include "rtwtypes.h" +#include "comms.h" #if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD) #include "hd44780.h" @@ -420,16 +421,20 @@ int main(void) { // ####### DEBUG SERIAL OUT ####### #if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3) - if (main_loop_counter % 25 == 0) { // Send data periodically every 125 ms - printf("in1:%i in2:%i cmdL:%i cmdR:%i BatADC:%i BatV:%i TempADC:%i Temp:%i\r\n", - input1[inIdx].raw, // 1: INPUT1 - input2[inIdx].raw, // 2: INPUT2 - cmdL, // 3: output command: [-1000, 1000] - cmdR, // 4: output command: [-1000, 1000] - adc_buffer.batt1, // 5: for battery voltage calibration - batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC, // 6: for verifying battery voltage calibration - board_temp_adcFilt, // 7: for board temperature calibration - board_temp_deg_c); // 8: for verifying board temperature calibration + if (main_loop_counter % 25 == 0) { // Send data periodically every 125 ms + #ifndef DEBUG_SERIAL_PROTOCOL + printf("in1:%i in2:%i cmdL:%i cmdR:%i BatADC:%i BatV:%i TempADC:%i Temp:%i\r\n", + input1[inIdx].raw, // 1: INPUT1 + input2[inIdx].raw, // 2: INPUT2 + cmdL, // 3: output command: [-1000, 1000] + cmdR, // 4: output command: [-1000, 1000] + adc_buffer.batt1, // 5: for battery voltage calibration + batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC, // 6: for verifying battery voltage calibration + board_temp_adcFilt, // 7: for board temperature calibration + board_temp_deg_c); // 8: for verifying board temperature calibration + #else + printParamVal(); + #endif } #endif diff --git a/Src/util.c b/Src/util.c index 61914aa..5f8bf88 100644 --- a/Src/util.c +++ b/Src/util.c @@ -29,6 +29,7 @@ #include "util.h" #include "BLDC_controller.h" #include "rtwtypes.h" +#include "comms.h" #if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD) #include "hd44780.h" @@ -231,239 +232,6 @@ static uint8_t standstillAcv = 0; #endif #endif -enum paramTypes {PARAMETER,VARIABLE}; - -// Keywords to match with param index -enum parameters {PCTRL_MOD_REQ, - PCTRL_TYP_SEL, - PI_MOT_MAX, - PN_MOT_MAX, - PFIELD_WEAK_ENA, - PFIELD_WEAK_HI, - PFIELD_WEAK_LO, - PFIELD_WEAK_MAX, - PPHASE_ADV_MAX, - VI_DC_LINK, - VSPEED_AVG, - VRATE, - VSPEED_COEFFICIENT, - VSTEER_COEFFICIENT, - }; - -parameter_entry params[] = { - // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Fix ,Callback Function ,Help text - {PARAMETER ,"CTRL_MOD_REQ" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,1 ,3 ,0 ,0 ,NULL ,"Ctrl mode [1] voltage [2] Speed [3] Torque"}, - {PARAMETER ,"CTRL_TYP_SEL" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,NULL ,"Ctrl type [0] Commutation [1] Sinusoidal [2] FOC"}, - {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,0 ,40 ,A2BIT_CONV ,4 ,NULL ,"Maximum phase current [A]"}, - {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,0 ,2000 ,0 ,4 ,NULL ,"Maximum motor [RPM]"}, - {PARAMETER ,"FIELD_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,NULL ,"Enable field weakening"}, - {PARAMETER ,"FIELD_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,4 ,Input_Lim_Init ,"Field weak high [RPM]"}, - {PARAMETER ,"FIELD_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,4 ,Input_Lim_Init ,"Field weak low [RPM)"}, - {PARAMETER ,"FIEL_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,4 ,NULL ,"Field weak max current [A](only for FOC)"}, - {PARAMETER ,"PHASE_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,4 ,NULL ,"Maximum Phase Advance angle [Deg](only for SIN)"}, - {VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink) ,&rtU_Right.i_DCLink ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,NULL ,"DC Link current [A]"}, - {VARIABLE ,"SPEED_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Motor Speed Average [RPM]"}, - {VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,4 ,NULL ,"Rate"}, - {VARIABLE ,"SPEED_COEFFICIENT" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,4 ,NULL ,"Speed Coefficient"}, - {VARIABLE ,"STEER_COEFFICIENT" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,4 ,NULL ,"Steer Coefficient"}, -}; - -uint8_t setParamVal(uint8_t index, int32_t newValue) { - // Only Parameters can be set - if (params[index].type == VARIABLE) return 0; - - int32_t value = newValue; - // check mean and max before conversion to internal values - if (value >= params[index].min && value <= params[index].max){ - - // Multiply to translate to internal format - if(params[index].div){ - value *= params[index].div; - } - - // Shift to translate to internal format - if (params[index].fix){ - value <<= params[index].fix; - } - - if (*(int32_t*)params[index].valueL != value){ - // if value is different, beep, cast and assign new value - beepShort(8); - switch (params[index].datatype){ - case UINT8_T: - if (params[index].valueL != NULL) *(uint8_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(uint8_t*)params[index].valueR = value; - break; - case UINT16_T: - if (params[index].valueL != NULL) *(uint16_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(uint16_t*)params[index].valueR = value; - break; - case UINT32_T: - if (params[index].valueL != NULL) *(uint32_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(uint32_t*)params[index].valueR = value; - break; - case INT8_T: - if (params[index].valueL != NULL) *(int8_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(int8_t*)params[index].valueR = value; - break; - case INT16_T: - if (params[index].valueL != NULL) *(int16_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(int16_t*)params[index].valueR = value; - break; - case INT32_T: - if (params[index].valueL != NULL) *(int32_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(int32_t*)params[index].valueR = value; - break; - } - } - // Run callback function if assigned - if (params[index].callback_function) (*params[index].callback_function)(); - return 1; - }else{ - return 0; - } -} - -uint32_t getParamVal(uint8_t index) { - int32_t value = 0; - - int countVar = 0; - if (params[index].valueL != NULL) countVar++; - if (params[index].valueR != NULL) countVar++; - - if (countVar > 0){ - // Read Left and Right values and calculate average - // If left and right have to be summed up, DIV field could be adapted to multiply by 2 - // Cast to parameter datatype - switch (params[index].datatype){ - case UINT8_T: - if (params[index].valueL != NULL) value += *(uint8_t*)params[index].valueL; - if (params[index].valueR != NULL) value += *(uint8_t*)params[index].valueR; - break; - case UINT16_T: - if (params[index].valueL != NULL) value += *(uint16_t*)params[index].valueL; - if (params[index].valueR != NULL) value += *(uint16_t*)params[index].valueR; - break; - case UINT32_T: - if (params[index].valueL != NULL) value += *(uint32_t*)params[index].valueL; - if (params[index].valueR != NULL) value += *(uint32_t*)params[index].valueR; - break; - case INT8_T: - if (params[index].valueL != NULL) value += *(int8_t*)params[index].valueL; - if (params[index].valueR != NULL) value += *(int8_t*)params[index].valueR; - break; - case INT16_T: - if (params[index].valueL != NULL) value += *(int16_t*)params[index].valueL; - if (params[index].valueR != NULL) value += *(int16_t*)params[index].valueR; - break; - case INT32_T: - if (params[index].valueL != NULL) value += *(int32_t*)params[index].valueL; - if (params[index].valueR != NULL) value += *(int32_t*)params[index].valueR; - break; - default: - value = 0; - } - - // Divide by number of values provided for the parameter - value /= countVar; - - // Divide to translate to external format - if(params[index].div){ - value /= params[index].div; - } - - // Shift to translate to external format - if(params[index].fix){ - value >>= params[index].fix; - } - - return value; - }else{ - // No variable was provided, return init value that might contain a macro - return params[index].init; - } - - return 0; - -} - -void dumpParamVal(){ - printf("*"); - for(int index=0;index 0; len--, userCommand++) { - if (*userCommand != '\n' && *userCommand != '\r') { // Do not accept 'new line' and 'carriage return' commands - //printf("Command = %c\r\n", *userCommand); - // handle_input(*userCommand); // -> Create this function to handle the user commands + + #ifndef DEBUG_SERIAL_PROTOCOL + for (; len > 0; len--, userCommand++) { + if (*userCommand != '\n' && *userCommand != '\r') { // Do not accept 'new line' and 'carriage return' commands + printf("Command = %c\r\n", *userCommand); + // handle_input(*userCommand); // -> Create this function to handle the user commands + } } - } + #else + handle_input(userCommand,len); + #endif } + #endif // SERIAL_DEBUG /* diff --git a/platformio.ini b/platformio.ini index fe5c48d..64b1a55 100644 --- a/platformio.ini +++ b/platformio.ini @@ -61,7 +61,7 @@ build_flags = -DSTM32F103xE -T./STM32F103RCTx_FLASH.ld -lc - -lm + -l -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization -D VARIANT_USART From cd40425e9950cbcbd578305a874c8db8feb1be49 Mon Sep 17 00:00:00 2001 From: Candas1 Date: Wed, 6 Jan 2021 12:54:08 +0100 Subject: [PATCH 07/14] Improvements --- Inc/comms.h | 4 +- Src/comms.c | 243 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 150 insertions(+), 97 deletions(-) diff --git a/Inc/comms.h b/Inc/comms.h index 0de28dc..4871c3f 100644 --- a/Inc/comms.h +++ b/Inc/comms.h @@ -70,6 +70,7 @@ void handle_input(uint8_t *userCommand, uint32_t len); typedef struct command_entry_struct command_entry; struct command_entry_struct { + const uint8_t type; const char *name; int8_t (*callback_function0)(); int8_t (*callback_function1)(uint8_t index); @@ -84,7 +85,7 @@ struct parameter_entry_struct { const uint8_t datatype; void *valueL; void *valueR; - const int32_t addr; + const uint16_t addr; const int32_t init; const int32_t min; const int32_t max; @@ -92,7 +93,6 @@ struct parameter_entry_struct { const uint8_t mul; const uint8_t fix; void (*callback_function)(); - uint8_t watch; const char *help; }; diff --git a/Src/comms.c b/Src/comms.c index 0fc1f4d..62f93f5 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -19,6 +19,8 @@ // Includes +#include +#include #include #include "stm32f1xx_hal.h" #include "config.h" @@ -38,65 +40,89 @@ extern ExtY rtY_Right; /* External outputs */ extern ExtU rtU_Right; /* External inputs */ extern P rtP_Right; + +extern InputStruct input1[]; // input structure +extern InputStruct input2[]; // input structure + extern uint16_t VirtAddVarTab[NB_OF_VAR]; extern int16_t speedAvg; // average measured speed extern int16_t speedAvgAbs; // average measured speed in absolute extern uint8_t ctrlModReqRaw; +extern adc_buf_t adc_buffer; +extern int16_t board_temp_deg_c; + +enum commandTypes {READ,WRITE}; // Function0 - Function with 0 parameter // Function1 - Function with 1 parameter (e.g. GET PARAM) // Function2 - Function with 2 parameter (e.g. SET PARAM XXXX) -command_entry commands[] = { - // Name Function0 Function1 Function2 Help - {"GET" ,printAllParamDef ,printParamDef ,NULL ,"Get Parameter/Variable Values"}, - {"SET" ,NULL ,NULL ,setParamValExt ,"Set Parameter with Value"}, - {"INIT" ,NULL ,initParamVal ,NULL ,"Init Parameter with Value from EEPROM or CONFIG.H"}, - {"SAVE" ,NULL ,saveParamVal ,NULL ,"Save Parameter Value to EEPROM"}, - {"HELP" ,printAllParamHelp ,printParamHelp ,NULL ,"Show Help"}, - {"WATCH" ,NULL ,watchParamVal ,NULL ,"Enable/Disable Watch for Parameter/Variable"}, +const command_entry commands[] = { + // Type ,Name ,Function0 ,Function1 ,Function2 ,Help + {READ ,"GET" ,printAllParamDef ,printParamDef ,NULL ,"Get Parameter/Variable"}, + {READ ,"HELP" ,printAllParamHelp ,printParamHelp ,NULL ,"Command/Parameter/Variable Help"}, + {READ ,"WATCH" ,NULL ,watchParamVal ,NULL ,"Toggle Parameter/Variable Watch"}, + {WRITE ,"SET" ,NULL ,NULL ,setParamValExt ,"Set Parameter"}, + {WRITE ,"INIT" ,NULL ,initParamVal ,NULL ,"Init Parameter from EEPROM or CONFIG.H"}, + {WRITE ,"SAVE" ,NULL ,saveParamVal ,NULL ,"Save Parameter to EEPROM"}, }; enum paramTypes {PARAMETER,VARIABLE}; -// Keywords to match with param index -enum parameters {PCTRL_MOD_REQ, - PCTRL_TYP_SEL, - PI_MOT_MAX, - PN_MOT_MAX, - PFIELD_WEAK_ENA, - PFIELD_WEAK_HI, - PFIELD_WEAK_LO, - PFIELD_WEAK_MAX, - PPHASE_ADV_MAX, - VI_DC_LINK, - VSPEED_AVG, - VRATE, - VSPEED_COEF, - VSTEER_COEF, - }; - -parameter_entry params[] = { - // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Watch ,Help text - {PARAMETER ,"CTRL_MOD_REQ" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,1 ,3 ,0 ,0 ,0 ,NULL ,0 ,"Ctrl mode [1] voltage [2] Speed [3] Torque"}, - {PARAMETER ,"CTRL_TYP_SEL" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,0 ,NULL ,0 ,"Ctrl type [0] Commutation [1] Sinusoidal [2] FOC"}, - {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,1 ,40 ,A2BIT_CONV ,0 ,4 ,NULL ,0 ,"Maximum phase current [A]"}, - {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,10 ,2000 ,0 ,0 ,4 ,NULL ,0 ,"Maximum motor [RPM]"}, - {PARAMETER ,"FIELD_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,0 ,NULL ,0 ,"Enable field weakening"}, - {PARAMETER ,"FIELD_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,0 ,4 ,Input_Lim_Init ,0 ,"Field weak high [RPM]"}, - {PARAMETER ,"FIELD_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,0 ,4 ,Input_Lim_Init ,0 ,"Field weak low [RPM)"}, - {PARAMETER ,"FIELD_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,0 ,4 ,NULL ,0 ,"Field weak max current [A](only for FOC)"}, - {PARAMETER ,"PHASE_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,0 ,4 ,NULL ,0 ,"Maximum Phase Advance angle [Deg](only for SIN)"}, - {VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink) ,&rtU_Right.i_DCLink ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,0 ,NULL ,0 ,"DC Link current [A]"}, - {VARIABLE ,"SPEED_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Motor Measured Average Speed [RPM]"}, - {VARIABLE ,"SPEEDL" ,ADD_PARAM(rtY_Left.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Left Motor Measured Speed [RPM]"}, - {VARIABLE ,"SPEEDR" ,ADD_PARAM(rtY_Right.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Right Motor Measured Speed [RPM]"}, - {VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,0 ,4 ,NULL ,0 ,"Rate *10"}, - {VARIABLE ,"SPEED_COEF" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,0 ,"Speed Coefficient *10"}, - {VARIABLE ,"STEER_COEF" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,0 ,"Steer Coefficient *10"}, - //{VARIABLE ,"BATV" ,ADD_PARAM(adc_buffer.batt1) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Battery voltage [V]*100"}, - //{VARIABLE ,"TEMP" ,ADD_PARAM(board_temp_deg_c) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,0 ,"Temperature [°C]*10"}, +const parameter_entry params[] = { + // CONTROL PARAMETERS + // Type ,Name ,Datatype ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text + {PARAMETER ,"CTRL_MOD" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,1 ,3 ,0 ,0 ,0 ,NULL ,"Ctrl mode 1:VLT 2:SPD 3:TRQ"}, + {PARAMETER ,"CTRL_TYP" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,0 ,NULL ,"Ctrl type 0:COM 1:SIN 2:FOC"}, + {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,1 ,40 ,A2BIT_CONV ,0 ,4 ,NULL ,"Max phase current A"}, + {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,10 ,2000 ,0 ,0 ,4 ,NULL ,"Max motor RPM"}, + {PARAMETER ,"FI_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,0 ,NULL ,"Enable field weak"}, + {PARAMETER ,"FI_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak high RPM"}, + {PARAMETER ,"FI_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak low RPM"}, + {PARAMETER ,"FI_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,0 ,4 ,NULL ,"Field weak max current A(FOC)"}, + {PARAMETER ,"PHA_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,0 ,4 ,NULL ,"Max Phase Adv angle Deg(SIN)"}, + // INPUT PARAMETERS + // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text + {PARAMETER ,"PRI_IN1_TYP" ,ADD_PARAM(input1[0].typ) ,NULL ,3 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input1 type"}, + {PARAMETER ,"PRI_IN1_MIN" ,ADD_PARAM(input1[0].min) ,NULL ,4 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 min"}, + {PARAMETER ,"PRI_IN1_MID" ,ADD_PARAM(input1[0].mid) ,NULL ,5 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 mid"}, + {PARAMETER ,"PRI_IN1_MAX" ,ADD_PARAM(input1[0].max) ,NULL ,6 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 max"}, + {PARAMETER ,"PRI_IN2_TYP" ,ADD_PARAM(input2[0].typ) ,NULL ,7 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input2 type"}, + {PARAMETER ,"PRI_IN2_MIN" ,ADD_PARAM(input2[0].min) ,NULL ,8 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 min"}, + {PARAMETER ,"PRI_IN2_MID" ,ADD_PARAM(input2[0].mid) ,NULL ,9 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 mid"}, + {PARAMETER ,"PRI_IN2_MAX" ,ADD_PARAM(input2[0].max) ,NULL ,10 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 max"}, + {VARIABLE ,"PRI_IN1_RAW" ,ADD_PARAM(input1[0].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 raw"}, + {VARIABLE ,"PRI_IN2_RAW" ,ADD_PARAM(input2[0].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 raw"}, + {VARIABLE ,"PRI_IN1_CMD" ,ADD_PARAM(input1[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 cmd"}, + {VARIABLE ,"PRI_IN2_CMD" ,ADD_PARAM(input2[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 cmd"}, +#if defined(PRI_INPUT1) && defined(PRI_INPUT2) && defined(AUX_INPUT1) && defined(AUX_INPUT2) + {PARAMETER ,"AUX_IN1_TYP" ,ADD_PARAM(input1[1].typ) ,NULL ,11 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input1 type"}, + {PARAMETER ,"AUX_IN1_MIN" ,ADD_PARAM(input1[1].min) ,NULL ,12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 min"}, + {PARAMETER ,"AUX_IN1_MID" ,ADD_PARAM(input1[1].mid) ,NULL ,13 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 mid"}, + {PARAMETER ,"AUX_IN1_MAX" ,ADD_PARAM(input1[1].max) ,NULL ,14 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 max"}, + {PARAMETER ,"AUX_IN2_TYP" ,ADD_PARAM(input2[1].typ) ,NULL ,15 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input2 type"}, + {PARAMETER ,"AUX_IN2_MIN" ,ADD_PARAM(input2[1].min) ,NULL ,16 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 min"}, + {PARAMETER ,"AUX_IN2_MID" ,ADD_PARAM(input2[1].mid) ,NULL ,17 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 mid"}, + {PARAMETER ,"AUX_IN2_MAX" ,ADD_PARAM(input2[1].max) ,NULL ,18 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 max"}, + {VARIABLE ,"AUX_IN1_RAW" ,ADD_PARAM(input1[1].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 raw"}, + {VARIABLE ,"AUX_IN2_RAW" ,ADD_PARAM(input2[1].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 raw"}, + {VARIABLE ,"AUX_IN1_CMD" ,ADD_PARAM(input1[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 cmd"}, + {VARIABLE ,"AUX_IN2_CMD" ,ADD_PARAM(input2[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 cmd"}, +#endif + // FEEDBACK + // Type ,Name ,Datatype, ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text + {VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink) ,&rtU_Right.i_DCLink ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,0 ,NULL ,"DC Link current A"}, + {VARIABLE ,"SPD_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Motor Measured Avg RPM"}, + {VARIABLE ,"SPDL" ,ADD_PARAM(rtY_Left.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Left Motor Measured RPM"}, + {VARIABLE ,"SPDR" ,ADD_PARAM(rtY_Right.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Right Motor Measured RPM"}, + {VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,0 ,4 ,NULL ,"Rate *10"}, + {VARIABLE ,"SPD_COEF" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,"Speed Coefficient *10"}, + {VARIABLE ,"STR_COEF" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,"Steer Coefficient *10"}, + {VARIABLE ,"BATV" ,ADD_PARAM(adc_buffer.batt1) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Battery voltage *100"}, + //{VARIABLE ,"TEMP" ,ADD_PARAM(board_temp_deg_c) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Temperature °C *10"}, }; +uint8_t * watchParamList; + // Translate from External format to Internal Format int32_t ExtToInt(uint8_t index,int32_t value){ // Multiply to translate to internal format @@ -104,23 +130,17 @@ int32_t ExtToInt(uint8_t index,int32_t value){ // Shift to translate to internal format if (params[index].fix) value <<= params[index].fix; // Divide for small number - if(params[index].mul)value /= params[index].mul; + if(params[index].mul) value /= params[index].mul; return value; } // Set Param with Value from external format -int8_t setParamValExt(uint8_t index, int32_t value) { - // Only Parameters can be set - if (params[index].type == VARIABLE){ - printf("! A variable cannot be SET\r\n"); - return 0; - } - +int8_t setParamValExt(uint8_t index, int32_t value) { // check min and max before conversion to internal values if (IN_RANGE(value,params[index].min,params[index].max)){ return setParamValInt(index,ExtToInt(index,value)); }else{ - printf("! Value %li not in range [min:%li max:%li]\r\n",value,params[index].min,params[index].max); + printf("! Value %li out of range [min:%li max:%li]",value,params[index].min,params[index].max); return 0; } } @@ -172,7 +192,7 @@ int32_t getParamValExt(uint8_t index) { int32_t getParamValInt(uint8_t index) { int32_t value = 0; - int countVar = 0; + int8_t countVar = 0; if (params[index].valueL != NULL) countVar++; if (params[index].valueR != NULL) countVar++; @@ -222,20 +242,31 @@ int32_t getParamValInt(uint8_t index) { // Set watch flag for parameter int8_t watchParamVal(uint8_t index){ - params[index].watch = (params[index].watch==0); + boolean_T found = 0; + uint8_t size = sizeof(watchParamList); + for(int i=0;i0) printf("\r\n"); return 1; } @@ -255,15 +286,23 @@ int8_t printParamHelp(uint8_t index){ // Print help for all parameters int8_t printAllParamHelp(){ - printf("? Commands:\r\n"); + printf("? Commands\r\n"); for(int i=0;i Date: Wed, 6 Jan 2021 22:15:29 +0100 Subject: [PATCH 08/14] Fix --- Inc/comms.h | 2 +- Src/comms.c | 74 ++++++++++++++++++++++++++--------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/Inc/comms.h b/Inc/comms.h index 4871c3f..78af21f 100644 --- a/Inc/comms.h +++ b/Inc/comms.h @@ -52,7 +52,7 @@ int32_t getParamValExt(uint8_t index); int8_t initParamVal(uint8_t index); int8_t incrParamVal(uint8_t index); -int8_t saveParamVal(uint8_t index); +int8_t saveAllParamVal(); int16_t getParamInitInt(uint8_t index); int32_t getParamInitExt(uint8_t index); int8_t printCommandHelp(uint8_t index); diff --git a/Src/comms.c b/Src/comms.c index 62f93f5..acc40ac 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -63,7 +63,7 @@ const command_entry commands[] = { {READ ,"WATCH" ,NULL ,watchParamVal ,NULL ,"Toggle Parameter/Variable Watch"}, {WRITE ,"SET" ,NULL ,NULL ,setParamValExt ,"Set Parameter"}, {WRITE ,"INIT" ,NULL ,initParamVal ,NULL ,"Init Parameter from EEPROM or CONFIG.H"}, - {WRITE ,"SAVE" ,NULL ,saveParamVal ,NULL ,"Save Parameter to EEPROM"}, + {WRITE ,"SAVE" ,saveAllParamVal ,NULL ,NULL ,"Save Parameters to EEPROM"}, }; enum paramTypes {PARAMETER,VARIABLE}; @@ -122,6 +122,7 @@ const parameter_entry params[] = { }; uint8_t * watchParamList; +uint8_t watchParamListSize = 0; // Translate from External format to Internal Format int32_t ExtToInt(uint8_t index,int32_t value){ @@ -136,13 +137,15 @@ int32_t ExtToInt(uint8_t index,int32_t value){ // Set Param with Value from external format int8_t setParamValExt(uint8_t index, int32_t value) { + int8_t ret = 0; // check min and max before conversion to internal values if (IN_RANGE(value,params[index].min,params[index].max)){ - return setParamValInt(index,ExtToInt(index,value)); + ret = setParamValInt(index,ExtToInt(index,value)); + printParamDef(index); }else{ - printf("! Value %li out of range [min:%li max:%li]",value,params[index].min,params[index].max); - return 0; + printf("! Value %li out of range [min:%li max:%li]\r\n",value,params[index].min,params[index].max); } + return ret; } // Set Param with value from internal format @@ -242,31 +245,31 @@ int32_t getParamValInt(uint8_t index) { // Set watch flag for parameter int8_t watchParamVal(uint8_t index){ - boolean_T found = 0; - uint8_t size = sizeof(watchParamList); - for(int i=0;i0) printf("\r\n"); + if (watchParamListSize>0) printf("\r\n"); return 1; } @@ -289,19 +292,19 @@ int8_t printAllParamHelp(){ printf("? Commands\r\n"); for(int i=0;i Date: Thu, 7 Jan 2021 23:49:03 +0100 Subject: [PATCH 09/14] Improvement --- Inc/comms.h | 10 +++ Src/comms.c | 199 ++++++++++++++++++++++++++++++++++++++++++++----- Src/main.c | 2 +- platformio.ini | 1 + 4 files changed, 191 insertions(+), 21 deletions(-) diff --git a/Inc/comms.h b/Inc/comms.h index 78af21f..30b24ba 100644 --- a/Inc/comms.h +++ b/Inc/comms.h @@ -66,8 +66,18 @@ int8_t watchParamVal(uint8_t index); int8_t findCommand(uint8_t *userCommand, uint32_t len); int8_t findParam(uint8_t *userCommand, uint32_t len); void handle_input(uint8_t *userCommand, uint32_t len); +void process_debug(); +typedef struct debug_command_struct debug_command; +struct debug_command_struct { + uint8_t semaphore; + uint8_t error; + int8_t command_index; + int8_t param_index; + int32_t param_value; +}; + typedef struct command_entry_struct command_entry; struct command_entry_struct { const uint8_t type; diff --git a/Src/comms.c b/Src/comms.c index acc40ac..c61f34e 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -32,6 +32,8 @@ #if defined(DEBUG_SERIAL_PROTOCOL) +#define MAX_PARAM_WATCH 15 + extern ExtY rtY_Left; /* External outputs */ extern ExtU rtU_Left; /* External inputs */ extern P rtP_Left; @@ -121,8 +123,8 @@ const parameter_entry params[] = { }; -uint8_t * watchParamList; -uint8_t watchParamListSize = 0; +debug_command command; +int8_t watchParamList[MAX_PARAM_WATCH] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; // Translate from External format to Internal Format int32_t ExtToInt(uint8_t index,int32_t value){ @@ -242,34 +244,31 @@ int32_t getParamValInt(uint8_t index) { return value; } - -// Set watch flag for parameter +// Add or remove parameter from watch list int8_t watchParamVal(uint8_t index){ - boolean_T found = 0; - for(int i=0;i-1;i++){ if (watchParamList[i] == index) found = 1; - if ( found && i < watchParamListSize - 1 ) watchParamList[i] = watchParamList[i+1]; + if (found) watchParamList[i] = (i < MAX_PARAM_WATCH-1)?watchParamList[i+1]:-1; } - - if (found){watchParamListSize--;}else{watchParamListSize++;} - if (watchParamListSize == 0){ - free(watchParamList); - }else{ - watchParamList = (uint8_t*) realloc(watchParamList, watchParamListSize * sizeof(uint8_t)); + if (!found){ + if (watchParamList[i] == -1){ + watchParamList[i] = index; + return 1; + } + printf("! Watch list is full\r\n"); + return 0; } - - if (!found && watchParamList != NULL) watchParamList[watchParamListSize-1] = index; - return 1; } // Print value for all parameters with watch flag int8_t printParamVal(){ - if (watchParamList == NULL) return 0; - for(int i=0;i-1;i++){ printf("%s:%li ",params[watchParamList[i]].name,getParamValExt(watchParamList[i])); } - if (watchParamListSize>0) printf("\r\n"); + if (i>0) printf("\r\n"); return 1; } @@ -424,6 +423,166 @@ int8_t findCommand(uint8_t *userCommand, uint32_t len){ } + +// Parse and save the command to be executed +void handle_input(uint8_t *userCommand, uint32_t len) +{ + + // If there is already an unprocessed command, exit + if (command.semaphore == 1) return; + + int8_t cindex = -1; + int8_t pindex = -1; + uint8_t size = 0; + + // Find Command + cindex = findCommand(userCommand,len); + if (cindex == -1){ + // Error - Command not found + command.error = 1; + return; + } + + // Skip command characters + size = strlen(commands[cindex].name); + {len-=size;userCommand+=size;} + // Skip if space + if (*userCommand == 0x20){len-=1;userCommand+=1;} + + if ( (*userCommand == '\n' || *userCommand == '\r') && + commands[cindex].callback_function0 != NULL){ + // Command without parameter + command.semaphore = 1; + command.command_index = cindex; + command.param_index = -1; + command.param_value = 0; + return; + } + + // Find parameter + pindex = findParam(userCommand,len); + if (pindex == -1){ + // Error - Parameter not found + command.error = 2; + return; + } + + if (commands[cindex].type == WRITE && params[pindex].type == VARIABLE){ + // Error - This command cannot be used with a Variable + command.error = 3; + return; + } + + if ( //(*userCommand == '\n' || *userCommand == '\r') && + commands[cindex].callback_function1 != NULL){ + // Command with parameter + command.semaphore = 1; + command.command_index = cindex; + command.param_index = pindex; + command.param_value = 0; + return; + } + + // Skip parameter characters + size = strlen(params[pindex].name); + {len-=size;userCommand+=size;} + // Skip if space + if (*userCommand == 0x20){len-=1;userCommand+=1;} + + + int32_t value = 0; + int8_t sign = 1; + int8_t count = 0; + + // Read sign + if (*userCommand == '-'){len-=1;userCommand+=1;sign =-1;} + // Read value + for (value=0; (unsigned)*userCommand-'0'<10; userCommand++){ + value = 10*value+(*userCommand-'0'); + count++; + // Error - Value out of range + if (value>MAX_int16_T){command.error = 4;return;} + } + + if (count == 0){ + // Error - Value required + command.error = 5; + return; + } + + // Apply sign + value*= sign; + + if ( //(*userCommand == '\n' || *userCommand == '\r') && + commands[cindex].callback_function2 != NULL){ + // Store command + // Command with parameter and value + command.semaphore = 1; + command.command_index = cindex; + command.param_index = pindex; + command.param_value = value; + } + +} + + +void process_debug() +{ + + // Print parameters from watch list + printParamVal(); + + // Process errors + switch(command.error){ + case 1: + printf("! Command not found\r\n"); + break; + case 2: + printf("! Parameter not found\r\n"); + break; + case 3: + printf("! This command cannot be used with a Variable\r\n"); + break; + case 4: + printf("! Value not in range\r\n"); + break; + case 5: + printf("! Value required\r\n"); + break; + } + + if (command.error != 0){command.error = 0;return;}; + if (command.semaphore == 0) return; + + int8_t ret = 0; + if (commands[command.command_index].callback_function0 != NULL && + command.param_index == -1){ + // This function needs no parameter + ret = (*commands[command.command_index].callback_function0)(); + if (ret==1){printf("OK\r\n");} + command.semaphore = 0; + return; + } + + if (commands[command.command_index].callback_function1 != NULL && + command.param_index != -1){ + // This function needs only a parameter + ret = (*commands[command.command_index].callback_function1)(command.param_index); + if (ret==1){printf("OK\r\n");} + command.semaphore = 0; + return; + } + + if (commands[command.command_index].callback_function2 != NULL && + command.param_index != -1){ + // This function needs an additional parameter + ret = (*commands[command.command_index].callback_function2)(command.param_index,command.param_value); + if (ret==1){printf("OK\r\n");} + command.semaphore = 0; + } +} + +/* void handle_input(uint8_t *userCommand, uint32_t len) { @@ -506,6 +665,6 @@ void handle_input(uint8_t *userCommand, uint32_t len) if (ret==1){printf("OK\r\n");} } -} +}*/ #endif \ No newline at end of file diff --git a/Src/main.c b/Src/main.c index 2ff55d1..80d0f66 100644 --- a/Src/main.c +++ b/Src/main.c @@ -433,7 +433,7 @@ int main(void) { board_temp_adcFilt, // 7: for board temperature calibration board_temp_deg_c); // 8: for verifying board temperature calibration #else - printParamVal(); + process_debug(); #endif } #endif diff --git a/platformio.ini b/platformio.ini index 64b1a55..1a91688 100644 --- a/platformio.ini +++ b/platformio.ini @@ -40,6 +40,7 @@ build_flags = -T./STM32F103RCTx_FLASH.ld -lc -lm +# -flto -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization -D VARIANT_ADC From aa5cb18aea7924df7eaf50dd95dd9d1bbccb58af Mon Sep 17 00:00:00 2001 From: Candas1 Date: Mon, 11 Jan 2021 21:34:17 +0100 Subject: [PATCH 10/14] Handle usart overflow mode --- Inc/comms.h | 6 +- Src/comms.c | 448 ++++++++++++++++++++++++---------------------------- Src/util.c | 11 +- 3 files changed, 221 insertions(+), 244 deletions(-) diff --git a/Inc/comms.h b/Inc/comms.h index 30b24ba..f942270 100644 --- a/Inc/comms.h +++ b/Inc/comms.h @@ -42,10 +42,10 @@ enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT}; #define ADD_PARAM(var) typename(var),&var -int32_t ExtToInt(uint8_t index,int32_t value); +int32_t extToInt(uint8_t index,int32_t value); int8_t setParamValInt(uint8_t index, int32_t newValue); int8_t setParamValExt(uint8_t index, int32_t newValue); -int32_t IntToExt(uint8_t index,int32_t value); +int32_t intToExt(uint8_t index,int32_t value); int32_t getParamValInt(uint8_t index); int32_t getParamValExt(uint8_t index); @@ -61,6 +61,7 @@ int8_t printAllParamHelp(); int8_t printParamVal(); int8_t printParamDef(uint8_t index); int8_t printAllParamDef(); +void printError(uint8_t errornum ); int8_t watchParamVal(uint8_t index); int8_t findCommand(uint8_t *userCommand, uint32_t len); @@ -97,6 +98,7 @@ struct parameter_entry_struct { void *valueR; const uint16_t addr; const int32_t init; + const uint8_t initFormat; const int32_t min; const int32_t max; const uint8_t div; diff --git a/Src/comms.c b/Src/comms.c index c61f34e..5302c7e 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -30,7 +30,16 @@ #include "util.h" #include "comms.h" -#if defined(DEBUG_SERIAL_PROTOCOL) +#if defined(DEBUG_SERIAL_PROTOCOL) && (defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3)) + +#ifdef CONTROL_ADC + #define RAW_MIN 0 + #define RAW_MAX 4095 +#else + #define RAW_MIN -1000 + #define RAW_MAX 1000 +#endif + #define MAX_PARAM_WATCH 15 @@ -71,116 +80,125 @@ const command_entry commands[] = { enum paramTypes {PARAMETER,VARIABLE}; const parameter_entry params[] = { // CONTROL PARAMETERS - // Type ,Name ,Datatype ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text - {PARAMETER ,"CTRL_MOD" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,1 ,3 ,0 ,0 ,0 ,NULL ,"Ctrl mode 1:VLT 2:SPD 3:TRQ"}, - {PARAMETER ,"CTRL_TYP" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,2 ,0 ,0 ,0 ,NULL ,"Ctrl type 0:COM 1:SIN 2:FOC"}, - {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,1 ,40 ,A2BIT_CONV ,0 ,4 ,NULL ,"Max phase current A"}, - {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,10 ,2000 ,0 ,0 ,4 ,NULL ,"Max motor RPM"}, - {PARAMETER ,"FI_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,1 ,0 ,0 ,0 ,NULL ,"Enable field weak"}, - {PARAMETER ,"FI_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,0 ,1500 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak high RPM"}, - {PARAMETER ,"FI_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,0 ,1000 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak low RPM"}, - {PARAMETER ,"FI_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,0 ,20 ,A2BIT_CONV ,0 ,4 ,NULL ,"Field weak max current A(FOC)"}, - {PARAMETER ,"PHA_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,0 ,55 ,0 ,0 ,4 ,NULL ,"Max Phase Adv angle Deg(SIN)"}, + // Type ,Name ,Datatype ,ValueL ptr ,ValueR ,EEPRM Addr ,Init Int/Ext ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text + {PARAMETER ,"CTRL_MOD" ,ADD_PARAM(ctrlModReqRaw) ,NULL ,0 ,CTRL_MOD_REQ ,0 ,1 ,3 ,0 ,0 ,0 ,NULL ,"Ctrl mode 1:VLT 2:SPD 3:TRQ"}, + {PARAMETER ,"CTRL_TYP" ,ADD_PARAM(rtP_Left.z_ctrlTypSel) ,&rtP_Right.z_ctrlTypSel ,0 ,CTRL_TYP_SEL ,0 ,0 ,2 ,0 ,0 ,0 ,NULL ,"Ctrl type 0:COM 1:SIN 2:FOC"}, + {PARAMETER ,"I_MOT_MAX" ,ADD_PARAM(rtP_Left.i_max) ,&rtP_Right.i_max ,1 ,I_MOT_MAX ,1 ,1 ,40 ,A2BIT_CONV ,0 ,4 ,NULL ,"Max phase current A"}, + {PARAMETER ,"N_MOT_MAX" ,ADD_PARAM(rtP_Left.n_max) ,&rtP_Right.n_max ,2 ,N_MOT_MAX ,1 ,10 ,2000 ,0 ,0 ,4 ,NULL ,"Max motor RPM"}, + {PARAMETER ,"FI_WEAK_ENA" ,ADD_PARAM(rtP_Left.b_fieldWeakEna) ,&rtP_Right.b_fieldWeakEna ,0 ,FIELD_WEAK_ENA ,0 ,0 ,1 ,0 ,0 ,0 ,NULL ,"Enable field weak"}, + {PARAMETER ,"FI_WEAK_HI" ,ADD_PARAM(rtP_Left.r_fieldWeakHi) ,&rtP_Right.r_fieldWeakHi ,0 ,FIELD_WEAK_HI ,1 ,0 ,1500 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak high RPM"}, + {PARAMETER ,"FI_WEAK_LO" ,ADD_PARAM(rtP_Left.r_fieldWeakLo) ,&rtP_Right.r_fieldWeakLo ,0 ,FIELD_WEAK_LO ,1 ,0 ,1000 ,0 ,0 ,4 ,Input_Lim_Init ,"Field weak low RPM"}, + {PARAMETER ,"FI_WEAK_MAX" ,ADD_PARAM(rtP_Left.id_fieldWeakMax) ,&rtP_Right.id_fieldWeakMax,0 ,FIELD_WEAK_MAX ,1 ,0 ,20 ,A2BIT_CONV ,0 ,4 ,NULL ,"Field weak max current A(FOC)"}, + {PARAMETER ,"PHA_ADV_MAX" ,ADD_PARAM(rtP_Left.a_phaAdvMax) ,&rtP_Right.a_phaAdvMax ,0 ,PHASE_ADV_MAX ,1 ,0 ,55 ,0 ,0 ,4 ,NULL ,"Max Phase Adv angle Deg(SIN)"}, // INPUT PARAMETERS - // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text - {PARAMETER ,"PRI_IN1_TYP" ,ADD_PARAM(input1[0].typ) ,NULL ,3 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input1 type"}, - {PARAMETER ,"PRI_IN1_MIN" ,ADD_PARAM(input1[0].min) ,NULL ,4 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 min"}, - {PARAMETER ,"PRI_IN1_MID" ,ADD_PARAM(input1[0].mid) ,NULL ,5 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 mid"}, - {PARAMETER ,"PRI_IN1_MAX" ,ADD_PARAM(input1[0].max) ,NULL ,6 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 max"}, - {PARAMETER ,"PRI_IN2_TYP" ,ADD_PARAM(input2[0].typ) ,NULL ,7 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input2 type"}, - {PARAMETER ,"PRI_IN2_MIN" ,ADD_PARAM(input2[0].min) ,NULL ,8 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 min"}, - {PARAMETER ,"PRI_IN2_MID" ,ADD_PARAM(input2[0].mid) ,NULL ,9 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 mid"}, - {PARAMETER ,"PRI_IN2_MAX" ,ADD_PARAM(input2[0].max) ,NULL ,10 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 max"}, - {VARIABLE ,"PRI_IN1_RAW" ,ADD_PARAM(input1[0].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 raw"}, - {VARIABLE ,"PRI_IN2_RAW" ,ADD_PARAM(input2[0].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 raw"}, - {VARIABLE ,"PRI_IN1_CMD" ,ADD_PARAM(input1[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 cmd"}, - {VARIABLE ,"PRI_IN2_CMD" ,ADD_PARAM(input2[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 cmd"}, + // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init Int/Ext ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text + {VARIABLE ,"IN1_RAW" ,ADD_PARAM(input1[0].raw) ,NULL ,0 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input1 raw"}, + {PARAMETER ,"IN1_TYP" ,ADD_PARAM(input1[0].typ) ,NULL ,3 ,0 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input1 type"}, + {PARAMETER ,"IN1_MIN" ,ADD_PARAM(input1[0].min) ,NULL ,4 ,RAW_MIN ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input1 min"}, + {PARAMETER ,"IN1_MID" ,ADD_PARAM(input1[0].mid) ,NULL ,5 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input1 mid"}, + {PARAMETER ,"IN1_MAX" ,ADD_PARAM(input1[0].max) ,NULL ,6 ,RAW_MAX ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input1 max"}, + {VARIABLE ,"IN1_CMD" ,ADD_PARAM(input1[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input1 cmd"}, + + {VARIABLE ,"IN2_RAW" ,ADD_PARAM(input2[0].raw) ,NULL ,0 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input2 raw"}, + {PARAMETER ,"IN2_TYP" ,ADD_PARAM(input2[0].typ) ,NULL ,7 ,0 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Input2 type"}, + {PARAMETER ,"IN2_MIN" ,ADD_PARAM(input2[0].min) ,NULL ,8 ,RAW_MIN ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input2 min"}, + {PARAMETER ,"IN2_MID" ,ADD_PARAM(input2[0].mid) ,NULL ,9 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input2 mid"}, + {PARAMETER ,"IN2_MAX" ,ADD_PARAM(input2[0].max) ,NULL ,10 ,RAW_MAX ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Input2 max"}, + {VARIABLE ,"IN2_CMD" ,ADD_PARAM(input2[0].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Input2 cmd"}, #if defined(PRI_INPUT1) && defined(PRI_INPUT2) && defined(AUX_INPUT1) && defined(AUX_INPUT2) - {PARAMETER ,"AUX_IN1_TYP" ,ADD_PARAM(input1[1].typ) ,NULL ,11 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input1 type"}, - {PARAMETER ,"AUX_IN1_MIN" ,ADD_PARAM(input1[1].min) ,NULL ,12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 min"}, - {PARAMETER ,"AUX_IN1_MID" ,ADD_PARAM(input1[1].mid) ,NULL ,13 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 mid"}, - {PARAMETER ,"AUX_IN1_MAX" ,ADD_PARAM(input1[1].max) ,NULL ,14 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 max"}, - {PARAMETER ,"AUX_IN2_TYP" ,ADD_PARAM(input2[1].typ) ,NULL ,15 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input2 type"}, - {PARAMETER ,"AUX_IN2_MIN" ,ADD_PARAM(input2[1].min) ,NULL ,16 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 min"}, - {PARAMETER ,"AUX_IN2_MID" ,ADD_PARAM(input2[1].mid) ,NULL ,17 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 mid"}, - {PARAMETER ,"AUX_IN2_MAX" ,ADD_PARAM(input2[1].max) ,NULL ,18 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 max"}, - {VARIABLE ,"AUX_IN1_RAW" ,ADD_PARAM(input1[1].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 raw"}, - {VARIABLE ,"AUX_IN2_RAW" ,ADD_PARAM(input2[1].raw) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 raw"}, - {VARIABLE ,"AUX_IN1_CMD" ,ADD_PARAM(input1[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 cmd"}, - {VARIABLE ,"AUX_IN2_CMD" ,ADD_PARAM(input2[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 cmd"}, + // Type ,Name ,ValueL ptr ,ValueR ,EEPRM Addr ,Init Int/Ext ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text + {VARIABLE ,"AUX_IN1_RAW" ,ADD_PARAM(input1[1].raw) ,NULL ,0 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input1 raw"}, + {PARAMETER ,"AUX_IN1_TYP" ,ADD_PARAM(input1[1].typ) ,NULL ,11 ,0 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input1 type"}, + {PARAMETER ,"AUX_IN1_MIN" ,ADD_PARAM(input1[1].min) ,NULL ,12 ,RAW_MIN ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input1 min"}, + {PARAMETER ,"AUX_IN1_MID" ,ADD_PARAM(input1[1].mid) ,NULL ,13 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input1 mid"}, + {PARAMETER ,"AUX_IN1_MAX" ,ADD_PARAM(input1[1].max) ,NULL ,14 ,RAW_MAX ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input1 max"}, + {VARIABLE ,"AUX_IN1_CMD" ,ADD_PARAM(input1[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input1 cmd"}, + + {VARIABLE ,"AUX_IN2_RAW" ,ADD_PARAM(input2[1].raw) ,NULL ,0 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input2 raw"}, + {PARAMETER ,"AUX_IN2_TYP" ,ADD_PARAM(input2[1].typ) ,NULL ,15 ,0 ,0 ,0 ,3 ,0 ,0 ,0 ,0 ,"Aux. input2 type"}, + {PARAMETER ,"AUX_IN2_MIN" ,ADD_PARAM(input2[1].min) ,NULL ,16 ,RAW_MIN ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input2 min"}, + {PARAMETER ,"AUX_IN2_MID" ,ADD_PARAM(input2[1].mid) ,NULL ,17 ,0 ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input2 mid"}, + {PARAMETER ,"AUX_IN2_MAX" ,ADD_PARAM(input2[1].max) ,NULL ,18 ,RAW_MAX ,0 ,RAW_MIN,RAW_MAX,0 ,0 ,0 ,0 ,"Aux. input2 max"}, + {VARIABLE ,"AUX_IN2_CMD" ,ADD_PARAM(input2[1].cmd) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,"Aux. input2 cmd"}, #endif // FEEDBACK - // Type ,Name ,Datatype, ValueL ptr ,ValueR ,EEPRM Addr ,Init ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text - {VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink) ,&rtU_Right.i_DCLink ,0 ,0 ,0 ,0 ,A2BIT_CONV ,0 ,0 ,NULL ,"DC Link current A"}, - {VARIABLE ,"SPD_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Motor Measured Avg RPM"}, - {VARIABLE ,"SPDL" ,ADD_PARAM(rtY_Left.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Left Motor Measured RPM"}, - {VARIABLE ,"SPDR" ,ADD_PARAM(rtY_Right.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Right Motor Measured RPM"}, - {VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,0 ,4 ,NULL ,"Rate *10"}, - {VARIABLE ,"SPD_COEF" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,"Speed Coefficient *10"}, - {VARIABLE ,"STR_COEF" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,10 ,14 ,NULL ,"Steer Coefficient *10"}, - {VARIABLE ,"BATV" ,ADD_PARAM(adc_buffer.batt1) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Battery voltage *100"}, - //{VARIABLE ,"TEMP" ,ADD_PARAM(board_temp_deg_c) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Temperature °C *10"}, + // Type ,Name ,Datatype, ValueL ptr ,ValueR ,EEPRM Addr ,Init Int/Ext ,Min ,Max ,Div ,Mul ,Fix ,Callback Function ,Help text + {VARIABLE ,"I_DC_LINK" ,ADD_PARAM(rtU_Left.i_DCLink) ,&rtU_Right.i_DCLink ,0 ,0 ,0 ,0 ,0 ,A2BIT_CONV ,100 ,0 ,NULL ,"DC Link current A *100"}, + {VARIABLE ,"SPD_AVG" ,ADD_PARAM(speedAvg) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Motor Measured Avg RPM"}, + {VARIABLE ,"SPDL" ,ADD_PARAM(rtY_Left.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Left Motor Measured RPM"}, + {VARIABLE ,"SPDR" ,ADD_PARAM(rtY_Right.n_mot) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Right Motor Measured RPM"}, + {VARIABLE ,"RATE" ,0 , NULL ,NULL ,0 ,RATE ,0 ,0 ,0 ,0 ,0 ,4 ,NULL ,"Rate *10"}, + {VARIABLE ,"SPD_COEF" ,0 , NULL ,NULL ,0 ,SPEED_COEFFICIENT ,0 ,0 ,0 ,0 ,10 ,14 ,NULL ,"Speed Coefficient *10"}, + {VARIABLE ,"STR_COEF" ,0 , NULL ,NULL ,0 ,STEER_COEFFICIENT ,0 ,0 ,0 ,0 ,10 ,14 ,NULL ,"Steer Coefficient *10"}, + {VARIABLE ,"BATV" ,ADD_PARAM(adc_buffer.batt1) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Battery voltage *100"}, + //{VARIABLE ,"TEMP" ,ADD_PARAM(board_temp_deg_c) ,NULL ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,NULL ,"Temperature °C *10"}, }; + +const char *errors[9] = { + "Command not found", // Err1 + "Parameter not found", // Err2 + "This command cannot be used with a Variable", // Err3 + "Value not in range", // Err4 + "Value expected", // Err5 + "Start of line expected", // Err6 + "End of line expected", // Err7 + "Parameter expected", // Err8 + "Uncaught error" // Err9 + "Watch list is full" // Err10 +}; + debug_command command; int8_t watchParamList[MAX_PARAM_WATCH] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; -// Translate from External format to Internal Format -int32_t ExtToInt(uint8_t index,int32_t value){ - // Multiply to translate to internal format - if(params[index].div) value *= params[index].div; - // Shift to translate to internal format - if (params[index].fix) value <<= params[index].fix; - // Divide for small number - if(params[index].mul) value /= params[index].mul; - return value; -} - // Set Param with Value from external format int8_t setParamValExt(uint8_t index, int32_t value) { int8_t ret = 0; // check min and max before conversion to internal values if (IN_RANGE(value,params[index].min,params[index].max)){ - ret = setParamValInt(index,ExtToInt(index,value)); + ret = setParamValInt(index,extToInt(index,value)); printParamDef(index); }else{ - printf("! Value %li out of range [min:%li max:%li]\r\n",value,params[index].min,params[index].max); + printError(4); // Error - Value out of range } return ret; } // Set Param with value from internal format int8_t setParamValInt(uint8_t index, int32_t newValue) { - int32_t value = newValue; - if (*(int32_t*)params[index].valueL != value){ + int32_t oldValue = getParamValInt(index); + if (oldValue != newValue){ // if value is different, beep, cast and assign new value switch (params[index].datatype){ case UINT8_T: - if (params[index].valueL != NULL) *(uint8_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(uint8_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(uint8_t*)params[index].valueL = newValue; + if (params[index].valueR != NULL) *(uint8_t*)params[index].valueR = newValue; break; case UINT16_T: - if (params[index].valueL != NULL) *(uint16_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(uint16_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(uint16_t*)params[index].valueL = newValue; + if (params[index].valueR != NULL) *(uint16_t*)params[index].valueR = newValue; break; case UINT32_T: - if (params[index].valueL != NULL) *(uint32_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(uint32_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(uint32_t*)params[index].valueL = newValue; + if (params[index].valueR != NULL) *(uint32_t*)params[index].valueR = newValue; break; case INT8_T: - if (params[index].valueL != NULL) *(int8_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(int8_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(int8_t*)params[index].valueL = newValue; + if (params[index].valueR != NULL) *(int8_t*)params[index].valueR = newValue; break; case INT16_T: - if (params[index].valueL != NULL) *(int16_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(int16_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(int16_t*)params[index].valueL = newValue; + if (params[index].valueR != NULL) *(int16_t*)params[index].valueR = newValue; break; case INT32_T: - if (params[index].valueL != NULL) *(int32_t*)params[index].valueL = value; - if (params[index].valueR != NULL) *(int32_t*)params[index].valueR = value; + if (params[index].valueL != NULL) *(int32_t*)params[index].valueL = newValue; + if (params[index].valueR != NULL) *(int32_t*)params[index].valueR = newValue; break; } + + // Beep if value was modified + beepShort(5); } // Run callback function if assigned @@ -190,7 +208,7 @@ int8_t setParamValInt(uint8_t index, int32_t newValue) { // Get Parameter Internal value and translate to external int32_t getParamValExt(uint8_t index) { - return IntToExt(index,getParamValInt(index)); + return intToExt(index,getParamValInt(index)); } // Get Parameter Internal Value @@ -256,7 +274,7 @@ int8_t watchParamVal(uint8_t index){ watchParamList[i] = index; return 1; } - printf("! Watch list is full\r\n"); + printError(10); return 0; } return 1; @@ -272,15 +290,15 @@ int8_t printParamVal(){ return 1; } -// Print help for parameter +// Print help for Command int8_t printCommandHelp(uint8_t index){ - printf("? %s:%s\r\n",commands[index].name,commands[index].help); + printf("? %s:\"%s\"\r\n",commands[index].name,commands[index].help); return 1; } // Print help for parameter int8_t printParamHelp(uint8_t index){ - printf("? %s:%s ",params[index].name,params[index].help); + printf("? %s:\"%s\" ",params[index].name,params[index].help); if (params[index].type == PARAMETER) printf("[min:%li max:%li]",params[index].min,params[index].max); printf("\r\n"); return 1; @@ -308,9 +326,9 @@ int8_t printAllParamHelp(){ return 1; } -// Print definition for parameter +// Print definition(name,value,initial value, min, max) for parameter int8_t printParamDef(uint8_t index){ - printf("# name:%s value:%li init:%li min:%li max:%li\r\n", + printf("# name:\"%s\" value:%li init:%li min:%li max:%li\r\n", params[index].name, // Parameter Name getParamValExt(index), // Parameter Value translated to external format getParamInitExt(index), // Parameter Init Value translated to external format @@ -319,15 +337,23 @@ int8_t printParamDef(uint8_t index){ return 1; } -// Print definition for all parameters +// Print definition(name,value,initial value, min, max) for all parameters int8_t printAllParamDef(){ for(int i=0;i 0){ + printError(command.error); + command.error = 0; + return; } - - if (command.error != 0){command.error = 0;return;}; + + // Nothing to do if (command.semaphore == 0) return; int8_t ret = 0; @@ -582,89 +635,4 @@ void process_debug() } } -/* -void handle_input(uint8_t *userCommand, uint32_t len) -{ - - int8_t cindex = -1; - int8_t pindex = -1; - uint8_t size = 0; - int8_t ret = 0; - - // Find Command - cindex = findCommand(userCommand,len); - if (cindex == -1){ - printf("! Command not found\r\n"); - return; - } - - // Skip command characters - size = strlen(commands[cindex].name); - {len-=size;userCommand+=size;} - // Skip if space - if (*userCommand == 0x20){len-=1;userCommand+=1;} - - if ( (*userCommand == '\n' || *userCommand == '\r') && - commands[cindex].callback_function0 != NULL){ - // This function needs no parameter - ret = (*commands[cindex].callback_function0)(); - if (ret==1){printf("OK\r\n");} - return; - } - - // Find parameter - pindex = findParam(userCommand,len); - if (pindex == -1){ - printf("! Parameter not found\r\n"); - return; - } - - if (commands[cindex].type == WRITE && params[pindex].type == VARIABLE){ - printf("! This command cannot be used with a Variable\r\n"); - return; - } - - if (commands[cindex].callback_function1 != NULL){ - // This function needs only a parameter - ret = (*commands[cindex].callback_function1)(pindex); - if (ret==1){printf("OK\r\n");} - return; - } - - // Skip parameter characters - size = strlen(params[pindex].name); - {len-=size;userCommand+=size;} - // Skip if space - if (*userCommand == 0x20){len-=1;userCommand+=1;} - - - int32_t value = 0; - int8_t sign = 1; - int8_t count = 0; - - // Read sign - if (*userCommand == '-'){len-=1;userCommand+=1;sign =-1;} - // Read value - for (value=0; (unsigned)*userCommand-'0'<10; userCommand++){ - value=10*value+(*userCommand-'0'); - count++; - if (value>MAX_int16_T){printf("! Value not in range\r\n");return;} - } - - if (count == 0){ - printf("! Value required\r\n"); - return; - } - - // Apply sign - value*= sign; - - if (commands[cindex].callback_function2 != NULL){ - // This function needs an additional parameter - ret = (*commands[cindex].callback_function2)(pindex,value); - if (ret==1){printf("OK\r\n");} - } - -}*/ - #endif \ No newline at end of file diff --git a/Src/util.c b/Src/util.c index 5f8bf88..1a0f305 100644 --- a/Src/util.c +++ b/Src/util.c @@ -1133,14 +1133,21 @@ void usart3_rx_check(void) #endif #if defined(DEBUG_SERIAL_USART3) + uint8_t *ptr; + if (pos != old_pos) { // Check change in received data if (pos > old_pos) { // "Linear" buffer mode: check if current position is over previous one usart_process_debug(&rx_buffer_R[old_pos], pos - old_pos); // Process data } else { // "Overflow" buffer mode - usart_process_debug(&rx_buffer_R[old_pos], rx_buffer_R_len - old_pos); // First Process data from the end of buffer + ptr = (uint8_t *) malloc( sizeof(uint8_t) * (rx_buffer_R_len - old_pos + pos) ); + memcpy(ptr,&rx_buffer_R[old_pos],rx_buffer_R_len - old_pos); // First Process data from the end of buffer if (pos > 0) { // Check and continue with beginning of buffer - usart_process_debug(&rx_buffer_R[0], pos); // Process remaining data + ptr += rx_buffer_R_len - old_pos; + memcpy(ptr,&rx_buffer_R[0], pos); // Process remaining data } + ptr -= rx_buffer_R_len - old_pos; + usart_process_debug(ptr, rx_buffer_R_len - old_pos + pos); + free( ptr ); } } #endif // DEBUG_SERIAL_USART3 From 1df6ecf7733a310b968d0a575a38d39c36b061c5 Mon Sep 17 00:00:00 2001 From: Candas1 Date: Tue, 12 Jan 2021 18:47:39 +0100 Subject: [PATCH 11/14] Fix --- platformio.ini | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/platformio.ini b/platformio.ini index 1a91688..d7612db 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ src_dir = Src ; ; Choose one or all variants get built ; -default_envs = VARIANT_ADC ; Variant for control via ADC input +;default_envs = VARIANT_ADC ; Variant for control via ADC input ;default_envs = VARIANT_USART ; Variant for Serial control via USART3 input ;default_envs = VARIANT_NUNCHUK ; Variant for Nunchuk controlled vehicle build ;default_envs = VARIANT_PPM ; Variant for RC-Remotes with PPM-Sum signal @@ -40,7 +40,6 @@ build_flags = -T./STM32F103RCTx_FLASH.ld -lc -lm -# -flto -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization -D VARIANT_ADC @@ -62,7 +61,7 @@ build_flags = -DSTM32F103xE -T./STM32F103RCTx_FLASH.ld -lc - -l + -lm -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization -D VARIANT_USART From d5d85c20ec76a5d2273e623f759d6b884410cce7 Mon Sep 17 00:00:00 2001 From: Candas1 Date: Wed, 27 Jan 2021 22:45:57 +0100 Subject: [PATCH 12/14] At startup, wait until on/off button is released, to prevent entering auto-calibration after flashing --- Src/main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Src/main.c b/Src/main.c index 80d0f66..9c86c2b 100644 --- a/Src/main.c +++ b/Src/main.c @@ -199,6 +199,8 @@ int main(void) { int16_t board_temp_adcFilt = adc_buffer.temp; int16_t board_temp_deg_c; + // Loop until button is released + while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) HAL_Delay(10); while(1) { HAL_Delay(DELAY_IN_MAIN_LOOP); // delay in ms From a564f368e2cdf7abda435586a572cae42ae65f6a Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Fri, 29 Jan 2021 19:47:03 +0100 Subject: [PATCH 13/14] Ascii Debug updates - added missing pointer to UART2 input user processing - added comms.c to Makefile - fixed #134 : uncomment #define AUTO_CALIBRATION_ENA to disable auto-calibration - small styling adjustments --- Inc/comms.h | 17 +++++++++-------- Inc/config.h | 5 +++-- Makefile | 1 + Src/comms.c | 6 ++++-- Src/main.c | 8 ++++---- Src/util.c | 39 ++++++++++++++++++++------------------- 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/Inc/comms.h b/Inc/comms.h index f942270..674e98b 100644 --- a/Inc/comms.h +++ b/Inc/comms.h @@ -23,6 +23,7 @@ #include "stm32f1xx_hal.h" +#if defined(DEBUG_SERIAL_PROTOCOL) enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT}; #define typename(x) _Generic((x), \ @@ -32,7 +33,7 @@ enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT}; int8_t: INT8_T, \ int16_t: INT16_T, \ int32_t: INT32_T, \ - int: INT, \ + int: INT, \ float: FLOAT) #define PARAM_SIZE(param) sizeof(param) / sizeof(parameter_entry) @@ -93,20 +94,20 @@ typedef struct parameter_entry_struct parameter_entry; struct parameter_entry_struct { const uint8_t type; const char *name; - const uint8_t datatype; + const uint8_t datatype; void *valueL; void *valueR; const uint16_t addr; - const int32_t init; + const int32_t init; const uint8_t initFormat; const int32_t min; - const int32_t max; + const int32_t max; const uint8_t div; const uint8_t mul; const uint8_t fix; - void (*callback_function)(); - const char *help; + void (*callback_function)(); + const char *help; }; - -#endif \ No newline at end of file +#endif // DEBUG_SERIAL_PROTOCOL +#endif // COMMS_H \ No newline at end of file diff --git a/Inc/config.h b/Inc/config.h index 0a7ace9..28775f3 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -172,6 +172,7 @@ #define ADC_MARGIN 100 // ADC input margin applied on the raw ADC min and max to make sure the MIN and MAX values are reached even in the presence of noise #define ADC_PROTECT_TIMEOUT 100 // ADC Protection: number of wrong / missing input commands before safety state is taken #define ADC_PROTECT_THRESH 200 // ADC Protection threshold below/above the MIN/MAX ADC values +#define AUTO_CALIBRATION_ENA // Enable/Disable input auto-calibration by holding power button pressed. Un-comment this if auto-calibration is not needed. /* FILTER is in fixdt(0,16,16): VAL_fixedPoint = VAL_floatingPoint * 2^16. In this case 6553 = 0.1 * 2^16 * Value of COEFFICIENT is in fixdt(1,16,14) @@ -227,7 +228,7 @@ * enable DEBUG_SERIAL_USART3 or DEBUG_SERIAL_USART2 * * - * DEBUG_SERIAL_ASCII output is: + * DEBUG ASCII output is: * // "in1:345 in2:1337 cmdL:0 cmdR:0 BatADC:0 BatV:0 TempADC:0 Temp:0\r\n" * * in1: (int16_t)input1[inIdx].raw); raw input1: ADC1, UART, PWM, PPM, iBUS @@ -243,6 +244,7 @@ // #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! +// #define DEBUG_SERIAL_PROTOCOL // uncomment this to send user commands to the board, change parameters and print specific signals (see comms.c for the user commands) // ########################### END OF DEBUG SERIAL ############################ @@ -291,7 +293,6 @@ #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! #endif - #define DEBUG_SERIAL_PROTOCOL // #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2! // #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3! #endif diff --git a/Makefile b/Makefile index 3d4cad7..e8e0f3d 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,7 @@ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \ Src/system_stm32f1xx.c \ Src/setup.c \ Src/control.c \ +Src/comms.c \ Src/util.c \ Src/main.c \ Src/bldc.c \ diff --git a/Src/comms.c b/Src/comms.c index 5302c7e..c6f8966 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -17,7 +17,6 @@ * along with this program. If not, see . */ - // Includes #include #include @@ -30,6 +29,7 @@ #include "util.h" #include "comms.h" +#if defined(DEBUG_SERIAL_PROTOCOL) #if defined(DEBUG_SERIAL_PROTOCOL) && (defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3)) #ifdef CONTROL_ADC @@ -635,4 +635,6 @@ void process_debug() } } -#endif \ No newline at end of file +#endif +#endif // DEBUG_SERIAL_PROTOCOL + diff --git a/Src/main.c b/Src/main.c index 9c86c2b..3d190f8 100644 --- a/Src/main.c +++ b/Src/main.c @@ -200,7 +200,7 @@ int main(void) { int16_t board_temp_deg_c; // Loop until button is released - while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) HAL_Delay(10); + while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { HAL_Delay(10); } while(1) { HAL_Delay(DELAY_IN_MAIN_LOOP); // delay in ms @@ -424,7 +424,9 @@ int main(void) { // ####### DEBUG SERIAL OUT ####### #if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3) if (main_loop_counter % 25 == 0) { // Send data periodically every 125 ms - #ifndef DEBUG_SERIAL_PROTOCOL + #if defined(DEBUG_SERIAL_PROTOCOL) + process_debug(); + #else printf("in1:%i in2:%i cmdL:%i cmdR:%i BatADC:%i BatV:%i TempADC:%i Temp:%i\r\n", input1[inIdx].raw, // 1: INPUT1 input2[inIdx].raw, // 2: INPUT2 @@ -434,8 +436,6 @@ int main(void) { batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC, // 6: for verifying battery voltage calibration board_temp_adcFilt, // 7: for board temperature calibration board_temp_deg_c); // 8: for verifying board temperature calibration - #else - process_debug(); #endif } #endif diff --git a/Src/util.c b/Src/util.c index 1a0f305..46d5b1e 100644 --- a/Src/util.c +++ b/Src/util.c @@ -483,6 +483,7 @@ void calcAvgSpeed(void) { * The Values will be saved to flash. Values are persistent if you flash with platformio. To erase them, make a full chip erase. */ void adcCalibLim(void) { +#ifdef AUTO_CALIBRATION_ENA calcAvgSpeed(); if (speedAvgAbs > 5) { // do not enter this mode if motors are spinning return; @@ -571,6 +572,7 @@ void adcCalibLim(void) { #endif #endif +#endif // AUTO_CALIBRATION_ENA } /* * Update Maximum Motor Current Limit (via ADC1) and Maximum Speed Limit (via ADC2) @@ -1063,14 +1065,20 @@ void usart2_rx_check(void) #endif #if defined(DEBUG_SERIAL_USART2) + uint8_t *ptr; if (pos != old_pos) { // Check change in received data if (pos > old_pos) { // "Linear" buffer mode: check if current position is over previous one usart_process_debug(&rx_buffer_L[old_pos], pos - old_pos); // Process data } else { // "Overflow" buffer mode - usart_process_debug(&rx_buffer_L[old_pos], rx_buffer_L_len - old_pos); // First Process data from the end of buffer + ptr = (uint8_t *) malloc(sizeof(uint8_t) * (rx_buffer_L_len - old_pos + pos)); + memcpy(ptr, &rx_buffer_L[old_pos], rx_buffer_L_len - old_pos); // First copy data from the end of buffer if (pos > 0) { // Check and continue with beginning of buffer - usart_process_debug(&rx_buffer_L[0], pos); // Process remaining data + ptr += rx_buffer_L_len - old_pos; + memcpy(ptr, &rx_buffer_L[0], pos); // Copy remaining data } + ptr -= rx_buffer_L_len - old_pos; + usart_process_debug(ptr, rx_buffer_L_len - old_pos + pos); // Process data + free(ptr); } } #endif // DEBUG_SERIAL_USART2 @@ -1134,26 +1142,25 @@ void usart3_rx_check(void) #if defined(DEBUG_SERIAL_USART3) uint8_t *ptr; - if (pos != old_pos) { // Check change in received data if (pos > old_pos) { // "Linear" buffer mode: check if current position is over previous one usart_process_debug(&rx_buffer_R[old_pos], pos - old_pos); // Process data } else { // "Overflow" buffer mode - ptr = (uint8_t *) malloc( sizeof(uint8_t) * (rx_buffer_R_len - old_pos + pos) ); - memcpy(ptr,&rx_buffer_R[old_pos],rx_buffer_R_len - old_pos); // First Process data from the end of buffer + ptr = (uint8_t *) malloc(sizeof(uint8_t) * (rx_buffer_R_len - old_pos + pos)); + memcpy(ptr, &rx_buffer_R[old_pos], rx_buffer_R_len - old_pos); // First copy data from the end of buffer if (pos > 0) { // Check and continue with beginning of buffer ptr += rx_buffer_R_len - old_pos; - memcpy(ptr,&rx_buffer_R[0], pos); // Process remaining data + memcpy(ptr, &rx_buffer_R[0], pos); // Copy remaining data } ptr -= rx_buffer_R_len - old_pos; - usart_process_debug(ptr, rx_buffer_R_len - old_pos + pos); - free( ptr ); + usart_process_debug(ptr, rx_buffer_R_len - old_pos + pos); // Process data + free(ptr); } } #endif // DEBUG_SERIAL_USART3 #ifdef CONTROL_SERIAL_USART3 - uint8_t *ptr; + uint8_t *ptr; if (pos != old_pos) { // Check change in received data ptr = (uint8_t *)&commandR_raw; // Initialize the pointer with command_raw address if (pos > old_pos && (pos - old_pos) == commandR_len) { // "Linear" buffer mode: check if current position is over previous one AND data length equals expected length @@ -1202,16 +1209,8 @@ void usart3_rx_check(void) #if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3) void usart_process_debug(uint8_t *userCommand, uint32_t len) { - - #ifndef DEBUG_SERIAL_PROTOCOL - for (; len > 0; len--, userCommand++) { - if (*userCommand != '\n' && *userCommand != '\r') { // Do not accept 'new line' and 'carriage return' commands - printf("Command = %c\r\n", *userCommand); - // handle_input(*userCommand); // -> Create this function to handle the user commands - } - } - #else - handle_input(userCommand,len); + #ifdef DEBUG_SERIAL_PROTOCOL + handle_input(userCommand, len); #endif } @@ -1542,9 +1541,11 @@ void poweroffPressCheck(void) { updateCurSpdLim(); beepShort(5); } else { // Long press: Calibrate ADC Limits + #ifdef AUTO_CALIBRATION_ENA beepLong(16); adcCalibLim(); beepShort(5); + #endif } } else if (cnt_press > 8) { // Short press: power off (80 ms debounce) poweroff(); From fc09e8b651b48b28c1101f18883beb892d57146d Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Fri, 29 Jan 2021 20:13:07 +0100 Subject: [PATCH 14/14] Update .travis.yml --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 29957e7..b0ebc5d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -48,8 +48,6 @@ jobs: - name: platformio script: platformio run language: python - python: - - "2.7" install: - pip install -U platformio - platformio update