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

Dependencies:   Crypto

Committer:
adehadd
Date:
Sat Mar 16 17:39:22 2019 +0000
Revision:
25:995865498aee
Parent:
24:be5fef3dace1
Child:
26:fb6151e5907d
Lab 3 up to controlling motor speed

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 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
iachinweze1 23:ab1cb51527d1 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);
iachinweze1 23:ab1cb51527d1 75
iachinweze1 24:be5fef3dace1 76 uint8_t stateCount[3];
iachinweze1 24:be5fef3dace1 77 uint8_t theStates[3];
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);
iachinweze1 24:be5fef3dace1 272 uint8_t cpyStateCount[3];
iachinweze1 24:be5fef3dace1 273 uint8_t cpyCurrentState;
iachinweze1 24:be5fef3dace1 274 while (_RUN) {
iachinweze1 23:ab1cb51527d1 275 t_motor_ctrl.signal_wait((int32_t)0x1);
iachinweze1 24:be5fef3dace1 276 core_util_critical_section_enter();
iachinweze1 24:be5fef3dace1 277 //Access shared variables here
iachinweze1 24:be5fef3dace1 278 std::copy(stateCount, stateCount+3, cpyStateCount);
adehadd 25:995865498aee 279 // TODO: A thing yes
iachinweze1 24:be5fef3dace1 280 cpyCurrentState = 0;
iachinweze1 24:be5fef3dace1 281 for (int i = 0; i < 3; ++i) {
iachinweze1 24:be5fef3dace1 282 stateCount[i] = 0;
iachinweze1 24:be5fef3dace1 283 }
iachinweze1 24:be5fef3dace1 284 core_util_critical_section_exit();
iachinweze1 24:be5fef3dace1 285
iachinweze1 24:be5fef3dace1 286 uint8_t iterElementMax = std::max_element(cpyStateCount, cpyStateCount+3) - cpyStateCount;
iachinweze1 24:be5fef3dace1 287
adehadd 25:995865498aee 288 int16_t ting[2] = {5,1}; // 360,60 (for degrees), 5,1 (for states)
adehadd 25:995865498aee 289 int16_t totalDegrees = ting[0] * cpyStateCount[iterElementMax];
iachinweze1 24:be5fef3dace1 290 int16_t stateDiff = theStates[iterElementMax]-cpyCurrentState;
iachinweze1 24:be5fef3dace1 291 if (stateDiff >= 0) {
adehadd 25:995865498aee 292 totalDegrees = totalDegrees + (ting[1]* stateDiff);
iachinweze1 24:be5fef3dace1 293 } else {
adehadd 25:995865498aee 294 totalDegrees = totalDegrees + (ting[1]*stateDiff*-1);
iachinweze1 24:be5fef3dace1 295 }
iachinweze1 24:be5fef3dace1 296 pc.printf("%u,%u,%u,%u. %.6i \r", iterElementMax, cpyStateCount[0],cpyStateCount[1],cpyStateCount[2], (totalDegrees*10));
adehadd 25:995865498aee 297
adehadd 25:995865498aee 298 //~~~~~Speed controller~~~~~~
adehadd 25:995865498aee 299 /*
adehadd 25:995865498aee 300 sError = (targetVel * 6) - abs(totalDegrees)*10; //Read global variable targetVel updated by interrupt and calculate error between target and reality
adehadd 25:995865498aee 301 int32_t Ys; //Initialise controller output Ys
adehadd 25:995865498aee 302 if (sError == -abs(velocity)) { //Check if user entered V0,
adehadd 25:995865498aee 303 Ys = MAXPWM; //and set the output to maximum as specified
adehadd 25:995865498aee 304 } else {
adehadd 25:995865498aee 305 Ys = (int)(Kp1 * sError); //If the user didn't enter V0 implement controller transfer function: Ys = Kp * (s -|v|) where,
adehadd 25:995865498aee 306 }
adehadd 25:995865498aee 307 */
iachinweze1 23:ab1cb51527d1 308 }
adehadd 25:995865498aee 309
iachinweze1 23:ab1cb51527d1 310 }
adehadd 20:c60f4785b556 311
iachinweze1 23:ab1cb51527d1 312 void motorCtrlTick(){
iachinweze1 23:ab1cb51527d1 313 t_motor_ctrl.signal_set(0x1);
iachinweze1 23:ab1cb51527d1 314 }
adehadd 20:c60f4785b556 315
adehadd 20:c60f4785b556 316
iachinweze1 23:ab1cb51527d1 317 //TODO: stop function, maybe use parent de-constructor
iachinweze1 23:ab1cb51527d1 318 //void stop_comm{}
iachinweze1 23:ab1cb51527d1 319
iachinweze1 23:ab1cb51527d1 320 // public:
iachinweze1 23:ab1cb51527d1 321
iachinweze1 23:ab1cb51527d1 322 volatile uint64_t newKey; // hash key
iachinweze1 23:ab1cb51527d1 323 Mutex newKey_mutex; // Restrict access to prevent deadlock.
adehadd 20:c60f4785b556 324
iachinweze1 23:ab1cb51527d1 325 Comm() : pc(SERIAL_TX, SERIAL_RX),
iachinweze1 23:ab1cb51527d1 326 t_comm_out(osPriorityAboveNormal, 1024),
iachinweze1 24:be5fef3dace1 327 t_motor_ctrl(osPriorityAboveNormal2, 1024)
iachinweze1 23:ab1cb51527d1 328 { // inherit from the RawSerial constructor
adehadd 20:c60f4785b556 329
iachinweze1 23:ab1cb51527d1 330 pc.printf("%s\n\r", "Welcome" );
iachinweze1 23:ab1cb51527d1 331 MAXCMDLENGTH = 18;
adehadd 20:c60f4785b556 332
iachinweze1 23:ab1cb51527d1 333 // reset buffer
iachinweze1 23:ab1cb51527d1 334 // MbedOS prints 'Embedded Systems are fun and do awesome things!'
iachinweze1 23:ab1cb51527d1 335 // if you print a null terminator
iachinweze1 23:ab1cb51527d1 336 pc.putc('>');
iachinweze1 23:ab1cb51527d1 337 for (int i = 0; i < MAXCMDLENGTH; ++i) {
iachinweze1 23:ab1cb51527d1 338 inCharQ[i] = '.';
iachinweze1 23:ab1cb51527d1 339 pc.putc('.');
CallumAlder 19:805c87360b55 340 }
iachinweze1 23:ab1cb51527d1 341 pc.putc('<');
iachinweze1 23:ab1cb51527d1 342 pc.putc('\r');
iachinweze1 23:ab1cb51527d1 343
iachinweze1 23:ab1cb51527d1 344 inCharQ[MAXCMDLENGTH] = '\0';
iachinweze1 23:ab1cb51527d1 345 strncpy(newCmd, inCharQ, MAXCMDLENGTH);
iachinweze1 23:ab1cb51527d1 346
iachinweze1 23:ab1cb51527d1 347 cmdIndx = 0;
iachinweze1 23:ab1cb51527d1 348
iachinweze1 23:ab1cb51527d1 349 inCharQIdx = 0;
iachinweze1 23:ab1cb51527d1 350 // inCharQIdx = MAXCMDLENGTH-1;
iachinweze1 23:ab1cb51527d1 351
iachinweze1 23:ab1cb51527d1 352
iachinweze1 23:ab1cb51527d1 353
iachinweze1 23:ab1cb51527d1 354 pc.attach(callback(this, &Comm::serialISR));
iachinweze1 23:ab1cb51527d1 355
iachinweze1 23:ab1cb51527d1 356 // Thread t_comm_in(osPriorityAboveNormal, 1024);
iachinweze1 23:ab1cb51527d1 357 // Thread t_comm_out(osPriorityAboveNormal, 1024);
iachinweze1 23:ab1cb51527d1 358 // Thread t_motor_ctrl(osPriorityAboveNormal, 1024);
iachinweze1 23:ab1cb51527d1 359
iachinweze1 23:ab1cb51527d1 360 motorPower = 300;
iachinweze1 23:ab1cb51527d1 361 targetVel = 45.0;
iachinweze1 23:ab1cb51527d1 362 targetRot = 459.0;
iachinweze1 23:ab1cb51527d1 363
adehadd 20:c60f4785b556 364
CallumAlder 19:805c87360b55 365
iachinweze1 23:ab1cb51527d1 366 /*MsgChar = {'m', 'R', 'V', 'r', 'v',
iachinweze1 23:ab1cb51527d1 367
iachinweze1 23:ab1cb51527d1 368 'h', 'K', 'n',
iachinweze1 23:ab1cb51527d1 369
iachinweze1 23:ab1cb51527d1 370 'T', 'r',
iachinweze1 23:ab1cb51527d1 371
iachinweze1 23:ab1cb51527d1 372 'e'};*/
iachinweze1 23:ab1cb51527d1 373 }
iachinweze1 23:ab1cb51527d1 374
iachinweze1 23:ab1cb51527d1 375
iachinweze1 23:ab1cb51527d1 376 void putMessage(msgType type, uint32_t message){
iachinweze1 23:ab1cb51527d1 377 msg *p_msg = mailStack.alloc();
iachinweze1 23:ab1cb51527d1 378 p_msg->type = type;
iachinweze1 23:ab1cb51527d1 379 p_msg->message = message;
iachinweze1 23:ab1cb51527d1 380 mailStack.put(p_msg);
iachinweze1 23:ab1cb51527d1 381 }
iachinweze1 23:ab1cb51527d1 382
iachinweze1 23:ab1cb51527d1 383 void start_comm(){
iachinweze1 23:ab1cb51527d1 384 _RUN = true;
iachinweze1 23:ab1cb51527d1 385
adehadd 20:c60f4785b556 386
iachinweze1 23:ab1cb51527d1 387 // reset buffer
iachinweze1 23:ab1cb51527d1 388 // MbedOS prints 'Embedded Systems are fun and do awesome things!'
iachinweze1 23:ab1cb51527d1 389 // if you print a null terminator
iachinweze1 23:ab1cb51527d1 390 pc.putc('>');
iachinweze1 23:ab1cb51527d1 391 for (int i = 0; i < MAXCMDLENGTH; ++i) {
iachinweze1 23:ab1cb51527d1 392 inCharQ[i] = '.';
iachinweze1 23:ab1cb51527d1 393 pc.putc('.');
iachinweze1 23:ab1cb51527d1 394 }
iachinweze1 23:ab1cb51527d1 395 pc.putc('<');
iachinweze1 23:ab1cb51527d1 396 pc.putc('\r');
iachinweze1 23:ab1cb51527d1 397
iachinweze1 23:ab1cb51527d1 398 inCharQ[MAXCMDLENGTH] = '\0';
iachinweze1 23:ab1cb51527d1 399 strncpy(newCmd, inCharQ, MAXCMDLENGTH);
adehadd 20:c60f4785b556 400
iachinweze1 23:ab1cb51527d1 401 // returnCursor();
adehadd 20:c60f4785b556 402
iachinweze1 23:ab1cb51527d1 403 // t_comm_in.start(callback(this, &Comm::commInFn));
iachinweze1 23:ab1cb51527d1 404 // this::thread::wait()
iachinweze1 23:ab1cb51527d1 405 // wait(1.0);
iachinweze1 23:ab1cb51527d1 406 t_comm_out.start(callback(this, &Comm::commOutFn));
iachinweze1 23:ab1cb51527d1 407 t_motor_ctrl.start(callback(this, &Comm::motorCtrlFn));
iachinweze1 23:ab1cb51527d1 408
iachinweze1 23:ab1cb51527d1 409
iachinweze1 23:ab1cb51527d1 410 }
iachinweze1 23:ab1cb51527d1 411
iachinweze1 23:ab1cb51527d1 412 char newCmd[]; // because unallocated must be defined at the bottom of the class
iachinweze1 23:ab1cb51527d1 413 char inCharQ[];
CallumAlder 19:805c87360b55 414 };
CallumAlder 19:805c87360b55 415
iachinweze1 12:41b3112021a3 416
adehadd 20:c60f4785b556 417
adehadd 20:c60f4785b556 418 //Set a given drive state
adehadd 20:c60f4785b556 419 void motorOut(int8_t driveState){
iachinweze1 23:ab1cb51527d1 420
adehadd 20:c60f4785b556 421 //Lookup the output byte from the drive state.
adehadd 20:c60f4785b556 422 int8_t driveOut = driveTable[driveState & 0x07];
iachinweze1 23:ab1cb51527d1 423
adehadd 20:c60f4785b556 424 //Turn off first
adehadd 20:c60f4785b556 425 if (~driveOut & 0x01) L1L = 0;
adehadd 20:c60f4785b556 426 if (~driveOut & 0x02) L1H = 1;
adehadd 20:c60f4785b556 427 if (~driveOut & 0x04) L2L = 0;
adehadd 20:c60f4785b556 428 if (~driveOut & 0x08) L2H = 1;
adehadd 20:c60f4785b556 429 if (~driveOut & 0x10) L3L = 0;
adehadd 20:c60f4785b556 430 if (~driveOut & 0x20) L3H = 1;
iachinweze1 23:ab1cb51527d1 431
adehadd 20:c60f4785b556 432 //Then turn on
adehadd 20:c60f4785b556 433 if (driveOut & 0x01) L1L = 1;
adehadd 20:c60f4785b556 434 if (driveOut & 0x02) L1H = 0;
adehadd 20:c60f4785b556 435 if (driveOut & 0x04) L2L = 1;
adehadd 20:c60f4785b556 436 if (driveOut & 0x08) L2H = 0;
adehadd 20:c60f4785b556 437 if (driveOut & 0x10) L3L = 1;
adehadd 20:c60f4785b556 438 if (driveOut & 0x20) L3H = 0;
iachinweze1 23:ab1cb51527d1 439 }
iachinweze1 23:ab1cb51527d1 440
iachinweze1 23:ab1cb51527d1 441 //Convert photointerrupter inputs to a rotor state
adehadd 20:c60f4785b556 442 inline int8_t readRotorState(){
adehadd 20:c60f4785b556 443 return stateMap[I1 + 2*I2 + 4*I3];
iachinweze1 23:ab1cb51527d1 444 }
adehadd 20:c60f4785b556 445
iachinweze1 23:ab1cb51527d1 446 //Basic synchronisation routine
adehadd 20:c60f4785b556 447 int8_t motorHome() {
adehadd 20:c60f4785b556 448 //Put the motor in drive state 0 and wait for it to stabilise
adehadd 20:c60f4785b556 449 motorOut(0);
adehadd 20:c60f4785b556 450 wait(2.0);
iachinweze1 23:ab1cb51527d1 451
adehadd 20:c60f4785b556 452 //Get the rotor state
adehadd 20:c60f4785b556 453 return readRotorState();
adehadd 20:c60f4785b556 454 }
adehadd 20:c60f4785b556 455
adehadd 20:c60f4785b556 456
adehadd 20:c60f4785b556 457 void stateUpdate(int8_t *params[]) { // () { // **params
iachinweze1 23:ab1cb51527d1 458 *params[0] = readRotorState();
adehadd 20:c60f4785b556 459 int8_t currentState = *params[0];
adehadd 20:c60f4785b556 460 int8_t offset = *params[1];
iachinweze1 23:ab1cb51527d1 461
iachinweze1 24:be5fef3dace1 462 switch (currentState) {
iachinweze1 24:be5fef3dace1 463 case 1:
iachinweze1 24:be5fef3dace1 464 stateCount[0]++;
iachinweze1 24:be5fef3dace1 465 break;
iachinweze1 24:be5fef3dace1 466 case (1 + lead):
iachinweze1 24:be5fef3dace1 467 stateCount[1]++;
iachinweze1 24:be5fef3dace1 468 break;
iachinweze1 24:be5fef3dace1 469 case (1 + (lead*2)):
iachinweze1 24:be5fef3dace1 470 stateCount[2]++;
iachinweze1 24:be5fef3dace1 471 break;
iachinweze1 24:be5fef3dace1 472 }
iachinweze1 24:be5fef3dace1 473
adehadd 20:c60f4785b556 474 motorOut((currentState - offset + lead + 6) % 6);
adehadd 20:c60f4785b556 475 }
adehadd 20:c60f4785b556 476
estott 0:de4320f74764 477 //Main
estott 0:de4320f74764 478 int main() {
CallumAlder 19:805c87360b55 479
iachinweze1 23:ab1cb51527d1 480 // std::ios::sync_with_stdio(false);
iachinweze1 23:ab1cb51527d1 481 Comm comm_plz;
adehadd 20:c60f4785b556 482
adehadd 20:c60f4785b556 483 // comm_plz.pc.printf("%s\n", "do i work bruh" ); // using printf of class is calm
adehadd 20:c60f4785b556 484 SHA256 Miner;
iachinweze1 23:ab1cb51527d1 485
CallumAlder 14:4e312fb83330 486 uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
CallumAlder 14:4e312fb83330 487 0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
CallumAlder 14:4e312fb83330 488 0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
CallumAlder 14:4e312fb83330 489 0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
CallumAlder 14:4e312fb83330 490 0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
CallumAlder 14:4e312fb83330 491 0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
CallumAlder 14:4e312fb83330 492 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
CallumAlder 14:4e312fb83330 493 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
CallumAlder 14:4e312fb83330 494 uint64_t* key = (uint64_t*)((int)sequence + 48);
CallumAlder 14:4e312fb83330 495 uint64_t* nonce = (uint64_t*)((int)sequence + 56);
CallumAlder 14:4e312fb83330 496 uint8_t hash[32];
CallumAlder 14:4e312fb83330 497 uint32_t length64 = 64;
CallumAlder 15:2f95f2fb68e3 498 uint32_t hashCounter = 0;
iachinweze1 23:ab1cb51527d1 499 Timer timer;
iachinweze1 23:ab1cb51527d1 500
adehadd 21:b296db05483d 501 float dutyC = 1; // 100%
adehadd 21:b296db05483d 502 float mtrPeriod = 2e-3; // motor period
adehadd 21:b296db05483d 503
adehadd 21:b296db05483d 504 pwmCtrl.period(mtrPeriod);
adehadd 21:b296db05483d 505 pwmCtrl.pulsewidth(mtrPeriod*dutyC);
adehadd 21:b296db05483d 506
adehadd 20:c60f4785b556 507 comm_plz.start_comm();
adehadd 20:c60f4785b556 508
adehadd 16:db7ef0a4aa23 509 // Motor States
adehadd 16:db7ef0a4aa23 510 int8_t orState = 0; //Rotot offset at motor state 0
adehadd 16:db7ef0a4aa23 511 int8_t currentState = 0; //Rotot offset at motor state 0
adehadd 18:7ee632098fd4 512 int8_t stateList[6]; //Rotot offset at motor state 0
estott 0:de4320f74764 513 //Run the motor synchronisation
adehadd 20:c60f4785b556 514 orState = motorHome();
iachinweze1 23:ab1cb51527d1 515
iachinweze1 24:be5fef3dace1 516 theStates[0] = orState;
iachinweze1 24:be5fef3dace1 517 theStates[1] = (orState + lead) % 6;
iachinweze1 24:be5fef3dace1 518 theStates[2] = (orState + (lead*2)) % 6;
iachinweze1 24:be5fef3dace1 519
CallumAlder 15:2f95f2fb68e3 520 // Add callbacks
iachinweze1 23:ab1cb51527d1 521 // I1.fall(&stateUpdate);
iachinweze1 23:ab1cb51527d1 522 // I2.fall(&stateUpdate);
iachinweze1 23:ab1cb51527d1 523 // I3.fall(&stateUpdate);
adehadd 16:db7ef0a4aa23 524 int8_t* params[2];
adehadd 16:db7ef0a4aa23 525 params[0] = &currentState;
adehadd 16:db7ef0a4aa23 526 params[1] = &orState;
iachinweze1 23:ab1cb51527d1 527
adehadd 20:c60f4785b556 528 I1.fall(callback(&stateUpdate,params));
adehadd 16:db7ef0a4aa23 529 I2.fall(callback(&stateUpdate,params));
adehadd 16:db7ef0a4aa23 530 I3.fall(callback(&stateUpdate,params));
iachinweze1 23:ab1cb51527d1 531
adehadd 18:7ee632098fd4 532 I1.rise(callback(&stateUpdate,params));
adehadd 18:7ee632098fd4 533 I2.rise(callback(&stateUpdate,params));
adehadd 18:7ee632098fd4 534 I3.rise(callback(&stateUpdate,params));
iachinweze1 23:ab1cb51527d1 535
CallumAlder 15:2f95f2fb68e3 536 // Push motor to move
iachinweze1 12:41b3112021a3 537 currentState = readRotorState();
iachinweze1 12:41b3112021a3 538 motorOut((currentState-orState+lead+6)%6); // We push it digitally
iachinweze1 23:ab1cb51527d1 539
adehadd 20:c60f4785b556 540 // pc.printf("Rotor origin: %x\n\r",orState);
adehadd 20:c60f4785b556 541 // orState is subtracted from future rotor state inputs to align rotor and motor states
adehadd 20:c60f4785b556 542 // intState = readRotorState();
adehadd 20:c60f4785b556 543 //if (intState != intStateOld) {
iachinweze1 23:ab1cb51527d1 544 // pc.printf("old:%d \t new:%d \t next:%d \n\r",intStateOld, intState, (intState-orState+lead+6)%6);
iachinweze1 23:ab1cb51527d1 545 // intStateOld = intState;
iachinweze1 23:ab1cb51527d1 546 // motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
iachinweze1 23:ab1cb51527d1 547 // }
iachinweze1 23:ab1cb51527d1 548
adehadd 21:b296db05483d 549 dutyC = 0.8;
adehadd 21:b296db05483d 550 pwmCtrl.pulsewidth(mtrPeriod*dutyC);
iachinweze1 12:41b3112021a3 551
CallumAlder 15:2f95f2fb68e3 552
iachinweze1 12:41b3112021a3 553 // Keep the program running indefinitely
CallumAlder 15:2f95f2fb68e3 554 timer.start(); // start timer
adehadd 18:7ee632098fd4 555 int stateCount = 0;
CallumAlder 15:2f95f2fb68e3 556 while (1) {
iachinweze1 23:ab1cb51527d1 557 // pc.printf("Current:%d \t Next:%d \n\r", currentState, (currentState-orState+lead+6)%6);
iachinweze1 23:ab1cb51527d1 558 comm_plz.newKey_mutex.lock();
adehadd 20:c60f4785b556 559 *key = comm_plz.newKey;
iachinweze1 23:ab1cb51527d1 560 comm_plz.newKey_mutex.unlock();
CallumAlder 15:2f95f2fb68e3 561 Miner.computeHash(hash, sequence, length64);
CallumAlder 15:2f95f2fb68e3 562 hashCounter++;
CallumAlder 15:2f95f2fb68e3 563 if ((hash[0]==0) && (hash[1]==0)){
adehadd 20:c60f4785b556 564 comm_plz.putMessage((Comm::msgType)7, *nonce);
CallumAlder 15:2f95f2fb68e3 565 }
CallumAlder 15:2f95f2fb68e3 566
adehadd 20:c60f4785b556 567 // Try a new nonce
CallumAlder 15:2f95f2fb68e3 568 (*nonce)++;
CallumAlder 15:2f95f2fb68e3 569
adehadd 18:7ee632098fd4 570 if (stateCount<6){
adehadd 18:7ee632098fd4 571 stateList[stateCount] = currentState;
adehadd 18:7ee632098fd4 572 stateCount++;
adehadd 18:7ee632098fd4 573 }
adehadd 18:7ee632098fd4 574 else {
CallumAlder 19:805c87360b55 575 //pc.printf("states");
CallumAlder 19:805c87360b55 576 //for(int i = 0; i < 6; ++i)
iachinweze1 23:ab1cb51527d1 577 //pc.printf("%02i,", stateList[i]);
iachinweze1 23:ab1cb51527d1 578 //pc.printf("\n\r");
iachinweze1 23:ab1cb51527d1 579 stateCount = 0;
adehadd 18:7ee632098fd4 580 }
adehadd 18:7ee632098fd4 581
adehadd 20:c60f4785b556 582 // Per Second i.e. when greater or equal to 1
CallumAlder 15:2f95f2fb68e3 583 if (timer.read() >= 1){
adehadd 20:c60f4785b556 584 comm_plz.putMessage((Comm::msgType)5, hashCounter);
CallumAlder 19:805c87360b55 585 //pc.printf("HashRate = %02u \n\r",hashCounter);
CallumAlder 15:2f95f2fb68e3 586 hashCounter=0;
CallumAlder 15:2f95f2fb68e3 587 timer.reset();
CallumAlder 15:2f95f2fb68e3 588 }
CallumAlder 15:2f95f2fb68e3 589 }
CallumAlder 19:805c87360b55 590 }