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

Dependencies:   Crypto

Committer:
CallumAlder
Date:
Mon Mar 18 15:37:20 2019 +0000
Revision:
29:c96439a60184
Parent:
28:4f02ac845e5d
Child:
30:fbae0e5f200d
a bit more motor

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 t_motor_ctrl;
iachinweze1 23:ab1cb51527d1 85 // Thread *p_motor_ctrl;
CallumAlder 19:805c87360b55 86
iachinweze1 23:ab1cb51527d1 87 bool _RUN;
iachinweze1 23:ab1cb51527d1 88
iachinweze1 23:ab1cb51527d1 89 RawSerial pc;
iachinweze1 23:ab1cb51527d1 90 // Queue<void, 8> inCharQ; // Input Character Queue
iachinweze1 23:ab1cb51527d1 91
iachinweze1 23:ab1cb51527d1 92
iachinweze1 23:ab1cb51527d1 93 static const char MsgChar[11];
CallumAlder 19:805c87360b55 94
iachinweze1 23:ab1cb51527d1 95 uint8_t MAXCMDLENGTH;
iachinweze1 23:ab1cb51527d1 96
iachinweze1 23:ab1cb51527d1 97 volatile uint8_t cmdIndx;
iachinweze1 23:ab1cb51527d1 98 volatile uint8_t inCharQIdx;
CallumAlder 19:805c87360b55 99
iachinweze1 23:ab1cb51527d1 100 volatile uint32_t motorPower; // motor toque
iachinweze1 23:ab1cb51527d1 101 volatile float targetVel;
iachinweze1 23:ab1cb51527d1 102 volatile float targetRot;
CallumAlder 19:805c87360b55 103
iachinweze1 23:ab1cb51527d1 104 enum msgType {motorState, posIn, velIn, posOut, velOut,
iachinweze1 23:ab1cb51527d1 105
iachinweze1 23:ab1cb51527d1 106 hashRate, keyAdded, nonceMatch,
iachinweze1 23:ab1cb51527d1 107
iachinweze1 23:ab1cb51527d1 108 torque, rotations,
adehadd 16:db7ef0a4aa23 109
iachinweze1 23:ab1cb51527d1 110 error};
iachinweze1 23:ab1cb51527d1 111
iachinweze1 23:ab1cb51527d1 112 typedef struct {
iachinweze1 23:ab1cb51527d1 113 msgType type;
iachinweze1 23:ab1cb51527d1 114 uint32_t message;
iachinweze1 23:ab1cb51527d1 115 } msg;
iachinweze1 23:ab1cb51527d1 116
iachinweze1 23:ab1cb51527d1 117 Mail<msg, 32> mailStack;
iachinweze1 23:ab1cb51527d1 118
iachinweze1 23:ab1cb51527d1 119 void serialISR(){
iachinweze1 23:ab1cb51527d1 120 if (pc.readable()) {
iachinweze1 23:ab1cb51527d1 121 char newChar = pc.getc();
iachinweze1 23:ab1cb51527d1 122 // inCharQ.put((void*)newChar); // void* = pointer to an unknown type that cannot be dereferenced
CallumAlder 19:805c87360b55 123
iachinweze1 23:ab1cb51527d1 124 if (inCharQIdx == (MAXCMDLENGTH)) {
iachinweze1 23:ab1cb51527d1 125 inCharQ[MAXCMDLENGTH] = '\0'; // force the string to have an end character
iachinweze1 23:ab1cb51527d1 126 putMessage(error, 1);
iachinweze1 23:ab1cb51527d1 127 inCharQIdx = 0; // reset buffer index
iachinweze1 23:ab1cb51527d1 128 // pc.putc('\r'); // carriage return moves to the start of the line
iachinweze1 23:ab1cb51527d1 129 // for (int i = 0; i < MAXCMDLENGTH; ++i)
iachinweze1 23:ab1cb51527d1 130 // {
iachinweze1 23:ab1cb51527d1 131 // inCharQ[i] = ' ';
iachinweze1 23:ab1cb51527d1 132 // pc.putc(' ');
iachinweze1 23:ab1cb51527d1 133 // }
iachinweze1 23:ab1cb51527d1 134
iachinweze1 23:ab1cb51527d1 135 // pc.putc('\r'); // carriage return moves to the start of the line
iachinweze1 23:ab1cb51527d1 136 }
iachinweze1 23:ab1cb51527d1 137 else{
iachinweze1 23:ab1cb51527d1 138 if(newChar != '\r'){ //While the command is not over,
iachinweze1 23:ab1cb51527d1 139 inCharQ[inCharQIdx] = newChar; //save input character and
iachinweze1 23:ab1cb51527d1 140 inCharQIdx++; //advance index
iachinweze1 23:ab1cb51527d1 141 pc.putc(newChar);
adehadd 20:c60f4785b556 142 }
adehadd 20:c60f4785b556 143 else{
iachinweze1 23:ab1cb51527d1 144 inCharQ[inCharQIdx] = '\0'; //When the command is finally over,
iachinweze1 23:ab1cb51527d1 145 strncpy(newCmd, inCharQ, MAXCMDLENGTH); // Will copy 18 characters from inCharQ to newCmd
iachinweze1 23:ab1cb51527d1 146 cmdParser(); //parse the command for decoding.
iachinweze1 23:ab1cb51527d1 147 for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer
iachinweze1 23:ab1cb51527d1 148 inCharQ[i] = ' ';
iachinweze1 23:ab1cb51527d1 149 inCharQIdx = 0; // reset index
CallumAlder 19:805c87360b55 150 }
CallumAlder 19:805c87360b55 151 }
CallumAlder 19:805c87360b55 152 }
CallumAlder 19:805c87360b55 153
iachinweze1 23:ab1cb51527d1 154
iachinweze1 23:ab1cb51527d1 155 }
iachinweze1 23:ab1cb51527d1 156
iachinweze1 23:ab1cb51527d1 157 /*void commInFn() {
iachinweze1 23:ab1cb51527d1 158 // if (_RUN)
iachinweze1 23:ab1cb51527d1 159
iachinweze1 23:ab1cb51527d1 160 while (_RUN) {
iachinweze1 23:ab1cb51527d1 161 osEvent newEvent = inCharQ.get();
iachinweze1 23:ab1cb51527d1 162 uint8_t newChar = (uint8_t)(newEvent.value.p); // size_t to type cast the 64bit pointer properly
iachinweze1 23:ab1cb51527d1 163 pc.putc(newChar);
iachinweze1 23:ab1cb51527d1 164 if(cmdIndx >= MAXCMDLENGTH){ //Make sure there is no overflow in comand.
iachinweze1 23:ab1cb51527d1 165 cmdIndx = 0;
iachinweze1 23:ab1cb51527d1 166 putMessage(error, 1);
iachinweze1 23:ab1cb51527d1 167 }
iachinweze1 23:ab1cb51527d1 168 else{
iachinweze1 23:ab1cb51527d1 169 if(newChar != '\r'){ //While the command is not over,
iachinweze1 23:ab1cb51527d1 170 newCmd[cmdIndx] = newChar; //save input character and
iachinweze1 23:ab1cb51527d1 171 cmdIndx++; //advance index
iachinweze1 23:ab1cb51527d1 172 }
iachinweze1 23:ab1cb51527d1 173 else{
iachinweze1 23:ab1cb51527d1 174 newCmd[cmdIndx] = '\0'; //When the command is finally over,
iachinweze1 23:ab1cb51527d1 175 cmdIndx = 0; //reset index and
iachinweze1 23:ab1cb51527d1 176 cmdParser(); //parse the command for decoding.
iachinweze1 23:ab1cb51527d1 177 }
iachinweze1 23:ab1cb51527d1 178 }
iachinweze1 23:ab1cb51527d1 179 }
iachinweze1 23:ab1cb51527d1 180 }*/
iachinweze1 23:ab1cb51527d1 181
iachinweze1 23:ab1cb51527d1 182 void returnCursor() {
iachinweze1 23:ab1cb51527d1 183 pc.putc('>');
iachinweze1 23:ab1cb51527d1 184 for (int i = 0; i < inCharQIdx; ++i) // reset cursor position
iachinweze1 23:ab1cb51527d1 185 pc.putc(inCharQ[i]);
iachinweze1 23:ab1cb51527d1 186 // for (int i = inCharQIdx; i < MAXCMDLENGTH; ++i) // fill remaining with blanks
iachinweze1 23:ab1cb51527d1 187 // pc.putc(' ');
iachinweze1 23:ab1cb51527d1 188 // pc.putc('<');
iachinweze1 23:ab1cb51527d1 189 }
iachinweze1 23:ab1cb51527d1 190
iachinweze1 23:ab1cb51527d1 191 void cmdParser(){
iachinweze1 23:ab1cb51527d1 192 switch(newCmd[0]) {
iachinweze1 23:ab1cb51527d1 193 case 'K': //(MsgChar[keyAdded])://
iachinweze1 23:ab1cb51527d1 194 newKey_mutex.lock(); //Ensure there is no deadlock
iachinweze1 23:ab1cb51527d1 195 sscanf(newCmd, "K%x", &newKey); //Find desired the Key code
iachinweze1 23:ab1cb51527d1 196 putMessage(keyAdded, newKey); //Print it out
iachinweze1 23:ab1cb51527d1 197 newKey_mutex.unlock();
iachinweze1 23:ab1cb51527d1 198 break;
iachinweze1 23:ab1cb51527d1 199 case 'V': //(MsgChar[velIn])://
iachinweze1 23:ab1cb51527d1 200 sscanf(newCmd, "V%f", &targetVel); //Find desired the target velocity
iachinweze1 23:ab1cb51527d1 201 putMessage(velIn, targetVel); //Print it out
iachinweze1 23:ab1cb51527d1 202 break;
iachinweze1 23:ab1cb51527d1 203 case 'R': //(MsgChar[posIn])://
iachinweze1 23:ab1cb51527d1 204 sscanf(newCmd, "R%f", &targetRot); //Find desired target rotation
iachinweze1 23:ab1cb51527d1 205 putMessage(posIn, targetRot); //Print it out
iachinweze1 23:ab1cb51527d1 206 break;
iachinweze1 23:ab1cb51527d1 207 case 'T': //(MsgChar[torque])://
iachinweze1 23:ab1cb51527d1 208 sscanf(newCmd, "T%d", &motorPower); //Find desired target torque
iachinweze1 23:ab1cb51527d1 209 putMessage(torque, motorPower); //Print it out
iachinweze1 23:ab1cb51527d1 210 break;
iachinweze1 23:ab1cb51527d1 211 default: break;
iachinweze1 23:ab1cb51527d1 212 }
iachinweze1 23:ab1cb51527d1 213 }
CallumAlder 19:805c87360b55 214
iachinweze1 23:ab1cb51527d1 215 //~~~~~Decode messages to print on serial port~~~~~
iachinweze1 23:ab1cb51527d1 216 void commOutFn() {
iachinweze1 23:ab1cb51527d1 217 while (_RUN) {
iachinweze1 23:ab1cb51527d1 218 osEvent newEvent = mailStack.get();
iachinweze1 23:ab1cb51527d1 219 msg *pMessage = (msg *) newEvent.value.p;
iachinweze1 23:ab1cb51527d1 220
iachinweze1 23:ab1cb51527d1 221 //Case switch to choose serial output based on incoming message
iachinweze1 23:ab1cb51527d1 222 switch (pMessage->type) {
iachinweze1 23:ab1cb51527d1 223 case motorState:
iachinweze1 23:ab1cb51527d1 224 pc.printf("The motor is currently in state %x\n\r", pMessage->message);
iachinweze1 23:ab1cb51527d1 225 break;
iachinweze1 23:ab1cb51527d1 226 case hashRate:
iachinweze1 23:ab1cb51527d1 227 pc.printf("\r>%s< Mining: %.4u Hash/s\r", inCharQ, (uint32_t) pMessage->message);
iachinweze1 23:ab1cb51527d1 228 returnCursor();
iachinweze1 23:ab1cb51527d1 229 break;
iachinweze1 23:ab1cb51527d1 230 case nonceMatch:
iachinweze1 23:ab1cb51527d1 231 pc.printf("\r>%s< Nonce found: %x\r", inCharQ, pMessage->message);
iachinweze1 23:ab1cb51527d1 232 returnCursor();
iachinweze1 23:ab1cb51527d1 233 break;
iachinweze1 23:ab1cb51527d1 234 case keyAdded:
iachinweze1 23:ab1cb51527d1 235 pc.printf("New Key Added:\t0x%016x\n\r", pMessage->message);
iachinweze1 23:ab1cb51527d1 236 break;
iachinweze1 23:ab1cb51527d1 237 case torque:
iachinweze1 23:ab1cb51527d1 238 pc.printf("Motor Torque set to:\t%d\n\r", pMessage->message);
iachinweze1 23:ab1cb51527d1 239 break;
iachinweze1 23:ab1cb51527d1 240 case velIn:
iachinweze1 23:ab1cb51527d1 241 pc.printf("Target Velocity set to:\t%.2f\n\r", targetVel);
iachinweze1 23:ab1cb51527d1 242 break;
iachinweze1 23:ab1cb51527d1 243 case velOut:
iachinweze1 23:ab1cb51527d1 244 pc.printf("Current Velocity:\t%.2f\n\r", \
iachinweze1 23:ab1cb51527d1 245 (float) ((int32_t) pMessage->message / 6));
iachinweze1 23:ab1cb51527d1 246 break;
iachinweze1 23:ab1cb51527d1 247 case posIn:
iachinweze1 23:ab1cb51527d1 248 pc.printf("Target Rotation set to:\t%.2f\n\r", \
iachinweze1 23:ab1cb51527d1 249 (float) ((int32_t) pMessage->message / 6));
iachinweze1 23:ab1cb51527d1 250 break;
iachinweze1 23:ab1cb51527d1 251 case posOut:
iachinweze1 23:ab1cb51527d1 252 pc.printf("Current Position:\t%.2f\n\r", \
iachinweze1 23:ab1cb51527d1 253 (float) ((int32_t) pMessage->message / 6));
iachinweze1 23:ab1cb51527d1 254 break;
iachinweze1 23:ab1cb51527d1 255 case error:
iachinweze1 23:ab1cb51527d1 256 pc.printf("\r>%s< Debugging position:%x\n\r", inCharQ, pMessage->message);
iachinweze1 23:ab1cb51527d1 257 for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer
iachinweze1 23:ab1cb51527d1 258 inCharQ[i] = ' ';
iachinweze1 23:ab1cb51527d1 259 break;
iachinweze1 23:ab1cb51527d1 260 default:
iachinweze1 23:ab1cb51527d1 261 pc.printf("Unknown Error. Message: %x\n\r", pMessage->message);
iachinweze1 23:ab1cb51527d1 262 break;
CallumAlder 19:805c87360b55 263 }
iachinweze1 23:ab1cb51527d1 264 mailStack.free(pMessage);
iachinweze1 23:ab1cb51527d1 265 }
iachinweze1 23:ab1cb51527d1 266 }
adehadd 20:c60f4785b556 267
iachinweze1 23:ab1cb51527d1 268 // attach_us -> runs funtion every 100ms
iachinweze1 23:ab1cb51527d1 269 void motorCtrlFn() {
iachinweze1 23:ab1cb51527d1 270 Ticker motorCtrlTicker;
iachinweze1 23:ab1cb51527d1 271 motorCtrlTicker.attach_us(callback(this,&Comm::motorCtrlTick), 1e5);
CallumAlder 28:4f02ac845e5d 272 while (1) {
iachinweze1 23:ab1cb51527d1 273 t_motor_ctrl.signal_wait((int32_t)0x1);
CallumAlder 28:4f02ac845e5d 274 pc.printf("B4115");
iachinweze1 23:ab1cb51527d1 275 }
iachinweze1 23:ab1cb51527d1 276 }
adehadd 20:c60f4785b556 277
iachinweze1 23:ab1cb51527d1 278 void motorCtrlTick(){
iachinweze1 23:ab1cb51527d1 279 t_motor_ctrl.signal_set(0x1);
iachinweze1 23:ab1cb51527d1 280 }
adehadd 20:c60f4785b556 281
adehadd 20:c60f4785b556 282
iachinweze1 23:ab1cb51527d1 283 //TODO: stop function, maybe use parent de-constructor
iachinweze1 23:ab1cb51527d1 284 //void stop_comm{}
iachinweze1 23:ab1cb51527d1 285
iachinweze1 23:ab1cb51527d1 286 // public:
iachinweze1 23:ab1cb51527d1 287
iachinweze1 23:ab1cb51527d1 288 volatile uint64_t newKey; // hash key
iachinweze1 23:ab1cb51527d1 289 Mutex newKey_mutex; // Restrict access to prevent deadlock.
adehadd 20:c60f4785b556 290
iachinweze1 23:ab1cb51527d1 291 Comm() : pc(SERIAL_TX, SERIAL_RX),
iachinweze1 23:ab1cb51527d1 292 t_comm_out(osPriorityAboveNormal, 1024),
CallumAlder 28:4f02ac845e5d 293 t_motor_ctrl(osPriorityAboveNormal, 1024)
iachinweze1 23:ab1cb51527d1 294 { // inherit from the RawSerial constructor
adehadd 20:c60f4785b556 295
iachinweze1 23:ab1cb51527d1 296 pc.printf("%s\n\r", "Welcome" );
iachinweze1 23:ab1cb51527d1 297 MAXCMDLENGTH = 18;
adehadd 20:c60f4785b556 298
iachinweze1 23:ab1cb51527d1 299 // reset buffer
iachinweze1 23:ab1cb51527d1 300 // MbedOS prints 'Embedded Systems are fun and do awesome things!'
iachinweze1 23:ab1cb51527d1 301 // if you print a null terminator
iachinweze1 23:ab1cb51527d1 302 pc.putc('>');
iachinweze1 23:ab1cb51527d1 303 for (int i = 0; i < MAXCMDLENGTH; ++i) {
iachinweze1 23:ab1cb51527d1 304 inCharQ[i] = '.';
iachinweze1 23:ab1cb51527d1 305 pc.putc('.');
CallumAlder 19:805c87360b55 306 }
iachinweze1 23:ab1cb51527d1 307 pc.putc('<');
iachinweze1 23:ab1cb51527d1 308 pc.putc('\r');
iachinweze1 23:ab1cb51527d1 309
iachinweze1 23:ab1cb51527d1 310 inCharQ[MAXCMDLENGTH] = '\0';
iachinweze1 23:ab1cb51527d1 311 strncpy(newCmd, inCharQ, MAXCMDLENGTH);
iachinweze1 23:ab1cb51527d1 312
iachinweze1 23:ab1cb51527d1 313 cmdIndx = 0;
iachinweze1 23:ab1cb51527d1 314
iachinweze1 23:ab1cb51527d1 315 inCharQIdx = 0;
iachinweze1 23:ab1cb51527d1 316 // inCharQIdx = MAXCMDLENGTH-1;
iachinweze1 23:ab1cb51527d1 317
iachinweze1 23:ab1cb51527d1 318
iachinweze1 23:ab1cb51527d1 319
iachinweze1 23:ab1cb51527d1 320 pc.attach(callback(this, &Comm::serialISR));
iachinweze1 23:ab1cb51527d1 321
iachinweze1 23:ab1cb51527d1 322 // Thread t_comm_in(osPriorityAboveNormal, 1024);
iachinweze1 23:ab1cb51527d1 323 // Thread t_comm_out(osPriorityAboveNormal, 1024);
iachinweze1 23:ab1cb51527d1 324 // Thread t_motor_ctrl(osPriorityAboveNormal, 1024);
iachinweze1 23:ab1cb51527d1 325
iachinweze1 23:ab1cb51527d1 326 motorPower = 300;
iachinweze1 23:ab1cb51527d1 327 targetVel = 45.0;
iachinweze1 23:ab1cb51527d1 328 targetRot = 459.0;
iachinweze1 23:ab1cb51527d1 329
adehadd 20:c60f4785b556 330
CallumAlder 19:805c87360b55 331
iachinweze1 23:ab1cb51527d1 332 /*MsgChar = {'m', 'R', 'V', 'r', 'v',
iachinweze1 23:ab1cb51527d1 333
iachinweze1 23:ab1cb51527d1 334 'h', 'K', 'n',
iachinweze1 23:ab1cb51527d1 335
iachinweze1 23:ab1cb51527d1 336 'T', 'r',
iachinweze1 23:ab1cb51527d1 337
iachinweze1 23:ab1cb51527d1 338 'e'};*/
iachinweze1 23:ab1cb51527d1 339 }
iachinweze1 23:ab1cb51527d1 340
iachinweze1 23:ab1cb51527d1 341
iachinweze1 23:ab1cb51527d1 342 void putMessage(msgType type, uint32_t message){
iachinweze1 23:ab1cb51527d1 343 msg *p_msg = mailStack.alloc();
iachinweze1 23:ab1cb51527d1 344 p_msg->type = type;
iachinweze1 23:ab1cb51527d1 345 p_msg->message = message;
iachinweze1 23:ab1cb51527d1 346 mailStack.put(p_msg);
iachinweze1 23:ab1cb51527d1 347 }
iachinweze1 23:ab1cb51527d1 348
iachinweze1 23:ab1cb51527d1 349 void start_comm(){
iachinweze1 23:ab1cb51527d1 350 _RUN = true;
iachinweze1 23:ab1cb51527d1 351
adehadd 20:c60f4785b556 352
iachinweze1 23:ab1cb51527d1 353 // reset buffer
iachinweze1 23:ab1cb51527d1 354 // MbedOS prints 'Embedded Systems are fun and do awesome things!'
iachinweze1 23:ab1cb51527d1 355 // if you print a null terminator
iachinweze1 23:ab1cb51527d1 356 pc.putc('>');
iachinweze1 23:ab1cb51527d1 357 for (int i = 0; i < MAXCMDLENGTH; ++i) {
iachinweze1 23:ab1cb51527d1 358 inCharQ[i] = '.';
iachinweze1 23:ab1cb51527d1 359 pc.putc('.');
iachinweze1 23:ab1cb51527d1 360 }
iachinweze1 23:ab1cb51527d1 361 pc.putc('<');
iachinweze1 23:ab1cb51527d1 362 pc.putc('\r');
iachinweze1 23:ab1cb51527d1 363
iachinweze1 23:ab1cb51527d1 364 inCharQ[MAXCMDLENGTH] = '\0';
iachinweze1 23:ab1cb51527d1 365 strncpy(newCmd, inCharQ, MAXCMDLENGTH);
adehadd 20:c60f4785b556 366
iachinweze1 23:ab1cb51527d1 367 // returnCursor();
adehadd 20:c60f4785b556 368
iachinweze1 23:ab1cb51527d1 369 // t_comm_in.start(callback(this, &Comm::commInFn));
iachinweze1 23:ab1cb51527d1 370 // this::thread::wait()
iachinweze1 23:ab1cb51527d1 371 // wait(1.0);
iachinweze1 23:ab1cb51527d1 372 t_comm_out.start(callback(this, &Comm::commOutFn));
iachinweze1 23:ab1cb51527d1 373 t_motor_ctrl.start(callback(this, &Comm::motorCtrlFn));
iachinweze1 23:ab1cb51527d1 374
iachinweze1 23:ab1cb51527d1 375
iachinweze1 23:ab1cb51527d1 376 }
iachinweze1 23:ab1cb51527d1 377
iachinweze1 23:ab1cb51527d1 378 char newCmd[]; // because unallocated must be defined at the bottom of the class
iachinweze1 23:ab1cb51527d1 379 char inCharQ[];
CallumAlder 19:805c87360b55 380 };
CallumAlder 19:805c87360b55 381
iachinweze1 12:41b3112021a3 382
adehadd 20:c60f4785b556 383
CallumAlder 28:4f02ac845e5d 384 class Motor{
CallumAlder 28:4f02ac845e5d 385
CallumAlder 28:4f02ac845e5d 386
CallumAlder 28:4f02ac845e5d 387 protected:
CallumAlder 28:4f02ac845e5d 388 int8_t orState; //Rotot offset at motor state 0
CallumAlder 28:4f02ac845e5d 389 int8_t currentState; //Rotot offset at motor state 0
CallumAlder 28:4f02ac845e5d 390 int8_t stateList[6]; //Rotot offset at motor state 0
CallumAlder 28:4f02ac845e5d 391 //Run the motor synchronisation
CallumAlder 28:4f02ac845e5d 392
CallumAlder 28:4f02ac845e5d 393 int8_t* params[2];
iachinweze1 23:ab1cb51527d1 394
CallumAlder 29:c96439a60184 395 float dutyC; // 1 = 100%
CallumAlder 29:c96439a60184 396 float mtrPeriod; // motor period
CallumAlder 29:c96439a60184 397 int stateCount;
CallumAlder 28:4f02ac845e5d 398
CallumAlder 28:4f02ac845e5d 399 public:
CallumAlder 28:4f02ac845e5d 400
CallumAlder 28:4f02ac845e5d 401 Motor(){
CallumAlder 28:4f02ac845e5d 402
CallumAlder 28:4f02ac845e5d 403 orState = motorHome();; //Rotot offset at motor state 0
CallumAlder 28:4f02ac845e5d 404 currentState = readRotorState();; //Rotot offset at motor state 0
CallumAlder 28:4f02ac845e5d 405 stateList[6]; //Rotot offset at motor state 0
CallumAlder 28:4f02ac845e5d 406
CallumAlder 28:4f02ac845e5d 407 motorOut((currentState-orState+lead+6)%6); // We push it digitally
CallumAlder 28:4f02ac845e5d 408
CallumAlder 28:4f02ac845e5d 409 dutyC = 0.8;
CallumAlder 29:c96439a60184 410 mtrPeriod = 2e-3; // motor period
CallumAlder 28:4f02ac845e5d 411 pwmCtrl.pulsewidth(mtrPeriod*dutyC);
CallumAlder 28:4f02ac845e5d 412
CallumAlder 28:4f02ac845e5d 413 params[0] = &currentState;
CallumAlder 28:4f02ac845e5d 414 params[1] = &orState;
iachinweze1 23:ab1cb51527d1 415
CallumAlder 29:c96439a60184 416 I1.fall(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 417 I2.fall(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 418 I3.fall(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 419 I1.rise(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 420 I2.rise(callback(this, &Motor::stateUpdate));
CallumAlder 29:c96439a60184 421 I3.rise(callback(this, &Motor::stateUpdate));
CallumAlder 28:4f02ac845e5d 422
CallumAlder 28:4f02ac845e5d 423 pwmCtrl.period(mtrPeriod);
CallumAlder 28:4f02ac845e5d 424 pwmCtrl.pulsewidth(mtrPeriod*dutyC);
CallumAlder 29:c96439a60184 425
CallumAlder 29:c96439a60184 426 stateCount = 0;
CallumAlder 28:4f02ac845e5d 427
CallumAlder 28:4f02ac845e5d 428 }
CallumAlder 28:4f02ac845e5d 429
CallumAlder 28:4f02ac845e5d 430
CallumAlder 28:4f02ac845e5d 431 //Set a given drive state
CallumAlder 28:4f02ac845e5d 432 void motorOut(int8_t driveState){
CallumAlder 28:4f02ac845e5d 433
CallumAlder 28:4f02ac845e5d 434 //Lookup the output byte from the drive state.
CallumAlder 28:4f02ac845e5d 435 int8_t driveOut = driveTable[driveState & 0x07];
CallumAlder 28:4f02ac845e5d 436
CallumAlder 28:4f02ac845e5d 437 //Turn off first
CallumAlder 28:4f02ac845e5d 438 if (~driveOut & 0x01) L1L = 0;
CallumAlder 28:4f02ac845e5d 439 if (~driveOut & 0x02) L1H = 1;
CallumAlder 28:4f02ac845e5d 440 if (~driveOut & 0x04) L2L = 0;
CallumAlder 28:4f02ac845e5d 441 if (~driveOut & 0x08) L2H = 1;
CallumAlder 28:4f02ac845e5d 442 if (~driveOut & 0x10) L3L = 0;
CallumAlder 28:4f02ac845e5d 443 if (~driveOut & 0x20) L3H = 1;
iachinweze1 23:ab1cb51527d1 444
CallumAlder 28:4f02ac845e5d 445 //Then turn on
CallumAlder 28:4f02ac845e5d 446 if (driveOut & 0x01) L1L = 1;
CallumAlder 28:4f02ac845e5d 447 if (driveOut & 0x02) L1H = 0;
CallumAlder 28:4f02ac845e5d 448 if (driveOut & 0x04) L2L = 1;
CallumAlder 28:4f02ac845e5d 449 if (driveOut & 0x08) L2H = 0;
CallumAlder 28:4f02ac845e5d 450 if (driveOut & 0x10) L3L = 1;
CallumAlder 28:4f02ac845e5d 451 if (driveOut & 0x20) L3H = 0;
CallumAlder 28:4f02ac845e5d 452 }
CallumAlder 28:4f02ac845e5d 453
CallumAlder 28:4f02ac845e5d 454 //Convert photointerrupter inputs to a rotor state
CallumAlder 28:4f02ac845e5d 455 inline int8_t readRotorState(){
CallumAlder 28:4f02ac845e5d 456 return stateMap[I1 + 2*I2 + 4*I3];
CallumAlder 28:4f02ac845e5d 457 }
adehadd 20:c60f4785b556 458
CallumAlder 28:4f02ac845e5d 459 //Basic synchronisation routine
CallumAlder 28:4f02ac845e5d 460 int8_t motorHome() {
CallumAlder 28:4f02ac845e5d 461 //Put the motor in drive state 0 and wait for it to stabilise
CallumAlder 28:4f02ac845e5d 462 motorOut(0);
CallumAlder 28:4f02ac845e5d 463 wait(2.0);
CallumAlder 28:4f02ac845e5d 464
CallumAlder 28:4f02ac845e5d 465 //Get the rotor state
CallumAlder 28:4f02ac845e5d 466 return readRotorState();
CallumAlder 28:4f02ac845e5d 467 }
iachinweze1 23:ab1cb51527d1 468
CallumAlder 28:4f02ac845e5d 469
CallumAlder 29:c96439a60184 470 void stateUpdate() { // () { // **params
CallumAlder 28:4f02ac845e5d 471 *params[0] = readRotorState();
CallumAlder 28:4f02ac845e5d 472 int8_t currentState = *params[0];
CallumAlder 28:4f02ac845e5d 473 int8_t offset = *params[1];
CallumAlder 28:4f02ac845e5d 474
CallumAlder 28:4f02ac845e5d 475 motorOut((currentState - offset + lead + 6) % 6);
CallumAlder 29:c96439a60184 476
CallumAlder 29:c96439a60184 477 if (stateCount<6){
CallumAlder 29:c96439a60184 478 stateList[stateCount] = currentState;
CallumAlder 29:c96439a60184 479 stateCount++;
CallumAlder 29:c96439a60184 480 }
CallumAlder 29:c96439a60184 481 else {
CallumAlder 29:c96439a60184 482 //pc.printf("states");
CallumAlder 29:c96439a60184 483 //for(int i = 0; i < 6; ++i)
CallumAlder 29:c96439a60184 484 //pc.printf("%02i,", stateList[i]);
CallumAlder 29:c96439a60184 485 //pc.printf("\n\r");
CallumAlder 29:c96439a60184 486 stateCount = 0;
CallumAlder 29:c96439a60184 487 }
CallumAlder 29:c96439a60184 488
CallumAlder 29:c96439a60184 489
CallumAlder 28:4f02ac845e5d 490 }
CallumAlder 28:4f02ac845e5d 491
CallumAlder 29:c96439a60184 492 };
adehadd 20:c60f4785b556 493
adehadd 20:c60f4785b556 494
adehadd 20:c60f4785b556 495
estott 0:de4320f74764 496 //Main
estott 0:de4320f74764 497 int main() {
CallumAlder 19:805c87360b55 498
iachinweze1 23:ab1cb51527d1 499 // std::ios::sync_with_stdio(false);
iachinweze1 23:ab1cb51527d1 500 Comm comm_plz;
adehadd 20:c60f4785b556 501
adehadd 20:c60f4785b556 502 // comm_plz.pc.printf("%s\n", "do i work bruh" ); // using printf of class is calm
adehadd 20:c60f4785b556 503 SHA256 Miner;
iachinweze1 23:ab1cb51527d1 504
CallumAlder 28:4f02ac845e5d 505 Motor motor;
CallumAlder 28:4f02ac845e5d 506
CallumAlder 14:4e312fb83330 507 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
CallumAlder 14:4e312fb83330 508 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
CallumAlder 14:4e312fb83330 509 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
CallumAlder 14:4e312fb83330 510 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
CallumAlder 14:4e312fb83330 511 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
CallumAlder 14:4e312fb83330 512 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
CallumAlder 14:4e312fb83330 513 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
CallumAlder 14:4e312fb83330 514 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
CallumAlder 14:4e312fb83330 515 uint64_t* key = (uint64_t*)((int)sequence + 48);
CallumAlder 14:4e312fb83330 516 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
CallumAlder 14:4e312fb83330 517 uint8_t hash[32];
CallumAlder 14:4e312fb83330 518 uint32_t length64 = 64;
CallumAlder 15:2f95f2fb68e3 519 uint32_t hashCounter = 0;
iachinweze1 23:ab1cb51527d1 520 Timer timer;
iachinweze1 23:ab1cb51527d1 521
adehadd 20:c60f4785b556 522 comm_plz.start_comm();
adehadd 20:c60f4785b556 523
adehadd 16:db7ef0a4aa23 524 // Motor States
iachinweze1 23:ab1cb51527d1 525
adehadd 20:c60f4785b556 526 // pc.printf("Rotor origin: %x\n\r",orState);
adehadd 20:c60f4785b556 527 // orState is subtracted from future rotor state inputs to align rotor and motor states
adehadd 20:c60f4785b556 528 // intState = readRotorState();
adehadd 20:c60f4785b556 529 //if (intState != intStateOld) {
iachinweze1 23:ab1cb51527d1 530 // pc.printf("old:%d \t new:%d \t next:%d \n\r",intStateOld, intState, (intState-orState+lead+6)%6);
iachinweze1 23:ab1cb51527d1 531 // intStateOld = intState;
iachinweze1 23:ab1cb51527d1 532 // motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
iachinweze1 23:ab1cb51527d1 533 // }
iachinweze1 23:ab1cb51527d1 534
iachinweze1 12:41b3112021a3 535 // Keep the program running indefinitely
CallumAlder 15:2f95f2fb68e3 536 timer.start(); // start timer
CallumAlder 29:c96439a60184 537
CallumAlder 15:2f95f2fb68e3 538 while (1) {
iachinweze1 23:ab1cb51527d1 539 // pc.printf("Current:%d \t Next:%d \n\r", currentState, (currentState-orState+lead+6)%6);
iachinweze1 23:ab1cb51527d1 540 comm_plz.newKey_mutex.lock();
adehadd 20:c60f4785b556 541 *key = comm_plz.newKey;
iachinweze1 23:ab1cb51527d1 542 comm_plz.newKey_mutex.unlock();
CallumAlder 15:2f95f2fb68e3 543 Miner.computeHash(hash, sequence, length64);
CallumAlder 15:2f95f2fb68e3 544 hashCounter++;
CallumAlder 15:2f95f2fb68e3 545 if ((hash[0]==0) && (hash[1]==0)){
adehadd 20:c60f4785b556 546 comm_plz.putMessage((Comm::msgType)7, *nonce);
CallumAlder 15:2f95f2fb68e3 547 }
CallumAlder 15:2f95f2fb68e3 548
adehadd 20:c60f4785b556 549 // Try a new nonce
CallumAlder 15:2f95f2fb68e3 550 (*nonce)++;
CallumAlder 15:2f95f2fb68e3 551
adehadd 20:c60f4785b556 552 // Per Second i.e. when greater or equal to 1
CallumAlder 15:2f95f2fb68e3 553 if (timer.read() >= 1){
adehadd 20:c60f4785b556 554 comm_plz.putMessage((Comm::msgType)5, hashCounter);
CallumAlder 19:805c87360b55 555 //pc.printf("HashRate = %02u \n\r",hashCounter);
CallumAlder 15:2f95f2fb68e3 556 hashCounter=0;
CallumAlder 15:2f95f2fb68e3 557 timer.reset();
CallumAlder 15:2f95f2fb68e3 558 }
CallumAlder 15:2f95f2fb68e3 559 }
CallumAlder 19:805c87360b55 560 }