David Salmon
/
ES_CW2_Starter_MDMA
ES2017 coursework 2
Fork of ES_CW2_Starter by
main.cpp@15:d9e50101a17e, 2017-03-09 (annotated)
- Committer:
- theMaO
- Date:
- Thu Mar 09 14:56:06 2017 +0000
- Revision:
- 15:d9e50101a17e
- Parent:
- 14:155e9a9147d4
- Parent:
- 13:87ab3b008803
- Child:
- 16:372c720015ab
merge; maybe
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
estott | 0:de4320f74764 | 1 | #include "mbed.h" |
estott | 0:de4320f74764 | 2 | #include "rtos.h" |
david_s95 | 5:e5313b695302 | 3 | #include <string> |
david_s95 | 10:0309d6c49f26 | 4 | #include "PID.h" |
david_s95 | 10:0309d6c49f26 | 5 | |
david_s95 | 10:0309d6c49f26 | 6 | //PID controller configuration |
david_s95 | 10:0309d6c49f26 | 7 | float PIDrate = 0.2; |
david_s95 | 13:87ab3b008803 | 8 | float Kc = 4.5; |
david_s95 | 13:87ab3b008803 | 9 | float Ti = 0.1; |
david_s95 | 13:87ab3b008803 | 10 | float Td = 0.5; |
david_s95 | 10:0309d6c49f26 | 11 | float speedControl = 0; |
david_s95 | 10:0309d6c49f26 | 12 | PID controller(Kc, Ti, Td, PIDrate); |
david_s95 | 10:0309d6c49f26 | 13 | Thread VPIDthread; |
estott | 0:de4320f74764 | 14 | |
estott | 0:de4320f74764 | 15 | //Photointerrupter input pins |
estott | 0:de4320f74764 | 16 | #define I1pin D2 |
estott | 2:4e88faab6988 | 17 | #define I2pin D11 |
estott | 2:4e88faab6988 | 18 | #define I3pin D12 |
estott | 2:4e88faab6988 | 19 | |
estott | 2:4e88faab6988 | 20 | //Incremental encoder input pins |
estott | 2:4e88faab6988 | 21 | #define CHA D7 |
david_s95 | 5:e5313b695302 | 22 | #define CHB D8 |
estott | 0:de4320f74764 | 23 | |
estott | 0:de4320f74764 | 24 | //Motor Drive output pins //Mask in output byte |
estott | 2:4e88faab6988 | 25 | #define L1Lpin D4 //0x01 |
estott | 2:4e88faab6988 | 26 | #define L1Hpin D5 //0x02 |
estott | 2:4e88faab6988 | 27 | #define L2Lpin D3 //0x04 |
estott | 2:4e88faab6988 | 28 | #define L2Hpin D6 //0x08 |
estott | 2:4e88faab6988 | 29 | #define L3Lpin D9 //0x10 |
estott | 0:de4320f74764 | 30 | #define L3Hpin D10 //0x20 |
estott | 0:de4320f74764 | 31 | |
david_s95 | 5:e5313b695302 | 32 | //Define sized for command arrays |
david_s95 | 5:e5313b695302 | 33 | #define ARRAYSIZE 8 |
david_s95 | 5:e5313b695302 | 34 | |
estott | 0:de4320f74764 | 35 | //Mapping from sequential drive states to motor phase outputs |
estott | 0:de4320f74764 | 36 | /* |
estott | 0:de4320f74764 | 37 | State L1 L2 L3 |
estott | 0:de4320f74764 | 38 | 0 H - L |
estott | 0:de4320f74764 | 39 | 1 - H L |
estott | 0:de4320f74764 | 40 | 2 L H - |
estott | 0:de4320f74764 | 41 | 3 L - H |
estott | 0:de4320f74764 | 42 | 4 - L H |
estott | 0:de4320f74764 | 43 | 5 H L - |
estott | 0:de4320f74764 | 44 | 6 - - - |
estott | 0:de4320f74764 | 45 | 7 - - - |
estott | 0:de4320f74764 | 46 | */ |
estott | 0:de4320f74764 | 47 | //Drive state to output table |
david_s95 | 9:575b29cbf5e4 | 48 | //const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
david_s95 | 9:575b29cbf5e4 | 49 | const int8_t driveTable[6] = {0x38, 0x2C, 0x0E, 0x0B, 0x23, 0x32}; |
david_s95 | 9:575b29cbf5e4 | 50 | |
david_s95 | 9:575b29cbf5e4 | 51 | //const int8_t cwState[7] = {0, 4, 0, 5, 2, 3, 1}; |
david_s95 | 9:575b29cbf5e4 | 52 | //const int8_t AcwState[7] = {0, 2, 4, 3, 0, 1, 5}; |
david_s95 | 9:575b29cbf5e4 | 53 | //const int8_t cwState[7] = {0x00, 0x23, 0x38, 0x32, 0x0E, 0x0B, 0x2C}; |
david_s95 | 9:575b29cbf5e4 | 54 | //const int8_t AcwState[7] = {0x00, 0x0E, 0x23, 0x0B, 0x38, 0x2C, 0x32}; |
david_s95 | 9:575b29cbf5e4 | 55 | const int8_t AcwState[7] = {0x00, 0x23, 0x38, 0x32, 0x0E, 0x0B, 0x2C}; |
david_s95 | 9:575b29cbf5e4 | 56 | const int8_t cwState[7] = {0x00, 0x0E, 0x23, 0x0B, 0x38, 0x2C, 0x32}; |
estott | 2:4e88faab6988 | 57 | |
estott | 0:de4320f74764 | 58 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
david_s95 | 9:575b29cbf5e4 | 59 | //const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
estott | 2:4e88faab6988 | 60 | //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed |
estott | 2:4e88faab6988 | 61 | |
estott | 2:4e88faab6988 | 62 | //Phase lead to make motor spin |
david_s95 | 9:575b29cbf5e4 | 63 | //int8_t lead = -2; //2 for forwards, -2 for backwards |
estott | 0:de4320f74764 | 64 | |
estott | 0:de4320f74764 | 65 | //Status LED |
estott | 0:de4320f74764 | 66 | DigitalOut led1(LED1); |
estott | 0:de4320f74764 | 67 | |
estott | 0:de4320f74764 | 68 | //Photointerrupter inputs |
david_s95 | 9:575b29cbf5e4 | 69 | DigitalIn I1(I1pin); |
david_s95 | 9:575b29cbf5e4 | 70 | //InterruptIn I1(I1pin); |
david_s95 | 9:575b29cbf5e4 | 71 | InterruptIn I2(I2pin); |
estott | 2:4e88faab6988 | 72 | DigitalIn I3(I3pin); |
estott | 0:de4320f74764 | 73 | |
david_s95 | 8:77627657da80 | 74 | InterruptIn qA(CHA); |
david_s95 | 8:77627657da80 | 75 | InterruptIn qB(CHB); |
david_s95 | 8:77627657da80 | 76 | |
estott | 0:de4320f74764 | 77 | //Motor Drive outputs |
david_s95 | 9:575b29cbf5e4 | 78 | //DigitalOut L1L(L1Lpin); |
david_s95 | 9:575b29cbf5e4 | 79 | //DigitalOut L1H(L1Hpin); |
david_s95 | 9:575b29cbf5e4 | 80 | //DigitalOut L2L(L2Lpin); |
david_s95 | 9:575b29cbf5e4 | 81 | //DigitalOut L2H(L2Hpin); |
david_s95 | 9:575b29cbf5e4 | 82 | //DigitalOut L3L(L3Lpin); |
david_s95 | 9:575b29cbf5e4 | 83 | //DigitalOut L3H(L3Hpin); |
david_s95 | 5:e5313b695302 | 84 | DigitalOut clk(LED1); |
david_s95 | 8:77627657da80 | 85 | DigitalOut Direction(LED2); |
david_s95 | 9:575b29cbf5e4 | 86 | DigitalOut testpin(D13); |
david_s95 | 9:575b29cbf5e4 | 87 | |
david_s95 | 9:575b29cbf5e4 | 88 | //NOTE, BusOut declares things in reverse (ie, 0, 1, 2, 3) compared to binary represenation |
david_s95 | 9:575b29cbf5e4 | 89 | BusOut motor(L1Lpin, L1Hpin, L2Lpin, L2Hpin, L3Lpin, L3Hpin); |
david_s95 | 5:e5313b695302 | 90 | |
david_s95 | 5:e5313b695302 | 91 | //Timeout function for rotating at set speed |
david_s95 | 5:e5313b695302 | 92 | Timeout spinTimer; |
david_s95 | 5:e5313b695302 | 93 | float spinWait = 10; |
david_s95 | 5:e5313b695302 | 94 | float revsec = 0; |
david_s95 | 5:e5313b695302 | 95 | |
theMaO | 14:155e9a9147d4 | 96 | //Variables for spinning N revolutions |
theMaO | 14:155e9a9147d4 | 97 | int8_t targetRevs = 0; |
theMaO | 14:155e9a9147d4 | 98 | int8_t currRevs = 0; |
theMaO | 14:155e9a9147d4 | 99 | |
david_s95 | 7:5932ed0bad6d | 100 | //Timer used for calculating speed |
david_s95 | 7:5932ed0bad6d | 101 | Timer speedTimer; |
david_s95 | 10:0309d6c49f26 | 102 | float measuredRevs = 0, revtimer = 0; |
david_s95 | 7:5932ed0bad6d | 103 | Ticker printSpeed; |
david_s95 | 7:5932ed0bad6d | 104 | |
david_s95 | 5:e5313b695302 | 105 | Serial pc(SERIAL_TX, SERIAL_RX); |
david_s95 | 5:e5313b695302 | 106 | |
david_s95 | 5:e5313b695302 | 107 | int8_t orState = 0; //Rotor offset at motor state 0 |
david_s95 | 5:e5313b695302 | 108 | int8_t intState = 0; |
david_s95 | 5:e5313b695302 | 109 | int8_t intStateOld = 0; |
david_s95 | 9:575b29cbf5e4 | 110 | int position = 0; |
david_s95 | 5:e5313b695302 | 111 | |
david_s95 | 5:e5313b695302 | 112 | int i=0; |
david_s95 | 10:0309d6c49f26 | 113 | int quadraturePosition=0; |
david_s95 | 9:575b29cbf5e4 | 114 | bool spinCW=0; |
david_s95 | 10:0309d6c49f26 | 115 | float u = 0; //Set point for VPI |
david_s95 | 5:e5313b695302 | 116 | |
estott | 0:de4320f74764 | 117 | //Set a given drive state |
david_s95 | 5:e5313b695302 | 118 | void motorOut(int8_t driveState) |
david_s95 | 5:e5313b695302 | 119 | { |
david_s95 | 5:e5313b695302 | 120 | |
david_s95 | 9:575b29cbf5e4 | 121 | //Set to zero |
david_s95 | 9:575b29cbf5e4 | 122 | motor=0x2A; |
david_s95 | 10:0309d6c49f26 | 123 | |
david_s95 | 9:575b29cbf5e4 | 124 | //Go to next state |
david_s95 | 9:575b29cbf5e4 | 125 | if(!spinCW) motor = AcwState[driveState]; |
david_s95 | 9:575b29cbf5e4 | 126 | else motor = cwState[driveState]; |
estott | 2:4e88faab6988 | 127 | //Lookup the output byte from the drive state. |
david_s95 | 9:575b29cbf5e4 | 128 | // int8_t driveOut = driveTable[driveState & 0x07]; |
david_s95 | 10:0309d6c49f26 | 129 | } |
david_s95 | 5:e5313b695302 | 130 | |
david_s95 | 10:0309d6c49f26 | 131 | inline void motorStop() |
david_s95 | 10:0309d6c49f26 | 132 | { |
david_s95 | 10:0309d6c49f26 | 133 | //revsec set to zero prevents recurring interrupt for constant speed |
david_s95 | 10:0309d6c49f26 | 134 | revsec = 0; |
david_s95 | 10:0309d6c49f26 | 135 | //0x2A turns all motor transistors off to prevent any power usage |
david_s95 | 10:0309d6c49f26 | 136 | motor = 0x2A; |
david_s95 | 5:e5313b695302 | 137 | } |
david_s95 | 5:e5313b695302 | 138 | |
david_s95 | 5:e5313b695302 | 139 | //Convert photointerrupter inputs to a rotor state |
david_s95 | 5:e5313b695302 | 140 | inline int8_t readRotorState() |
david_s95 | 5:e5313b695302 | 141 | { |
david_s95 | 9:575b29cbf5e4 | 142 | return (I1 + 2*I2 + 4*I3); |
david_s95 | 5:e5313b695302 | 143 | } |
estott | 0:de4320f74764 | 144 | |
david_s95 | 5:e5313b695302 | 145 | //Basic synchronisation routine |
david_s95 | 5:e5313b695302 | 146 | int8_t motorHome() |
david_s95 | 5:e5313b695302 | 147 | { |
estott | 0:de4320f74764 | 148 | //Put the motor in drive state 0 and wait for it to stabilise |
david_s95 | 9:575b29cbf5e4 | 149 | motor=cwState[1]; |
estott | 0:de4320f74764 | 150 | wait(1.0); |
david_s95 | 10:0309d6c49f26 | 151 | |
david_s95 | 9:575b29cbf5e4 | 152 | position = 0; |
david_s95 | 5:e5313b695302 | 153 | |
estott | 0:de4320f74764 | 154 | //Get the rotor state |
estott | 2:4e88faab6988 | 155 | return readRotorState(); |
estott | 0:de4320f74764 | 156 | } |
david_s95 | 5:e5313b695302 | 157 | |
david_s95 | 5:e5313b695302 | 158 | void fixedSpeed() |
david_s95 | 5:e5313b695302 | 159 | { |
david_s95 | 5:e5313b695302 | 160 | intState = readRotorState(); |
david_s95 | 6:4edbe75736d9 | 161 | //Increment state machine to next state |
david_s95 | 9:575b29cbf5e4 | 162 | motorOut(intState); |
david_s95 | 6:4edbe75736d9 | 163 | //If spinning is required, attach the necessary wait to the |
david_s95 | 6:4edbe75736d9 | 164 | //timeout interrupt to call this function again and |
david_s95 | 6:4edbe75736d9 | 165 | //keep the motor spinning at the right speed |
david_s95 | 5:e5313b695302 | 166 | if(revsec) spinTimer.attach(&fixedSpeed, spinWait); |
david_s95 | 5:e5313b695302 | 167 | } |
david_s95 | 5:e5313b695302 | 168 | |
theMaO | 14:155e9a9147d4 | 169 | void spinToPos(){ |
theMaO | 14:155e9a9147d4 | 170 | currRevs = 0; |
theMaO | 14:155e9a9147d4 | 171 | |
theMaO | 14:155e9a9147d4 | 172 | } |
theMaO | 14:155e9a9147d4 | 173 | |
david_s95 | 10:0309d6c49f26 | 174 | void rps() |
david_s95 | 7:5932ed0bad6d | 175 | { |
david_s95 | 10:0309d6c49f26 | 176 | |
david_s95 | 10:0309d6c49f26 | 177 | clk=!clk; |
david_s95 | 10:0309d6c49f26 | 178 | speedTimer.stop(); |
david_s95 | 10:0309d6c49f26 | 179 | revtimer = speedTimer.read_ms(); |
david_s95 | 10:0309d6c49f26 | 180 | speedTimer.reset(); |
david_s95 | 10:0309d6c49f26 | 181 | speedTimer.start(); |
david_s95 | 10:0309d6c49f26 | 182 | |
david_s95 | 10:0309d6c49f26 | 183 | measuredRevs = 1000/(revtimer); |
david_s95 | 10:0309d6c49f26 | 184 | quadraturePosition=0; |
david_s95 | 10:0309d6c49f26 | 185 | |
david_s95 | 7:5932ed0bad6d | 186 | } |
david_s95 | 7:5932ed0bad6d | 187 | |
david_s95 | 10:0309d6c49f26 | 188 | void VPID() |
david_s95 | 10:0309d6c49f26 | 189 | { |
david_s95 | 12:8ea29b18d289 | 190 | controller.setMode(1); |
david_s95 | 10:0309d6c49f26 | 191 | while(1) { |
david_s95 | 10:0309d6c49f26 | 192 | controller.setSetPoint(revsec); |
david_s95 | 10:0309d6c49f26 | 193 | // printf("revsec: %2.3f\r\n", revsec); |
david_s95 | 10:0309d6c49f26 | 194 | controller.setProcessValue(measuredRevs); |
david_s95 | 10:0309d6c49f26 | 195 | speedControl = controller.compute(); |
david_s95 | 10:0309d6c49f26 | 196 | // printf("speed setpoint: %2.3f\r\n", speedControl); |
david_s95 | 12:8ea29b18d289 | 197 | if(speedControl<0) speedControl = -speedControl; |
david_s95 | 12:8ea29b18d289 | 198 | else if (speedControl==0) speedControl = 1; |
david_s95 | 12:8ea29b18d289 | 199 | spinWait = (1/speedControl)/6; |
david_s95 | 10:0309d6c49f26 | 200 | Thread::wait(PIDrate); |
david_s95 | 10:0309d6c49f26 | 201 | } |
david_s95 | 10:0309d6c49f26 | 202 | } |
david_s95 | 7:5932ed0bad6d | 203 | |
theMaO | 15:d9e50101a17e | 204 | <<<<<<< local |
theMaO | 15:d9e50101a17e | 205 | ======= |
david_s95 | 13:87ab3b008803 | 206 | //void edgeRiseA() |
david_s95 | 13:87ab3b008803 | 207 | //{ |
david_s95 | 13:87ab3b008803 | 208 | // pos++; |
david_s95 | 13:87ab3b008803 | 209 | // if(pos>=468) { |
david_s95 | 13:87ab3b008803 | 210 | //// Direction=!Direction; |
david_s95 | 13:87ab3b008803 | 211 | // pos=pos%468; |
david_s95 | 13:87ab3b008803 | 212 | //// testpin=!testpin; |
david_s95 | 13:87ab3b008803 | 213 | // } |
david_s95 | 13:87ab3b008803 | 214 | // if(qB) DIR = 0; |
david_s95 | 13:87ab3b008803 | 215 | // else DIR = 1; |
david_s95 | 13:87ab3b008803 | 216 | //// clk=DIR; |
david_s95 | 13:87ab3b008803 | 217 | ////CLOCKWISE: A rises before B -> On A edge, B low -> DIR = 1 |
david_s95 | 13:87ab3b008803 | 218 | ////ANTICLOCKWISE: B rises before A -> On A edge, B high-> DIR = 0 |
david_s95 | 13:87ab3b008803 | 219 | //} |
david_s95 | 13:87ab3b008803 | 220 | |
david_s95 | 13:87ab3b008803 | 221 | void sing(float singFreq) |
david_s95 | 9:575b29cbf5e4 | 222 | { |
david_s95 | 13:87ab3b008803 | 223 | motor = driveTable[1]; |
david_s95 | 13:87ab3b008803 | 224 | wait(1.0); |
david_s95 | 13:87ab3b008803 | 225 | float singDelayus = 1000000/singFreq; |
david_s95 | 13:87ab3b008803 | 226 | for(int count=0; count<singFreq; count++) { |
david_s95 | 13:87ab3b008803 | 227 | motor = driveTable[2]; |
david_s95 | 13:87ab3b008803 | 228 | wait_us(singDelayus); |
david_s95 | 13:87ab3b008803 | 229 | motor = driveTable[1]; |
david_s95 | 8:77627657da80 | 230 | } |
david_s95 | 13:87ab3b008803 | 231 | singFreq = 15000; |
david_s95 | 13:87ab3b008803 | 232 | singDelayus = 1000000/singFreq; |
david_s95 | 13:87ab3b008803 | 233 | for(int count=0; count<singFreq; count++) { |
david_s95 | 13:87ab3b008803 | 234 | motor = driveTable[2]; |
david_s95 | 13:87ab3b008803 | 235 | wait_us(singDelayus); |
david_s95 | 13:87ab3b008803 | 236 | motor = driveTable[1]; |
david_s95 | 13:87ab3b008803 | 237 | } |
david_s95 | 8:77627657da80 | 238 | } |
david_s95 | 8:77627657da80 | 239 | |
david_s95 | 13:87ab3b008803 | 240 | //void edgeIncr() |
david_s95 | 13:87ab3b008803 | 241 | //{ |
david_s95 | 13:87ab3b008803 | 242 | // pos++; |
david_s95 | 13:87ab3b008803 | 243 | // if(pos>=468) { |
david_s95 | 13:87ab3b008803 | 244 | //// Direction=!Direction; |
david_s95 | 13:87ab3b008803 | 245 | // pos=pos%468; |
david_s95 | 13:87ab3b008803 | 246 | //// testpin=!testpin; |
david_s95 | 13:87ab3b008803 | 247 | // } |
david_s95 | 13:87ab3b008803 | 248 | //} |
david_s95 | 8:77627657da80 | 249 | |
david_s95 | 9:575b29cbf5e4 | 250 | //#define WAIT 2 |
mengkiang | 4:f8a9ce214db9 | 251 | //Main function |
theMaO | 15:d9e50101a17e | 252 | >>>>>>> other |
david_s95 | 5:e5313b695302 | 253 | int main() |
david_s95 | 5:e5313b695302 | 254 | { |
david_s95 | 9:575b29cbf5e4 | 255 | pc.printf("spin\n\r"); |
david_s95 | 10:0309d6c49f26 | 256 | |
estott | 0:de4320f74764 | 257 | //Run the motor synchronisation |
estott | 2:4e88faab6988 | 258 | orState = motorHome(); |
david_s95 | 6:4edbe75736d9 | 259 | //orState is subtracted from future rotor state inputs to align rotor and motor states |
david_s95 | 6:4edbe75736d9 | 260 | |
estott | 2:4e88faab6988 | 261 | pc.printf("Rotor origin: %x\n\r",orState); |
david_s95 | 5:e5313b695302 | 262 | |
david_s95 | 6:4edbe75736d9 | 263 | char command[ARRAYSIZE]; |
david_s95 | 6:4edbe75736d9 | 264 | int index=0; |
david_s95 | 6:4edbe75736d9 | 265 | int units = 0, tens = 0, decimals = 0; |
david_s95 | 6:4edbe75736d9 | 266 | char ch; |
david_s95 | 9:575b29cbf5e4 | 267 | testpin=0; |
david_s95 | 12:8ea29b18d289 | 268 | int vartens = 0, varunits = 0, vardecs = 0; |
david_s95 | 7:5932ed0bad6d | 269 | |
david_s95 | 10:0309d6c49f26 | 270 | speedTimer.reset(); |
david_s95 | 10:0309d6c49f26 | 271 | speedTimer.start(); |
david_s95 | 10:0309d6c49f26 | 272 | I2.mode(PullNone); |
david_s95 | 10:0309d6c49f26 | 273 | I2.fall(&rps); |
theMaO | 15:d9e50101a17e | 274 | <<<<<<< local |
theMaO | 15:d9e50101a17e | 275 | ======= |
david_s95 | 13:87ab3b008803 | 276 | |
david_s95 | 9:575b29cbf5e4 | 277 | // qA.rise(&edgeRiseA); |
david_s95 | 9:575b29cbf5e4 | 278 | // qB.rise(&edgeIncr); |
david_s95 | 9:575b29cbf5e4 | 279 | // qA.fall(&edgeIncr); |
david_s95 | 9:575b29cbf5e4 | 280 | // qB.fall(&edgeIncr); |
theMaO | 15:d9e50101a17e | 281 | >>>>>>> other |
david_s95 | 7:5932ed0bad6d | 282 | |
david_s95 | 10:0309d6c49f26 | 283 | VPIDthread.start(VPID); |
david_s95 | 10:0309d6c49f26 | 284 | |
david_s95 | 5:e5313b695302 | 285 | while(1) { |
david_s95 | 9:575b29cbf5e4 | 286 | // clk = I2; |
david_s95 | 6:4edbe75736d9 | 287 | //Toggle LED so we know something's happening |
david_s95 | 8:77627657da80 | 288 | // clk = !clk; |
david_s95 | 7:5932ed0bad6d | 289 | |
david_s95 | 6:4edbe75736d9 | 290 | //If there's a character to read from the serial port |
david_s95 | 6:4edbe75736d9 | 291 | if (pc.readable()) { |
david_s95 | 7:5932ed0bad6d | 292 | |
david_s95 | 6:4edbe75736d9 | 293 | //Clear index counter and control variables |
david_s95 | 6:4edbe75736d9 | 294 | index = 0; |
david_s95 | 7:5932ed0bad6d | 295 | // revsec = spinWait = 0; |
david_s95 | 7:5932ed0bad6d | 296 | |
david_s95 | 6:4edbe75736d9 | 297 | //Read each value from the serial port until Enter key is pressed |
david_s95 | 6:4edbe75736d9 | 298 | do { |
david_s95 | 6:4edbe75736d9 | 299 | //Read character |
david_s95 | 6:4edbe75736d9 | 300 | ch = pc.getc(); |
david_s95 | 6:4edbe75736d9 | 301 | //Print character to serial for visual feedback |
david_s95 | 6:4edbe75736d9 | 302 | pc.putc(ch); |
david_s95 | 6:4edbe75736d9 | 303 | //Add character to input array |
david_s95 | 6:4edbe75736d9 | 304 | command[index++]=ch; // put it into the value array and increment the index |
david_s95 | 7:5932ed0bad6d | 305 | //d10 and d13 used for detecting Enter key on Windows/Unix/Mac |
david_s95 | 6:4edbe75736d9 | 306 | } while(ch != 10 && ch != 13); |
david_s95 | 7:5932ed0bad6d | 307 | |
david_s95 | 6:4edbe75736d9 | 308 | //Start new line on terminal for printing data |
david_s95 | 6:4edbe75736d9 | 309 | pc.putc('\n'); |
david_s95 | 6:4edbe75736d9 | 310 | pc.putc('\r'); |
david_s95 | 7:5932ed0bad6d | 311 | |
david_s95 | 6:4edbe75736d9 | 312 | //Analyse the input string |
david_s95 | 6:4edbe75736d9 | 313 | switch (command[0]) { |
david_s95 | 7:5932ed0bad6d | 314 | //If a V was typed... |
david_s95 | 6:4edbe75736d9 | 315 | case 'V': |
david_s95 | 7:5932ed0bad6d | 316 | units = 0, tens = 0, decimals = 0; |
david_s95 | 7:5932ed0bad6d | 317 | //For each character received, subtract ASCII 0 from ASCII |
david_s95 | 7:5932ed0bad6d | 318 | //representation to obtain the integer value of the number |
david_s95 | 9:575b29cbf5e4 | 319 | if(command[1]=='-') { |
david_s95 | 9:575b29cbf5e4 | 320 | spinCW = 0; |
david_s95 | 9:575b29cbf5e4 | 321 | //If decimal point is in the second character (eg, V-.1) |
david_s95 | 9:575b29cbf5e4 | 322 | if(command[2]=='.') { |
david_s95 | 9:575b29cbf5e4 | 323 | //Extract decimal rev/s |
david_s95 | 9:575b29cbf5e4 | 324 | decimals = command[3] - '0'; |
david_s95 | 7:5932ed0bad6d | 325 | |
david_s95 | 9:575b29cbf5e4 | 326 | //If decimal point is in the third character (eg, V-0.1) |
david_s95 | 9:575b29cbf5e4 | 327 | } else if(command[3]=='.') { |
david_s95 | 9:575b29cbf5e4 | 328 | units = command[2] - '0'; |
david_s95 | 9:575b29cbf5e4 | 329 | decimals = command[4] - '0'; |
david_s95 | 7:5932ed0bad6d | 330 | |
david_s95 | 9:575b29cbf5e4 | 331 | //If decimal point is in the fourth character (eg, V-10.1) |
david_s95 | 9:575b29cbf5e4 | 332 | } else if(command[4]=='.') { |
david_s95 | 9:575b29cbf5e4 | 333 | tens = command[2] - '0'; |
david_s95 | 9:575b29cbf5e4 | 334 | units = command[3] - '0'; |
david_s95 | 9:575b29cbf5e4 | 335 | decimals = command[5] - '0'; |
david_s95 | 9:575b29cbf5e4 | 336 | } |
david_s95 | 9:575b29cbf5e4 | 337 | } else { |
david_s95 | 9:575b29cbf5e4 | 338 | spinCW = 1; |
david_s95 | 9:575b29cbf5e4 | 339 | //If decimal point is in the second character (eg, V.1) |
david_s95 | 9:575b29cbf5e4 | 340 | if(command[1]=='.') { |
david_s95 | 9:575b29cbf5e4 | 341 | //Extract decimal rev/s |
david_s95 | 9:575b29cbf5e4 | 342 | decimals = command[2] - '0'; |
david_s95 | 7:5932ed0bad6d | 343 | |
david_s95 | 9:575b29cbf5e4 | 344 | //If decimal point is in the third character (eg, V0.1) |
david_s95 | 9:575b29cbf5e4 | 345 | } else if(command[2]=='.') { |
david_s95 | 9:575b29cbf5e4 | 346 | units = command[1] - '0'; |
david_s95 | 9:575b29cbf5e4 | 347 | decimals = command[3] - '0'; |
david_s95 | 9:575b29cbf5e4 | 348 | |
david_s95 | 9:575b29cbf5e4 | 349 | //If decimal point is in the fourth character (eg, V10.1) |
david_s95 | 9:575b29cbf5e4 | 350 | } else if(command[3]=='.') { |
david_s95 | 9:575b29cbf5e4 | 351 | tens = command[1] - '0'; |
david_s95 | 9:575b29cbf5e4 | 352 | units = command[2] - '0'; |
david_s95 | 9:575b29cbf5e4 | 353 | decimals = command[4] - '0'; |
david_s95 | 9:575b29cbf5e4 | 354 | } |
david_s95 | 6:4edbe75736d9 | 355 | } |
david_s95 | 7:5932ed0bad6d | 356 | |
david_s95 | 6:4edbe75736d9 | 357 | //Calculate the number of revolutions per second required |
david_s95 | 6:4edbe75736d9 | 358 | revsec = float(tens)*10 + float(units) + float(decimals)/10; |
david_s95 | 6:4edbe75736d9 | 359 | //Calculate the required wait period |
david_s95 | 12:8ea29b18d289 | 360 | |
david_s95 | 7:5932ed0bad6d | 361 | |
david_s95 | 6:4edbe75736d9 | 362 | //Print values for verification |
david_s95 | 12:8ea29b18d289 | 363 | pc.printf("Rev/S: %2.4f\n\r", revsec); |
david_s95 | 7:5932ed0bad6d | 364 | |
david_s95 | 6:4edbe75736d9 | 365 | //Run the function to start rotating at a fixed speed |
david_s95 | 6:4edbe75736d9 | 366 | fixedSpeed(); |
david_s95 | 6:4edbe75736d9 | 367 | break; |
david_s95 | 7:5932ed0bad6d | 368 | //If anything unexpected was received |
theMaO | 14:155e9a9147d4 | 369 | |
theMaO | 14:155e9a9147d4 | 370 | case 'R': |
theMaO | 14:155e9a9147d4 | 371 | hdrds = 0; units = 0, tens = 0, decimals = 0; |
theMaO | 14:155e9a9147d4 | 372 | //For each character received, subtract ASCII 0 from ASCII |
theMaO | 14:155e9a9147d4 | 373 | //representation to obtain the integer value of the number |
theMaO | 14:155e9a9147d4 | 374 | if(command[1]=='-') { |
theMaO | 14:155e9a9147d4 | 375 | spinCW = 0; |
theMaO | 14:155e9a9147d4 | 376 | //If decimal point is in the second character (eg, V-.1) |
theMaO | 14:155e9a9147d4 | 377 | if(command[2]=='.') { |
theMaO | 14:155e9a9147d4 | 378 | //Extract decimal rev/s |
theMaO | 14:155e9a9147d4 | 379 | decimals = command[3] - '0'; |
theMaO | 14:155e9a9147d4 | 380 | |
theMaO | 14:155e9a9147d4 | 381 | //If decimal point is in the third character (eg, V-0.1) |
theMaO | 14:155e9a9147d4 | 382 | } else if(command[3]=='.') { |
theMaO | 14:155e9a9147d4 | 383 | units = command[2] - '0'; |
theMaO | 14:155e9a9147d4 | 384 | decimals = command[4] - '0'; |
theMaO | 14:155e9a9147d4 | 385 | |
theMaO | 14:155e9a9147d4 | 386 | //If decimal point is in the fourth character (eg, V-10.1) |
theMaO | 14:155e9a9147d4 | 387 | } else if(command[4]=='.') { |
theMaO | 14:155e9a9147d4 | 388 | tens = command[2] - '0'; |
theMaO | 14:155e9a9147d4 | 389 | units = command[3] - '0'; |
theMaO | 14:155e9a9147d4 | 390 | decimals = command[5] - '0'; |
theMaO | 14:155e9a9147d4 | 391 | } else if(command[5]=='.'){ |
theMaO | 14:155e9a9147d4 | 392 | hdrds = command[2] - '0'; |
theMaO | 14:155e9a9147d4 | 393 | tens = command[3] - '0'; |
theMaO | 14:155e9a9147d4 | 394 | units = command[4] - '0'; |
theMaO | 14:155e9a9147d4 | 395 | decimals = command[6] - '0'; |
theMaO | 14:155e9a9147d4 | 396 | } |
theMaO | 14:155e9a9147d4 | 397 | } else { |
theMaO | 14:155e9a9147d4 | 398 | spinCW = 1; |
theMaO | 14:155e9a9147d4 | 399 | //If decimal point is in the second character (eg, V.1) |
theMaO | 14:155e9a9147d4 | 400 | if(command[1]=='.') { |
theMaO | 14:155e9a9147d4 | 401 | //Extract decimal rev/s |
theMaO | 14:155e9a9147d4 | 402 | decimals = command[2] - '0'; |
theMaO | 14:155e9a9147d4 | 403 | |
theMaO | 14:155e9a9147d4 | 404 | //If decimal point is in the third character (eg, V0.1) |
theMaO | 14:155e9a9147d4 | 405 | } else if(command[2]=='.') { |
theMaO | 14:155e9a9147d4 | 406 | units = command[1] - '0'; |
theMaO | 14:155e9a9147d4 | 407 | decimals = command[3] - '0'; |
theMaO | 14:155e9a9147d4 | 408 | |
theMaO | 14:155e9a9147d4 | 409 | //If decimal point is in the fourth character (eg, V10.1) |
theMaO | 14:155e9a9147d4 | 410 | } else if(command[3]=='.') { |
theMaO | 14:155e9a9147d4 | 411 | tens = command[1] - '0'; |
theMaO | 14:155e9a9147d4 | 412 | units = command[2] - '0'; |
theMaO | 14:155e9a9147d4 | 413 | decimals = command[4] - '0'; |
theMaO | 14:155e9a9147d4 | 414 | } |
theMaO | 14:155e9a9147d4 | 415 | else if(command[5]=='.'){ |
theMaO | 14:155e9a9147d4 | 416 | hdrds = command[2] - '0'; |
theMaO | 14:155e9a9147d4 | 417 | tens = command[3] - '0'; |
theMaO | 14:155e9a9147d4 | 418 | units = command[4] - '0'; |
theMaO | 14:155e9a9147d4 | 419 | decimals = command[6] - '0'; |
theMaO | 14:155e9a9147d4 | 420 | } |
theMaO | 14:155e9a9147d4 | 421 | } |
theMaO | 14:155e9a9147d4 | 422 | //Calculate the number of revolutions required |
theMaO | 14:155e9a9147d4 | 423 | targetRevs = float(hundreds)*100 + float(tens)*10 + float(units) + float(decimals)/10; |
theMaO | 14:155e9a9147d4 | 424 | |
theMaO | 14:155e9a9147d4 | 425 | //Print values for verification |
theMaO | 14:155e9a9147d4 | 426 | pc.printf("Target revs: %2.4f\n\r", targetRevs); |
theMaO | 14:155e9a9147d4 | 427 | |
theMaO | 14:155e9a9147d4 | 428 | //Run the function to start rotating at a fixed speed |
theMaO | 14:155e9a9147d4 | 429 | spinToPos(); |
theMaO | 14:155e9a9147d4 | 430 | break; |
theMaO | 14:155e9a9147d4 | 431 | |
theMaO | 14:155e9a9147d4 | 432 | |
david_s95 | 7:5932ed0bad6d | 433 | case 's': |
david_s95 | 9:575b29cbf5e4 | 434 | // pc.printf("Revs / sec: %2.2f\r", revs); |
david_s95 | 9:575b29cbf5e4 | 435 | // printSpeed.attach(&speedo, 1.0); |
david_s95 | 10:0309d6c49f26 | 436 | printf("Measured: %2.3f, revsec: %2.3f\r\n", measuredRevs, revsec); |
david_s95 | 10:0309d6c49f26 | 437 | printf("speed setpoint: %2.3f\r\n", speedControl); |
david_s95 | 7:5932ed0bad6d | 438 | break; |
david_s95 | 8:77627657da80 | 439 | case 't': |
david_s95 | 10:0309d6c49f26 | 440 | // pc.printf("%d\n\r", pos); |
david_s95 | 8:77627657da80 | 441 | break; |
david_s95 | 12:8ea29b18d289 | 442 | |
david_s95 | 12:8ea29b18d289 | 443 | case 'K': |
david_s95 | 12:8ea29b18d289 | 444 | vartens = command[1] - '0'; |
david_s95 | 12:8ea29b18d289 | 445 | varunits = command[2] - '0'; |
david_s95 | 12:8ea29b18d289 | 446 | vardecs = command[4] - '0'; |
david_s95 | 12:8ea29b18d289 | 447 | Kc = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
david_s95 | 12:8ea29b18d289 | 448 | printf("Kc: %2.1f\r\n", Kc); |
david_s95 | 12:8ea29b18d289 | 449 | controller.setTunings(Kc, Ti, Td); |
david_s95 | 12:8ea29b18d289 | 450 | // controller.setMode(1); |
david_s95 | 12:8ea29b18d289 | 451 | break; |
david_s95 | 12:8ea29b18d289 | 452 | case 'i': |
david_s95 | 12:8ea29b18d289 | 453 | vartens = command[1] - '0'; |
david_s95 | 12:8ea29b18d289 | 454 | varunits = command[2] - '0'; |
david_s95 | 12:8ea29b18d289 | 455 | vardecs = command[4] - '0'; |
david_s95 | 12:8ea29b18d289 | 456 | Ti = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
david_s95 | 12:8ea29b18d289 | 457 | printf("Ti: %2.1f\r\n", Ti); |
david_s95 | 12:8ea29b18d289 | 458 | controller.setTunings(Kc, Ti, Td); |
david_s95 | 12:8ea29b18d289 | 459 | // controller.setMode(1); |
david_s95 | 12:8ea29b18d289 | 460 | break; |
david_s95 | 12:8ea29b18d289 | 461 | case 'd': |
david_s95 | 12:8ea29b18d289 | 462 | vartens = command[1] - '0'; |
david_s95 | 12:8ea29b18d289 | 463 | varunits = command[2] - '0'; |
david_s95 | 12:8ea29b18d289 | 464 | vardecs = command[4] - '0'; |
david_s95 | 12:8ea29b18d289 | 465 | Td = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
david_s95 | 12:8ea29b18d289 | 466 | printf("Td: %2.1f\r\n", Td); |
david_s95 | 12:8ea29b18d289 | 467 | controller.setTunings(Kc, Ti, Td); |
david_s95 | 12:8ea29b18d289 | 468 | // controller.setMode(1); |
david_s95 | 12:8ea29b18d289 | 469 | break; |
david_s95 | 13:87ab3b008803 | 470 | case 'l': |
david_s95 | 13:87ab3b008803 | 471 | sing(10000); |
david_s95 | 13:87ab3b008803 | 472 | break; |
david_s95 | 6:4edbe75736d9 | 473 | default: |
david_s95 | 6:4edbe75736d9 | 474 | //Set speed variables to zero to stop motor spinning |
david_s95 | 6:4edbe75736d9 | 475 | //Print error message |
david_s95 | 10:0309d6c49f26 | 476 | motorStop(); |
david_s95 | 10:0309d6c49f26 | 477 | pc.printf("Error in received data 0\n\r"); |
david_s95 | 6:4edbe75736d9 | 478 | break; |
david_s95 | 6:4edbe75736d9 | 479 | } |
david_s95 | 6:4edbe75736d9 | 480 | } |
david_s95 | 7:5932ed0bad6d | 481 | // printSpeed.attach(&speedo, 1.0); |
david_s95 | 7:5932ed0bad6d | 482 | // pc.printf("Revs / sec: %2.2f\r", revs); |
estott | 2:4e88faab6988 | 483 | } |
david_s95 | 5:e5313b695302 | 484 | |
estott | 0:de4320f74764 | 485 | } |