Bayley Wang
/
flash_configuration
flash based config testing
main.cpp@3:82c00c8e2cb4, 2017-03-07 (annotated)
- Committer:
- bwang
- Date:
- Tue Mar 07 08:06:39 2017 +0000
- Revision:
- 3:82c00c8e2cb4
- Parent:
- 2:cfc39b0843ae
lot's more commands
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 0:941e150e78b6 | 1 | #include "mbed.h" |
bwang | 3:82c00c8e2cb4 | 2 | #include "math.h" |
bwang | 1:df6bbacb7bb9 | 3 | |
bwang | 0:941e150e78b6 | 4 | #include "PreferenceWriter.h" |
bwang | 0:941e150e78b6 | 5 | #include "FlashWriter.h" |
bwang | 1:df6bbacb7bb9 | 6 | #include "CommandProcessor.h" |
bwang | 2:cfc39b0843ae | 7 | #include "config.h" |
bwang | 0:941e150e78b6 | 8 | |
bwang | 0:941e150e78b6 | 9 | Serial pc(USBTX, USBRX); |
bwang | 0:941e150e78b6 | 10 | PreferenceWriter pref(6); |
bwang | 3:82c00c8e2cb4 | 11 | PwmOut led(LED1); |
bwang | 0:941e150e78b6 | 12 | |
bwang | 3:82c00c8e2cb4 | 13 | int index = 0, loop_counter = 0; |
bwang | 1:df6bbacb7bb9 | 14 | char linebuf[128]; |
bwang | 3:82c00c8e2cb4 | 15 | float p = 0.0f; |
bwang | 3:82c00c8e2cb4 | 16 | float torque_pct = 0.0f, user_cmd = 0.0f; |
bwang | 3:82c00c8e2cb4 | 17 | float vout = 0.0f; |
bwang | 1:df6bbacb7bb9 | 18 | |
bwang | 1:df6bbacb7bb9 | 19 | void rxCallback() { |
bwang | 1:df6bbacb7bb9 | 20 | while (pc.readable()) { |
bwang | 1:df6bbacb7bb9 | 21 | char c = pc.getc(); |
bwang | 1:df6bbacb7bb9 | 22 | if (c != 127 && c != '\r' && c != '\t') { |
bwang | 1:df6bbacb7bb9 | 23 | linebuf[index] = c; |
bwang | 1:df6bbacb7bb9 | 24 | index++; |
bwang | 1:df6bbacb7bb9 | 25 | pc.putc(c); |
bwang | 1:df6bbacb7bb9 | 26 | } else if (c == 127) { |
bwang | 1:df6bbacb7bb9 | 27 | if (index > 0) { |
bwang | 1:df6bbacb7bb9 | 28 | index--; |
bwang | 1:df6bbacb7bb9 | 29 | pc.putc(c); |
bwang | 1:df6bbacb7bb9 | 30 | } |
bwang | 1:df6bbacb7bb9 | 31 | } else if (c == '\r') { |
bwang | 1:df6bbacb7bb9 | 32 | linebuf[index] = 0; |
bwang | 1:df6bbacb7bb9 | 33 | pc.putc(c); |
bwang | 1:df6bbacb7bb9 | 34 | processCmd(&pc, &pref, linebuf); |
bwang | 1:df6bbacb7bb9 | 35 | index = 0; |
bwang | 1:df6bbacb7bb9 | 36 | pc.putc('>'); |
bwang | 1:df6bbacb7bb9 | 37 | } |
bwang | 1:df6bbacb7bb9 | 38 | } |
bwang | 1:df6bbacb7bb9 | 39 | } |
bwang | 1:df6bbacb7bb9 | 40 | |
bwang | 3:82c00c8e2cb4 | 41 | void slow_loop() { |
bwang | 3:82c00c8e2cb4 | 42 | switch (BREMS_src) { |
bwang | 3:82c00c8e2cb4 | 43 | case CMD_SRC_RC: |
bwang | 3:82c00c8e2cb4 | 44 | //rc throttle code here; |
bwang | 3:82c00c8e2cb4 | 45 | break; |
bwang | 3:82c00c8e2cb4 | 46 | case CMD_SRC_ANALOG: |
bwang | 3:82c00c8e2cb4 | 47 | //analog throttle code here; |
bwang | 3:82c00c8e2cb4 | 48 | break; |
bwang | 3:82c00c8e2cb4 | 49 | case CMD_SRC_SERIAL: |
bwang | 3:82c00c8e2cb4 | 50 | case CMD_SRC_TERMINAL: |
bwang | 3:82c00c8e2cb4 | 51 | case CMD_SRC_CAN: |
bwang | 3:82c00c8e2cb4 | 52 | //we presume these are set by free-running interrupts; |
bwang | 3:82c00c8e2cb4 | 53 | break; |
bwang | 3:82c00c8e2cb4 | 54 | default: |
bwang | 3:82c00c8e2cb4 | 55 | break; |
bwang | 2:cfc39b0843ae | 56 | } |
bwang | 2:cfc39b0843ae | 57 | } |
bwang | 2:cfc39b0843ae | 58 | |
bwang | 3:82c00c8e2cb4 | 59 | void commutate() { |
bwang | 3:82c00c8e2cb4 | 60 | p += 0.001f; |
bwang | 3:82c00c8e2cb4 | 61 | if (p >= 6.28318f) p = 0.0f; |
bwang | 3:82c00c8e2cb4 | 62 | |
bwang | 3:82c00c8e2cb4 | 63 | loop_counter++; |
bwang | 3:82c00c8e2cb4 | 64 | if (loop_counter % 50 == 0) { |
bwang | 3:82c00c8e2cb4 | 65 | loop_counter = 0; |
bwang | 3:82c00c8e2cb4 | 66 | slow_loop(); |
bwang | 3:82c00c8e2cb4 | 67 | } |
bwang | 3:82c00c8e2cb4 | 68 | |
bwang | 3:82c00c8e2cb4 | 69 | switch (BREMS_op) { |
bwang | 3:82c00c8e2cb4 | 70 | case OP_TORQUE: |
bwang | 3:82c00c8e2cb4 | 71 | torque_pct = user_cmd; |
bwang | 3:82c00c8e2cb4 | 72 | break; |
bwang | 3:82c00c8e2cb4 | 73 | case OP_DRIVING: |
bwang | 3:82c00c8e2cb4 | 74 | //driving mode here; |
bwang | 3:82c00c8e2cb4 | 75 | break; |
bwang | 3:82c00c8e2cb4 | 76 | case OP_SPEED: |
bwang | 3:82c00c8e2cb4 | 77 | //speed loop here; |
bwang | 3:82c00c8e2cb4 | 78 | break; |
bwang | 3:82c00c8e2cb4 | 79 | case OP_POSITION: |
bwang | 3:82c00c8e2cb4 | 80 | //position loop here; |
bwang | 3:82c00c8e2cb4 | 81 | break; |
bwang | 3:82c00c8e2cb4 | 82 | } |
bwang | 3:82c00c8e2cb4 | 83 | |
bwang | 3:82c00c8e2cb4 | 84 | switch (BREMS_mode) { |
bwang | 3:82c00c8e2cb4 | 85 | case MODE_RUN: |
bwang | 3:82c00c8e2cb4 | 86 | vout = torque_pct * sinf(p); |
bwang | 3:82c00c8e2cb4 | 87 | break; |
bwang | 3:82c00c8e2cb4 | 88 | case MODE_CFG: |
bwang | 3:82c00c8e2cb4 | 89 | vout = 0.0f; |
bwang | 3:82c00c8e2cb4 | 90 | break; |
bwang | 3:82c00c8e2cb4 | 91 | case MODE_ZERO: |
bwang | 3:82c00c8e2cb4 | 92 | //zero'ing code here; |
bwang | 3:82c00c8e2cb4 | 93 | break; |
bwang | 3:82c00c8e2cb4 | 94 | case MODE_CHR: |
bwang | 3:82c00c8e2cb4 | 95 | //wizarding code here; |
bwang | 3:82c00c8e2cb4 | 96 | break; |
bwang | 3:82c00c8e2cb4 | 97 | default: |
bwang | 3:82c00c8e2cb4 | 98 | break; |
bwang | 3:82c00c8e2cb4 | 99 | } |
bwang | 3:82c00c8e2cb4 | 100 | |
bwang | 3:82c00c8e2cb4 | 101 | led = 0.5f + 0.5f * vout; |
bwang | 3:82c00c8e2cb4 | 102 | } |
bwang | 3:82c00c8e2cb4 | 103 | |
bwang | 3:82c00c8e2cb4 | 104 | int main() { |
bwang | 0:941e150e78b6 | 105 | pc.baud(115200); |
bwang | 1:df6bbacb7bb9 | 106 | pc.attach(rxCallback); |
bwang | 3:82c00c8e2cb4 | 107 | pc.printf("%s\n", "FOC'ed in the bot version A"); |
bwang | 3:82c00c8e2cb4 | 108 | cmd_reload(&pc, &pref); |
bwang | 1:df6bbacb7bb9 | 109 | pc.printf("%s", ">"); |
bwang | 0:941e150e78b6 | 110 | |
bwang | 2:cfc39b0843ae | 111 | Ticker tick; |
bwang | 2:cfc39b0843ae | 112 | tick.attach_us(commutate, 200); |
bwang | 2:cfc39b0843ae | 113 | |
bwang | 0:941e150e78b6 | 114 | for(;;) { |
bwang | 0:941e150e78b6 | 115 | } |
bwang | 0:941e150e78b6 | 116 | } |