fork of what I have been writing
ES_CW2_Starter_STARFISH/main.cpp@0:19fd8c1944fb, 2020-02-28 (annotated)
- Committer:
- kubitz
- Date:
- Fri Feb 28 17:36:48 2020 +0000
- Revision:
- 0:19fd8c1944fb
- Child:
- 1:e9be8b2c8c69
asdf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kubitz | 0:19fd8c1944fb | 1 | #include "mbed.h" |
kubitz | 0:19fd8c1944fb | 2 | #include "SHA256.h" |
kubitz | 0:19fd8c1944fb | 3 | |
kubitz | 0:19fd8c1944fb | 4 | Timer timer_nonce; |
kubitz | 0:19fd8c1944fb | 5 | Mail<string, 16> mail_box; |
kubitz | 0:19fd8c1944fb | 6 | |
kubitz | 0:19fd8c1944fb | 7 | |
kubitz | 0:19fd8c1944fb | 8 | uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64, |
kubitz | 0:19fd8c1944fb | 9 | 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73, |
kubitz | 0:19fd8c1944fb | 10 | 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E, |
kubitz | 0:19fd8c1944fb | 11 | 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20, |
kubitz | 0:19fd8c1944fb | 12 | 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20, |
kubitz | 0:19fd8c1944fb | 13 | 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20, |
kubitz | 0:19fd8c1944fb | 14 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
kubitz | 0:19fd8c1944fb | 15 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
kubitz | 0:19fd8c1944fb | 16 | uint64_t* key = (uint64_t*)&sequence[48]; |
kubitz | 0:19fd8c1944fb | 17 | uint64_t* nonce = (uint64_t*)&sequence[56]; |
kubitz | 0:19fd8c1944fb | 18 | uint32_t successful_nonce = 0; |
kubitz | 0:19fd8c1944fb | 19 | uint32_t last_nonce_number = 0; |
kubitz | 0:19fd8c1944fb | 20 | |
kubitz | 0:19fd8c1944fb | 21 | uint8_t hash[32]; |
kubitz | 0:19fd8c1944fb | 22 | uint32_t previous_time; |
kubitz | 0:19fd8c1944fb | 23 | //Photointerrupter input pins |
kubitz | 0:19fd8c1944fb | 24 | #define I1pin D3 |
kubitz | 0:19fd8c1944fb | 25 | #define I2pin D6 |
kubitz | 0:19fd8c1944fb | 26 | #define I3pin D5 |
kubitz | 0:19fd8c1944fb | 27 | |
kubitz | 0:19fd8c1944fb | 28 | //Incremental encoder input pins |
kubitz | 0:19fd8c1944fb | 29 | #define CHApin D12 |
kubitz | 0:19fd8c1944fb | 30 | #define CHBpin D11 |
kubitz | 0:19fd8c1944fb | 31 | |
kubitz | 0:19fd8c1944fb | 32 | //Motor Drive output pins //Mask in output byte |
kubitz | 0:19fd8c1944fb | 33 | #define L1Lpin D1 //0x01 |
kubitz | 0:19fd8c1944fb | 34 | #define L1Hpin A3 //0x02 |
kubitz | 0:19fd8c1944fb | 35 | #define L2Lpin D0 //0x04 |
kubitz | 0:19fd8c1944fb | 36 | #define L2Hpin A6 //0x08 |
kubitz | 0:19fd8c1944fb | 37 | #define L3Lpin D10 //0x10 |
kubitz | 0:19fd8c1944fb | 38 | #define L3Hpin D2 //0x20 |
kubitz | 0:19fd8c1944fb | 39 | |
kubitz | 0:19fd8c1944fb | 40 | #define PWMpin D9 |
kubitz | 0:19fd8c1944fb | 41 | |
kubitz | 0:19fd8c1944fb | 42 | //Motor current sense |
kubitz | 0:19fd8c1944fb | 43 | #define MCSPpin A1 |
kubitz | 0:19fd8c1944fb | 44 | #define MCSNpin A0 |
kubitz | 0:19fd8c1944fb | 45 | |
kubitz | 0:19fd8c1944fb | 46 | //Test outputs |
kubitz | 0:19fd8c1944fb | 47 | #define TP0pin D4 |
kubitz | 0:19fd8c1944fb | 48 | #define TP1pin D13 |
kubitz | 0:19fd8c1944fb | 49 | #define TP2pin A2 |
kubitz | 0:19fd8c1944fb | 50 | |
kubitz | 0:19fd8c1944fb | 51 | //Mapping from sequential drive states to motor phase outputs |
kubitz | 0:19fd8c1944fb | 52 | /* |
kubitz | 0:19fd8c1944fb | 53 | State L1 L2 L3 |
kubitz | 0:19fd8c1944fb | 54 | 0 H - L |
kubitz | 0:19fd8c1944fb | 55 | 1 - H L |
kubitz | 0:19fd8c1944fb | 56 | 2 L H - |
kubitz | 0:19fd8c1944fb | 57 | 3 L - H |
kubitz | 0:19fd8c1944fb | 58 | 4 - L H |
kubitz | 0:19fd8c1944fb | 59 | 5 H L - |
kubitz | 0:19fd8c1944fb | 60 | 6 - - - |
kubitz | 0:19fd8c1944fb | 61 | 7 - - - |
kubitz | 0:19fd8c1944fb | 62 | */ |
kubitz | 0:19fd8c1944fb | 63 | //Drive state to output table |
kubitz | 0:19fd8c1944fb | 64 | const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00}; |
kubitz | 0:19fd8c1944fb | 65 | |
kubitz | 0:19fd8c1944fb | 66 | //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid |
kubitz | 0:19fd8c1944fb | 67 | const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07}; |
kubitz | 0:19fd8c1944fb | 68 | //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 | 69 | |
kubitz | 0:19fd8c1944fb | 70 | //Phase lead to make motor spin |
kubitz | 0:19fd8c1944fb | 71 | const int8_t lead = 2; //2 for forwards, -2 for backwards |
kubitz | 0:19fd8c1944fb | 72 | |
kubitz | 0:19fd8c1944fb | 73 | //Status LED |
kubitz | 0:19fd8c1944fb | 74 | DigitalOut led1(LED1); |
kubitz | 0:19fd8c1944fb | 75 | |
kubitz | 0:19fd8c1944fb | 76 | //Photointerrupter inputs |
kubitz | 0:19fd8c1944fb | 77 | InterruptIn I1(I1pin); |
kubitz | 0:19fd8c1944fb | 78 | InterruptIn I2(I2pin); |
kubitz | 0:19fd8c1944fb | 79 | InterruptIn I3(I3pin); |
kubitz | 0:19fd8c1944fb | 80 | |
kubitz | 0:19fd8c1944fb | 81 | //Motor Drive outputs |
kubitz | 0:19fd8c1944fb | 82 | DigitalOut L1L(L1Lpin); |
kubitz | 0:19fd8c1944fb | 83 | DigitalOut L1H(L1Hpin); |
kubitz | 0:19fd8c1944fb | 84 | DigitalOut L2L(L2Lpin); |
kubitz | 0:19fd8c1944fb | 85 | DigitalOut L2H(L2Hpin); |
kubitz | 0:19fd8c1944fb | 86 | DigitalOut L3L(L3Lpin); |
kubitz | 0:19fd8c1944fb | 87 | DigitalOut L3H(L3Hpin); |
kubitz | 0:19fd8c1944fb | 88 | |
kubitz | 0:19fd8c1944fb | 89 | DigitalOut TP1(TP1pin); |
kubitz | 0:19fd8c1944fb | 90 | PwmOut MotorPWM(PWMpin); |
kubitz | 0:19fd8c1944fb | 91 | |
kubitz | 0:19fd8c1944fb | 92 | int8_t orState = 0; //Rotot offset at motor state 0 |
kubitz | 0:19fd8c1944fb | 93 | int8_t intState = 0; |
kubitz | 0:19fd8c1944fb | 94 | int8_t intStateOld = 0; |
kubitz | 0:19fd8c1944fb | 95 | |
kubitz | 0:19fd8c1944fb | 96 | |
kubitz | 0:19fd8c1944fb | 97 | //Set a given drive state |
kubitz | 0:19fd8c1944fb | 98 | void motorOut(int8_t driveState){ |
kubitz | 0:19fd8c1944fb | 99 | |
kubitz | 0:19fd8c1944fb | 100 | //Lookup the output byte from the drive state. |
kubitz | 0:19fd8c1944fb | 101 | int8_t driveOut = driveTable[driveState & 0x07]; |
kubitz | 0:19fd8c1944fb | 102 | |
kubitz | 0:19fd8c1944fb | 103 | //Turn off first |
kubitz | 0:19fd8c1944fb | 104 | if (~driveOut & 0x01) L1L = 0; |
kubitz | 0:19fd8c1944fb | 105 | if (~driveOut & 0x02) L1H = 1; |
kubitz | 0:19fd8c1944fb | 106 | if (~driveOut & 0x04) L2L = 0; |
kubitz | 0:19fd8c1944fb | 107 | if (~driveOut & 0x08) L2H = 1; |
kubitz | 0:19fd8c1944fb | 108 | if (~driveOut & 0x10) L3L = 0; |
kubitz | 0:19fd8c1944fb | 109 | if (~driveOut & 0x20) L3H = 1; |
kubitz | 0:19fd8c1944fb | 110 | |
kubitz | 0:19fd8c1944fb | 111 | //Then turn on |
kubitz | 0:19fd8c1944fb | 112 | if (driveOut & 0x01) L1L = 1; |
kubitz | 0:19fd8c1944fb | 113 | if (driveOut & 0x02) L1H = 0; |
kubitz | 0:19fd8c1944fb | 114 | if (driveOut & 0x04) L2L = 1; |
kubitz | 0:19fd8c1944fb | 115 | if (driveOut & 0x08) L2H = 0; |
kubitz | 0:19fd8c1944fb | 116 | if (driveOut & 0x10) L3L = 1; |
kubitz | 0:19fd8c1944fb | 117 | if (driveOut & 0x20) L3H = 0; |
kubitz | 0:19fd8c1944fb | 118 | } |
kubitz | 0:19fd8c1944fb | 119 | |
kubitz | 0:19fd8c1944fb | 120 | //Convert photointerrupter inputs to a rotor state |
kubitz | 0:19fd8c1944fb | 121 | inline int8_t readRotorState(){ |
kubitz | 0:19fd8c1944fb | 122 | return stateMap[I1 + 2*I2 + 4*I3]; |
kubitz | 0:19fd8c1944fb | 123 | } |
kubitz | 0:19fd8c1944fb | 124 | |
kubitz | 0:19fd8c1944fb | 125 | //Basic synchronisation routine |
kubitz | 0:19fd8c1944fb | 126 | int8_t motorHome() { |
kubitz | 0:19fd8c1944fb | 127 | //Put the motor in drive state 0 and wait for it to stabilise |
kubitz | 0:19fd8c1944fb | 128 | motorOut(0); |
kubitz | 0:19fd8c1944fb | 129 | wait(2.0); |
kubitz | 0:19fd8c1944fb | 130 | |
kubitz | 0:19fd8c1944fb | 131 | //Get the rotor state |
kubitz | 0:19fd8c1944fb | 132 | return readRotorState(); |
kubitz | 0:19fd8c1944fb | 133 | } |
kubitz | 0:19fd8c1944fb | 134 | |
kubitz | 0:19fd8c1944fb | 135 | void move() { |
kubitz | 0:19fd8c1944fb | 136 | intState = readRotorState(); |
kubitz | 0:19fd8c1944fb | 137 | motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive |
kubitz | 0:19fd8c1944fb | 138 | intStateOld = intState; |
kubitz | 0:19fd8c1944fb | 139 | } |
kubitz | 0:19fd8c1944fb | 140 | |
kubitz | 0:19fd8c1944fb | 141 | void print_mails(){ |
kubitz | 0:19fd8c1944fb | 142 | |
kubitz | 0:19fd8c1944fb | 143 | |
kubitz | 0:19fd8c1944fb | 144 | |
kubitz | 0:19fd8c1944fb | 145 | } |
kubitz | 0:19fd8c1944fb | 146 | |
kubitz | 0:19fd8c1944fb | 147 | //Main |
kubitz | 0:19fd8c1944fb | 148 | int main() { |
kubitz | 0:19fd8c1944fb | 149 | |
kubitz | 0:19fd8c1944fb | 150 | const int32_t PWM_PRD = 2500; |
kubitz | 0:19fd8c1944fb | 151 | MotorPWM.period_us(PWM_PRD); |
kubitz | 0:19fd8c1944fb | 152 | MotorPWM.pulsewidth_us(PWM_PRD); |
kubitz | 0:19fd8c1944fb | 153 | |
kubitz | 0:19fd8c1944fb | 154 | //Initialise the serial port |
kubitz | 0:19fd8c1944fb | 155 | Serial pc(SERIAL_TX, SERIAL_RX); |
kubitz | 0:19fd8c1944fb | 156 | pc.printf("Hello\n\r"); |
kubitz | 0:19fd8c1944fb | 157 | |
kubitz | 0:19fd8c1944fb | 158 | //Run the motor synchronisation |
kubitz | 0:19fd8c1944fb | 159 | orState = motorHome(); |
kubitz | 0:19fd8c1944fb | 160 | pc.printf("Rotor origin: %x\n\r",orState); |
kubitz | 0:19fd8c1944fb | 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 | 0:19fd8c1944fb | 168 | timer_nonce.start(); |
kubitz | 0:19fd8c1944fb | 169 | previous_time = timer_nonce.read(); |
kubitz | 0:19fd8c1944fb | 170 | |
kubitz | 0:19fd8c1944fb | 171 | while(1==1){ |
kubitz | 0:19fd8c1944fb | 172 | uint8_t hash[32]; |
kubitz | 0:19fd8c1944fb | 173 | |
kubitz | 0:19fd8c1944fb | 174 | *nonce ++; |
kubitz | 0:19fd8c1944fb | 175 | SHA256::computeHash(hash, (uint8_t*)sequence, 64); |
kubitz | 0:19fd8c1944fb | 176 | mail_box.put(mail); |
kubitz | 0:19fd8c1944fb | 177 | |
kubitz | 0:19fd8c1944fb | 178 | if ((hash[0]==0)&&(hash[1]==0)){ |
kubitz | 0:19fd8c1944fb | 179 | last_nonce_number = successful_nonce; |
kubitz | 0:19fd8c1944fb | 180 | successful_nonce++; |
kubitz | 0:19fd8c1944fb | 181 | printf("Successful Nonce -- hash: "); |
kubitz | 0:19fd8c1944fb | 182 | for(int i = 0; i < 32; ++i) |
kubitz | 0:19fd8c1944fb | 183 | printf("\n"); |
kubitz | 0:19fd8c1944fb | 184 | } |
kubitz | 0:19fd8c1944fb | 185 | |
kubitz | 0:19fd8c1944fb | 186 | if ((timer_nonce.read()-previous_time) > 1000){ |
kubitz | 0:19fd8c1944fb | 187 | printf("Computation Rate: %d computation /sec", (*nonce-last_nonce_number)); |
kubitz | 0:19fd8c1944fb | 188 | last_nonce_number = *nonce; |
kubitz | 0:19fd8c1944fb | 189 | } |
kubitz | 0:19fd8c1944fb | 190 | |
kubitz | 0:19fd8c1944fb | 191 | } |
kubitz | 0:19fd8c1944fb | 192 | |
kubitz | 0:19fd8c1944fb | 193 | return 0; |
kubitz | 0:19fd8c1944fb | 194 | |
kubitz | 0:19fd8c1944fb | 195 | } |
kubitz | 0:19fd8c1944fb | 196 |