Bayley Wang
/
flash_configuration
flash based config testing
CommandProcessor/CommandProcessor.cpp@2:cfc39b0843ae, 2017-03-02 (annotated)
- Committer:
- bwang
- Date:
- Thu Mar 02 07:36:56 2017 +0000
- Revision:
- 2:cfc39b0843ae
- Parent:
- 1:df6bbacb7bb9
- Child:
- 3:82c00c8e2cb4
stubs for run/cfg mode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 1:df6bbacb7bb9 | 1 | #include "mbed.h" |
bwang | 1:df6bbacb7bb9 | 2 | #include "CommandProcessor.h" |
bwang | 1:df6bbacb7bb9 | 3 | #include "PreferenceWriter.h" |
bwang | 2:cfc39b0843ae | 4 | #include "globals.h" |
bwang | 1:df6bbacb7bb9 | 5 | |
bwang | 1:df6bbacb7bb9 | 6 | #include "config.h" |
bwang | 1:df6bbacb7bb9 | 7 | #include "config_driving.h" |
bwang | 1:df6bbacb7bb9 | 8 | #include "config_inverter.h" |
bwang | 1:df6bbacb7bb9 | 9 | #include "config_logging.h" |
bwang | 1:df6bbacb7bb9 | 10 | #include "config_loop.h" |
bwang | 1:df6bbacb7bb9 | 11 | #include "config_motor.h" |
bwang | 1:df6bbacb7bb9 | 12 | #include "config_pins.h" |
bwang | 1:df6bbacb7bb9 | 13 | #include "config_table.h" |
bwang | 1:df6bbacb7bb9 | 14 | |
bwang | 1:df6bbacb7bb9 | 15 | void cmd_ls(Serial *pc) { |
bwang | 1:df6bbacb7bb9 | 16 | DPRINT(TORQUE_MODE); |
bwang | 1:df6bbacb7bb9 | 17 | FPRINT(MAX_TQPCT_PLUS); |
bwang | 1:df6bbacb7bb9 | 18 | FPRINT(MAX_TQPCT_MINUS); |
bwang | 1:df6bbacb7bb9 | 19 | FPRINT(TORQUE_MAX); |
bwang | 1:df6bbacb7bb9 | 20 | FPRINT(W_MAX); |
bwang | 1:df6bbacb7bb9 | 21 | FPRINT(BUS_VOLTAGE); |
bwang | 1:df6bbacb7bb9 | 22 | FPRINT(F_SW); |
bwang | 1:df6bbacb7bb9 | 23 | FPRINT(K_LOOP_D); |
bwang | 1:df6bbacb7bb9 | 24 | FPRINT(KI_BASE_D); |
bwang | 1:df6bbacb7bb9 | 25 | FPRINT(K_LOOP_Q); |
bwang | 1:df6bbacb7bb9 | 26 | FPRINT(KI_BASE_Q); |
bwang | 1:df6bbacb7bb9 | 27 | FPRINT(F_SLOW_LOOP); |
bwang | 1:df6bbacb7bb9 | 28 | FPRINT(INTEGRAL_MAX); |
bwang | 1:df6bbacb7bb9 | 29 | FPRINT(W_FILTER_STRENGTH); |
bwang | 1:df6bbacb7bb9 | 30 | FPRINT(DQ_FILTER_STRENGTH); |
bwang | 1:df6bbacb7bb9 | 31 | FPRINT(THROTTLE_FILTER_STRENGTH); |
bwang | 1:df6bbacb7bb9 | 32 | FPRINT(KP_D); |
bwang | 1:df6bbacb7bb9 | 33 | FPRINT(KI_D); |
bwang | 1:df6bbacb7bb9 | 34 | FPRINT(KP_Q); |
bwang | 1:df6bbacb7bb9 | 35 | FPRINT(KI_Q); |
bwang | 1:df6bbacb7bb9 | 36 | FPRINT(SLOW_LOOP_COUNTER); |
bwang | 1:df6bbacb7bb9 | 37 | FPRINT(POLE_PAIRS); |
bwang | 1:df6bbacb7bb9 | 38 | FPRINT(POS_OFFSET); |
bwang | 1:df6bbacb7bb9 | 39 | FPRINT(RESOLVER_LOBES); |
bwang | 1:df6bbacb7bb9 | 40 | DPRINT(CPR); |
bwang | 1:df6bbacb7bb9 | 41 | FPRINT(Ld); |
bwang | 1:df6bbacb7bb9 | 42 | FPRINT(Lq); |
bwang | 1:df6bbacb7bb9 | 43 | FPRINT(FLUX_LINKAGE); |
bwang | 1:df6bbacb7bb9 | 44 | FPRINT(Rs); |
bwang | 1:df6bbacb7bb9 | 45 | FPRINT(KT); |
bwang | 1:df6bbacb7bb9 | 46 | FPRINT(W_SAFE); |
bwang | 1:df6bbacb7bb9 | 47 | FPRINT(W_CRAZY); |
bwang | 1:df6bbacb7bb9 | 48 | DPRINT(TH_LIMIT_LOW); |
bwang | 1:df6bbacb7bb9 | 49 | DPRINT(TH_LIMIT_HIGH); |
bwang | 1:df6bbacb7bb9 | 50 | DPRINT(TH_LIMIT_CRAZY); |
bwang | 1:df6bbacb7bb9 | 51 | DPRINT(ROWS); |
bwang | 1:df6bbacb7bb9 | 52 | DPRINT(COLUMNS); |
bwang | 1:df6bbacb7bb9 | 53 | FPRINT(W_STEP); |
bwang | 1:df6bbacb7bb9 | 54 | } |
bwang | 1:df6bbacb7bb9 | 55 | |
bwang | 1:df6bbacb7bb9 | 56 | void cmd_ls2(Serial *pc, char *buf) { |
bwang | 1:df6bbacb7bb9 | 57 | float *fptr = checkf(buf); |
bwang | 1:df6bbacb7bb9 | 58 | if (fptr != NULL) pc->printf("%s: %f\n", buf, *fptr); |
bwang | 1:df6bbacb7bb9 | 59 | int *nptr = NULL; |
bwang | 1:df6bbacb7bb9 | 60 | if (fptr == NULL) nptr = checkn(buf); |
bwang | 1:df6bbacb7bb9 | 61 | if (nptr != NULL) pc->printf("%s: %d\n", buf, *nptr); |
bwang | 1:df6bbacb7bb9 | 62 | if (nptr == NULL && fptr == NULL) pc->printf("%s\n", "No Such Parameter"); |
bwang | 1:df6bbacb7bb9 | 63 | } |
bwang | 1:df6bbacb7bb9 | 64 | |
bwang | 1:df6bbacb7bb9 | 65 | void cmd_defaults(Serial *pc) { |
bwang | 1:df6bbacb7bb9 | 66 | DEFAULT(TORQUE_MODE); |
bwang | 1:df6bbacb7bb9 | 67 | DEFAULT(MAX_TQPCT_PLUS); |
bwang | 1:df6bbacb7bb9 | 68 | DEFAULT(MAX_TQPCT_MINUS); |
bwang | 1:df6bbacb7bb9 | 69 | DEFAULT(TORQUE_MAX); |
bwang | 1:df6bbacb7bb9 | 70 | DEFAULT(W_MAX); |
bwang | 1:df6bbacb7bb9 | 71 | DEFAULT(BUS_VOLTAGE); |
bwang | 1:df6bbacb7bb9 | 72 | DEFAULT(F_SW); |
bwang | 1:df6bbacb7bb9 | 73 | DEFAULT(K_LOOP_D); |
bwang | 1:df6bbacb7bb9 | 74 | DEFAULT(KI_BASE_D); |
bwang | 1:df6bbacb7bb9 | 75 | DEFAULT(K_LOOP_Q); |
bwang | 1:df6bbacb7bb9 | 76 | DEFAULT(KI_BASE_Q); |
bwang | 1:df6bbacb7bb9 | 77 | DEFAULT(F_SLOW_LOOP); |
bwang | 1:df6bbacb7bb9 | 78 | DEFAULT(INTEGRAL_MAX); |
bwang | 1:df6bbacb7bb9 | 79 | DEFAULT(W_FILTER_STRENGTH); |
bwang | 1:df6bbacb7bb9 | 80 | DEFAULT(DQ_FILTER_STRENGTH); |
bwang | 1:df6bbacb7bb9 | 81 | DEFAULT(THROTTLE_FILTER_STRENGTH); |
bwang | 1:df6bbacb7bb9 | 82 | DEFAULT(KP_D); |
bwang | 1:df6bbacb7bb9 | 83 | DEFAULT(KI_D); |
bwang | 1:df6bbacb7bb9 | 84 | DEFAULT(KP_Q); |
bwang | 1:df6bbacb7bb9 | 85 | DEFAULT(KI_Q); |
bwang | 1:df6bbacb7bb9 | 86 | DEFAULT(SLOW_LOOP_COUNTER); |
bwang | 1:df6bbacb7bb9 | 87 | DEFAULT(POLE_PAIRS); |
bwang | 1:df6bbacb7bb9 | 88 | DEFAULT(POS_OFFSET); |
bwang | 1:df6bbacb7bb9 | 89 | DEFAULT(RESOLVER_LOBES); |
bwang | 1:df6bbacb7bb9 | 90 | DEFAULT(CPR); |
bwang | 1:df6bbacb7bb9 | 91 | DEFAULT(Ld); |
bwang | 1:df6bbacb7bb9 | 92 | DEFAULT(Lq); |
bwang | 1:df6bbacb7bb9 | 93 | DEFAULT(FLUX_LINKAGE); |
bwang | 1:df6bbacb7bb9 | 94 | DEFAULT(Rs); |
bwang | 1:df6bbacb7bb9 | 95 | DEFAULT(KT); |
bwang | 1:df6bbacb7bb9 | 96 | DEFAULT(W_SAFE); |
bwang | 1:df6bbacb7bb9 | 97 | DEFAULT(W_CRAZY); |
bwang | 1:df6bbacb7bb9 | 98 | DEFAULT(TH_LIMIT_LOW); |
bwang | 1:df6bbacb7bb9 | 99 | DEFAULT(TH_LIMIT_HIGH); |
bwang | 1:df6bbacb7bb9 | 100 | DEFAULT(TH_LIMIT_CRAZY); |
bwang | 1:df6bbacb7bb9 | 101 | DEFAULT(ROWS); |
bwang | 1:df6bbacb7bb9 | 102 | DEFAULT(COLUMNS); |
bwang | 1:df6bbacb7bb9 | 103 | DEFAULT(W_STEP); |
bwang | 1:df6bbacb7bb9 | 104 | pc->printf("Defaults Loaded\n"); |
bwang | 1:df6bbacb7bb9 | 105 | } |
bwang | 1:df6bbacb7bb9 | 106 | |
bwang | 1:df6bbacb7bb9 | 107 | void cmd_reload(Serial *pc, PreferenceWriter *pref) { |
bwang | 1:df6bbacb7bb9 | 108 | pref->load(); |
bwang | 1:df6bbacb7bb9 | 109 | pc->printf("Flash Values Loaded\n"); |
bwang | 1:df6bbacb7bb9 | 110 | } |
bwang | 1:df6bbacb7bb9 | 111 | |
bwang | 1:df6bbacb7bb9 | 112 | void cmd_set(Serial *pc, char *buf, char *val) { |
bwang | 1:df6bbacb7bb9 | 113 | float *fptr = checkf(buf); |
bwang | 1:df6bbacb7bb9 | 114 | if (fptr != NULL) *fptr = (float) (atof(val)); |
bwang | 1:df6bbacb7bb9 | 115 | int *nptr = NULL; |
bwang | 1:df6bbacb7bb9 | 116 | if (fptr == NULL) nptr = checkn(buf); |
bwang | 1:df6bbacb7bb9 | 117 | if (nptr != NULL) *nptr = (int) (atoi(val)); |
bwang | 1:df6bbacb7bb9 | 118 | cmd_ls2(pc, buf); |
bwang | 1:df6bbacb7bb9 | 119 | if (nptr == NULL && fptr == NULL) pc->printf("%s\n", "No Such Parameter"); |
bwang | 1:df6bbacb7bb9 | 120 | } |
bwang | 1:df6bbacb7bb9 | 121 | |
bwang | 1:df6bbacb7bb9 | 122 | void cmd_flush(Serial *pc, PreferenceWriter *pref) { |
bwang | 1:df6bbacb7bb9 | 123 | if (!pref->ready()) pref->open(); |
bwang | 1:df6bbacb7bb9 | 124 | pref->flush(); |
bwang | 1:df6bbacb7bb9 | 125 | } |
bwang | 1:df6bbacb7bb9 | 126 | |
bwang | 2:cfc39b0843ae | 127 | void cmd_cfg(Serial *pc) { |
bwang | 2:cfc39b0843ae | 128 | mode = MODE_CFG; |
bwang | 2:cfc39b0843ae | 129 | pc->printf("%s\n", "Config mode ON"); |
bwang | 2:cfc39b0843ae | 130 | } |
bwang | 2:cfc39b0843ae | 131 | |
bwang | 2:cfc39b0843ae | 132 | void cmd_exit(Serial *pc) { |
bwang | 2:cfc39b0843ae | 133 | mode = MODE_RUN; |
bwang | 2:cfc39b0843ae | 134 | pc->printf("%s\n", "Run mode ON"); |
bwang | 2:cfc39b0843ae | 135 | } |
bwang | 2:cfc39b0843ae | 136 | |
bwang | 1:df6bbacb7bb9 | 137 | void processCmd(Serial *pc, PreferenceWriter *pref, char *buf) { |
bwang | 1:df6bbacb7bb9 | 138 | char *tokens[10]; |
bwang | 1:df6bbacb7bb9 | 139 | int len = tokenize(buf, tokens, 10); |
bwang | 1:df6bbacb7bb9 | 140 | |
bwang | 2:cfc39b0843ae | 141 | if (mode == MODE_RUN) { |
bwang | 2:cfc39b0843ae | 142 | if (len == 1 && strcmp(tokens[0], "cfg") == 0) { |
bwang | 2:cfc39b0843ae | 143 | cmd_cfg(pc); |
bwang | 2:cfc39b0843ae | 144 | } else { |
bwang | 2:cfc39b0843ae | 145 | return; |
bwang | 2:cfc39b0843ae | 146 | } |
bwang | 2:cfc39b0843ae | 147 | } |
bwang | 2:cfc39b0843ae | 148 | |
bwang | 1:df6bbacb7bb9 | 149 | switch (len) { |
bwang | 1:df6bbacb7bb9 | 150 | case 1: |
bwang | 1:df6bbacb7bb9 | 151 | if (strcmp(tokens[0], "ls") == 0) cmd_ls(pc); |
bwang | 1:df6bbacb7bb9 | 152 | if (strcmp(tokens[0], "defaults") == 0) cmd_defaults(pc); |
bwang | 1:df6bbacb7bb9 | 153 | if (strcmp(tokens[0], "reload") == 0) cmd_reload(pc, pref); |
bwang | 1:df6bbacb7bb9 | 154 | if (strcmp(tokens[0], "load") == 0) cmd_reload(pc, pref); |
bwang | 1:df6bbacb7bb9 | 155 | if (strcmp(tokens[0], "flush") == 0) cmd_flush(pc, pref); |
bwang | 2:cfc39b0843ae | 156 | if (strcmp(tokens[0], "exit") == 0) cmd_exit(pc); |
bwang | 1:df6bbacb7bb9 | 157 | break; |
bwang | 1:df6bbacb7bb9 | 158 | case 2: |
bwang | 1:df6bbacb7bb9 | 159 | if (strcmp(tokens[0], "ls") == 0) cmd_ls2(pc, tokens[1]); |
bwang | 1:df6bbacb7bb9 | 160 | if (strcmp(tokens[0], "get") == 0) cmd_ls2(pc, tokens[1]); |
bwang | 1:df6bbacb7bb9 | 161 | break; |
bwang | 1:df6bbacb7bb9 | 162 | case 3: |
bwang | 1:df6bbacb7bb9 | 163 | if (strcmp(tokens[0], "set") == 0) cmd_set(pc, tokens[1], tokens[2]); |
bwang | 1:df6bbacb7bb9 | 164 | default: |
bwang | 1:df6bbacb7bb9 | 165 | break; |
bwang | 1:df6bbacb7bb9 | 166 | } |
bwang | 1:df6bbacb7bb9 | 167 | } |
bwang | 1:df6bbacb7bb9 | 168 | |
bwang | 1:df6bbacb7bb9 | 169 | int tokenize(char *buf, char **out, int max) { |
bwang | 1:df6bbacb7bb9 | 170 | char* tok; |
bwang | 1:df6bbacb7bb9 | 171 | int k = 0; |
bwang | 1:df6bbacb7bb9 | 172 | |
bwang | 1:df6bbacb7bb9 | 173 | tok = strtok(buf, " "); |
bwang | 1:df6bbacb7bb9 | 174 | |
bwang | 1:df6bbacb7bb9 | 175 | while(tok != NULL && k < max) { |
bwang | 1:df6bbacb7bb9 | 176 | out[k] = tok; |
bwang | 1:df6bbacb7bb9 | 177 | k++; |
bwang | 1:df6bbacb7bb9 | 178 | tok = strtok(NULL, " "); |
bwang | 1:df6bbacb7bb9 | 179 | } |
bwang | 1:df6bbacb7bb9 | 180 | return k; |
bwang | 1:df6bbacb7bb9 | 181 | } |
bwang | 1:df6bbacb7bb9 | 182 | |
bwang | 1:df6bbacb7bb9 | 183 | #define __check(x) if(strcmp(s, #x) == 0) return &_##x |
bwang | 1:df6bbacb7bb9 | 184 | float* checkf(char *s) { |
bwang | 1:df6bbacb7bb9 | 185 | __check(MAX_TQPCT_PLUS); |
bwang | 1:df6bbacb7bb9 | 186 | __check(MAX_TQPCT_MINUS); |
bwang | 1:df6bbacb7bb9 | 187 | __check(TORQUE_MAX); |
bwang | 1:df6bbacb7bb9 | 188 | __check(W_MAX); |
bwang | 1:df6bbacb7bb9 | 189 | __check(BUS_VOLTAGE); |
bwang | 1:df6bbacb7bb9 | 190 | __check(F_SW); |
bwang | 1:df6bbacb7bb9 | 191 | __check(K_LOOP_D); |
bwang | 1:df6bbacb7bb9 | 192 | __check(KI_BASE_D); |
bwang | 1:df6bbacb7bb9 | 193 | __check(K_LOOP_Q); |
bwang | 1:df6bbacb7bb9 | 194 | __check(KI_BASE_Q); |
bwang | 1:df6bbacb7bb9 | 195 | __check(F_SLOW_LOOP); |
bwang | 1:df6bbacb7bb9 | 196 | __check(INTEGRAL_MAX); |
bwang | 1:df6bbacb7bb9 | 197 | __check(W_FILTER_STRENGTH); |
bwang | 1:df6bbacb7bb9 | 198 | __check(DQ_FILTER_STRENGTH); |
bwang | 1:df6bbacb7bb9 | 199 | __check(THROTTLE_FILTER_STRENGTH); |
bwang | 1:df6bbacb7bb9 | 200 | __check(KP_D); |
bwang | 1:df6bbacb7bb9 | 201 | __check(KI_D); |
bwang | 1:df6bbacb7bb9 | 202 | __check(KP_Q); |
bwang | 1:df6bbacb7bb9 | 203 | __check(KI_Q); |
bwang | 1:df6bbacb7bb9 | 204 | __check(POLE_PAIRS); |
bwang | 1:df6bbacb7bb9 | 205 | __check(POS_OFFSET); |
bwang | 1:df6bbacb7bb9 | 206 | __check(RESOLVER_LOBES); |
bwang | 1:df6bbacb7bb9 | 207 | __check(Ld); |
bwang | 1:df6bbacb7bb9 | 208 | __check(Lq); |
bwang | 1:df6bbacb7bb9 | 209 | __check(FLUX_LINKAGE); |
bwang | 1:df6bbacb7bb9 | 210 | __check(Rs); |
bwang | 1:df6bbacb7bb9 | 211 | __check(KT); |
bwang | 1:df6bbacb7bb9 | 212 | __check(W_SAFE); |
bwang | 1:df6bbacb7bb9 | 213 | __check(W_CRAZY); |
bwang | 1:df6bbacb7bb9 | 214 | __check(W_STEP); |
bwang | 1:df6bbacb7bb9 | 215 | return NULL; |
bwang | 1:df6bbacb7bb9 | 216 | } |
bwang | 1:df6bbacb7bb9 | 217 | |
bwang | 1:df6bbacb7bb9 | 218 | int* checkn(char *s) { |
bwang | 1:df6bbacb7bb9 | 219 | __check(TORQUE_MODE); |
bwang | 1:df6bbacb7bb9 | 220 | __check(CPR); |
bwang | 1:df6bbacb7bb9 | 221 | __check(TH_LIMIT_LOW); |
bwang | 1:df6bbacb7bb9 | 222 | __check(TH_LIMIT_HIGH); |
bwang | 1:df6bbacb7bb9 | 223 | __check(TH_LIMIT_CRAZY); |
bwang | 1:df6bbacb7bb9 | 224 | __check(ROWS); |
bwang | 1:df6bbacb7bb9 | 225 | __check(COLUMNS); |
bwang | 1:df6bbacb7bb9 | 226 | return NULL; |
bwang | 1:df6bbacb7bb9 | 227 | } |