fork of what I have been writing
ES_CW2_Starter_STARFISH/main.cpp@11:038d3ba0d720, 2020-03-06 (annotated)
- Committer:
- kubitz
- Date:
- Fri Mar 06 14:27:16 2020 +0000
- Revision:
- 11:038d3ba0d720
- Parent:
- 10:3669e3d832ed
- Child:
- 12:38afe92e67d0
File tree - restructured
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kubitz | 5:de6430aee646 | 1 | |
kubitz | 11:038d3ba0d720 | 2 | #include "import.h" |
kubitz | 9:4135d0c8dc10 | 3 | |
kubitz | 9:4135d0c8dc10 | 4 | // Declaration of threads |
kubitz | 6:5f4a954cb8bc | 5 | Thread thread_crypto; |
kubitz | 9:4135d0c8dc10 | 6 | Thread thread_processor; |
kubitz | 10:3669e3d832ed | 7 | |
kubitz | 10:3669e3d832ed | 8 | |
kubitz | 9:4135d0c8dc10 | 9 | // Timing variables for printing calculation rate |
kubitz | 9:4135d0c8dc10 | 10 | Timer timer_nonce; |
kubitz | 9:4135d0c8dc10 | 11 | uint32_t previous_time; |
kubitz | 9:4135d0c8dc10 | 12 | |
kubitz | 11:038d3ba0d720 | 13 | |
kubitz | 0:19fd8c1944fb | 14 | //Photointerrupter input pins |
kubitz | 0:19fd8c1944fb | 15 | #define I1pin D3 |
kubitz | 0:19fd8c1944fb | 16 | #define I2pin D6 |
kubitz | 0:19fd8c1944fb | 17 | #define I3pin D5 |
kubitz | 0:19fd8c1944fb | 18 | |
kubitz | 0:19fd8c1944fb | 19 | //Incremental encoder input pins |
kubitz | 9:4135d0c8dc10 | 20 | #define CHApin D12 |
kubitz | 9:4135d0c8dc10 | 21 | #define CHBpin D11 |
kubitz | 0:19fd8c1944fb | 22 | |
kubitz | 0:19fd8c1944fb | 23 | //Motor Drive output pins //Mask in output byte |
kubitz | 9:4135d0c8dc10 | 24 | #define L1Lpin D1 //0x01 |
kubitz | 9:4135d0c8dc10 | 25 | #define L1Hpin A3 //0x02 |
kubitz | 9:4135d0c8dc10 | 26 | #define L2Lpin D0 //0x04 |
kubitz | 9:4135d0c8dc10 | 27 | #define L2Hpin A6 //0x08 |
kubitz | 9:4135d0c8dc10 | 28 | #define L3Lpin D10 //0x10 |
kubitz | 9:4135d0c8dc10 | 29 | #define L3Hpin D2 //0x20 |
kubitz | 0:19fd8c1944fb | 30 | |
kubitz | 0:19fd8c1944fb | 31 | #define PWMpin D9 |
kubitz | 0:19fd8c1944fb | 32 | |
kubitz | 0:19fd8c1944fb | 33 | //Motor current sense |
kubitz | 9:4135d0c8dc10 | 34 | #define MCSPpin A1 |
kubitz | 9:4135d0c8dc10 | 35 | #define MCSNpin A0 |
kubitz | 0:19fd8c1944fb | 36 | |
kubitz | 0:19fd8c1944fb | 37 | //Test outputs |
kubitz | 0:19fd8c1944fb | 38 | #define TP0pin D4 |
kubitz | 0:19fd8c1944fb | 39 | #define TP1pin D13 |
kubitz | 0:19fd8c1944fb | 40 | #define TP2pin A2 |
kubitz | 0:19fd8c1944fb | 41 | |
kubitz | 0:19fd8c1944fb | 42 | //Mapping from sequential drive states to motor phase outputs |
kubitz | 0:19fd8c1944fb | 43 | /* |
kubitz | 0:19fd8c1944fb | 44 | State L1 L2 L3 |
kubitz | 0:19fd8c1944fb | 45 | 0 H - L |
kubitz | 0:19fd8c1944fb | 46 | 1 - H L |
kubitz | 0:19fd8c1944fb | 47 | 2 L H - |
kubitz | 0:19fd8c1944fb | 48 | 3 L - H |
kubitz | 0:19fd8c1944fb | 49 | 4 - L H |
kubitz | 0:19fd8c1944fb | 50 | 5 H L - |
kubitz | 0:19fd8c1944fb | 51 | 6 - - - |
kubitz | 0:19fd8c1944fb | 52 | 7 - - - |
kubitz | 0:19fd8c1944fb | 53 | */ |
kubitz | 0:19fd8c1944fb | 54 | //Drive state to output table |
kubitz | 9:4135d0c8dc10 | 55 | const int8_t driveTable[] = {0x12, 0x18, 0x09, 0x21, 0x24, 0x06, 0x00, 0x00}; |
kubitz | 0:19fd8c1944fb | 56 | |
kubitz | 0:19fd8c1944fb | 57 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
kubitz | 9:4135d0c8dc10 | 58 | const int8_t stateMap[] = {0x07, 0x05, 0x03, 0x04, 0x01, 0x00, 0x02, 0x07}; |
kubitz | 0:19fd8c1944fb | 59 | //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed |
kubitz | 0:19fd8c1944fb | 60 | |
kubitz | 0:19fd8c1944fb | 61 | //Phase lead to make motor spin |
kubitz | 9:4135d0c8dc10 | 62 | const int8_t lead = 2; //2 for forwards, -2 for backwards |
kubitz | 0:19fd8c1944fb | 63 | |
kubitz | 0:19fd8c1944fb | 64 | //Status LED |
kubitz | 0:19fd8c1944fb | 65 | DigitalOut led1(LED1); |
kubitz | 0:19fd8c1944fb | 66 | |
kubitz | 0:19fd8c1944fb | 67 | //Photointerrupter inputs |
kubitz | 0:19fd8c1944fb | 68 | InterruptIn I1(I1pin); |
kubitz | 0:19fd8c1944fb | 69 | InterruptIn I2(I2pin); |
kubitz | 0:19fd8c1944fb | 70 | InterruptIn I3(I3pin); |
kubitz | 0:19fd8c1944fb | 71 | |
kubitz | 0:19fd8c1944fb | 72 | //Motor Drive outputs |
kubitz | 0:19fd8c1944fb | 73 | DigitalOut L1L(L1Lpin); |
kubitz | 0:19fd8c1944fb | 74 | DigitalOut L1H(L1Hpin); |
kubitz | 0:19fd8c1944fb | 75 | DigitalOut L2L(L2Lpin); |
kubitz | 0:19fd8c1944fb | 76 | DigitalOut L2H(L2Hpin); |
kubitz | 0:19fd8c1944fb | 77 | DigitalOut L3L(L3Lpin); |
kubitz | 0:19fd8c1944fb | 78 | DigitalOut L3H(L3Hpin); |
kubitz | 0:19fd8c1944fb | 79 | |
kubitz | 0:19fd8c1944fb | 80 | DigitalOut TP1(TP1pin); |
kubitz | 0:19fd8c1944fb | 81 | PwmOut MotorPWM(PWMpin); |
kubitz | 0:19fd8c1944fb | 82 | |
kubitz | 9:4135d0c8dc10 | 83 | int8_t orState = 0; //Rotot offset at motor state 0 |
kubitz | 0:19fd8c1944fb | 84 | int8_t intState = 0; |
kubitz | 0:19fd8c1944fb | 85 | int8_t intStateOld = 0; |
kubitz | 0:19fd8c1944fb | 86 | |
kubitz | 0:19fd8c1944fb | 87 | //Set a given drive state |
kubitz | 9:4135d0c8dc10 | 88 | void motorOut(int8_t driveState) |
kubitz | 9:4135d0c8dc10 | 89 | { |
kubitz | 9:4135d0c8dc10 | 90 | |
kubitz | 0:19fd8c1944fb | 91 | //Lookup the output byte from the drive state. |
kubitz | 0:19fd8c1944fb | 92 | int8_t driveOut = driveTable[driveState & 0x07]; |
kubitz | 9:4135d0c8dc10 | 93 | |
kubitz | 0:19fd8c1944fb | 94 | //Turn off first |
kubitz | 9:4135d0c8dc10 | 95 | if (~driveOut & 0x01) |
kubitz | 9:4135d0c8dc10 | 96 | L1L = 0; |
kubitz | 9:4135d0c8dc10 | 97 | if (~driveOut & 0x02) |
kubitz | 9:4135d0c8dc10 | 98 | L1H = 1; |
kubitz | 9:4135d0c8dc10 | 99 | if (~driveOut & 0x04) |
kubitz | 9:4135d0c8dc10 | 100 | L2L = 0; |
kubitz | 9:4135d0c8dc10 | 101 | if (~driveOut & 0x08) |
kubitz | 9:4135d0c8dc10 | 102 | L2H = 1; |
kubitz | 9:4135d0c8dc10 | 103 | if (~driveOut & 0x10) |
kubitz | 9:4135d0c8dc10 | 104 | L3L = 0; |
kubitz | 9:4135d0c8dc10 | 105 | if (~driveOut & 0x20) |
kubitz | 9:4135d0c8dc10 | 106 | L3H = 1; |
kubitz | 9:4135d0c8dc10 | 107 | |
kubitz | 0:19fd8c1944fb | 108 | //Then turn on |
kubitz | 9:4135d0c8dc10 | 109 | if (driveOut & 0x01) |
kubitz | 9:4135d0c8dc10 | 110 | L1L = 1; |
kubitz | 9:4135d0c8dc10 | 111 | if (driveOut & 0x02) |
kubitz | 9:4135d0c8dc10 | 112 | L1H = 0; |
kubitz | 9:4135d0c8dc10 | 113 | if (driveOut & 0x04) |
kubitz | 9:4135d0c8dc10 | 114 | L2L = 1; |
kubitz | 9:4135d0c8dc10 | 115 | if (driveOut & 0x08) |
kubitz | 9:4135d0c8dc10 | 116 | L2H = 0; |
kubitz | 9:4135d0c8dc10 | 117 | if (driveOut & 0x10) |
kubitz | 9:4135d0c8dc10 | 118 | L3L = 1; |
kubitz | 9:4135d0c8dc10 | 119 | if (driveOut & 0x20) |
kubitz | 9:4135d0c8dc10 | 120 | L3H = 0; |
kubitz | 9:4135d0c8dc10 | 121 | } |
kubitz | 0:19fd8c1944fb | 122 | |
kubitz | 9:4135d0c8dc10 | 123 | //Convert photointerrupter inputs to a rotor state |
kubitz | 9:4135d0c8dc10 | 124 | inline int8_t readRotorState() |
kubitz | 9:4135d0c8dc10 | 125 | { |
kubitz | 9:4135d0c8dc10 | 126 | return stateMap[I1 + 2 * I2 + 4 * I3]; |
kubitz | 9:4135d0c8dc10 | 127 | } |
kubitz | 9:4135d0c8dc10 | 128 | |
kubitz | 9:4135d0c8dc10 | 129 | //Basic synchronisation routine |
kubitz | 9:4135d0c8dc10 | 130 | int8_t motorHome() |
kubitz | 9:4135d0c8dc10 | 131 | { |
kubitz | 0:19fd8c1944fb | 132 | //Put the motor in drive state 0 and wait for it to stabilise |
kubitz | 0:19fd8c1944fb | 133 | motorOut(0); |
kubitz | 0:19fd8c1944fb | 134 | wait(2.0); |
kubitz | 9:4135d0c8dc10 | 135 | |
kubitz | 0:19fd8c1944fb | 136 | //Get the rotor state |
kubitz | 0:19fd8c1944fb | 137 | return readRotorState(); |
kubitz | 0:19fd8c1944fb | 138 | } |
kubitz | 0:19fd8c1944fb | 139 | |
kubitz | 9:4135d0c8dc10 | 140 | void move() |
kubitz | 9:4135d0c8dc10 | 141 | { |
kubitz | 0:19fd8c1944fb | 142 | intState = readRotorState(); |
kubitz | 9:4135d0c8dc10 | 143 | motorOut((intState - orState + lead + 6) % 6); //+6 to make sure the remainder is positive |
kubitz | 0:19fd8c1944fb | 144 | intStateOld = intState; |
kubitz | 0:19fd8c1944fb | 145 | } |
kubitz | 0:19fd8c1944fb | 146 | |
kubitz | 6:5f4a954cb8bc | 147 | |
kubitz | 0:19fd8c1944fb | 148 | //Main |
kubitz | 9:4135d0c8dc10 | 149 | int main() |
kubitz | 9:4135d0c8dc10 | 150 | { |
kubitz | 9:4135d0c8dc10 | 151 | pc.attach(&serialISR); |
kubitz | 0:19fd8c1944fb | 152 | const int32_t PWM_PRD = 2500; |
kubitz | 0:19fd8c1944fb | 153 | MotorPWM.period_us(PWM_PRD); |
kubitz | 0:19fd8c1944fb | 154 | MotorPWM.pulsewidth_us(PWM_PRD); |
kubitz | 9:4135d0c8dc10 | 155 | |
kubitz | 0:19fd8c1944fb | 156 | pc.printf("Hello\n\r"); |
kubitz | 9:4135d0c8dc10 | 157 | |
kubitz | 0:19fd8c1944fb | 158 | //Run the motor synchronisation |
kubitz | 0:19fd8c1944fb | 159 | orState = motorHome(); |
kubitz | 9:4135d0c8dc10 | 160 | pc.printf("Rotor origin: %x\n\r", orState); |
kubitz | 9:4135d0c8dc10 | 161 | |
kubitz | 0:19fd8c1944fb | 162 | I1.rise(&move); |
kubitz | 0:19fd8c1944fb | 163 | I1.fall(&move); |
kubitz | 0:19fd8c1944fb | 164 | I2.rise(&move); |
kubitz | 0:19fd8c1944fb | 165 | I2.fall(&move); |
kubitz | 0:19fd8c1944fb | 166 | I3.rise(&move); |
kubitz | 0:19fd8c1944fb | 167 | I3.fall(&move); |
kubitz | 8:c30a4106d08c | 168 | |
kubitz | 9:4135d0c8dc10 | 169 | // Initialize threads and timers |
kubitz | 9:4135d0c8dc10 | 170 | timer_nonce.start(); |
kubitz | 7:aef5b29d7a7c | 171 | thread_crypto.start(thread_crypto_print); |
kubitz | 9:4135d0c8dc10 | 172 | thread_processor.start(thread_processor_callback); |
kubitz | 9:4135d0c8dc10 | 173 | uint8_t hash[32]; |
kubitz | 5:de6430aee646 | 174 | |
kubitz | 9:4135d0c8dc10 | 175 | while (1) |
kubitz | 9:4135d0c8dc10 | 176 | { |
kubitz | 10:3669e3d832ed | 177 | // Set main as lowest priority thread |
kubitz | 10:3669e3d832ed | 178 | |
kubitz | 10:3669e3d832ed | 179 | NewKey_mutex.lock(); |
kubitz | 10:3669e3d832ed | 180 | *key = NewKey; |
kubitz | 10:3669e3d832ed | 181 | NewKey_mutex.unlock(); |
kubitz | 10:3669e3d832ed | 182 | |
kubitz | 9:4135d0c8dc10 | 183 | SHA256::computeHash(hash, (uint8_t *)sequence, 64); |
kubitz | 5:de6430aee646 | 184 | *nonce = *nonce + 1; |
kubitz | 5:de6430aee646 | 185 | |
kubitz | 9:4135d0c8dc10 | 186 | if ((hash[0] == 0) && (hash[1] == 0)) |
kubitz | 9:4135d0c8dc10 | 187 | { |
kubitz | 0:19fd8c1944fb | 188 | last_nonce_number = successful_nonce; |
kubitz | 10:3669e3d832ed | 189 | putMessageCrypto(*nonce); |
kubitz | 0:19fd8c1944fb | 190 | successful_nonce++; |
kubitz | 9:4135d0c8dc10 | 191 | } |
kubitz | 9:4135d0c8dc10 | 192 | |
kubitz | 9:4135d0c8dc10 | 193 | if ((timer_nonce.read_ms() - previous_time) > 1000) |
kubitz | 9:4135d0c8dc10 | 194 | { |
kubitz | 9:4135d0c8dc10 | 195 | //pc.printf("Computation Rate: %lu computation /sec\n\r", (*nonce - last_nonce_number)); |
kubitz | 0:19fd8c1944fb | 196 | last_nonce_number = *nonce; |
kubitz | 5:de6430aee646 | 197 | previous_time = timer_nonce.read_ms(); |
kubitz | 0:19fd8c1944fb | 198 | } |
kubitz | 0:19fd8c1944fb | 199 | } |
kubitz | 0:19fd8c1944fb | 200 | |
kubitz | 9:4135d0c8dc10 | 201 | return 0; |
kubitz | 9:4135d0c8dc10 | 202 | } |