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

Dependencies:   Crypto

Committer:
CallumAlder
Date:
Sat Mar 16 18:16:39 2019 +0000
Revision:
28:4f02ac845e5d
Parent:
24:be5fef3dace1
Child:
29:c96439a60184
Motor Class

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