Olaf Sikorski
/
motor-mining
This is probably never gonna get done
main.cpp@15:f43d1d4e4260, 2019-03-12 (annotated)
- Committer:
- tanyuzhuo
- Date:
- Tue Mar 12 15:42:01 2019 +0000
- Revision:
- 15:f43d1d4e4260
- Parent:
- 14:0481b606d10e
- Child:
- 16:10d53b056b17
fixed truncation error (formatting issues)
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 |
estott | 0:de4320f74764 | 58 | DigitalOut L1L(L1Lpin); |
estott | 0:de4320f74764 | 59 | DigitalOut L1H(L1Hpin); |
estott | 0:de4320f74764 | 60 | DigitalOut L2L(L2Lpin); |
estott | 0:de4320f74764 | 61 | DigitalOut L2H(L2Hpin); |
estott | 0:de4320f74764 | 62 | DigitalOut L3L(L3Lpin); |
estott | 0:de4320f74764 | 63 | DigitalOut L3H(L3Hpin); |
estott | 0:de4320f74764 | 64 | |
peterith | 13:c51d828531d5 | 65 | int8_t orState = 0; |
tanyuzhuo | 12:899cd6bf9844 | 66 | int8_t intState = 0; |
tanyuzhuo | 12:899cd6bf9844 | 67 | int8_t intStateOld = 0; |
tanyuzhuo | 12:899cd6bf9844 | 68 | |
peterith | 14:0481b606d10e | 69 | typedef struct { |
peterith | 14:0481b606d10e | 70 | uint64_t nonce; |
peterith | 14:0481b606d10e | 71 | } mail_t; |
peterith | 14:0481b606d10e | 72 | |
peterith | 14:0481b606d10e | 73 | Mail<mail_t, 16> mail_box; |
peterith | 14:0481b606d10e | 74 | Thread commandProcessorthread; |
peterith | 14:0481b606d10e | 75 | RawSerial pc(SERIAL_TX, SERIAL_RX); |
peterith | 14:0481b606d10e | 76 | Queue<void, 8> inCharQ; |
peterith | 14:0481b606d10e | 77 | Mutex newKey_mutex; |
peterith | 14:0481b606d10e | 78 | uint64_t newKey = 0; |
peterith | 14:0481b606d10e | 79 | |
peterith | 14:0481b606d10e | 80 | void putMessage(uint64_t *nonce){ |
peterith | 14:0481b606d10e | 81 | mail_t *mail = mail_box.alloc(); |
peterith | 14:0481b606d10e | 82 | mail->nonce = *nonce; |
peterith | 14:0481b606d10e | 83 | mail_box.put(mail); |
peterith | 14:0481b606d10e | 84 | } |
peterith | 14:0481b606d10e | 85 | |
peterith | 14:0481b606d10e | 86 | void serialISR() { |
peterith | 14:0481b606d10e | 87 | uint8_t newChar = pc.getc(); |
peterith | 14:0481b606d10e | 88 | inCharQ.put((void*) newChar); |
peterith | 14:0481b606d10e | 89 | } |
peterith | 14:0481b606d10e | 90 | |
peterith | 14:0481b606d10e | 91 | void commandProcessor() { |
peterith | 14:0481b606d10e | 92 | pc.attach(&serialISR); |
peterith | 14:0481b606d10e | 93 | char command[18]; |
peterith | 14:0481b606d10e | 94 | //char k; |
peterith | 14:0481b606d10e | 95 | uint64_t receivedKey; |
peterith | 14:0481b606d10e | 96 | uint8_t index = 0; |
peterith | 14:0481b606d10e | 97 | while(1) { |
peterith | 14:0481b606d10e | 98 | osEvent newEvent = inCharQ.get(); |
peterith | 14:0481b606d10e | 99 | uint8_t newChar = (uint8_t) newEvent.value.p; |
peterith | 14:0481b606d10e | 100 | command[index] = newChar; |
peterith | 14:0481b606d10e | 101 | index++; |
peterith | 14:0481b606d10e | 102 | if (newChar == '\r') { |
peterith | 14:0481b606d10e | 103 | command[17] = '\0'; |
peterith | 14:0481b606d10e | 104 | receivedKey = strtoull(command, NULL, 16); |
peterith | 14:0481b606d10e | 105 | //receivedKey = 2147483648; |
peterith | 14:0481b606d10e | 106 | //sscanf(command, "%d", &receivedKey); |
tanyuzhuo | 15:f43d1d4e4260 | 107 | pc.printf("Received key: %016llx\n\r", receivedKey); |
peterith | 14:0481b606d10e | 108 | memset(command, 0, sizeof(command)); |
peterith | 14:0481b606d10e | 109 | newKey_mutex.lock(); |
peterith | 14:0481b606d10e | 110 | newKey = receivedKey; |
peterith | 14:0481b606d10e | 111 | newKey_mutex.unlock(); |
peterith | 14:0481b606d10e | 112 | index = 0; |
peterith | 14:0481b606d10e | 113 | } else { |
peterith | 14:0481b606d10e | 114 | pc.printf("Current command: %s\n\r", command); |
peterith | 14:0481b606d10e | 115 | } |
peterith | 14:0481b606d10e | 116 | } |
peterith | 14:0481b606d10e | 117 | } |
peterith | 14:0481b606d10e | 118 | |
estott | 0:de4320f74764 | 119 | //Set a given drive state |
estott | 0:de4320f74764 | 120 | void motorOut(int8_t driveState){ |
estott | 0:de4320f74764 | 121 | |
estott | 2:4e88faab6988 | 122 | //Lookup the output byte from the drive state. |
estott | 2:4e88faab6988 | 123 | int8_t driveOut = driveTable[driveState & 0x07]; |
estott | 2:4e88faab6988 | 124 | |
estott | 2:4e88faab6988 | 125 | //Turn off first |
estott | 2:4e88faab6988 | 126 | if (~driveOut & 0x01) L1L = 0; |
estott | 2:4e88faab6988 | 127 | if (~driveOut & 0x02) L1H = 1; |
estott | 2:4e88faab6988 | 128 | if (~driveOut & 0x04) L2L = 0; |
estott | 2:4e88faab6988 | 129 | if (~driveOut & 0x08) L2H = 1; |
estott | 2:4e88faab6988 | 130 | if (~driveOut & 0x10) L3L = 0; |
estott | 2:4e88faab6988 | 131 | if (~driveOut & 0x20) L3H = 1; |
estott | 2:4e88faab6988 | 132 | |
estott | 2:4e88faab6988 | 133 | //Then turn on |
estott | 2:4e88faab6988 | 134 | if (driveOut & 0x01) L1L = 1; |
estott | 2:4e88faab6988 | 135 | if (driveOut & 0x02) L1H = 0; |
estott | 2:4e88faab6988 | 136 | if (driveOut & 0x04) L2L = 1; |
estott | 2:4e88faab6988 | 137 | if (driveOut & 0x08) L2H = 0; |
estott | 2:4e88faab6988 | 138 | if (driveOut & 0x10) L3L = 1; |
estott | 2:4e88faab6988 | 139 | if (driveOut & 0x20) L3H = 0; |
peterith | 13:c51d828531d5 | 140 | } |
estott | 0:de4320f74764 | 141 | |
peterith | 13:c51d828531d5 | 142 | //Convert photointerrupter inputs to a rotor state |
estott | 0:de4320f74764 | 143 | inline int8_t readRotorState(){ |
estott | 2:4e88faab6988 | 144 | return stateMap[I1 + 2*I2 + 4*I3]; |
peterith | 13:c51d828531d5 | 145 | } |
estott | 0:de4320f74764 | 146 | |
estott | 2:4e88faab6988 | 147 | int8_t motorHome() { |
estott | 0:de4320f74764 | 148 | motorOut(0); |
estott | 3:569b35e2a602 | 149 | wait(2.0); |
estott | 2:4e88faab6988 | 150 | return readRotorState(); |
estott | 0:de4320f74764 | 151 | } |
peterith | 13:c51d828531d5 | 152 | |
peterith | 13:c51d828531d5 | 153 | void push() { |
peterith | 13:c51d828531d5 | 154 | intState = readRotorState(); |
peterith | 13:c51d828531d5 | 155 | if (intState != intStateOld) { |
peterith | 13:c51d828531d5 | 156 | intStateOld = intState; |
peterith | 13:c51d828531d5 | 157 | motorOut((intState - orState + lead +6) % 6); //+6 to make sure the remainder is positive |
peterith | 13:c51d828531d5 | 158 | } |
peterith | 13:c51d828531d5 | 159 | } |
peterith | 13:c51d828531d5 | 160 | |
peterith | 13:c51d828531d5 | 161 | int main() { |
peterith | 14:0481b606d10e | 162 | //Serial pc(SERIAL_TX, SERIAL_RX); |
peterith | 14:0481b606d10e | 163 | |
peterith | 14:0481b606d10e | 164 | commandProcessorthread.start(commandProcessor); |
peterith | 14:0481b606d10e | 165 | |
peterith | 13:c51d828531d5 | 166 | pc.printf("Hello Pete\n\r"); |
peterith | 14:0481b606d10e | 167 | |
estott | 2:4e88faab6988 | 168 | orState = motorHome(); |
peterith | 13:c51d828531d5 | 169 | pc.printf("Rotor origin: %x\n\r", orState); |
estott | 0:de4320f74764 | 170 | |
tanyuzhuo | 12:899cd6bf9844 | 171 | I1.rise(&push); |
tanyuzhuo | 12:899cd6bf9844 | 172 | I2.rise(&push); |
tanyuzhuo | 12:899cd6bf9844 | 173 | I3.rise(&push); |
peterith | 13:c51d828531d5 | 174 | |
peterith | 13:c51d828531d5 | 175 | I1.fall(&push); |
peterith | 13:c51d828531d5 | 176 | I2.fall(&push); |
peterith | 13:c51d828531d5 | 177 | I3.fall(&push); |
peterith | 13:c51d828531d5 | 178 | |
peterith | 13:c51d828531d5 | 179 | while(1) { |
peterith | 13:c51d828531d5 | 180 | SHA256 sha; |
peterith | 13:c51d828531d5 | 181 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
peterith | 13:c51d828531d5 | 182 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
peterith | 13:c51d828531d5 | 183 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
peterith | 13:c51d828531d5 | 184 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
peterith | 13:c51d828531d5 | 185 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
peterith | 13:c51d828531d5 | 186 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
peterith | 13:c51d828531d5 | 187 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
peterith | 13:c51d828531d5 | 188 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
peterith | 13:c51d828531d5 | 189 | uint64_t* key = (uint64_t*) ((int) sequence + 48); |
peterith | 13:c51d828531d5 | 190 | uint64_t* nonce = (uint64_t*) ((int) sequence + 56); |
peterith | 13:c51d828531d5 | 191 | uint8_t hash[32]; |
peterith | 13:c51d828531d5 | 192 | |
peterith | 13:c51d828531d5 | 193 | Timer t; |
peterith | 13:c51d828531d5 | 194 | t.start(); |
peterith | 14:0481b606d10e | 195 | unsigned currentTime = 0; |
peterith | 14:0481b606d10e | 196 | unsigned currentCount = 0; |
peterith | 14:0481b606d10e | 197 | |
peterith | 14:0481b606d10e | 198 | for (unsigned i = 0; i <= UINT_MAX; i++) { |
peterith | 13:c51d828531d5 | 199 | (*nonce)++; |
peterith | 14:0481b606d10e | 200 | newKey_mutex.lock(); |
peterith | 14:0481b606d10e | 201 | *key = newKey; |
peterith | 14:0481b606d10e | 202 | newKey_mutex.unlock(); |
peterith | 14:0481b606d10e | 203 | sha.computeHash(hash, sequence, 64); |
peterith | 13:c51d828531d5 | 204 | if (hash[0] == 0 && hash[1] == 0) { |
peterith | 14:0481b606d10e | 205 | //putMessage(nonce); |
peterith | 13:c51d828531d5 | 206 | pc.printf("Successful nonce: %016x\n\r", *nonce); |
peterith | 13:c51d828531d5 | 207 | } |
peterith | 14:0481b606d10e | 208 | if ((unsigned) t.read() == currentTime) { |
peterith | 14:0481b606d10e | 209 | //pc.printf("Hash rate: %d\n\r", i - currentCount); |
tanyuzhuo | 15:f43d1d4e4260 | 210 | pc.printf("Current key: %016llx\n\r", *key); |
peterith | 14:0481b606d10e | 211 | currentTime++; |
peterith | 14:0481b606d10e | 212 | currentCount = i; |
peterith | 13:c51d828531d5 | 213 | } |
peterith | 13:c51d828531d5 | 214 | } |
peterith | 14:0481b606d10e | 215 | t.stop(); |
peterith | 13:c51d828531d5 | 216 | } |
estott | 0:de4320f74764 | 217 | } |