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

Dependencies:   Crypto

Committer:
CallumAlder
Date:
Mon Mar 18 18:04:29 2019 +0000
Revision:
32:fc5e00d9f74d
Parent:
31:b10ca6cf39bf
Child:
33:f1dc3b160eac
IFEHASAMANGINA

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"
adehadd 20:c60f4785b556 3 // #include <iostream>
adehadd 20:c60f4785b556 4 // #include "rtos.h"
CallumAlder 14:4e312fb83330 5
CallumAlder 19:805c87360b55 6 /*TODO:
iachinweze1 23:ab1cb51527d1 7 Change
CallumAlder 19:805c87360b55 8 Indx
CallumAlder 19:805c87360b55 9 newCmd
CallumAlder 19:805c87360b55 10 MAXCMDLENGTH
CallumAlder 28:4f02ac845e5d 11 move the global variables to a class because we arent paeasents
CallumAlder 19:805c87360b55 12 */
estott 0:de4320f74764 13
estott 0:de4320f74764 14 //Photointerrupter input pins
estott 10:a4b5723b6c9d 15 #define I1pin D3
estott 10:a4b5723b6c9d 16 #define I2pin D6
estott 10:a4b5723b6c9d 17 #define I3pin D5
estott 2:4e88faab6988 18
estott 2:4e88faab6988 19 //Incremental encoder input pins
estott 10:a4b5723b6c9d 20 #define CHApin D12
estott 10:a4b5723b6c9d 21 #define CHBpin D11
estott 0:de4320f74764 22
estott 0:de4320f74764 23 //Motor Drive output pins //Mask in output byte
estott 10:a4b5723b6c9d 24 #define L1Lpin D1 //0x01
estott 10:a4b5723b6c9d 25 #define L1Hpin A3 //0x02
estott 10:a4b5723b6c9d 26 #define L2Lpin D0 //0x04
estott 10:a4b5723b6c9d 27 #define L2Hpin A6 //0x08
estott 10:a4b5723b6c9d 28 #define L3Lpin D10 //0x10
estott 10:a4b5723b6c9d 29 #define L3Hpin D2 //0x20
estott 10:a4b5723b6c9d 30
estott 10:a4b5723b6c9d 31 #define PWMpin D9
estott 5:08f338b5e4d9 32
estott 5:08f338b5e4d9 33 //Motor current sense
estott 5:08f338b5e4d9 34 #define MCSPpin A1
estott 5:08f338b5e4d9 35 #define MCSNpin A0
estott 0:de4320f74764 36
estott 0:de4320f74764 37 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 38 /*
estott 0:de4320f74764 39 State L1 L2 L3
estott 0:de4320f74764 40 0 H - L
estott 0:de4320f74764 41 1 - H L
estott 0:de4320f74764 42 2 L H -
estott 0:de4320f74764 43 3 L - H
estott 0:de4320f74764 44 4 - L H
estott 0:de4320f74764 45 5 H L -
estott 0:de4320f74764 46 6 - - -
estott 0:de4320f74764 47 7 - - -
estott 0:de4320f74764 48 */
CallumAlder 28:4f02ac845e5d 49
CallumAlder 29:c96439a60184 50 //Status LED
CallumAlder 29:c96439a60184 51 DigitalOut led1(LED1);
CallumAlder 28:4f02ac845e5d 52
CallumAlder 29:c96439a60184 53 //Photointerrupter inputs
CallumAlder 29:c96439a60184 54 InterruptIn I1(I1pin);
CallumAlder 29:c96439a60184 55 InterruptIn I2(I2pin);
CallumAlder 29:c96439a60184 56 InterruptIn I3(I3pin);
CallumAlder 28:4f02ac845e5d 57
CallumAlder 29:c96439a60184 58 //Motor Drive outputs
CallumAlder 29:c96439a60184 59 DigitalOut L1L(L1Lpin);
CallumAlder 29:c96439a60184 60 DigitalOut L1H(L1Hpin);
CallumAlder 29:c96439a60184 61 DigitalOut L2L(L2Lpin);
CallumAlder 29:c96439a60184 62 DigitalOut L2H(L2Hpin);
CallumAlder 29:c96439a60184 63 DigitalOut L3L(L3Lpin);
CallumAlder 29:c96439a60184 64 DigitalOut L3H(L3Hpin);
CallumAlder 28:4f02ac845e5d 65
CallumAlder 29:c96439a60184 66 PwmOut pwmCtrl(PWMpin);
CallumAlder 28:4f02ac845e5d 67
estott 0:de4320f74764 68 //Drive state to output table
estott 0:de4320f74764 69 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 70
estott 0:de4320f74764 71 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
iachinweze1 23:ab1cb51527d1 72 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 73 //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 74
estott 2:4e88faab6988 75 //Phase lead to make motor spin
estott 3:569b35e2a602 76 const int8_t lead = 2; //2 for forwards, -2 for backwards
estott 0:de4320f74764 77
CallumAlder 19:805c87360b55 78
adehadd 20:c60f4785b556 79 class Comm /*: public T_*/{
iachinweze1 23:ab1cb51527d1 80
iachinweze1 23:ab1cb51527d1 81 public:
iachinweze1 23:ab1cb51527d1 82
iachinweze1 23:ab1cb51527d1 83 Thread t_comm_out;
iachinweze1 23:ab1cb51527d1 84 // Thread *p_motor_ctrl;
CallumAlder 19:805c87360b55 85
iachinweze1 23:ab1cb51527d1 86 bool _RUN;
iachinweze1 23:ab1cb51527d1 87
iachinweze1 23:ab1cb51527d1 88 RawSerial pc;
iachinweze1 23:ab1cb51527d1 89 // Queue<void, 8> inCharQ; // Input Character Queue
iachinweze1 23:ab1cb51527d1 90
iachinweze1 23:ab1cb51527d1 91
iachinweze1 23:ab1cb51527d1 92 static const char MsgChar[11];
CallumAlder 19:805c87360b55 93
iachinweze1 23:ab1cb51527d1 94 uint8_t MAXCMDLENGTH;
iachinweze1 23:ab1cb51527d1 95
iachinweze1 23:ab1cb51527d1 96 volatile uint8_t cmdIndx;
iachinweze1 23:ab1cb51527d1 97 volatile uint8_t inCharQIdx;
CallumAlder 19:805c87360b55 98
iachinweze1 23:ab1cb51527d1 99 volatile uint32_t motorPower; // motor toque
iachinweze1 23:ab1cb51527d1 100 volatile float targetVel;
iachinweze1 23:ab1cb51527d1 101 volatile float targetRot;
CallumAlder 19:805c87360b55 102
iachinweze1 23:ab1cb51527d1 103 enum msgType {motorState, posIn, velIn, posOut, velOut,
iachinweze1 23:ab1cb51527d1 104
iachinweze1 23:ab1cb51527d1 105 hashRate, keyAdded, nonceMatch,
iachinweze1 23:ab1cb51527d1 106
iachinweze1 23:ab1cb51527d1 107 torque, rotations,
adehadd 16:db7ef0a4aa23 108
iachinweze1 23:ab1cb51527d1 109 error};
iachinweze1 23:ab1cb51527d1 110
iachinweze1 23:ab1cb51527d1 111 typedef struct {
iachinweze1 23:ab1cb51527d1 112 msgType type;
iachinweze1 23:ab1cb51527d1 113 uint32_t message;
iachinweze1 23:ab1cb51527d1 114 } msg;
iachinweze1 23:ab1cb51527d1 115
iachinweze1 23:ab1cb51527d1 116 Mail<msg, 32> mailStack;
iachinweze1 23:ab1cb51527d1 117
iachinweze1 23:ab1cb51527d1 118 void serialISR(){
iachinweze1 23:ab1cb51527d1 119 if (pc.readable()) {
iachinweze1 23:ab1cb51527d1 120 char newChar = pc.getc();
iachinweze1 23:ab1cb51527d1 121 // inCharQ.put((void*)newChar); // void* = pointer to an unknown type that cannot be dereferenced
CallumAlder 19:805c87360b55 122
iachinweze1 23:ab1cb51527d1 123 if (inCharQIdx == (MAXCMDLENGTH)) {
iachinweze1 23:ab1cb51527d1 124 inCharQ[MAXCMDLENGTH] = '\0'; // force the string to have an end character
iachinweze1 23:ab1cb51527d1 125 putMessage(error, 1);
iachinweze1 23:ab1cb51527d1 126 inCharQIdx = 0; // reset buffer index
iachinweze1 23:ab1cb51527d1 127 // pc.putc('\r'); // carriage return moves to the start of the line
iachinweze1 23:ab1cb51527d1 128 // for (int i = 0; i < MAXCMDLENGTH; ++i)
iachinweze1 23:ab1cb51527d1 129 // {
iachinweze1 23:ab1cb51527d1 130 // inCharQ[i] = ' ';
iachinweze1 23:ab1cb51527d1 131 // pc.putc(' ');
iachinweze1 23:ab1cb51527d1 132 // }
iachinweze1 23:ab1cb51527d1 133
iachinweze1 23:ab1cb51527d1 134 // pc.putc('\r'); // carriage return moves to the start of the line
iachinweze1 23:ab1cb51527d1 135 }
iachinweze1 23:ab1cb51527d1 136 else{
iachinweze1 23:ab1cb51527d1 137 if(newChar != '\r'){ //While the command is not over,
iachinweze1 23:ab1cb51527d1 138 inCharQ[inCharQIdx] = newChar; //save input character and
iachinweze1 23:ab1cb51527d1 139 inCharQIdx++; //advance index
iachinweze1 23:ab1cb51527d1 140 pc.putc(newChar);
adehadd 20:c60f4785b556 141 }
adehadd 20:c60f4785b556 142 else{
iachinweze1 23:ab1cb51527d1 143 inCharQ[inCharQIdx] = '\0'; //When the command is finally over,
iachinweze1 23:ab1cb51527d1 144 strncpy(newCmd, inCharQ, MAXCMDLENGTH); // Will copy 18 characters from inCharQ to newCmd
iachinweze1 23:ab1cb51527d1 145 cmdParser(); //parse the command for decoding.
iachinweze1 23:ab1cb51527d1 146 for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer
iachinweze1 23:ab1cb51527d1 147 inCharQ[i] = ' ';
iachinweze1 23:ab1cb51527d1 148 inCharQIdx = 0; // reset index
CallumAlder 19:805c87360b55 149 }
CallumAlder 19:805c87360b55 150 }
CallumAlder 19:805c87360b55 151 }
CallumAlder 19:805c87360b55 152
iachinweze1 23:ab1cb51527d1 153
iachinweze1 23:ab1cb51527d1 154 }
iachinweze1 23:ab1cb51527d1 155
iachinweze1 23:ab1cb51527d1 156 void returnCursor() {
iachinweze1 23:ab1cb51527d1 157 pc.putc('>');
iachinweze1 23:ab1cb51527d1 158 for (int i = 0; i < inCharQIdx; ++i) // reset cursor position
iachinweze1 23:ab1cb51527d1 159 pc.putc(inCharQ[i]);
iachinweze1 23:ab1cb51527d1 160 // for (int i = inCharQIdx; i < MAXCMDLENGTH; ++i) // fill remaining with blanks
iachinweze1 23:ab1cb51527d1 161 // pc.putc(' ');
iachinweze1 23:ab1cb51527d1 162 // pc.putc('<');
iachinweze1 23:ab1cb51527d1 163 }
iachinweze1 23:ab1cb51527d1 164
iachinweze1 23:ab1cb51527d1 165 void cmdParser(){
iachinweze1 23:ab1cb51527d1 166 switch(newCmd[0]) {
iachinweze1 23:ab1cb51527d1 167 case 'K': //(MsgChar[keyAdded])://
iachinweze1 23:ab1cb51527d1 168 newKey_mutex.lock(); //Ensure there is no deadlock
iachinweze1 23:ab1cb51527d1 169 sscanf(newCmd, "K%x", &newKey); //Find desired the Key code
iachinweze1 23:ab1cb51527d1 170 putMessage(keyAdded, newKey); //Print it out
iachinweze1 23:ab1cb51527d1 171 newKey_mutex.unlock();
iachinweze1 23:ab1cb51527d1 172 break;
iachinweze1 23:ab1cb51527d1 173 case 'V': //(MsgChar[velIn])://
iachinweze1 23:ab1cb51527d1 174 sscanf(newCmd, "V%f", &targetVel); //Find desired the target velocity
iachinweze1 23:ab1cb51527d1 175 putMessage(velIn, targetVel); //Print it out
iachinweze1 23:ab1cb51527d1 176 break;
iachinweze1 23:ab1cb51527d1 177 case 'R': //(MsgChar[posIn])://
iachinweze1 23:ab1cb51527d1 178 sscanf(newCmd, "R%f", &targetRot); //Find desired target rotation
iachinweze1 23:ab1cb51527d1 179 putMessage(posIn, targetRot); //Print it out
iachinweze1 23:ab1cb51527d1 180 break;
iachinweze1 23:ab1cb51527d1 181 case 'T': //(MsgChar[torque])://
iachinweze1 23:ab1cb51527d1 182 sscanf(newCmd, "T%d", &motorPower); //Find desired target torque
iachinweze1 23:ab1cb51527d1 183 putMessage(torque, motorPower); //Print it out
iachinweze1 23:ab1cb51527d1 184 break;
iachinweze1 23:ab1cb51527d1 185 default: break;
iachinweze1 23:ab1cb51527d1 186 }
iachinweze1 23:ab1cb51527d1 187 }
CallumAlder 19:805c87360b55 188
iachinweze1 23:ab1cb51527d1 189 //~~~~~Decode messages to print on serial port~~~~~
iachinweze1 23:ab1cb51527d1 190 void commOutFn() {
iachinweze1 23:ab1cb51527d1 191 while (_RUN) {
iachinweze1 23:ab1cb51527d1 192 osEvent newEvent = mailStack.get();
iachinweze1 23:ab1cb51527d1 193 msg *pMessage = (msg *) newEvent.value.p;
iachinweze1 23:ab1cb51527d1 194
iachinweze1 23:ab1cb51527d1 195 //Case switch to choose serial output based on incoming message
iachinweze1 23:ab1cb51527d1 196 switch (pMessage->type) {
iachinweze1 23:ab1cb51527d1 197 case motorState:
iachinweze1 23:ab1cb51527d1 198 pc.printf("The motor is currently in state %x\n\r", pMessage->message);
iachinweze1 23:ab1cb51527d1 199 break;
iachinweze1 23:ab1cb51527d1 200 case hashRate:
iachinweze1 23:ab1cb51527d1 201 pc.printf("\r>%s< Mining: %.4u Hash/s\r", inCharQ, (uint32_t) pMessage->message);
iachinweze1 23:ab1cb51527d1 202 returnCursor();
iachinweze1 23:ab1cb51527d1 203 break;
iachinweze1 23:ab1cb51527d1 204 case nonceMatch:
iachinweze1 23:ab1cb51527d1 205 pc.printf("\r>%s< Nonce found: %x\r", inCharQ, pMessage->message);
iachinweze1 23:ab1cb51527d1 206 returnCursor();
iachinweze1 23:ab1cb51527d1 207 break;
iachinweze1 23:ab1cb51527d1 208 case keyAdded:
iachinweze1 23:ab1cb51527d1 209 pc.printf("New Key Added:\t0x%016x\n\r", pMessage->message);
iachinweze1 23:ab1cb51527d1 210 break;
iachinweze1 23:ab1cb51527d1 211 case torque:
iachinweze1 23:ab1cb51527d1 212 pc.printf("Motor Torque set to:\t%d\n\r", pMessage->message);
iachinweze1 23:ab1cb51527d1 213 break;
iachinweze1 23:ab1cb51527d1 214 case velIn:
iachinweze1 23:ab1cb51527d1 215 pc.printf("Target Velocity set to:\t%.2f\n\r", targetVel);
iachinweze1 23:ab1cb51527d1 216 break;
iachinweze1 23:ab1cb51527d1 217 case velOut:
iachinweze1 23:ab1cb51527d1 218 pc.printf("Current Velocity:\t%.2f\n\r", \
iachinweze1 23:ab1cb51527d1 219 (float) ((int32_t) pMessage->message / 6));
iachinweze1 23:ab1cb51527d1 220 break;
iachinweze1 23:ab1cb51527d1 221 case posIn:
iachinweze1 23:ab1cb51527d1 222 pc.printf("Target Rotation set to:\t%.2f\n\r", \
iachinweze1 23:ab1cb51527d1 223 (float) ((int32_t) pMessage->message / 6));
iachinweze1 23:ab1cb51527d1 224 break;
iachinweze1 23:ab1cb51527d1 225 case posOut:
iachinweze1 23:ab1cb51527d1 226 pc.printf("Current Position:\t%.2f\n\r", \
iachinweze1 23:ab1cb51527d1 227 (float) ((int32_t) pMessage->message / 6));
iachinweze1 23:ab1cb51527d1 228 break;
iachinweze1 23:ab1cb51527d1 229 case error:
iachinweze1 23:ab1cb51527d1 230 pc.printf("\r>%s< Debugging position:%x\n\r", inCharQ, pMessage->message);
iachinweze1 23:ab1cb51527d1 231 for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer
iachinweze1 23:ab1cb51527d1 232 inCharQ[i] = ' ';
iachinweze1 23:ab1cb51527d1 233 break;
iachinweze1 23:ab1cb51527d1 234 default:
iachinweze1 23:ab1cb51527d1 235 pc.printf("Unknown Error. Message: %x\n\r", pMessage->message);
iachinweze1 23:ab1cb51527d1 236 break;
CallumAlder 19:805c87360b55 237 }
iachinweze1 23:ab1cb51527d1 238 mailStack.free(pMessage);
iachinweze1 23:ab1cb51527d1 239 }
iachinweze1 23:ab1cb51527d1 240 }
adehadd 20:c60f4785b556 241
adehadd 20:c60f4785b556 242
adehadd 20:c60f4785b556 243
adehadd 20:c60f4785b556 244
iachinweze1 23:ab1cb51527d1 245 //TODO: stop function, maybe use parent de-constructor
iachinweze1 23:ab1cb51527d1 246 //void stop_comm{}
iachinweze1 23:ab1cb51527d1 247
iachinweze1 23:ab1cb51527d1 248 // public:
iachinweze1 23:ab1cb51527d1 249
iachinweze1 23:ab1cb51527d1 250 volatile uint64_t newKey; // hash key
iachinweze1 23:ab1cb51527d1 251 Mutex newKey_mutex; // Restrict access to prevent deadlock.
adehadd 20:c60f4785b556 252
iachinweze1 23:ab1cb51527d1 253 Comm() : pc(SERIAL_TX, SERIAL_RX),
adehadd 30:fbae0e5f200d 254 t_comm_out(osPriorityAboveNormal, 1024)
iachinweze1 23:ab1cb51527d1 255 { // inherit from the RawSerial constructor
adehadd 20:c60f4785b556 256
iachinweze1 23:ab1cb51527d1 257 pc.printf("%s\n\r", "Welcome" );
iachinweze1 23:ab1cb51527d1 258 MAXCMDLENGTH = 18;
adehadd 20:c60f4785b556 259
iachinweze1 23:ab1cb51527d1 260 // reset buffer
iachinweze1 23:ab1cb51527d1 261 // MbedOS prints 'Embedded Systems are fun and do awesome things!'
iachinweze1 23:ab1cb51527d1 262 // if you print a null terminator
iachinweze1 23:ab1cb51527d1 263 pc.putc('>');
iachinweze1 23:ab1cb51527d1 264 for (int i = 0; i < MAXCMDLENGTH; ++i) {
iachinweze1 23:ab1cb51527d1 265 inCharQ[i] = '.';
iachinweze1 23:ab1cb51527d1 266 pc.putc('.');
CallumAlder 19:805c87360b55 267 }
iachinweze1 23:ab1cb51527d1 268 pc.putc('<');
iachinweze1 23:ab1cb51527d1 269 pc.putc('\r');
iachinweze1 23:ab1cb51527d1 270
iachinweze1 23:ab1cb51527d1 271 inCharQ[MAXCMDLENGTH] = '\0';
iachinweze1 23:ab1cb51527d1 272 strncpy(newCmd, inCharQ, MAXCMDLENGTH);
iachinweze1 23:ab1cb51527d1 273
iachinweze1 23:ab1cb51527d1 274 cmdIndx = 0;
iachinweze1 23:ab1cb51527d1 275
iachinweze1 23:ab1cb51527d1 276 inCharQIdx = 0;
iachinweze1 23:ab1cb51527d1 277 // inCharQIdx = MAXCMDLENGTH-1;
iachinweze1 23:ab1cb51527d1 278
iachinweze1 23:ab1cb51527d1 279
iachinweze1 23:ab1cb51527d1 280
iachinweze1 23:ab1cb51527d1 281 pc.attach(callback(this, &Comm::serialISR));
iachinweze1 23:ab1cb51527d1 282
iachinweze1 23:ab1cb51527d1 283 // Thread t_comm_in(osPriorityAboveNormal, 1024);
iachinweze1 23:ab1cb51527d1 284 // Thread t_comm_out(osPriorityAboveNormal, 1024);
iachinweze1 23:ab1cb51527d1 285 // Thread t_motor_ctrl(osPriorityAboveNormal, 1024);
iachinweze1 23:ab1cb51527d1 286
iachinweze1 23:ab1cb51527d1 287 motorPower = 300;
iachinweze1 23:ab1cb51527d1 288 targetVel = 45.0;
iachinweze1 23:ab1cb51527d1 289 targetRot = 459.0;
iachinweze1 23:ab1cb51527d1 290
adehadd 20:c60f4785b556 291
CallumAlder 19:805c87360b55 292
iachinweze1 23:ab1cb51527d1 293 /*MsgChar = {'m', 'R', 'V', 'r', 'v',
iachinweze1 23:ab1cb51527d1 294
iachinweze1 23:ab1cb51527d1 295 'h', 'K', 'n',
iachinweze1 23:ab1cb51527d1 296
iachinweze1 23:ab1cb51527d1 297 'T', 'r',
iachinweze1 23:ab1cb51527d1 298
iachinweze1 23:ab1cb51527d1 299 'e'};*/
iachinweze1 23:ab1cb51527d1 300 }
iachinweze1 23:ab1cb51527d1 301
iachinweze1 23:ab1cb51527d1 302
iachinweze1 23:ab1cb51527d1 303 void putMessage(msgType type, uint32_t message){
iachinweze1 23:ab1cb51527d1 304 msg *p_msg = mailStack.alloc();
iachinweze1 23:ab1cb51527d1 305 p_msg->type = type;
iachinweze1 23:ab1cb51527d1 306 p_msg->message = message;
iachinweze1 23:ab1cb51527d1 307 mailStack.put(p_msg);
iachinweze1 23:ab1cb51527d1 308 }
iachinweze1 23:ab1cb51527d1 309
iachinweze1 23:ab1cb51527d1 310 void start_comm(){
iachinweze1 23:ab1cb51527d1 311 _RUN = true;
iachinweze1 23:ab1cb51527d1 312
adehadd 20:c60f4785b556 313
iachinweze1 23:ab1cb51527d1 314 // reset buffer
iachinweze1 23:ab1cb51527d1 315 // MbedOS prints 'Embedded Systems are fun and do awesome things!'
iachinweze1 23:ab1cb51527d1 316 // if you print a null terminator
iachinweze1 23:ab1cb51527d1 317 pc.putc('>');
iachinweze1 23:ab1cb51527d1 318 for (int i = 0; i < MAXCMDLENGTH; ++i) {
iachinweze1 23:ab1cb51527d1 319 inCharQ[i] = '.';
iachinweze1 23:ab1cb51527d1 320 pc.putc('.');
iachinweze1 23:ab1cb51527d1 321 }
iachinweze1 23:ab1cb51527d1 322 pc.putc('<');
iachinweze1 23:ab1cb51527d1 323 pc.putc('\r');
iachinweze1 23:ab1cb51527d1 324
iachinweze1 23:ab1cb51527d1 325 inCharQ[MAXCMDLENGTH] = '\0';
iachinweze1 23:ab1cb51527d1 326 strncpy(newCmd, inCharQ, MAXCMDLENGTH);
adehadd 20:c60f4785b556 327
iachinweze1 23:ab1cb51527d1 328 // returnCursor();
adehadd 20:c60f4785b556 329
iachinweze1 23:ab1cb51527d1 330 // t_comm_in.start(callback(this, &Comm::commInFn));
iachinweze1 23:ab1cb51527d1 331 // this::thread::wait()
iachinweze1 23:ab1cb51527d1 332 // wait(1.0);
iachinweze1 23:ab1cb51527d1 333 t_comm_out.start(callback(this, &Comm::commOutFn));
adehadd 30:fbae0e5f200d 334
iachinweze1 23:ab1cb51527d1 335
iachinweze1 23:ab1cb51527d1 336
iachinweze1 23:ab1cb51527d1 337 }
iachinweze1 23:ab1cb51527d1 338
iachinweze1 23:ab1cb51527d1 339 char newCmd[]; // because unallocated must be defined at the bottom of the class
iachinweze1 23:ab1cb51527d1 340 char inCharQ[];
CallumAlder 19:805c87360b55 341 };
CallumAlder 19:805c87360b55 342
iachinweze1 12:41b3112021a3 343
adehadd 30:fbae0e5f200d 344 class Motor {
CallumAlder 28:4f02ac845e5d 345
CallumAlder 28:4f02ac845e5d 346
CallumAlder 28:4f02ac845e5d 347 protected:
adehadd 31:b10ca6cf39bf 348 int8_t orState; //Rotor offset at motor state 0, motor specific
adehadd 30:fbae0e5f200d 349 volatile int8_t currentState; //Current Rotor State
adehadd 30:fbae0e5f200d 350 volatile int8_t stateList[6]; //All possible rotor states stored
adehadd 30:fbae0e5f200d 351
adehadd 30:fbae0e5f200d 352 //Phase lead to make motor spin
adehadd 30:fbae0e5f200d 353 int8_t lead;
adehadd 30:fbae0e5f200d 354
adehadd 30:fbae0e5f200d 355 Comm* p_comm;
adehadd 30:fbae0e5f200d 356
CallumAlder 28:4f02ac845e5d 357 //Run the motor synchronisation
CallumAlder 28:4f02ac845e5d 358
adehadd 30:fbae0e5f200d 359 float dutyC; // 1 = 100%
adehadd 30:fbae0e5f200d 360 float mtrPeriod; // motor period
adehadd 30:fbae0e5f200d 361 uint8_t stateCount[3]; // State Counter
adehadd 30:fbae0e5f200d 362 uint8_t theStates[3]; // The Key states
iachinweze1 23:ab1cb51527d1 363
adehadd 30:fbae0e5f200d 364 Thread t_motor_ctrl; // Thread for motor Control
CallumAlder 28:4f02ac845e5d 365
CallumAlder 28:4f02ac845e5d 366 public:
CallumAlder 28:4f02ac845e5d 367
adehadd 31:b10ca6cf39bf 368 Motor() : t_motor_ctrl(osPriorityAboveNormal, 1024)
adehadd 30:fbae0e5f200d 369 {
adehadd 30:fbae0e5f200d 370 // Set Power to maximum to drive motorHome()
adehadd 30:fbae0e5f200d 371 dutyC = 1;
CallumAlder 29:c96439a60184 372 mtrPeriod = 2e-3; // motor period
adehadd 30:fbae0e5f200d 373 pwmCtrl.period(mtrPeriod);
CallumAlder 28:4f02ac845e5d 374 pwmCtrl.pulsewidth(mtrPeriod*dutyC);
CallumAlder 28:4f02ac845e5d 375
adehadd 30:fbae0e5f200d 376 orState = motorHome(); //Rotot offset at motor state 0
adehadd 30:fbae0e5f200d 377 currentState = readRotorState(); //Current Rotor State
adehadd 30:fbae0e5f200d 378 // stateList[6] = {0,0,0, 0,0,0}; //All possible rotor states stored
adehadd 30:fbae0e5f200d 379
adehadd 30:fbae0e5f200d 380 theStates[0] = orState;
adehadd 30:fbae0e5f200d 381 theStates[1] = (orState + lead) % 6;
adehadd 30:fbae0e5f200d 382 theStates[2] = (orState + (lead*2)) % 6;
iachinweze1 23:ab1cb51527d1 383
adehadd 30:fbae0e5f200d 384 lead = 2; //2 for forwards, -2 for backwards
adehadd 30:fbae0e5f200d 385
adehadd 31:b10ca6cf39bf 386 stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0;
adehadd 31:b10ca6cf39bf 387 theStates[0] = 0; theStates[1] = 0; theStates[2] = 0;
adehadd 31:b10ca6cf39bf 388
adehadd 31:b10ca6cf39bf 389 p_comm = NULL; // null pointer for now
adehadd 30:fbae0e5f200d 390
adehadd 30:fbae0e5f200d 391 }
adehadd 30:fbae0e5f200d 392
adehadd 30:fbae0e5f200d 393
adehadd 31:b10ca6cf39bf 394 void motorStart(Comm *comm) {
adehadd 30:fbae0e5f200d 395
adehadd 30:fbae0e5f200d 396 // Establish Photointerrupter Service Routines (auto choose next state)
CallumAlder 29:c96439a60184 397 I1.fall(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 398 I2.fall(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 399 I3.fall(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 400 I1.rise(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 401 I2.rise(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 402 I3.rise(callback(this, &Motor::stateUpdate));
CallumAlder 28:4f02ac845e5d 403
adehadd 30:fbae0e5f200d 404 // push digitally so if motor is static it will start moving
adehadd 30:fbae0e5f200d 405 motorOut((currentState-orState+lead+6)%6); // We push it digitally
adehadd 30:fbae0e5f200d 406
adehadd 30:fbae0e5f200d 407 // Default a lower duty cylce
adehadd 30:fbae0e5f200d 408 dutyC = 0.8;
CallumAlder 28:4f02ac845e5d 409 pwmCtrl.period(mtrPeriod);
CallumAlder 28:4f02ac845e5d 410 pwmCtrl.pulsewidth(mtrPeriod*dutyC);
CallumAlder 28:4f02ac845e5d 411
adehadd 30:fbae0e5f200d 412 // Start motor control thread
adehadd 31:b10ca6cf39bf 413 t_motor_ctrl.start(callback(this, &Motor::motorCtrlFn));
adehadd 31:b10ca6cf39bf 414
adehadd 31:b10ca6cf39bf 415 p_comm = comm;
CallumAlder 28:4f02ac845e5d 416 }
CallumAlder 28:4f02ac845e5d 417
CallumAlder 28:4f02ac845e5d 418 //Set a given drive state
adehadd 30:fbae0e5f200d 419 void motorOut(int8_t driveState) {
CallumAlder 28:4f02ac845e5d 420
CallumAlder 28:4f02ac845e5d 421 //Lookup the output byte from the drive state.
CallumAlder 28:4f02ac845e5d 422 int8_t driveOut = driveTable[driveState & 0x07];
CallumAlder 28:4f02ac845e5d 423
CallumAlder 28:4f02ac845e5d 424 //Turn off first
CallumAlder 28:4f02ac845e5d 425 if (~driveOut & 0x01) L1L = 0;
CallumAlder 28:4f02ac845e5d 426 if (~driveOut & 0x02) L1H = 1;
CallumAlder 28:4f02ac845e5d 427 if (~driveOut & 0x04) L2L = 0;
CallumAlder 28:4f02ac845e5d 428 if (~driveOut & 0x08) L2H = 1;
CallumAlder 28:4f02ac845e5d 429 if (~driveOut & 0x10) L3L = 0;
CallumAlder 28:4f02ac845e5d 430 if (~driveOut & 0x20) L3H = 1;
iachinweze1 23:ab1cb51527d1 431
CallumAlder 28:4f02ac845e5d 432 //Then turn on
CallumAlder 28:4f02ac845e5d 433 if (driveOut & 0x01) L1L = 1;
CallumAlder 28:4f02ac845e5d 434 if (driveOut & 0x02) L1H = 0;
CallumAlder 28:4f02ac845e5d 435 if (driveOut & 0x04) L2L = 1;
CallumAlder 28:4f02ac845e5d 436 if (driveOut & 0x08) L2H = 0;
CallumAlder 28:4f02ac845e5d 437 if (driveOut & 0x10) L3L = 1;
CallumAlder 28:4f02ac845e5d 438 if (driveOut & 0x20) L3H = 0;
CallumAlder 28:4f02ac845e5d 439 }
CallumAlder 28:4f02ac845e5d 440
CallumAlder 28:4f02ac845e5d 441 //Convert photointerrupter inputs to a rotor state
adehadd 30:fbae0e5f200d 442 inline int8_t readRotorState() {
CallumAlder 28:4f02ac845e5d 443 return stateMap[I1 + 2*I2 + 4*I3];
CallumAlder 28:4f02ac845e5d 444 }
adehadd 20:c60f4785b556 445
CallumAlder 28:4f02ac845e5d 446 //Basic synchronisation routine
CallumAlder 28:4f02ac845e5d 447 int8_t motorHome() {
CallumAlder 28:4f02ac845e5d 448 //Put the motor in drive state 0 and wait for it to stabilise
CallumAlder 28:4f02ac845e5d 449 motorOut(0);
CallumAlder 28:4f02ac845e5d 450 wait(2.0);
CallumAlder 28:4f02ac845e5d 451
CallumAlder 28:4f02ac845e5d 452 //Get the rotor state
CallumAlder 28:4f02ac845e5d 453 return readRotorState();
CallumAlder 28:4f02ac845e5d 454 }
iachinweze1 23:ab1cb51527d1 455
CallumAlder 28:4f02ac845e5d 456
CallumAlder 29:c96439a60184 457 void stateUpdate() { // () { // **params
adehadd 30:fbae0e5f200d 458 currentState = readRotorState();
CallumAlder 28:4f02ac845e5d 459
adehadd 30:fbae0e5f200d 460 // Store into state counter
adehadd 31:b10ca6cf39bf 461 if (currentState == theStates[0])
adehadd 30:fbae0e5f200d 462 stateCount[0]++;
adehadd 31:b10ca6cf39bf 463 else if (currentState == theStates[1])
adehadd 30:fbae0e5f200d 464 stateCount[1]++;
adehadd 31:b10ca6cf39bf 465 else if (currentState == theStates[2])
adehadd 30:fbae0e5f200d 466 stateCount[2]++;
adehadd 31:b10ca6cf39bf 467
adehadd 31:b10ca6cf39bf 468
adehadd 30:fbae0e5f200d 469 // (Current - Offset + lead + 6) %6
adehadd 30:fbae0e5f200d 470 motorOut((currentState - orState + lead + 6) % 6);
adehadd 30:fbae0e5f200d 471
adehadd 30:fbae0e5f200d 472 }
adehadd 30:fbae0e5f200d 473
adehadd 30:fbae0e5f200d 474
adehadd 30:fbae0e5f200d 475
adehadd 30:fbae0e5f200d 476 // attach_us -> runs funtion every 100ms
adehadd 30:fbae0e5f200d 477 void motorCtrlFn() {
adehadd 30:fbae0e5f200d 478 Ticker motorCtrlTicker;
adehadd 31:b10ca6cf39bf 479 motorCtrlTicker.attach_us(callback(this,&Motor::motorCtrlTick), 1e5);
adehadd 30:fbae0e5f200d 480 while (1) {
adehadd 30:fbae0e5f200d 481 t_motor_ctrl.signal_wait((int32_t)0x1);
adehadd 31:b10ca6cf39bf 482 p_comm->pc.printf("B4115");
CallumAlder 29:c96439a60184 483 }
adehadd 30:fbae0e5f200d 484 }
adehadd 30:fbae0e5f200d 485
adehadd 30:fbae0e5f200d 486 void motorCtrlTick(){
adehadd 30:fbae0e5f200d 487 t_motor_ctrl.signal_set(0x1);
CallumAlder 28:4f02ac845e5d 488 }
CallumAlder 29:c96439a60184 489 };
adehadd 20:c60f4785b556 490
adehadd 20:c60f4785b556 491
estott 0:de4320f74764 492 int main() {
CallumAlder 19:805c87360b55 493
CallumAlder 32:fc5e00d9f74d 494 // Declare Objects
CallumAlder 32:fc5e00d9f74d 495 Comm comm_port;
CallumAlder 32:fc5e00d9f74d 496 SHA256 miner;
CallumAlder 32:fc5e00d9f74d 497 Motor motor;
CallumAlder 32:fc5e00d9f74d 498
CallumAlder 32:fc5e00d9f74d 499 // Start Motor and Comm Port
CallumAlder 32:fc5e00d9f74d 500 motor.motorStart(&comm_port);
CallumAlder 32:fc5e00d9f74d 501 comm_port.start_comm();
adehadd 20:c60f4785b556 502
CallumAlder 32:fc5e00d9f74d 503 // Declare Hash Variables
CallumAlder 14:4e312fb83330 504 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
CallumAlder 14:4e312fb83330 505 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
CallumAlder 14:4e312fb83330 506 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
CallumAlder 14:4e312fb83330 507 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
CallumAlder 14:4e312fb83330 508 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
CallumAlder 14:4e312fb83330 509 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
CallumAlder 14:4e312fb83330 510 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
CallumAlder 14:4e312fb83330 511 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
CallumAlder 14:4e312fb83330 512 uint64_t* key = (uint64_t*)((int)sequence + 48);
CallumAlder 14:4e312fb83330 513 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
CallumAlder 14:4e312fb83330 514 uint8_t hash[32];
CallumAlder 14:4e312fb83330 515 uint32_t length64 = 64;
CallumAlder 15:2f95f2fb68e3 516 uint32_t hashCounter = 0;
CallumAlder 32:fc5e00d9f74d 517
CallumAlder 32:fc5e00d9f74d 518 // Begin Main Timer
iachinweze1 23:ab1cb51527d1 519 Timer timer;
CallumAlder 32:fc5e00d9f74d 520 timer.start();
CallumAlder 29:c96439a60184 521
CallumAlder 32:fc5e00d9f74d 522 // Loop Program
CallumAlder 15:2f95f2fb68e3 523 while (1) {
CallumAlder 32:fc5e00d9f74d 524
CallumAlder 32:fc5e00d9f74d 525 // Mutex For Access Control
CallumAlder 32:fc5e00d9f74d 526 comm_port.newKey_mutex.lock();
CallumAlder 32:fc5e00d9f74d 527 *key = comm_port.newKey;
CallumAlder 32:fc5e00d9f74d 528 comm_port.newKey_mutex.unlock();
CallumAlder 32:fc5e00d9f74d 529
CallumAlder 32:fc5e00d9f74d 530 // Compute Hash and Counter
CallumAlder 32:fc5e00d9f74d 531 miner.computeHash(hash, sequence, length64);
CallumAlder 15:2f95f2fb68e3 532 hashCounter++;
CallumAlder 32:fc5e00d9f74d 533
CallumAlder 32:fc5e00d9f74d 534 // Enum Casting and Condition
CallumAlder 15:2f95f2fb68e3 535 if ((hash[0]==0) && (hash[1]==0)){
CallumAlder 32:fc5e00d9f74d 536 comm_port.putMessage((Comm::msgType)7, *nonce);
CallumAlder 15:2f95f2fb68e3 537 }
CallumAlder 15:2f95f2fb68e3 538
CallumAlder 32:fc5e00d9f74d 539 // Try Nonce
CallumAlder 15:2f95f2fb68e3 540 (*nonce)++;
CallumAlder 32:fc5e00d9f74d 541
CallumAlder 32:fc5e00d9f74d 542 // Display via Comm Port
CallumAlder 15:2f95f2fb68e3 543 if (timer.read() >= 1){
CallumAlder 32:fc5e00d9f74d 544 comm_port.putMessage((Comm::msgType)5, hashCounter);
CallumAlder 15:2f95f2fb68e3 545 hashCounter=0;
CallumAlder 15:2f95f2fb68e3 546 timer.reset();
CallumAlder 15:2f95f2fb68e3 547 }
CallumAlder 15:2f95f2fb68e3 548 }
CallumAlder 19:805c87360b55 549 }