![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
This is probably never gonna get done
main.cpp@18:e48c0910c71e, 2019-03-18 (annotated)
- Committer:
- tanyuzhuo
- Date:
- Mon Mar 18 16:30:12 2019 +0000
- Revision:
- 18:e48c0910c71e
- Parent:
- 17:ff5300ba5442
- Child:
- 20:5bd9f9e406d1
part of motor control and velocity display
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
estott | 0:de4320f74764 | 1 | #include "mbed.h" |
peterith | 13:c51d828531d5 | 2 | #include "Crypto.h" |
estott | 0:de4320f74764 | 3 | |
estott | 0:de4320f74764 | 4 | //Photointerrupter input pins |
estott | 10:a4b5723b6c9d | 5 | #define I1pin D3 |
estott | 10:a4b5723b6c9d | 6 | #define I2pin D6 |
estott | 10:a4b5723b6c9d | 7 | #define I3pin D5 |
estott | 2:4e88faab6988 | 8 | |
estott | 2:4e88faab6988 | 9 | //Incremental encoder input pins |
peterith | 13:c51d828531d5 | 10 | #define CHApin D12 |
peterith | 13:c51d828531d5 | 11 | #define CHBpin D11 |
estott | 0:de4320f74764 | 12 | |
estott | 0:de4320f74764 | 13 | //Motor Drive output pins //Mask in output byte |
estott | 10:a4b5723b6c9d | 14 | #define L1Lpin D1 //0x01 |
estott | 10:a4b5723b6c9d | 15 | #define L1Hpin A3 //0x02 |
estott | 10:a4b5723b6c9d | 16 | #define L2Lpin D0 //0x04 |
peterith | 13:c51d828531d5 | 17 | #define L2Hpin A6 //0x08 |
peterith | 13:c51d828531d5 | 18 | #define L3Lpin D10 //0x10 |
peterith | 13:c51d828531d5 | 19 | #define L3Hpin D2 //0x20 |
estott | 10:a4b5723b6c9d | 20 | |
estott | 10:a4b5723b6c9d | 21 | #define PWMpin D9 |
estott | 5:08f338b5e4d9 | 22 | |
estott | 5:08f338b5e4d9 | 23 | //Motor current sense |
peterith | 13:c51d828531d5 | 24 | #define MCSPpin A1 |
peterith | 13:c51d828531d5 | 25 | #define MCSNpin A0 |
estott | 0:de4320f74764 | 26 | |
estott | 0:de4320f74764 | 27 | //Mapping from sequential drive states to motor phase outputs |
estott | 0:de4320f74764 | 28 | /* |
estott | 0:de4320f74764 | 29 | State L1 L2 L3 |
estott | 0:de4320f74764 | 30 | 0 H - L |
estott | 0:de4320f74764 | 31 | 1 - H L |
estott | 0:de4320f74764 | 32 | 2 L H - |
estott | 0:de4320f74764 | 33 | 3 L - H |
estott | 0:de4320f74764 | 34 | 4 - L H |
estott | 0:de4320f74764 | 35 | 5 H L - |
estott | 0:de4320f74764 | 36 | 6 - - - |
estott | 0:de4320f74764 | 37 | 7 - - - |
estott | 0:de4320f74764 | 38 | */ |
estott | 0:de4320f74764 | 39 | //Drive state to output table |
estott | 0:de4320f74764 | 40 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
estott | 2:4e88faab6988 | 41 | |
estott | 0:de4320f74764 | 42 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
estott | 2:4e88faab6988 | 43 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
estott | 2:4e88faab6988 | 44 | //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 | 45 | |
estott | 2:4e88faab6988 | 46 | //Phase lead to make motor spin |
estott | 3:569b35e2a602 | 47 | const int8_t lead = 2; //2 for forwards, -2 for backwards |
estott | 0:de4320f74764 | 48 | |
estott | 0:de4320f74764 | 49 | //Status LED |
estott | 0:de4320f74764 | 50 | DigitalOut led1(LED1); |
estott | 0:de4320f74764 | 51 | |
estott | 0:de4320f74764 | 52 | //Photointerrupter inputs |
tanyuzhuo | 12:899cd6bf9844 | 53 | InterruptIn I1(I1pin); |
tanyuzhuo | 12:899cd6bf9844 | 54 | InterruptIn I2(I2pin); |
tanyuzhuo | 12:899cd6bf9844 | 55 | InterruptIn I3(I3pin); |
tanyuzhuo | 12:899cd6bf9844 | 56 | |
estott | 0:de4320f74764 | 57 | //Motor Drive outputs |
tanyuzhuo | 18:e48c0910c71e | 58 | PwmOut L1L(L1Lpin); |
estott | 0:de4320f74764 | 59 | DigitalOut L1H(L1Hpin); |
tanyuzhuo | 18:e48c0910c71e | 60 | PwmOut L2L(L2Lpin); |
estott | 0:de4320f74764 | 61 | DigitalOut L2H(L2Hpin); |
tanyuzhuo | 18:e48c0910c71e | 62 | PwmOut L3L(L3Lpin); |
estott | 0:de4320f74764 | 63 | DigitalOut L3H(L3Hpin); |
tanyuzhuo | 18:e48c0910c71e | 64 | |
estott | 0:de4320f74764 | 65 | |
peterith | 13:c51d828531d5 | 66 | int8_t orState = 0; |
tanyuzhuo | 12:899cd6bf9844 | 67 | int8_t intState = 0; |
tanyuzhuo | 12:899cd6bf9844 | 68 | int8_t intStateOld = 0; |
tanyuzhuo | 17:ff5300ba5442 | 69 | int32_t revoCounter = 0; //Counts the number of revolutions |
tanyuzhuo | 18:e48c0910c71e | 70 | int32_t motorVelocity; |
tanyuzhuo | 18:e48c0910c71e | 71 | //Phase lead to make motor spin |
tanyuzhuo | 18:e48c0910c71e | 72 | int8_t lead = -2; //2 for forwards, -2 for backwards |
tanyuzhuo | 12:899cd6bf9844 | 73 | |
peterith | 14:0481b606d10e | 74 | typedef struct { |
peterith | 14:0481b606d10e | 75 | uint64_t nonce; |
tanyuzhuo | 18:e48c0910c71e | 76 | float data; |
peterith | 14:0481b606d10e | 77 | } mail_t; |
peterith | 14:0481b606d10e | 78 | |
peterith | 14:0481b606d10e | 79 | Mail<mail_t, 16> mail_box; |
peterith | 14:0481b606d10e | 80 | Thread commandProcessorthread; |
tanyuzhuo | 16:10d53b056b17 | 81 | Thread bitcointhread; |
peterith | 14:0481b606d10e | 82 | RawSerial pc(SERIAL_TX, SERIAL_RX); |
peterith | 14:0481b606d10e | 83 | Queue<void, 8> inCharQ; |
peterith | 14:0481b606d10e | 84 | Mutex newKey_mutex; |
peterith | 14:0481b606d10e | 85 | uint64_t newKey = 0; |
peterith | 14:0481b606d10e | 86 | |
tanyuzhuo | 18:e48c0910c71e | 87 | volatile float newRev; |
tanyuzhuo | 18:e48c0910c71e | 88 | volatile float maxSpeed = 300; |
tanyuzhuo | 18:e48c0910c71e | 89 | uint32_t pulseWidth; |
tanyuzhuo | 18:e48c0910c71e | 90 | float motorPosition_command; |
tanyuzhuo | 18:e48c0910c71e | 91 | float motorPosition; |
tanyuzhuo | 18:e48c0910c71e | 92 | |
tanyuzhuo | 18:e48c0910c71e | 93 | // mail to queue messages for serial port |
tanyuzhuo | 18:e48c0910c71e | 94 | void putMessage(uint64_t *nonce,float data){ |
peterith | 14:0481b606d10e | 95 | mail_t *mail = mail_box.alloc(); |
peterith | 14:0481b606d10e | 96 | mail->nonce = *nonce; |
tanyuzhuo | 18:e48c0910c71e | 97 | mail->data = *data; |
peterith | 14:0481b606d10e | 98 | mail_box.put(mail); |
peterith | 14:0481b606d10e | 99 | } |
peterith | 14:0481b606d10e | 100 | |
peterith | 14:0481b606d10e | 101 | void serialISR() { |
peterith | 14:0481b606d10e | 102 | uint8_t newChar = pc.getc(); |
peterith | 14:0481b606d10e | 103 | inCharQ.put((void*) newChar); |
peterith | 14:0481b606d10e | 104 | } |
peterith | 14:0481b606d10e | 105 | |
peterith | 14:0481b606d10e | 106 | void commandProcessor() { |
peterith | 14:0481b606d10e | 107 | pc.attach(&serialISR); |
tanyuzhuo | 17:ff5300ba5442 | 108 | char command[19]; |
tanyuzhuo | 17:ff5300ba5442 | 109 | char* number; |
peterith | 14:0481b606d10e | 110 | //char k; |
peterith | 14:0481b606d10e | 111 | uint64_t receivedKey; |
peterith | 14:0481b606d10e | 112 | uint8_t index = 0; |
peterith | 14:0481b606d10e | 113 | while(1) { |
peterith | 14:0481b606d10e | 114 | osEvent newEvent = inCharQ.get(); |
peterith | 14:0481b606d10e | 115 | uint8_t newChar = (uint8_t) newEvent.value.p; |
peterith | 14:0481b606d10e | 116 | command[index] = newChar; |
peterith | 14:0481b606d10e | 117 | index++; |
tanyuzhuo | 17:ff5300ba5442 | 118 | if (newChar == '\r') { |
peterith | 14:0481b606d10e | 119 | command[17] = '\0'; |
tanyuzhuo | 17:ff5300ba5442 | 120 | |
tanyuzhuo | 17:ff5300ba5442 | 121 | if (command[0] == 'R') { |
tanyuzhuo | 17:ff5300ba5442 | 122 | pc.printf("Rotation command\n"); |
tanyuzhuo | 18:e48c0910c71e | 123 | |
tanyuzhuo | 17:ff5300ba5442 | 124 | pc.printf("%s", command); |
tanyuzhuo | 17:ff5300ba5442 | 125 | } |
tanyuzhuo | 17:ff5300ba5442 | 126 | else if (command[0] == 'V') { |
tanyuzhuo | 17:ff5300ba5442 | 127 | pc.printf("Max speed command\n"); |
tanyuzhuo | 17:ff5300ba5442 | 128 | pc.printf("%s", command); |
tanyuzhuo | 17:ff5300ba5442 | 129 | } |
tanyuzhuo | 17:ff5300ba5442 | 130 | else if (command[0] == 'K') { |
tanyuzhuo | 17:ff5300ba5442 | 131 | if (index == 18){ // when index is 18 means you entered K and 16 digits |
tanyuzhuo | 17:ff5300ba5442 | 132 | number = command +1; //super bad solution, but I don't know how to work with strings in C |
tanyuzhuo | 17:ff5300ba5442 | 133 | receivedKey = strtoull(number, NULL, 16); |
tanyuzhuo | 17:ff5300ba5442 | 134 | //receivedKey = 2147483648; |
tanyuzhuo | 17:ff5300ba5442 | 135 | //sscanf(command, "%d", &receivedKey); |
tanyuzhuo | 17:ff5300ba5442 | 136 | pc.printf("Received key: %016llx\n\r", receivedKey); |
tanyuzhuo | 17:ff5300ba5442 | 137 | newKey_mutex.lock(); |
tanyuzhuo | 17:ff5300ba5442 | 138 | newKey = receivedKey; |
tanyuzhuo | 17:ff5300ba5442 | 139 | newKey_mutex.unlock(); |
tanyuzhuo | 17:ff5300ba5442 | 140 | } else { |
tanyuzhuo | 17:ff5300ba5442 | 141 | pc.printf("Not a valid key!"); |
tanyuzhuo | 17:ff5300ba5442 | 142 | }; |
tanyuzhuo | 17:ff5300ba5442 | 143 | } |
tanyuzhuo | 17:ff5300ba5442 | 144 | else if (command[0] == 'T') { |
tanyuzhuo | 17:ff5300ba5442 | 145 | pc.printf("Melody command\n"); |
tanyuzhuo | 17:ff5300ba5442 | 146 | pc.printf("%s", command); |
tanyuzhuo | 17:ff5300ba5442 | 147 | } |
peterith | 14:0481b606d10e | 148 | memset(command, 0, sizeof(command)); |
peterith | 14:0481b606d10e | 149 | index = 0; |
peterith | 14:0481b606d10e | 150 | } else { |
peterith | 14:0481b606d10e | 151 | pc.printf("Current command: %s\n\r", command); |
peterith | 14:0481b606d10e | 152 | } |
peterith | 14:0481b606d10e | 153 | } |
peterith | 14:0481b606d10e | 154 | } |
tanyuzhuo | 17:ff5300ba5442 | 155 | |
tanyuzhuo | 16:10d53b056b17 | 156 | void bitcoin(){ |
tanyuzhuo | 16:10d53b056b17 | 157 | while(1) { |
tanyuzhuo | 16:10d53b056b17 | 158 | SHA256 sha; |
tanyuzhuo | 16:10d53b056b17 | 159 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
tanyuzhuo | 16:10d53b056b17 | 160 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
tanyuzhuo | 16:10d53b056b17 | 161 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
tanyuzhuo | 16:10d53b056b17 | 162 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
tanyuzhuo | 16:10d53b056b17 | 163 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
tanyuzhuo | 16:10d53b056b17 | 164 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
tanyuzhuo | 16:10d53b056b17 | 165 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
tanyuzhuo | 16:10d53b056b17 | 166 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
tanyuzhuo | 16:10d53b056b17 | 167 | uint64_t* key = (uint64_t*) ((int) sequence + 48); |
tanyuzhuo | 16:10d53b056b17 | 168 | uint64_t* nonce = (uint64_t*) ((int) sequence + 56); |
tanyuzhuo | 16:10d53b056b17 | 169 | uint8_t hash[32]; |
tanyuzhuo | 16:10d53b056b17 | 170 | |
tanyuzhuo | 16:10d53b056b17 | 171 | Timer t; |
tanyuzhuo | 16:10d53b056b17 | 172 | t.start(); |
tanyuzhuo | 16:10d53b056b17 | 173 | unsigned currentTime = 0; |
tanyuzhuo | 16:10d53b056b17 | 174 | unsigned currentCount = 0; |
tanyuzhuo | 16:10d53b056b17 | 175 | |
tanyuzhuo | 16:10d53b056b17 | 176 | for (unsigned i = 0; i <= UINT_MAX; i++) { |
tanyuzhuo | 16:10d53b056b17 | 177 | (*nonce)++; |
tanyuzhuo | 16:10d53b056b17 | 178 | newKey_mutex.lock(); |
tanyuzhuo | 16:10d53b056b17 | 179 | *key = newKey; |
tanyuzhuo | 16:10d53b056b17 | 180 | newKey_mutex.unlock(); |
tanyuzhuo | 16:10d53b056b17 | 181 | sha.computeHash(hash, sequence, 64); |
tanyuzhuo | 16:10d53b056b17 | 182 | if (hash[0] == 0 && hash[1] == 0) { |
tanyuzhuo | 16:10d53b056b17 | 183 | //putMessage(nonce); |
tanyuzhuo | 16:10d53b056b17 | 184 | pc.printf("Successful nonce: %016x\n\r", *nonce); |
tanyuzhuo | 16:10d53b056b17 | 185 | } |
tanyuzhuo | 16:10d53b056b17 | 186 | if ((unsigned) t.read() == currentTime) { |
tanyuzhuo | 16:10d53b056b17 | 187 | //pc.printf("Hash rate: %d\n\r", i - currentCount); |
tanyuzhuo | 16:10d53b056b17 | 188 | pc.printf("Current key: %016llx\n\r", *key); |
tanyuzhuo | 16:10d53b056b17 | 189 | currentTime++; |
tanyuzhuo | 16:10d53b056b17 | 190 | currentCount = i; |
tanyuzhuo | 16:10d53b056b17 | 191 | } |
tanyuzhuo | 16:10d53b056b17 | 192 | } |
tanyuzhuo | 16:10d53b056b17 | 193 | t.stop(); |
tanyuzhuo | 16:10d53b056b17 | 194 | } |
tanyuzhuo | 16:10d53b056b17 | 195 | } |
tanyuzhuo | 18:e48c0910c71e | 196 | |
tanyuzhuo | 18:e48c0910c71e | 197 | |
tanyuzhuo | 18:e48c0910c71e | 198 | |
tanyuzhuo | 18:e48c0910c71e | 199 | void motorCtrlTick(){ |
tanyuzhuo | 18:e48c0910c71e | 200 | motorCtrlT.signal_set(0x1); |
tanyuzhuo | 18:e48c0910c71e | 201 | } |
tanyuzhuo | 18:e48c0910c71e | 202 | |
tanyuzhuo | 18:e48c0910c71e | 203 | |
estott | 0:de4320f74764 | 204 | //Set a given drive state |
tanyuzhuo | 18:e48c0910c71e | 205 | void motorOut(int8_t driveState,uint32_t motorTorque){ |
estott | 0:de4320f74764 | 206 | |
estott | 2:4e88faab6988 | 207 | //Lookup the output byte from the drive state. |
estott | 2:4e88faab6988 | 208 | int8_t driveOut = driveTable[driveState & 0x07]; |
estott | 2:4e88faab6988 | 209 | |
estott | 2:4e88faab6988 | 210 | //Turn off first |
tanyuzhuo | 18:e48c0910c71e | 211 | if (~driveOut & 0x01) L1L.pulsewidth(0); |
estott | 2:4e88faab6988 | 212 | if (~driveOut & 0x02) L1H = 1; |
tanyuzhuo | 18:e48c0910c71e | 213 | if (~driveOut & 0x04) L2L.pulsewidth(0); |
estott | 2:4e88faab6988 | 214 | if (~driveOut & 0x08) L2H = 1; |
tanyuzhuo | 18:e48c0910c71e | 215 | if (~driveOut & 0x10) L3L.pulsewidth(0); |
estott | 2:4e88faab6988 | 216 | if (~driveOut & 0x20) L3H = 1; |
estott | 2:4e88faab6988 | 217 | |
estott | 2:4e88faab6988 | 218 | //Then turn on |
tanyuzhuo | 18:e48c0910c71e | 219 | if (driveOut & 0x01) L1L.pulsewidth(motorTorque); |
estott | 2:4e88faab6988 | 220 | if (driveOut & 0x02) L1H = 0; |
tanyuzhuo | 18:e48c0910c71e | 221 | if (driveOut & 0x04) L2L.pulsewidth(motorTorque); |
estott | 2:4e88faab6988 | 222 | if (driveOut & 0x08) L2H = 0; |
tanyuzhuo | 18:e48c0910c71e | 223 | if (driveOut & 0x10) L3L.pulsewidth(motorTorque); |
estott | 2:4e88faab6988 | 224 | if (driveOut & 0x20) L3H = 0; |
peterith | 13:c51d828531d5 | 225 | } |
estott | 0:de4320f74764 | 226 | |
peterith | 13:c51d828531d5 | 227 | //Convert photointerrupter inputs to a rotor state |
estott | 0:de4320f74764 | 228 | inline int8_t readRotorState(){ |
estott | 2:4e88faab6988 | 229 | return stateMap[I1 + 2*I2 + 4*I3]; |
peterith | 13:c51d828531d5 | 230 | } |
estott | 0:de4320f74764 | 231 | |
estott | 2:4e88faab6988 | 232 | int8_t motorHome() { |
tanyuzhuo | 18:e48c0910c71e | 233 | //Put the motor in drive state 0 and wait for it to stabilize |
tanyuzhuo | 18:e48c0910c71e | 234 | L1L.period(2000); |
tanyuzhuo | 18:e48c0910c71e | 235 | L2L.period(2000); |
tanyuzhuo | 18:e48c0910c71e | 236 | L3L.period(2000); |
tanyuzhuo | 18:e48c0910c71e | 237 | motorOut(0,200); |
estott | 3:569b35e2a602 | 238 | wait(2.0); |
estott | 2:4e88faab6988 | 239 | return readRotorState(); |
estott | 0:de4320f74764 | 240 | } |
peterith | 13:c51d828531d5 | 241 | |
tanyuzhuo | 18:e48c0910c71e | 242 | //orState is subtracted from future rotor state inputs to align rotor and motor states |
tanyuzhuo | 18:e48c0910c71e | 243 | int8_t orState = motorHome(); |
tanyuzhuo | 18:e48c0910c71e | 244 | // ISR to handle the updating of the motor position |
tanyuzhuo | 18:e48c0910c71e | 245 | void motorISR() { |
tanyuzhuo | 18:e48c0910c71e | 246 | static int8_t oldRotorState; |
tanyuzhuo | 18:e48c0910c71e | 247 | int8_t rotorState = readRotorState(); |
tanyuzhuo | 18:e48c0910c71e | 248 | motorOut((rotorState-orState+lead+6)%6,pulseWidth); //+6 to make sure the remainder is positive |
tanyuzhuo | 18:e48c0910c71e | 249 | if (rotorState - oldRotorState == 5) motorPosition --; |
tanyuzhuo | 18:e48c0910c71e | 250 | else if (rotorState - oldRotorState == -5) motorPosition ++; |
tanyuzhuo | 18:e48c0910c71e | 251 | else motorPosition += (rotorState - oldRotorState); |
tanyuzhuo | 18:e48c0910c71e | 252 | oldRotorState = rotorState; |
tanyuzhuo | 18:e48c0910c71e | 253 | } |
tanyuzhuo | 18:e48c0910c71e | 254 | /*void push() { |
peterith | 13:c51d828531d5 | 255 | intState = readRotorState(); |
peterith | 13:c51d828531d5 | 256 | if (intState != intStateOld) { |
peterith | 13:c51d828531d5 | 257 | intStateOld = intState; |
peterith | 13:c51d828531d5 | 258 | motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive |
peterith | 13:c51d828531d5 | 259 | } |
tanyuzhuo | 18:e48c0910c71e | 260 | }*/ |
peterith | 13:c51d828531d5 | 261 | |
tanyuzhuo | 18:e48c0910c71e | 262 | void motorCtrlFn(){ |
tanyuzhuo | 18:e48c0910c71e | 263 | int32_t counter=0; |
tanyuzhuo | 18:e48c0910c71e | 264 | static int32_t oldmotorPosition; |
tanyuzhuo | 18:e48c0910c71e | 265 | // Timer to count time passed between ticks to calculate velocity |
tanyuzhuo | 18:e48c0910c71e | 266 | Timer motorTime; |
tanyuzhuo | 18:e48c0910c71e | 267 | motorTime.start(); |
tanyuzhuo | 18:e48c0910c71e | 268 | float motorPos; |
tanyuzhuo | 18:e48c0910c71e | 269 | float windingSpeed; |
tanyuzhuo | 18:e48c0910c71e | 270 | float windingRev; |
tanyuzhuo | 18:e48c0910c71e | 271 | |
tanyuzhuo | 18:e48c0910c71e | 272 | Ticker motorCtrlTicker; |
tanyuzhuo | 18:e48c0910c71e | 273 | motorCtrlTicker.attach_us(&motorCtrlTick,100000); |
tanyuzhuo | 18:e48c0910c71e | 274 | while(1){ |
tanyuzhuo | 18:e48c0910c71e | 275 | motorCtrlT.signal_wait(0x1); |
tanyuzhuo | 18:e48c0910c71e | 276 | // convert state change into rotations |
tanyuzhuo | 18:e48c0910c71e | 277 | windingSpeed = maxSpeed*6; |
tanyuzhuo | 18:e48c0910c71e | 278 | windingRev = newRev*6; |
tanyuzhuo | 18:e48c0910c71e | 279 | motorPos = motorPosition; |
tanyuzhuo | 18:e48c0910c71e | 280 | motorVelocity = (motorPos - oldmotorPosition)/motorTime.read(); |
tanyuzhuo | 18:e48c0910c71e | 281 | oldmotorPosition = motorPos; |
tanyuzhuo | 18:e48c0910c71e | 282 | motorTime.reset(); |
tanyuzhuo | 18:e48c0910c71e | 283 | // Serial output to monitor speed and position |
tanyuzhuo | 18:e48c0910c71e | 284 | counter++; |
tanyuzhuo | 18:e48c0910c71e | 285 | if(counter == 10){ |
tanyuzhuo | 18:e48c0910c71e | 286 | counter = 0; |
tanyuzhuo | 18:e48c0910c71e | 287 | //display velocity and motor position |
tanyuzhuo | 18:e48c0910c71e | 288 | putMessage(3,(float)(motorPos/6.0)); |
tanyuzhuo | 18:e48c0910c71e | 289 | putMessage(4,(float)(motorVelocity/6.0)); |
tanyuzhuo | 18:e48c0910c71e | 290 | } |
tanyuzhuo | 18:e48c0910c71e | 291 | } |
peterith | 13:c51d828531d5 | 292 | int main() { |
peterith | 14:0481b606d10e | 293 | //Serial pc(SERIAL_TX, SERIAL_RX); |
peterith | 14:0481b606d10e | 294 | |
tanyuzhuo | 17:ff5300ba5442 | 295 | //Initialise bincoin mining and communication |
tanyuzhuo | 16:10d53b056b17 | 296 | bitcointhread.set_priority(osPriorityNormal); |
tanyuzhuo | 16:10d53b056b17 | 297 | commandProcessorthread.set_priority(osPriorityHigh); |
peterith | 14:0481b606d10e | 298 | commandProcessorthread.start(commandProcessor); |
tanyuzhuo | 16:10d53b056b17 | 299 | bitcointhread.start(bitcoin); |
tanyuzhuo | 17:ff5300ba5442 | 300 | |
tanyuzhuo | 18:e48c0910c71e | 301 | //PWM.period(0.002f); //Set PWM period in seconds |
tanyuzhuo | 18:e48c0910c71e | 302 | //PWM.write(0.5); //Set PWM duty in % |
peterith | 14:0481b606d10e | 303 | |
peterith | 13:c51d828531d5 | 304 | pc.printf("Hello Pete\n\r"); |
peterith | 14:0481b606d10e | 305 | |
estott | 2:4e88faab6988 | 306 | orState = motorHome(); |
peterith | 13:c51d828531d5 | 307 | pc.printf("Rotor origin: %x\n\r", orState); |
estott | 0:de4320f74764 | 308 | |
tanyuzhuo | 12:899cd6bf9844 | 309 | I1.rise(&push); |
tanyuzhuo | 12:899cd6bf9844 | 310 | I2.rise(&push); |
tanyuzhuo | 12:899cd6bf9844 | 311 | I3.rise(&push); |
peterith | 13:c51d828531d5 | 312 | |
peterith | 13:c51d828531d5 | 313 | I1.fall(&push); |
peterith | 13:c51d828531d5 | 314 | I2.fall(&push); |
peterith | 13:c51d828531d5 | 315 | I3.fall(&push); |
peterith | 13:c51d828531d5 | 316 | |
tanyuzhuo | 16:10d53b056b17 | 317 | |
estott | 0:de4320f74764 | 318 | } |