Callum and Adel's changes on 12/02/19

Dependencies:   Crypto

Committer:
CallumAlder
Date:
Tue Mar 05 15:48:43 2019 +0000
Revision:
15:2f95f2fb68e3
Parent:
14:4e312fb83330
Child:
16:db7ef0a4aa23
Lab 1 completed (hashing and motor control)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CallumAlder 14:4e312fb83330 1 #include "SHA256.h"
estott 0:de4320f74764 2 #include "mbed.h"
CallumAlder 14:4e312fb83330 3 //#include <iostream>
CallumAlder 14:4e312fb83330 4
estott 0:de4320f74764 5
estott 0:de4320f74764 6 //Photointerrupter input pins
estott 10:a4b5723b6c9d 7 #define I1pin D3
estott 10:a4b5723b6c9d 8 #define I2pin D6
estott 10:a4b5723b6c9d 9 #define I3pin D5
estott 2:4e88faab6988 10
estott 2:4e88faab6988 11 //Incremental encoder input pins
estott 10:a4b5723b6c9d 12 #define CHApin D12
estott 10:a4b5723b6c9d 13 #define CHBpin D11
estott 0:de4320f74764 14
estott 0:de4320f74764 15 //Motor Drive output pins //Mask in output byte
estott 10:a4b5723b6c9d 16 #define L1Lpin D1 //0x01
estott 10:a4b5723b6c9d 17 #define L1Hpin A3 //0x02
estott 10:a4b5723b6c9d 18 #define L2Lpin D0 //0x04
estott 10:a4b5723b6c9d 19 #define L2Hpin A6 //0x08
estott 10:a4b5723b6c9d 20 #define L3Lpin D10 //0x10
estott 10:a4b5723b6c9d 21 #define L3Hpin D2 //0x20
estott 10:a4b5723b6c9d 22
estott 10:a4b5723b6c9d 23 #define PWMpin D9
estott 5:08f338b5e4d9 24
estott 5:08f338b5e4d9 25 //Motor current sense
estott 5:08f338b5e4d9 26 #define MCSPpin A1
estott 5:08f338b5e4d9 27 #define MCSNpin A0
estott 0:de4320f74764 28
estott 0:de4320f74764 29 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 30 /*
estott 0:de4320f74764 31 State L1 L2 L3
estott 0:de4320f74764 32 0 H - L
estott 0:de4320f74764 33 1 - H L
estott 0:de4320f74764 34 2 L H -
estott 0:de4320f74764 35 3 L - H
estott 0:de4320f74764 36 4 - L H
estott 0:de4320f74764 37 5 H L -
estott 0:de4320f74764 38 6 - - -
estott 0:de4320f74764 39 7 - - -
estott 0:de4320f74764 40 */
estott 0:de4320f74764 41 //Drive state to output table
estott 0:de4320f74764 42 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 43
estott 0:de4320f74764 44 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
estott 2:4e88faab6988 45 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 46 //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 47
estott 2:4e88faab6988 48 //Phase lead to make motor spin
estott 3:569b35e2a602 49 const int8_t lead = 2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 50
iachinweze1 12:41b3112021a3 51 // Global States
iachinweze1 12:41b3112021a3 52 // TODO: Can we not use globals?
iachinweze1 12:41b3112021a3 53 int8_t orState = 0; //Rotot offset at motor state 0
iachinweze1 12:41b3112021a3 54 int8_t currentState = 0; //Rotot offset at motor state 0
iachinweze1 12:41b3112021a3 55
estott 0:de4320f74764 56 //Status LED
estott 0:de4320f74764 57 DigitalOut led1(LED1);
estott 0:de4320f74764 58
estott 0:de4320f74764 59 //Photointerrupter inputs
iachinweze1 12:41b3112021a3 60 InterruptIn I1(I1pin);
iachinweze1 12:41b3112021a3 61 InterruptIn I2(I2pin);
iachinweze1 12:41b3112021a3 62 InterruptIn I3(I3pin);
estott 0:de4320f74764 63
estott 0:de4320f74764 64 //Motor Drive outputs
estott 0:de4320f74764 65 DigitalOut L1L(L1Lpin);
estott 0:de4320f74764 66 DigitalOut L1H(L1Hpin);
estott 0:de4320f74764 67 DigitalOut L2L(L2Lpin);
estott 0:de4320f74764 68 DigitalOut L2H(L2Hpin);
estott 0:de4320f74764 69 DigitalOut L3L(L3Lpin);
estott 0:de4320f74764 70 DigitalOut L3H(L3Hpin);
estott 0:de4320f74764 71
estott 0:de4320f74764 72 //Set a given drive state
estott 0:de4320f74764 73 void motorOut(int8_t driveState){
estott 0:de4320f74764 74
estott 2:4e88faab6988 75 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 76 int8_t driveOut = driveTable[driveState & 0x07];
estott 2:4e88faab6988 77
estott 2:4e88faab6988 78 //Turn off first
estott 2:4e88faab6988 79 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 80 if (~driveOut & 0x02) L1H = 1;
estott 2:4e88faab6988 81 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 82 if (~driveOut & 0x08) L2H = 1;
estott 2:4e88faab6988 83 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 84 if (~driveOut & 0x20) L3H = 1;
estott 2:4e88faab6988 85
estott 2:4e88faab6988 86 //Then turn on
estott 2:4e88faab6988 87 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 88 if (driveOut & 0x02) L1H = 0;
estott 2:4e88faab6988 89 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 90 if (driveOut & 0x08) L2H = 0;
estott 2:4e88faab6988 91 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 92 if (driveOut & 0x20) L3H = 0;
estott 0:de4320f74764 93 }
estott 0:de4320f74764 94
estott 2:4e88faab6988 95 //Convert photointerrupter inputs to a rotor state
estott 0:de4320f74764 96 inline int8_t readRotorState(){
estott 2:4e88faab6988 97 return stateMap[I1 + 2*I2 + 4*I3];
estott 0:de4320f74764 98 }
estott 0:de4320f74764 99
estott 0:de4320f74764 100 //Basic synchronisation routine
estott 2:4e88faab6988 101 int8_t motorHome() {
estott 0:de4320f74764 102 //Put the motor in drive state 0 and wait for it to stabilise
estott 0:de4320f74764 103 motorOut(0);
estott 3:569b35e2a602 104 wait(2.0);
estott 0:de4320f74764 105
estott 0:de4320f74764 106 //Get the rotor state
estott 2:4e88faab6988 107 return readRotorState();
estott 0:de4320f74764 108 }
iachinweze1 12:41b3112021a3 109
iachinweze1 12:41b3112021a3 110 void stateUpdate(/*int8_t *currentState, int8_t offset, int8_t lead*/) {
iachinweze1 12:41b3112021a3 111 // TODO: Global fix
iachinweze1 12:41b3112021a3 112 currentState = readRotorState();
iachinweze1 12:41b3112021a3 113 motorOut((currentState - orState + lead + 6) % 6);
iachinweze1 12:41b3112021a3 114 }
iachinweze1 12:41b3112021a3 115
estott 0:de4320f74764 116 //Main
estott 0:de4320f74764 117 int main() {
estott 0:de4320f74764 118 //Initialise the serial port
CallumAlder 14:4e312fb83330 119 Serial pc(SERIAL_TX, SERIAL_RX);
CallumAlder 14:4e312fb83330 120 pc.printf("Hello\n\r");
CallumAlder 14:4e312fb83330 121
CallumAlder 14:4e312fb83330 122 // std::ios::sync_with_stdio(false);
CallumAlder 14:4e312fb83330 123 SHA256::SHA256 Miner;
CallumAlder 14:4e312fb83330 124
CallumAlder 14:4e312fb83330 125 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
CallumAlder 14:4e312fb83330 126 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
CallumAlder 14:4e312fb83330 127 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
CallumAlder 14:4e312fb83330 128 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
CallumAlder 14:4e312fb83330 129 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
CallumAlder 14:4e312fb83330 130 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
CallumAlder 14:4e312fb83330 131 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
CallumAlder 14:4e312fb83330 132 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
CallumAlder 14:4e312fb83330 133 uint64_t* key = (uint64_t*)((int)sequence + 48);
CallumAlder 14:4e312fb83330 134 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
CallumAlder 14:4e312fb83330 135 uint8_t hash[32];
CallumAlder 14:4e312fb83330 136 uint32_t length64 = 64;
CallumAlder 15:2f95f2fb68e3 137 uint32_t hashCounter = 0;
CallumAlder 15:2f95f2fb68e3 138 Timer timer;
estott 0:de4320f74764 139
estott 0:de4320f74764 140 //Run the motor synchronisation
estott 2:4e88faab6988 141 orState = motorHome();
iachinweze1 12:41b3112021a3 142
CallumAlder 15:2f95f2fb68e3 143 // Add callbacks
iachinweze1 12:41b3112021a3 144 I1.fall(&stateUpdate);
iachinweze1 12:41b3112021a3 145 I2.fall(&stateUpdate);
iachinweze1 12:41b3112021a3 146 I3.fall(&stateUpdate);
iachinweze1 12:41b3112021a3 147
CallumAlder 15:2f95f2fb68e3 148 // Push motor to move
iachinweze1 12:41b3112021a3 149 currentState = readRotorState();
iachinweze1 12:41b3112021a3 150 motorOut((currentState-orState+lead+6)%6); // We push it digitally
estott 0:de4320f74764 151
iachinweze1 12:41b3112021a3 152 // pc.printf("Rotor origin: %x\n\r",orState);
iachinweze1 12:41b3112021a3 153 // orState is subtracted from future rotor state inputs to align rotor and motor states
iachinweze1 12:41b3112021a3 154 // intState = readRotorState();
iachinweze1 12:41b3112021a3 155 //if (intState != intStateOld) {
iachinweze1 12:41b3112021a3 156 // pc.printf("old:%d \t new:%d \t next:%d \n\r",intStateOld, intState, (intState-orState+lead+6)%6);
iachinweze1 12:41b3112021a3 157 // intStateOld = intState;
iachinweze1 12:41b3112021a3 158 // motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
iachinweze1 12:41b3112021a3 159 // }
iachinweze1 12:41b3112021a3 160
CallumAlder 15:2f95f2fb68e3 161
iachinweze1 12:41b3112021a3 162 // Keep the program running indefinitely
CallumAlder 15:2f95f2fb68e3 163 timer.start(); // start timer
CallumAlder 15:2f95f2fb68e3 164 while (1) {
CallumAlder 15:2f95f2fb68e3 165 // pc.printf("Current:%d \t Next:%d \n\r", currentState, (currentState-orState+lead+6)%6);
CallumAlder 15:2f95f2fb68e3 166 Miner.computeHash(hash, sequence, length64);
CallumAlder 15:2f95f2fb68e3 167 hashCounter++;
CallumAlder 15:2f95f2fb68e3 168 if ((hash[0]==0) && (hash[1]==0)){
CallumAlder 15:2f95f2fb68e3 169 pc.printf("hash: ");
CallumAlder 15:2f95f2fb68e3 170 for(int i = 0; i < 32; ++i)
CallumAlder 15:2f95f2fb68e3 171 pc.printf("%02x", hash[i]);
CallumAlder 15:2f95f2fb68e3 172 pc.printf("\n\r");
CallumAlder 15:2f95f2fb68e3 173 }
CallumAlder 15:2f95f2fb68e3 174
CallumAlder 15:2f95f2fb68e3 175 (*nonce)++;
CallumAlder 15:2f95f2fb68e3 176
CallumAlder 15:2f95f2fb68e3 177 // Per Second i.e. when greater or equal to 1
CallumAlder 15:2f95f2fb68e3 178 if (timer.read() >= 1){
CallumAlder 15:2f95f2fb68e3 179 pc.printf("HashRate = %02u \n\r",hashCounter);
CallumAlder 15:2f95f2fb68e3 180 hashCounter=0;
CallumAlder 15:2f95f2fb68e3 181 timer.reset();
CallumAlder 15:2f95f2fb68e3 182 }
CallumAlder 15:2f95f2fb68e3 183 }
CallumAlder 15:2f95f2fb68e3 184
estott 0:de4320f74764 185 }
estott 0:de4320f74764 186
CallumAlder 15:2f95f2fb68e3 187
CallumAlder 15:2f95f2fb68e3 188
CallumAlder 15:2f95f2fb68e3 189