flash based config testing

Dependencies:   mbed

Committer:
bwang
Date:
Thu Mar 02 07:04:47 2017 +0000
Revision:
1:df6bbacb7bb9
Child:
2:cfc39b0843ae
now with configuration terminal!

Who changed what in which revision?

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