Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ES_CW2_Starter by
main.cpp@17:9cd9f82027ca, 2017-03-09 (annotated)
- Committer:
- theMaO
- Date:
- Thu Mar 09 15:15:37 2017 +0000
- Revision:
- 17:9cd9f82027ca
- Parent:
- 16:372c720015ab
- Child:
- 18:55cd33a3e69f
added code for spin to position function
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[6] = {0x38, 0x2C, 0x0E, 0x0B, 0x23, 0x32}; |
| david_s95 | 9:575b29cbf5e4 | 49 | |
| david_s95 | 9:575b29cbf5e4 | 50 | const int8_t AcwState[7] = {0x00, 0x23, 0x38, 0x32, 0x0E, 0x0B, 0x2C}; |
| david_s95 | 9:575b29cbf5e4 | 51 | const int8_t cwState[7] = {0x00, 0x0E, 0x23, 0x0B, 0x38, 0x2C, 0x32}; |
| estott | 2:4e88faab6988 | 52 | |
| estott | 0:de4320f74764 | 53 | //Status LED |
| estott | 0:de4320f74764 | 54 | DigitalOut led1(LED1); |
| estott | 0:de4320f74764 | 55 | |
| estott | 0:de4320f74764 | 56 | //Photointerrupter inputs |
| david_s95 | 9:575b29cbf5e4 | 57 | DigitalIn I1(I1pin); |
| david_s95 | 9:575b29cbf5e4 | 58 | //InterruptIn I1(I1pin); |
| david_s95 | 9:575b29cbf5e4 | 59 | InterruptIn I2(I2pin); |
| estott | 2:4e88faab6988 | 60 | DigitalIn I3(I3pin); |
| estott | 0:de4320f74764 | 61 | |
| david_s95 | 8:77627657da80 | 62 | InterruptIn qA(CHA); |
| david_s95 | 8:77627657da80 | 63 | InterruptIn qB(CHB); |
| david_s95 | 8:77627657da80 | 64 | |
| estott | 0:de4320f74764 | 65 | //Motor Drive outputs |
| david_s95 | 5:e5313b695302 | 66 | DigitalOut clk(LED1); |
| david_s95 | 8:77627657da80 | 67 | DigitalOut Direction(LED2); |
| david_s95 | 9:575b29cbf5e4 | 68 | DigitalOut testpin(D13); |
| david_s95 | 9:575b29cbf5e4 | 69 | |
| david_s95 | 9:575b29cbf5e4 | 70 | //NOTE, BusOut declares things in reverse (ie, 0, 1, 2, 3) compared to binary represenation |
| david_s95 | 9:575b29cbf5e4 | 71 | BusOut motor(L1Lpin, L1Hpin, L2Lpin, L2Hpin, L3Lpin, L3Hpin); |
| david_s95 | 5:e5313b695302 | 72 | |
| david_s95 | 5:e5313b695302 | 73 | //Timeout function for rotating at set speed |
| david_s95 | 5:e5313b695302 | 74 | Timeout spinTimer; |
| david_s95 | 5:e5313b695302 | 75 | float spinWait = 10; |
| david_s95 | 5:e5313b695302 | 76 | float revsec = 0; |
| david_s95 | 5:e5313b695302 | 77 | |
| theMaO | 14:155e9a9147d4 | 78 | //Variables for spinning N revolutions |
| theMaO | 14:155e9a9147d4 | 79 | int8_t targetRevs = 0; |
| theMaO | 14:155e9a9147d4 | 80 | int8_t currRevs = 0; |
| theMaO | 14:155e9a9147d4 | 81 | |
| david_s95 | 7:5932ed0bad6d | 82 | //Timer used for calculating speed |
| david_s95 | 7:5932ed0bad6d | 83 | Timer speedTimer; |
| david_s95 | 10:0309d6c49f26 | 84 | float measuredRevs = 0, revtimer = 0; |
| david_s95 | 7:5932ed0bad6d | 85 | Ticker printSpeed; |
| david_s95 | 7:5932ed0bad6d | 86 | |
| david_s95 | 5:e5313b695302 | 87 | Serial pc(SERIAL_TX, SERIAL_RX); |
| david_s95 | 5:e5313b695302 | 88 | |
| david_s95 | 5:e5313b695302 | 89 | int8_t orState = 0; //Rotor offset at motor state 0 |
| david_s95 | 5:e5313b695302 | 90 | int8_t intState = 0; |
| david_s95 | 5:e5313b695302 | 91 | int8_t intStateOld = 0; |
| david_s95 | 9:575b29cbf5e4 | 92 | int position = 0; |
| david_s95 | 5:e5313b695302 | 93 | |
| david_s95 | 5:e5313b695302 | 94 | int i=0; |
| david_s95 | 10:0309d6c49f26 | 95 | int quadraturePosition=0; |
| david_s95 | 9:575b29cbf5e4 | 96 | bool spinCW=0; |
| david_s95 | 10:0309d6c49f26 | 97 | float u = 0; //Set point for VPI |
| david_s95 | 5:e5313b695302 | 98 | |
| estott | 0:de4320f74764 | 99 | //Set a given drive state |
| david_s95 | 5:e5313b695302 | 100 | void motorOut(int8_t driveState) |
| david_s95 | 5:e5313b695302 | 101 | { |
| david_s95 | 5:e5313b695302 | 102 | |
| david_s95 | 9:575b29cbf5e4 | 103 | //Set to zero |
| david_s95 | 9:575b29cbf5e4 | 104 | motor=0x2A; |
| david_s95 | 10:0309d6c49f26 | 105 | |
| david_s95 | 9:575b29cbf5e4 | 106 | //Go to next state |
| david_s95 | 9:575b29cbf5e4 | 107 | if(!spinCW) motor = AcwState[driveState]; |
| david_s95 | 9:575b29cbf5e4 | 108 | else motor = cwState[driveState]; |
| estott | 2:4e88faab6988 | 109 | //Lookup the output byte from the drive state. |
| david_s95 | 9:575b29cbf5e4 | 110 | // int8_t driveOut = driveTable[driveState & 0x07]; |
| david_s95 | 10:0309d6c49f26 | 111 | } |
| david_s95 | 5:e5313b695302 | 112 | |
| david_s95 | 10:0309d6c49f26 | 113 | inline void motorStop() |
| david_s95 | 10:0309d6c49f26 | 114 | { |
| david_s95 | 10:0309d6c49f26 | 115 | //revsec set to zero prevents recurring interrupt for constant speed |
| david_s95 | 10:0309d6c49f26 | 116 | revsec = 0; |
| david_s95 | 10:0309d6c49f26 | 117 | //0x2A turns all motor transistors off to prevent any power usage |
| david_s95 | 10:0309d6c49f26 | 118 | motor = 0x2A; |
| david_s95 | 5:e5313b695302 | 119 | } |
| david_s95 | 5:e5313b695302 | 120 | |
| david_s95 | 5:e5313b695302 | 121 | //Convert photointerrupter inputs to a rotor state |
| david_s95 | 5:e5313b695302 | 122 | inline int8_t readRotorState() |
| david_s95 | 5:e5313b695302 | 123 | { |
| david_s95 | 9:575b29cbf5e4 | 124 | return (I1 + 2*I2 + 4*I3); |
| david_s95 | 5:e5313b695302 | 125 | } |
| estott | 0:de4320f74764 | 126 | |
| david_s95 | 5:e5313b695302 | 127 | //Basic synchronisation routine |
| david_s95 | 5:e5313b695302 | 128 | int8_t motorHome() |
| david_s95 | 5:e5313b695302 | 129 | { |
| estott | 0:de4320f74764 | 130 | //Put the motor in drive state 0 and wait for it to stabilise |
| david_s95 | 9:575b29cbf5e4 | 131 | motor=cwState[1]; |
| estott | 0:de4320f74764 | 132 | wait(1.0); |
| david_s95 | 10:0309d6c49f26 | 133 | |
| david_s95 | 9:575b29cbf5e4 | 134 | position = 0; |
| david_s95 | 5:e5313b695302 | 135 | |
| estott | 0:de4320f74764 | 136 | //Get the rotor state |
| estott | 2:4e88faab6988 | 137 | return readRotorState(); |
| estott | 0:de4320f74764 | 138 | } |
| david_s95 | 5:e5313b695302 | 139 | |
| david_s95 | 5:e5313b695302 | 140 | void fixedSpeed() |
| david_s95 | 5:e5313b695302 | 141 | { |
| david_s95 | 5:e5313b695302 | 142 | intState = readRotorState(); |
| david_s95 | 6:4edbe75736d9 | 143 | //Increment state machine to next state |
| david_s95 | 9:575b29cbf5e4 | 144 | motorOut(intState); |
| david_s95 | 6:4edbe75736d9 | 145 | //If spinning is required, attach the necessary wait to the |
| david_s95 | 6:4edbe75736d9 | 146 | //timeout interrupt to call this function again and |
| david_s95 | 6:4edbe75736d9 | 147 | //keep the motor spinning at the right speed |
| david_s95 | 5:e5313b695302 | 148 | if(revsec) spinTimer.attach(&fixedSpeed, spinWait); |
| david_s95 | 5:e5313b695302 | 149 | } |
| david_s95 | 5:e5313b695302 | 150 | |
| theMaO | 14:155e9a9147d4 | 151 | void spinToPos(){ |
| theMaO | 14:155e9a9147d4 | 152 | currRevs = 0; |
| theMaO | 14:155e9a9147d4 | 153 | |
| theMaO | 17:9cd9f82027ca | 154 | revsec = 50.0; |
| theMaO | 17:9cd9f82027ca | 155 | fixedSpeed(); |
| theMaO | 17:9cd9f82027ca | 156 | while (targetRevs - currRevs > 100){ |
| theMaO | 17:9cd9f82027ca | 157 | } |
| theMaO | 17:9cd9f82027ca | 158 | |
| theMaO | 17:9cd9f82027ca | 159 | remRevs = targetRevs - currRevs; |
| theMaO | 17:9cd9f82027ca | 160 | while (remRevs > 3){ |
| theMaO | 17:9cd9f82027ca | 161 | revsec = remRevs/3; |
| theMaO | 17:9cd9f82027ca | 162 | remRevs = targetRevs - currRevs; |
| theMaO | 17:9cd9f82027ca | 163 | } |
| theMaO | 17:9cd9f82027ca | 164 | |
| theMaO | 17:9cd9f82027ca | 165 | |
| theMaO | 14:155e9a9147d4 | 166 | } |
| theMaO | 14:155e9a9147d4 | 167 | |
| david_s95 | 10:0309d6c49f26 | 168 | void rps() |
| david_s95 | 7:5932ed0bad6d | 169 | { |
| david_s95 | 10:0309d6c49f26 | 170 | |
| david_s95 | 10:0309d6c49f26 | 171 | clk=!clk; |
| david_s95 | 10:0309d6c49f26 | 172 | speedTimer.stop(); |
| david_s95 | 10:0309d6c49f26 | 173 | revtimer = speedTimer.read_ms(); |
| david_s95 | 10:0309d6c49f26 | 174 | speedTimer.reset(); |
| david_s95 | 10:0309d6c49f26 | 175 | speedTimer.start(); |
| david_s95 | 10:0309d6c49f26 | 176 | |
| david_s95 | 10:0309d6c49f26 | 177 | measuredRevs = 1000/(revtimer); |
| david_s95 | 10:0309d6c49f26 | 178 | quadraturePosition=0; |
| theMaO | 17:9cd9f82027ca | 179 | currRevs = currRevs + 1; |
| david_s95 | 10:0309d6c49f26 | 180 | |
| david_s95 | 7:5932ed0bad6d | 181 | } |
| david_s95 | 7:5932ed0bad6d | 182 | |
| david_s95 | 10:0309d6c49f26 | 183 | void VPID() |
| david_s95 | 10:0309d6c49f26 | 184 | { |
| david_s95 | 12:8ea29b18d289 | 185 | controller.setMode(1); |
| david_s95 | 10:0309d6c49f26 | 186 | while(1) { |
| david_s95 | 10:0309d6c49f26 | 187 | controller.setSetPoint(revsec); |
| david_s95 | 10:0309d6c49f26 | 188 | // printf("revsec: %2.3f\r\n", revsec); |
| david_s95 | 10:0309d6c49f26 | 189 | controller.setProcessValue(measuredRevs); |
| david_s95 | 10:0309d6c49f26 | 190 | speedControl = controller.compute(); |
| david_s95 | 10:0309d6c49f26 | 191 | // printf("speed setpoint: %2.3f\r\n", speedControl); |
| david_s95 | 12:8ea29b18d289 | 192 | if(speedControl<0) speedControl = -speedControl; |
| david_s95 | 12:8ea29b18d289 | 193 | else if (speedControl==0) speedControl = 1; |
| david_s95 | 12:8ea29b18d289 | 194 | spinWait = (1/speedControl)/6; |
| david_s95 | 10:0309d6c49f26 | 195 | Thread::wait(PIDrate); |
| david_s95 | 10:0309d6c49f26 | 196 | } |
| david_s95 | 10:0309d6c49f26 | 197 | } |
| david_s95 | 7:5932ed0bad6d | 198 | |
| david_s95 | 13:87ab3b008803 | 199 | void sing(float singFreq) |
| david_s95 | 9:575b29cbf5e4 | 200 | { |
| david_s95 | 13:87ab3b008803 | 201 | motor = driveTable[1]; |
| david_s95 | 13:87ab3b008803 | 202 | wait(1.0); |
| david_s95 | 13:87ab3b008803 | 203 | float singDelayus = 1000000/singFreq; |
| david_s95 | 13:87ab3b008803 | 204 | for(int count=0; count<singFreq; count++) { |
| david_s95 | 13:87ab3b008803 | 205 | motor = driveTable[2]; |
| david_s95 | 13:87ab3b008803 | 206 | wait_us(singDelayus); |
| david_s95 | 13:87ab3b008803 | 207 | motor = driveTable[1]; |
| david_s95 | 8:77627657da80 | 208 | } |
| david_s95 | 13:87ab3b008803 | 209 | singFreq = 15000; |
| david_s95 | 13:87ab3b008803 | 210 | singDelayus = 1000000/singFreq; |
| david_s95 | 13:87ab3b008803 | 211 | for(int count=0; count<singFreq; count++) { |
| david_s95 | 13:87ab3b008803 | 212 | motor = driveTable[2]; |
| david_s95 | 13:87ab3b008803 | 213 | wait_us(singDelayus); |
| david_s95 | 13:87ab3b008803 | 214 | motor = driveTable[1]; |
| david_s95 | 13:87ab3b008803 | 215 | } |
| david_s95 | 8:77627657da80 | 216 | } |
| david_s95 | 8:77627657da80 | 217 | |
| david_s95 | 8:77627657da80 | 218 | |
| mengkiang | 4:f8a9ce214db9 | 219 | //Main function |
| theMaO | 15:d9e50101a17e | 220 | >>>>>>> other |
| david_s95 | 5:e5313b695302 | 221 | int main() |
| david_s95 | 5:e5313b695302 | 222 | { |
| david_s95 | 9:575b29cbf5e4 | 223 | pc.printf("spin\n\r"); |
| david_s95 | 10:0309d6c49f26 | 224 | |
| estott | 0:de4320f74764 | 225 | //Run the motor synchronisation |
| estott | 2:4e88faab6988 | 226 | orState = motorHome(); |
| david_s95 | 6:4edbe75736d9 | 227 | //orState is subtracted from future rotor state inputs to align rotor and motor states |
| david_s95 | 6:4edbe75736d9 | 228 | |
| estott | 2:4e88faab6988 | 229 | pc.printf("Rotor origin: %x\n\r",orState); |
| david_s95 | 5:e5313b695302 | 230 | |
| david_s95 | 6:4edbe75736d9 | 231 | char command[ARRAYSIZE]; |
| david_s95 | 6:4edbe75736d9 | 232 | int index=0; |
| david_s95 | 6:4edbe75736d9 | 233 | int units = 0, tens = 0, decimals = 0; |
| david_s95 | 6:4edbe75736d9 | 234 | char ch; |
| david_s95 | 9:575b29cbf5e4 | 235 | testpin=0; |
| david_s95 | 12:8ea29b18d289 | 236 | int vartens = 0, varunits = 0, vardecs = 0; |
| david_s95 | 7:5932ed0bad6d | 237 | |
| david_s95 | 10:0309d6c49f26 | 238 | speedTimer.reset(); |
| david_s95 | 10:0309d6c49f26 | 239 | speedTimer.start(); |
| david_s95 | 10:0309d6c49f26 | 240 | I2.mode(PullNone); |
| david_s95 | 10:0309d6c49f26 | 241 | I2.fall(&rps); |
| david_s95 | 7:5932ed0bad6d | 242 | |
| david_s95 | 10:0309d6c49f26 | 243 | VPIDthread.start(VPID); |
| david_s95 | 10:0309d6c49f26 | 244 | |
| david_s95 | 5:e5313b695302 | 245 | while(1) { |
| david_s95 | 9:575b29cbf5e4 | 246 | // clk = I2; |
| david_s95 | 6:4edbe75736d9 | 247 | //Toggle LED so we know something's happening |
| david_s95 | 8:77627657da80 | 248 | // clk = !clk; |
| david_s95 | 7:5932ed0bad6d | 249 | |
| david_s95 | 6:4edbe75736d9 | 250 | //If there's a character to read from the serial port |
| david_s95 | 6:4edbe75736d9 | 251 | if (pc.readable()) { |
| david_s95 | 7:5932ed0bad6d | 252 | |
| david_s95 | 6:4edbe75736d9 | 253 | //Clear index counter and control variables |
| david_s95 | 6:4edbe75736d9 | 254 | index = 0; |
| david_s95 | 7:5932ed0bad6d | 255 | // revsec = spinWait = 0; |
| david_s95 | 7:5932ed0bad6d | 256 | |
| david_s95 | 6:4edbe75736d9 | 257 | //Read each value from the serial port until Enter key is pressed |
| david_s95 | 6:4edbe75736d9 | 258 | do { |
| david_s95 | 6:4edbe75736d9 | 259 | //Read character |
| david_s95 | 6:4edbe75736d9 | 260 | ch = pc.getc(); |
| david_s95 | 6:4edbe75736d9 | 261 | //Print character to serial for visual feedback |
| david_s95 | 6:4edbe75736d9 | 262 | pc.putc(ch); |
| david_s95 | 6:4edbe75736d9 | 263 | //Add character to input array |
| david_s95 | 6:4edbe75736d9 | 264 | command[index++]=ch; // put it into the value array and increment the index |
| david_s95 | 7:5932ed0bad6d | 265 | //d10 and d13 used for detecting Enter key on Windows/Unix/Mac |
| david_s95 | 6:4edbe75736d9 | 266 | } while(ch != 10 && ch != 13); |
| david_s95 | 7:5932ed0bad6d | 267 | |
| david_s95 | 6:4edbe75736d9 | 268 | //Start new line on terminal for printing data |
| david_s95 | 6:4edbe75736d9 | 269 | pc.putc('\n'); |
| david_s95 | 6:4edbe75736d9 | 270 | pc.putc('\r'); |
| david_s95 | 7:5932ed0bad6d | 271 | |
| david_s95 | 6:4edbe75736d9 | 272 | //Analyse the input string |
| david_s95 | 6:4edbe75736d9 | 273 | switch (command[0]) { |
| david_s95 | 7:5932ed0bad6d | 274 | //If a V was typed... |
| david_s95 | 6:4edbe75736d9 | 275 | case 'V': |
| david_s95 | 7:5932ed0bad6d | 276 | units = 0, tens = 0, decimals = 0; |
| david_s95 | 7:5932ed0bad6d | 277 | //For each character received, subtract ASCII 0 from ASCII |
| david_s95 | 7:5932ed0bad6d | 278 | //representation to obtain the integer value of the number |
| david_s95 | 9:575b29cbf5e4 | 279 | if(command[1]=='-') { |
| david_s95 | 9:575b29cbf5e4 | 280 | spinCW = 0; |
| david_s95 | 9:575b29cbf5e4 | 281 | //If decimal point is in the second character (eg, V-.1) |
| david_s95 | 9:575b29cbf5e4 | 282 | if(command[2]=='.') { |
| david_s95 | 9:575b29cbf5e4 | 283 | //Extract decimal rev/s |
| david_s95 | 9:575b29cbf5e4 | 284 | decimals = command[3] - '0'; |
| david_s95 | 7:5932ed0bad6d | 285 | |
| david_s95 | 9:575b29cbf5e4 | 286 | //If decimal point is in the third character (eg, V-0.1) |
| david_s95 | 9:575b29cbf5e4 | 287 | } else if(command[3]=='.') { |
| david_s95 | 9:575b29cbf5e4 | 288 | units = command[2] - '0'; |
| david_s95 | 9:575b29cbf5e4 | 289 | decimals = command[4] - '0'; |
| david_s95 | 7:5932ed0bad6d | 290 | |
| david_s95 | 9:575b29cbf5e4 | 291 | //If decimal point is in the fourth character (eg, V-10.1) |
| david_s95 | 9:575b29cbf5e4 | 292 | } else if(command[4]=='.') { |
| david_s95 | 9:575b29cbf5e4 | 293 | tens = command[2] - '0'; |
| david_s95 | 9:575b29cbf5e4 | 294 | units = command[3] - '0'; |
| david_s95 | 9:575b29cbf5e4 | 295 | decimals = command[5] - '0'; |
| david_s95 | 9:575b29cbf5e4 | 296 | } |
| david_s95 | 9:575b29cbf5e4 | 297 | } else { |
| david_s95 | 9:575b29cbf5e4 | 298 | spinCW = 1; |
| david_s95 | 9:575b29cbf5e4 | 299 | //If decimal point is in the second character (eg, V.1) |
| david_s95 | 9:575b29cbf5e4 | 300 | if(command[1]=='.') { |
| david_s95 | 9:575b29cbf5e4 | 301 | //Extract decimal rev/s |
| david_s95 | 9:575b29cbf5e4 | 302 | decimals = command[2] - '0'; |
| david_s95 | 7:5932ed0bad6d | 303 | |
| david_s95 | 9:575b29cbf5e4 | 304 | //If decimal point is in the third character (eg, V0.1) |
| david_s95 | 9:575b29cbf5e4 | 305 | } else if(command[2]=='.') { |
| david_s95 | 9:575b29cbf5e4 | 306 | units = command[1] - '0'; |
| david_s95 | 9:575b29cbf5e4 | 307 | decimals = command[3] - '0'; |
| david_s95 | 9:575b29cbf5e4 | 308 | |
| david_s95 | 9:575b29cbf5e4 | 309 | //If decimal point is in the fourth character (eg, V10.1) |
| david_s95 | 9:575b29cbf5e4 | 310 | } else if(command[3]=='.') { |
| david_s95 | 9:575b29cbf5e4 | 311 | tens = command[1] - '0'; |
| david_s95 | 9:575b29cbf5e4 | 312 | units = command[2] - '0'; |
| david_s95 | 9:575b29cbf5e4 | 313 | decimals = command[4] - '0'; |
| david_s95 | 9:575b29cbf5e4 | 314 | } |
| david_s95 | 6:4edbe75736d9 | 315 | } |
| david_s95 | 7:5932ed0bad6d | 316 | |
| david_s95 | 6:4edbe75736d9 | 317 | //Calculate the number of revolutions per second required |
| david_s95 | 6:4edbe75736d9 | 318 | revsec = float(tens)*10 + float(units) + float(decimals)/10; |
| david_s95 | 6:4edbe75736d9 | 319 | //Calculate the required wait period |
| david_s95 | 12:8ea29b18d289 | 320 | |
| david_s95 | 7:5932ed0bad6d | 321 | |
| david_s95 | 6:4edbe75736d9 | 322 | //Print values for verification |
| david_s95 | 12:8ea29b18d289 | 323 | pc.printf("Rev/S: %2.4f\n\r", revsec); |
| david_s95 | 7:5932ed0bad6d | 324 | |
| david_s95 | 6:4edbe75736d9 | 325 | //Run the function to start rotating at a fixed speed |
| david_s95 | 6:4edbe75736d9 | 326 | fixedSpeed(); |
| david_s95 | 6:4edbe75736d9 | 327 | break; |
| david_s95 | 7:5932ed0bad6d | 328 | //If anything unexpected was received |
| theMaO | 14:155e9a9147d4 | 329 | |
| theMaO | 14:155e9a9147d4 | 330 | case 'R': |
| theMaO | 14:155e9a9147d4 | 331 | hdrds = 0; units = 0, tens = 0, decimals = 0; |
| theMaO | 14:155e9a9147d4 | 332 | //For each character received, subtract ASCII 0 from ASCII |
| theMaO | 14:155e9a9147d4 | 333 | //representation to obtain the integer value of the number |
| theMaO | 14:155e9a9147d4 | 334 | if(command[1]=='-') { |
| theMaO | 14:155e9a9147d4 | 335 | spinCW = 0; |
| theMaO | 14:155e9a9147d4 | 336 | //If decimal point is in the second character (eg, V-.1) |
| theMaO | 14:155e9a9147d4 | 337 | if(command[2]=='.') { |
| theMaO | 14:155e9a9147d4 | 338 | //Extract decimal rev/s |
| theMaO | 14:155e9a9147d4 | 339 | decimals = command[3] - '0'; |
| theMaO | 14:155e9a9147d4 | 340 | |
| theMaO | 14:155e9a9147d4 | 341 | //If decimal point is in the third character (eg, V-0.1) |
| theMaO | 14:155e9a9147d4 | 342 | } else if(command[3]=='.') { |
| theMaO | 14:155e9a9147d4 | 343 | units = command[2] - '0'; |
| theMaO | 14:155e9a9147d4 | 344 | decimals = command[4] - '0'; |
| theMaO | 14:155e9a9147d4 | 345 | |
| theMaO | 14:155e9a9147d4 | 346 | //If decimal point is in the fourth character (eg, V-10.1) |
| theMaO | 14:155e9a9147d4 | 347 | } else if(command[4]=='.') { |
| theMaO | 14:155e9a9147d4 | 348 | tens = command[2] - '0'; |
| theMaO | 14:155e9a9147d4 | 349 | units = command[3] - '0'; |
| theMaO | 14:155e9a9147d4 | 350 | decimals = command[5] - '0'; |
| theMaO | 14:155e9a9147d4 | 351 | } else if(command[5]=='.'){ |
| theMaO | 14:155e9a9147d4 | 352 | hdrds = command[2] - '0'; |
| theMaO | 14:155e9a9147d4 | 353 | tens = command[3] - '0'; |
| theMaO | 14:155e9a9147d4 | 354 | units = command[4] - '0'; |
| theMaO | 14:155e9a9147d4 | 355 | decimals = command[6] - '0'; |
| theMaO | 14:155e9a9147d4 | 356 | } |
| theMaO | 14:155e9a9147d4 | 357 | } else { |
| theMaO | 14:155e9a9147d4 | 358 | spinCW = 1; |
| theMaO | 14:155e9a9147d4 | 359 | //If decimal point is in the second character (eg, V.1) |
| theMaO | 14:155e9a9147d4 | 360 | if(command[1]=='.') { |
| theMaO | 14:155e9a9147d4 | 361 | //Extract decimal rev/s |
| theMaO | 14:155e9a9147d4 | 362 | decimals = command[2] - '0'; |
| theMaO | 14:155e9a9147d4 | 363 | |
| theMaO | 14:155e9a9147d4 | 364 | //If decimal point is in the third character (eg, V0.1) |
| theMaO | 14:155e9a9147d4 | 365 | } else if(command[2]=='.') { |
| theMaO | 14:155e9a9147d4 | 366 | units = command[1] - '0'; |
| theMaO | 14:155e9a9147d4 | 367 | decimals = command[3] - '0'; |
| theMaO | 14:155e9a9147d4 | 368 | |
| theMaO | 14:155e9a9147d4 | 369 | //If decimal point is in the fourth character (eg, V10.1) |
| theMaO | 14:155e9a9147d4 | 370 | } else if(command[3]=='.') { |
| theMaO | 14:155e9a9147d4 | 371 | tens = command[1] - '0'; |
| theMaO | 14:155e9a9147d4 | 372 | units = command[2] - '0'; |
| theMaO | 14:155e9a9147d4 | 373 | decimals = command[4] - '0'; |
| theMaO | 14:155e9a9147d4 | 374 | } |
| theMaO | 17:9cd9f82027ca | 375 | else if(command[4]=='.'){ |
| theMaO | 17:9cd9f82027ca | 376 | hdrds = command[1] - '0'; |
| theMaO | 17:9cd9f82027ca | 377 | tens = command[2] - '0'; |
| theMaO | 17:9cd9f82027ca | 378 | units = command[3] - '0'; |
| theMaO | 17:9cd9f82027ca | 379 | decimals = command[5] - '0'; |
| theMaO | 14:155e9a9147d4 | 380 | } |
| theMaO | 14:155e9a9147d4 | 381 | } |
| theMaO | 14:155e9a9147d4 | 382 | //Calculate the number of revolutions required |
| theMaO | 17:9cd9f82027ca | 383 | targetRevs = float(hdrds)*100 + float(tens)*10 + float(units) + float(decimals)/10; |
| theMaO | 14:155e9a9147d4 | 384 | |
| theMaO | 14:155e9a9147d4 | 385 | //Print values for verification |
| theMaO | 14:155e9a9147d4 | 386 | pc.printf("Target revs: %2.4f\n\r", targetRevs); |
| theMaO | 14:155e9a9147d4 | 387 | |
| theMaO | 14:155e9a9147d4 | 388 | //Run the function to start rotating at a fixed speed |
| theMaO | 14:155e9a9147d4 | 389 | spinToPos(); |
| theMaO | 14:155e9a9147d4 | 390 | break; |
| theMaO | 14:155e9a9147d4 | 391 | |
| theMaO | 14:155e9a9147d4 | 392 | |
| david_s95 | 7:5932ed0bad6d | 393 | case 's': |
| david_s95 | 9:575b29cbf5e4 | 394 | // pc.printf("Revs / sec: %2.2f\r", revs); |
| david_s95 | 9:575b29cbf5e4 | 395 | // printSpeed.attach(&speedo, 1.0); |
| david_s95 | 10:0309d6c49f26 | 396 | printf("Measured: %2.3f, revsec: %2.3f\r\n", measuredRevs, revsec); |
| david_s95 | 10:0309d6c49f26 | 397 | printf("speed setpoint: %2.3f\r\n", speedControl); |
| david_s95 | 7:5932ed0bad6d | 398 | break; |
| david_s95 | 8:77627657da80 | 399 | case 't': |
| david_s95 | 10:0309d6c49f26 | 400 | // pc.printf("%d\n\r", pos); |
| david_s95 | 8:77627657da80 | 401 | break; |
| david_s95 | 12:8ea29b18d289 | 402 | |
| david_s95 | 12:8ea29b18d289 | 403 | case 'K': |
| david_s95 | 12:8ea29b18d289 | 404 | vartens = command[1] - '0'; |
| david_s95 | 12:8ea29b18d289 | 405 | varunits = command[2] - '0'; |
| david_s95 | 12:8ea29b18d289 | 406 | vardecs = command[4] - '0'; |
| david_s95 | 12:8ea29b18d289 | 407 | Kc = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
| david_s95 | 12:8ea29b18d289 | 408 | printf("Kc: %2.1f\r\n", Kc); |
| david_s95 | 12:8ea29b18d289 | 409 | controller.setTunings(Kc, Ti, Td); |
| david_s95 | 12:8ea29b18d289 | 410 | // controller.setMode(1); |
| david_s95 | 12:8ea29b18d289 | 411 | break; |
| david_s95 | 12:8ea29b18d289 | 412 | case 'i': |
| david_s95 | 12:8ea29b18d289 | 413 | vartens = command[1] - '0'; |
| david_s95 | 12:8ea29b18d289 | 414 | varunits = command[2] - '0'; |
| david_s95 | 12:8ea29b18d289 | 415 | vardecs = command[4] - '0'; |
| david_s95 | 12:8ea29b18d289 | 416 | Ti = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
| david_s95 | 12:8ea29b18d289 | 417 | printf("Ti: %2.1f\r\n", Ti); |
| david_s95 | 12:8ea29b18d289 | 418 | controller.setTunings(Kc, Ti, Td); |
| david_s95 | 12:8ea29b18d289 | 419 | // controller.setMode(1); |
| david_s95 | 12:8ea29b18d289 | 420 | break; |
| david_s95 | 12:8ea29b18d289 | 421 | case 'd': |
| david_s95 | 12:8ea29b18d289 | 422 | vartens = command[1] - '0'; |
| david_s95 | 12:8ea29b18d289 | 423 | varunits = command[2] - '0'; |
| david_s95 | 12:8ea29b18d289 | 424 | vardecs = command[4] - '0'; |
| david_s95 | 12:8ea29b18d289 | 425 | Td = float(vartens)*10 + float(varunits) + float(vardecs)/10; |
| david_s95 | 12:8ea29b18d289 | 426 | printf("Td: %2.1f\r\n", Td); |
| david_s95 | 12:8ea29b18d289 | 427 | controller.setTunings(Kc, Ti, Td); |
| david_s95 | 12:8ea29b18d289 | 428 | // controller.setMode(1); |
| david_s95 | 12:8ea29b18d289 | 429 | break; |
| david_s95 | 13:87ab3b008803 | 430 | case 'l': |
| david_s95 | 13:87ab3b008803 | 431 | sing(10000); |
| david_s95 | 13:87ab3b008803 | 432 | break; |
| david_s95 | 6:4edbe75736d9 | 433 | default: |
| david_s95 | 6:4edbe75736d9 | 434 | //Set speed variables to zero to stop motor spinning |
| david_s95 | 6:4edbe75736d9 | 435 | //Print error message |
| david_s95 | 10:0309d6c49f26 | 436 | motorStop(); |
| david_s95 | 10:0309d6c49f26 | 437 | pc.printf("Error in received data 0\n\r"); |
| david_s95 | 6:4edbe75736d9 | 438 | break; |
| david_s95 | 6:4edbe75736d9 | 439 | } |
| david_s95 | 6:4edbe75736d9 | 440 | } |
| david_s95 | 7:5932ed0bad6d | 441 | // printSpeed.attach(&speedo, 1.0); |
| david_s95 | 7:5932ed0bad6d | 442 | // pc.printf("Revs / sec: %2.2f\r", revs); |
| estott | 2:4e88faab6988 | 443 | } |
| david_s95 | 5:e5313b695302 | 444 | |
| estott | 0:de4320f74764 | 445 | } |
