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

Dependencies:   Crypto

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