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

Dependencies:   Crypto

Files at this revision

API Documentation at this revision

Comitter:
adehadd
Date:
Sat Mar 16 18:25:38 2019 +0000
Parent:
26:fb6151e5907d
Child:
42:121148278dae
Commit message:
oh dear

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Mar 16 18:19:08 2019 +0000
+++ b/main.cpp	Sat Mar 16 18:25:38 2019 +0000
@@ -1,54 +1,37 @@
-//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~INCLUDES~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#include "SHA256.h"
 #include "mbed.h"
-#include "Crypto.h" // Library used for Bitcoin mining.
-#include "rtos.h"   // Real time operating system library for threads etc.
-
-
-
-
-//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~DEFINITIONS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// #include <iostream>
+// #include "rtos.h"
 
-//~~~~~~~~~~~~~~Photointerrupter pins~~~~~~~~~~~~~~
-#define I1pin D2
-#define I2pin D11
-#define I3pin D12
+/*TODO:
+Change
+Indx
+newCmd
+MAXCMDLENGTH
+*/
 
-
-////~~~~~~~~~~Incremental encoder pins//~~~~~~~~~~~
-#define CHA   D7
-#define CHB   D8  
-
+//Photointerrupter input pins
+#define I1pin D3
+#define I2pin D6
+#define I3pin D5
 
-//~~Motor Drive output pins~/Mask in output byte~~~
-#define L1Lpin D4           //0x01
-#define L1Hpin D5           //0x02
-#define L2Lpin D3           //0x04
-#define L2Hpin D6           //0x08
-#define L3Lpin D9           //0x10
-#define L3Hpin D10          //0x20
-
-
-//~~~~~~~~Maximum command length accepted~~~~~~~~~~~
-#define MAXCMDLENGTH 18
-
+//Incremental encoder input pins
+#define CHApin   D12
+#define CHBpin   D11
 
-//~~~~~~~~Maximum PWM allowed due to 50% restriction
-#define MAXPWM 1000
-
-
-//~~~~~~~Enumeration of message identifiers~~~~~~~~~
-enum MsgCode {Msg_motorState, Msg_hashRate, Msg_nonceMatch, Msg_keyAdded, Msg_velocityOut, Msg_velocityIn,  Msg_positionIn, Msg_positionOut, Msg_rotations, Msg_torque, Msg_error};
-
+//Motor Drive output pins   //Mask in output byte
+#define L1Lpin D1           //0x01
+#define L1Hpin A3           //0x02
+#define L2Lpin D0           //0x04
+#define L2Hpin A6          //0x08
+#define L3Lpin D10           //0x10
+#define L3Hpin D2          //0x20
 
-//~~~~~~~New data type to carry the messages~~~~~~~~
-typedef struct {
-    MsgCode code;
-    uint32_t data;
-} message_t;
+#define PWMpin D9
 
-
-
-//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Global Variables~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//Motor current sense
+#define MCSPpin   A1
+#define MCSNpin   A0
 
 //Mapping from sequential drive states to motor phase outputs
 /*
@@ -62,451 +45,594 @@
 6       -   -   -
 7       -   -   -
 */
-
-
-//~~~~~~~~~~~Drive state to             output table~~~~~~~~~~~~
+//Drive state to output table
 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
 
-
-//Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid.
-const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};  
-//Alternative if phase order of input or drive is reversed.
-//const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07};
-
+//Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
+const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
+//const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
 
-////~~~~~~~~~Phase lead to make motor spin~~~~~~~~~
-int8_t lead = 2;  //2 for forwards, -2 for backwards
-
+//Phase lead to make motor spin
+const int8_t lead = 2;  //2 for forwards, -2 for backwards
 
-//~~~~~~~~~~~~~~~~~~Rotor states~~~~~~~~~~~~~~~~~~~
-int8_t orState = 0;              // Rotor offset at motor state 0 
-volatile int8_t intStateOld = 0; // Motor old state. Type is volatile since
-                                 // its value may change in ISR 
-
-//~~~~~~~~~~~~~~~~~~~Status LED~~~~~~~~~~~~~~~~~~~~
+//Status LED
 DigitalOut led1(LED1);
 
-
-//~~~~~~~~~~~~~Photointerrupter inputs~~~~~~~~~~~~~
+//Photointerrupter inputs
 InterruptIn I1(I1pin);
 InterruptIn I2(I2pin);
 InterruptIn I3(I3pin);
 
-
-//~~~~~~~~~~~~~~Motor Drive outputs~~~~~~~~~~~~~~~~
-PwmOut     L1L(L1Lpin);
+//Motor Drive outputs
+DigitalOut L1L(L1Lpin);
 DigitalOut L1H(L1Hpin);
-PwmOut     L2L(L2Lpin);
+DigitalOut L2L(L2Lpin);
 DigitalOut L2H(L2Hpin);
-PwmOut     L3L(L3Lpin);
+DigitalOut L3L(L3Lpin);
 DigitalOut L3H(L3Hpin);
 
+PwmOut pwmCtrl(PWMpin);
 
-//~Dats structure to pass information between threads~
-Mail<message_t,16> outMessages;
+uint8_t stateCount[3];   
+uint8_t theStates[3];
+
+class Comm /*: public T_*/{
+
+public:
+
+    Thread t_comm_out;
+    Thread t_motor_ctrl;
+    // Thread *p_motor_ctrl;
+
+    bool _RUN;
+
+    RawSerial pc;
+    // Queue<void, 8> inCharQ;  // Input Character Queue
 
 
-//~~~~~~~~~~~~~~~~~~~~Queue~~~~~~~~~~~~~~~~~~~~~~~~
-Queue<void, 8> inCharQ;
+    static const char MsgChar[11];
+
+    uint8_t MAXCMDLENGTH;
 
+    volatile uint8_t cmdIndx;
+    volatile uint8_t inCharQIdx;
+
+    volatile uint32_t motorPower; // motor toque
+    volatile float targetVel;
+    volatile float targetRot;
+
+    enum msgType {motorState, posIn, velIn, posOut, velOut,
 
-//~~~~~~~~~~~~Serial command buffer~~~~~~~~~~~~~~~
-char newCmd[MAXCMDLENGTH];
-volatile uint8_t cmdIndx = 0;
+        hashRate, keyAdded, nonceMatch,
+
+        torque, rotations,
+
+        error};
 
+    typedef struct {
+        msgType type;
+        uint32_t message;
+    } msg;
+
+    Mail<msg, 32> mailStack;
 
-//~~~~~~~~~~Key to be passed for mining~~~~~~~~~~~
-volatile uint64_t newKey;   // Key
-Mutex newKey_mutex;         // Restrict access to prevent deadlock.
+    void serialISR(){
+        if (pc.readable()) {
+            char newChar = pc.getc();
+            // inCharQ.put((void*)newChar); // void* = pointer to an unknown type that cannot be dereferenced
 
+            if (inCharQIdx == (MAXCMDLENGTH)) {
+                inCharQ[MAXCMDLENGTH] = '\0'; // force the string to have an end character
+                putMessage(error, 1);
+                inCharQIdx = 0; // reset buffer index
+                // pc.putc('\r'); // carriage return moves to the start of the line
+                // for (int i = 0; i < MAXCMDLENGTH; ++i)
+                // {
+                //     inCharQ[i] = ' ';
+                //     pc.putc(' ');
+                // }
 
-//~~~~~~~~~~~~~~Initial conditions~~~~~~~~~~~~~~~~
-volatile uint32_t motorPower = 300; // motor toque
-volatile float targetVel = 45.0;
-volatile float targetRot = 459.0;
+                // pc.putc('\r'); // carriage return moves to the start of the line
+            }
+            else{
+                if(newChar != '\r'){                //While the command is not over,
+                    inCharQ[inCharQIdx] = newChar;      //save input character and
+                    inCharQIdx++;                      //advance index
+                    pc.putc(newChar);
+                }
+                else{
+                    inCharQ[inCharQIdx] = '\0';         //When the command is finally over,
+                    strncpy(newCmd, inCharQ, MAXCMDLENGTH); // Will copy 18 characters from inCharQ to newCmd
+                    cmdParser();                    //parse the command for decoding.
+                    for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer
+                        inCharQ[i] = ' ';
+                    inCharQIdx = 0; // reset index
+                }
+            }
+        }
 
 
-//~~~~~~~~~~~Motor position variable~~~~~~~~~~~~~~
-volatile int32_t motorPos; // Motor position updated by interrupt.
+    }
+
+    /*void commInFn() {
+        // if (_RUN)
 
+        while (_RUN) {
+            osEvent newEvent = inCharQ.get();
+            uint8_t newChar = (uint8_t)(newEvent.value.p); // size_t to type cast the 64bit pointer properly
+            pc.putc(newChar);
+            if(cmdIndx >= MAXCMDLENGTH){            //Make sure there is no overflow in comand.
+                cmdIndx = 0;
+                putMessage(error, 1);
+            }
+            else{
+                if(newChar != '\r'){                //While the command is not over,
+                    newCmd[cmdIndx] = newChar;      //save input character and
+                    cmdIndx++;                      //advance index
+                }
+                else{
+                    newCmd[cmdIndx] = '\0';         //When the command is finally over,
+                    cmdIndx = 0;                    //reset index and
+                    cmdParser();                    //parse the command for decoding.
+                }
+            }
+        }
+    }*/
 
-//~~~~~~~~~~Serial port connection~~~~~~~~~~~~~~~~
-RawSerial pc(SERIAL_TX, SERIAL_RX);
+    void returnCursor() {
+        pc.putc('>');
+        for (int i = 0; i < inCharQIdx; ++i) // reset cursor position
+            pc.putc(inCharQ[i]);
+        // for (int i = inCharQIdx; i < MAXCMDLENGTH; ++i) // fill remaining with blanks
+        //         pc.putc(' ');
+        // pc.putc('<');
+    }
 
+    void cmdParser(){
+        switch(newCmd[0]) {
+            case 'K': //(MsgChar[keyAdded])://
+                newKey_mutex.lock();                        //Ensure there is no deadlock
+                sscanf(newCmd, "K%x", &newKey);             //Find desired the Key code
+                putMessage(keyAdded, newKey);           //Print it out
+                newKey_mutex.unlock();
+                break;
+            case 'V': //(MsgChar[velIn])://
+                sscanf(newCmd, "V%f", &targetVel);          //Find desired the target velocity
+                putMessage(velIn, targetVel);      //Print it out
+                break;
+            case 'R': //(MsgChar[posIn])://
+                sscanf(newCmd, "R%f", &targetRot);          //Find desired target rotation
+                putMessage(posIn, targetRot);      //Print it out
+                break;
+            case 'T': //(MsgChar[torque])://
+                sscanf(newCmd, "T%d", &motorPower);         //Find desired target torque
+                putMessage(torque, motorPower);         //Print it out
+                break;
+            default: break;
+        }
+    }
+
+    //~~~~~Decode messages to print on serial port~~~~~
+    void commOutFn() {
+        while (_RUN) {
+            osEvent newEvent = mailStack.get();
+            msg *pMessage = (msg *) newEvent.value.p;
 
-
+            //Case switch to choose serial output based on incoming message
+            switch (pMessage->type) {
+                case motorState:
+                    pc.printf("The motor is currently in state %x\n\r", pMessage->message);
+                    break;
+                case hashRate:
+                    pc.printf("\r>%s< Mining: %.4u Hash/s\r", inCharQ, (uint32_t) pMessage->message);
+                    returnCursor();
+                    break;
+                case nonceMatch:
+                    pc.printf("\r>%s< Nonce found: %x\r", inCharQ, pMessage->message);
+                    returnCursor();
+                    break;
+                case keyAdded:
+                    pc.printf("New Key Added:\t0x%016x\n\r", pMessage->message);
+                    break;
+                case torque:
+                    pc.printf("Motor Torque set to:\t%d\n\r", pMessage->message);
+                    break;
+                case velIn:
+                    pc.printf("Target Velocity set to:\t%.2f\n\r", targetVel);
+                    break;
+                case velOut:
+                    pc.printf("Current Velocity:\t%.2f\n\r", \
+                            (float) ((int32_t) pMessage->message / 6));
+                    break;
+                case posIn:
+                    pc.printf("Target Rotation set to:\t%.2f\n\r", \
+                            (float) ((int32_t) pMessage->message / 6));
+                    break;
+                case posOut:
+                    pc.printf("Current Position:\t%.2f\n\r", \
+                            (float) ((int32_t) pMessage->message / 6));
+                    break;
+                case error:
+                    pc.printf("\r>%s< Debugging position:%x\n\r", inCharQ, pMessage->message);
+                    for (int i = 0; i < MAXCMDLENGTH; ++i) // reset buffer
+                        inCharQ[i] = ' ';
+                    break;
+                default:
+                    pc.printf("Unknown Error. Message: %x\n\r", pMessage->message);
+                    break;
+            }
+            mailStack.free(pMessage);
+        }
+    }
 
-//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Threads~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+    // attach_us -> runs funtion every 100ms 
+    void motorCtrlFn() {
+        Ticker motorCtrlTicker;
+        motorCtrlTicker.attach_us(callback(this,&Comm::motorCtrlTick), 1e5);
+        uint8_t cpyStateCount[3]; 
+        uint8_t cpyCurrentState; 
+
+        int16_t ting[2] = {5,1}; // 360,60 (for degrees), 5,1 (for states)
+        uint8_t iterElementMax;
+        int16_t totalDegrees;
+        int16_t stateDiff;
+
+        int32_t velocity;                                   //Variable for local velocity calculation
+        int32_t locMotorPos;                                //Local copy of motor position
+        static int32_t oldMotorPos = 0;                     //Old motor position used for calculations
+        static uint8_t motorCtrlCounter = 0;                //Counter to be reset every 10 iterations to get velocity calculation in seconds
+        int32_t torque;                                     //Local variable to set motor torque
+        float sError;                                       //Velocity error between target and reality
+        float rError;                                       //Rotation error between target and reality
+        static float rErrorOld;                             //Old rotation error used for calculation
 
-Thread commOutT(osPriorityAboveNormal,1024);  // Output to serial port.
-Thread commInT(osPriorityAboveNormal,1024);   // Input from serial port.
-Thread motorCtrlT(osPriorityNormal,1024);     // Motor control thread.
+        //~~~Controller constants~~~~
+        int32_t Kp1=22;                                     //Proportional controller constants 
+        int32_t Kp2=22;                                     //Calculated by trial and error to give optimal accuracy  
+        float   Kd=15.5;    
+        
+
+        while (_RUN) {
+            t_motor_ctrl.signal_wait((int32_t)0x1);
+            core_util_critical_section_enter();
+            //Access shared variables here
+            std::copy(stateCount, stateCount+3, cpyStateCount);  
+            // TODO: A thing yes
+            cpyCurrentState = 0;
+            for (int i = 0; i < 3; ++i) {
+                stateCount[i] = 0; 
+            }
+            core_util_critical_section_exit();
+
+            iterElementMax = std::max_element(cpyStateCount, cpyStateCount+3) - cpyStateCount; 
+
+           
+            totalDegrees = ting[0] * cpyStateCount[iterElementMax];
+            stateDiff = theStates[iterElementMax]-cpyCurrentState;
+            if (stateDiff >= 0) {
+                totalDegrees = totalDegrees + (ting[1]* stateDiff);  
+            } else {
+                totalDegrees = totalDegrees + (ting[1]*stateDiff*-1); 
+            }
+            pc.printf("%u,%u,%u,%u. %.6i \r", iterElementMax, cpyStateCount[0],cpyStateCount[1],cpyStateCount[2], (totalDegrees*10));
+        
+            /*
+            //~~~~~Speed controller~~~~~~
+            velocity = totalDegrees*10;
+            sError = (targetVel * 6) - abs(velocity);        //Read global variable targetVel updated by interrupt and calculate error between target and reality
+            int32_t Ys;                                      //Initialise controller output Ys  (s=speed)
+            if (sError == -abs(velocity)) {                  //Check if user entered V0, 
+                Ys = MAXPWM;                                 //and set the output to maximum as specified
+            } else {
+                Ys = (int)(Kp1 * sError);                    //If the user didn't enter V0 implement controller transfer function: Ys = Kp * (s -|v|) where,
+            }                                                //Ys = controller output, Kp = prop controller constant, s = target velocity and v is the measured velocity
+            
+            //~~~~~Rotation control~~~~~~
+            rError = targetRot - (locMotorPos/6);            //Read global variable targetRot updated by interrupt and calculate the rotation error. 
+            int32_t Yr;                                      //Initialise controller output Yr (r=rotations)
+            Yr = Kp2*rError + Kd*(rError - rErrorOld);       //Implement controller transfer function Ys= Kp*Er + Kd* (dEr/dt)        
+            rErrorOld = rError;                              //Update rotation error
+            if(rError < 0){                                  //Use the sign of the error to set controller wrt direction of rotation
+                Ys = -Ys;                               
+            }
+
+            if((velocity>=0 && Ys<Yr) || (velocity<0 && Ys>Yr)){        //Choose Ys or Yr based on distance from target value so that it takes 
+                torque = Ys;                                            //appropriate steps in the right direction to reach target value
+            } else {
+                torque = Yr;
+            }
+            if(torque < 0){                                             //Variable torque cannot be negative since it sets the PWM  
+                torque = -torque;                                       //Hence we make the value positive, 
+                lead = -2;                                              //and instead set the direction to the opposite one
+            } else {
+                lead = 2;
+            }
+            if(torque > MAXPWM){                                        //In case the calculated PWM is higher than our maximum 50% allowance,
+                torque = MAXPWM;                                        //Set it to our max.
+            }   
+
+            motorPower = torque;                                        //Lastly, update global variable motorPower which is updated by interrupt        
+            */
+        }
+
+    }
+
+    void motorCtrlTick(){
+        t_motor_ctrl.signal_set(0x1);
+    }
 
 
+    //TODO: stop function, maybe use parent de-constructor
+    //void stop_comm{}
 
+    // public:
+
+    volatile uint64_t newKey;   // hash key
+    Mutex newKey_mutex;         // Restrict access to prevent deadlock.
 
-//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Function declarations~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+    Comm() :  pc(SERIAL_TX, SERIAL_RX),
+              t_comm_out(osPriorityAboveNormal, 1024),
+              t_motor_ctrl(osPriorityAboveNormal2, 1024)
+    { // inherit from the RawSerial constructor
+
+        pc.printf("%s\n\r", "Welcome" );
+        MAXCMDLENGTH = 18;
 
-void motorOut(int8_t driveState, uint32_t pw);
-inline int8_t readRotorState();
-int8_t motorHome();
-void motorISR();
-void cmdParser();
-void commOutFn();
-void putMessage(MsgCode code, uint32_t data);
-void serialISR();
-void commInFn();
-void motorCtrlFn();
-void motorCtrlTick(); 
+        // reset buffer
+        // MbedOS prints 'Embedded Systems are fun and do awesome things!'
+        // if you print a null terminator
+        pc.putc('>');
+        for (int i = 0; i < MAXCMDLENGTH; ++i) {
+            inCharQ[i] = '.';
+            pc.putc('.');
+        }
+        pc.putc('<');
+        pc.putc('\r');
 
+        inCharQ[MAXCMDLENGTH] = '\0';
+        strncpy(newCmd, inCharQ, MAXCMDLENGTH);
+
+        cmdIndx = 0;
+
+        inCharQIdx = 0;
+        // inCharQIdx = MAXCMDLENGTH-1;
 
 
 
-//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Main~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+        pc.attach(callback(this, &Comm::serialISR));
 
-int main() {
-    //~~~~~~~~~~~~~Initial serial prints~~~~~~~~~~~~~
-    pc.printf("\n\r\n\r Hello \n\r");
-    pc.printf("\n\r\n\rGroup: IndiCorp \n\r");
-    pc.printf("Initial hardcoded conditions:\n\r");
-    pc.printf("\tVelocity:\t%f\n\r", targetVel);
-    pc.printf("\tRotation:\t%f\n\r", targetRot);    
-   
+        // Thread t_comm_in(osPriorityAboveNormal, 1024);
+        // Thread t_comm_out(osPriorityAboveNormal, 1024);
+        // Thread t_motor_ctrl(osPriorityAboveNormal, 1024);
 
-    //~~~~~~~~~~~~~~~Start all threads~~~~~~~~~~~~~~~
-    commOutT.start(commOutFn);
-    commInT.start(commInFn);
-    motorCtrlT.start(motorCtrlFn);
-    
-   
-    //~~~~~~~~~~~~~~Attach ISR to serial~~~~~~~~~~~~
-    pc.attach(&serialISR);
-    
-   
-    //~~~~~~~~Attach ISR to photointerrupters~~~~~~~
-    I1.rise(&motorISR);
-    I1.fall(&motorISR);
-    I2.rise(&motorISR);
-    I2.fall(&motorISR);
-    I3.rise(&motorISR);
-    I3.fall(&motorISR);
-    
+        motorPower = 300;
+        targetVel = 45.0;
+        targetRot = 459.0;
+
+
+
+        /*MsgChar = {'m', 'R', 'V', 'r', 'v',
+
+                  'h', 'K', 'n',
 
-    //~~~~~~~~~Declare Bitcoin Variables~~~~~~~~~~~
-    SHA256 sha256Inst;
-    uint8_t sequence[] = {\
-        0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,\
-        0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,\
-        0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,\
-        0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,\
-        0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,\
-        0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,\
-        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,\
-        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
-    };
-    uint64_t* key = (uint64_t*)((int)sequence + 48);
-    uint64_t* nonce = (uint64_t*)((int)sequence + 56);
-    uint8_t hash[32];
-    uint32_t sequenceLength = 64;
-    uint32_t hashCounter = 0;
-    Timer bitcoinTimer;
+                  'T', 'r',
+
+                  'e'};*/
+    }
+
 
-    
-    //Set PWM period to max 2000 due to hardware limitations
-    L1L.period_us(2000); 
-    L2L.period_us(2000);
-    L3L.period_us(2000);
-    
+    void putMessage(msgType type, uint32_t message){
+        msg *p_msg = mailStack.alloc();
+        p_msg->type = type;
+        p_msg->message = message;
+        mailStack.put(p_msg);
+    }
 
-    /* Run the motor synchronisation: orState is subtracted from future rotor
-       state inputs to align rotor and motor states */
-    orState = motorHome();
-    pc.printf("Rotor origin: %x\n\r", orState); //Print state for debugging purposes.
-    
-   
-    //~~~~~~Give the motor a kick to begin~~~~~~~~
-    motorISR();
-    
+    void start_comm(){
+        _RUN = true;
 
 
-    //~~~~~~~~~~~~~~~~Mining loop~~~~~~~~~~~~~~~~~
-    bitcoinTimer.start();          // start timer
-    while (1) {
-        newKey_mutex.lock();
-        (*key) = newKey;
-        newKey_mutex.unlock();
-        sha256Inst.computeHash(hash, sequence, sequenceLength);
-        hashCounter++;
-        if ((hash[0]==0) && (hash[1]==0)){
-            putMessage(Msg_nonceMatch, *nonce);   // matching nonce 7
+        // reset buffer
+        // MbedOS prints 'Embedded Systems are fun and do awesome things!'
+        // if you print a null terminator
+        pc.putc('>');
+        for (int i = 0; i < MAXCMDLENGTH; ++i) {
+            inCharQ[i] = '.';
+            pc.putc('.');
         }
+        pc.putc('<');
+        pc.putc('\r');
+
+        inCharQ[MAXCMDLENGTH] = '\0';
+        strncpy(newCmd, inCharQ, MAXCMDLENGTH);
+
+        // returnCursor();
 
-        (*nonce)++;
+        // t_comm_in.start(callback(this, &Comm::commInFn));
+        // this::thread::wait()
+        // wait(1.0);
+        t_comm_out.start(callback(this, &Comm::commOutFn));
+        t_motor_ctrl.start(callback(this, &Comm::motorCtrlFn));
+
+
+    }
 
-        if (bitcoinTimer.read() >= 1){
-            putMessage(Msg_hashRate, hashCounter); // 5
-            hashCounter=0;
-            bitcoinTimer.reset();
-        }
-    }
-}
+    char newCmd[];  // because unallocated must be defined at the bottom of the class
+    char inCharQ[];
+};
 
-//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Functions Definitions~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
 
-//~~~~~~~~~~~~Set a given drive state~~~~~~~~~~~~
-void motorOut(int8_t driveState, uint32_t pw){
+//Set a given drive state
+void motorOut(int8_t driveState){
 
     //Lookup the output byte from the drive state.
     int8_t driveOut = driveTable[driveState & 0x07];
-      
+
     //Turn off first
-    if (~driveOut & 0x01) L1L.pulsewidth_us(0);
+    if (~driveOut & 0x01) L1L = 0;
     if (~driveOut & 0x02) L1H = 1;
-    if (~driveOut & 0x04) L2L.pulsewidth_us(0);
+    if (~driveOut & 0x04) L2L = 0;
     if (~driveOut & 0x08) L2H = 1;
-    if (~driveOut & 0x10) L3L.pulsewidth_us(0);
+    if (~driveOut & 0x10) L3L = 0;
     if (~driveOut & 0x20) L3H = 1;
-    
+
     //Then turn on
-    if (driveOut & 0x01) L1L.pulsewidth_us(pw);
+    if (driveOut & 0x01) L1L = 1;
     if (driveOut & 0x02) L1H = 0;
-    if (driveOut & 0x04) L2L.pulsewidth_us(pw);
+    if (driveOut & 0x04) L2L = 1;
     if (driveOut & 0x08) L2H = 0;
-    if (driveOut & 0x10) L3L.pulsewidth_us(pw);
+    if (driveOut & 0x10) L3L = 1;
     if (driveOut & 0x20) L3H = 0;
 }
-    
 
-//~Convert photointerrupter inputs to a rotor state~
+//Convert photointerrupter inputs to a rotor state
 inline int8_t readRotorState(){
     return stateMap[I1 + 2*I2 + 4*I3];
 }
 
-//~~~~~~Basic motor synchronisation routine~~~~~~   
+//Basic synchronisation routine
 int8_t motorHome() {
     //Put the motor in drive state 0 and wait for it to stabilise
-    motorOut(0, MAXPWM); // set to max PWM
+    motorOut(0);
     wait(2.0);
-    
+
     //Get the rotor state
     return readRotorState();
 }
 
 
-//~~~~~~~~~Motor ISR (photointerrupters)~~~~~~~~~
-void motorISR() {
-    static int8_t oldRotorState;
-    int8_t rotorState = readRotorState();
-    
-    motorOut((rotorState-orState+lead+6)%6,motorPower);
-    
-    // update motorPosition and oldRotorState
-    if (rotorState - oldRotorState == 5) motorPos--;
-    else if (rotorState - oldRotorState == -5) motorPos++;
-    else motorPos += (rotorState - oldRotorState);
-    oldRotorState = rotorState;
+void stateUpdate(int8_t *params[]) { // () { // **params
+    *params[0] = readRotorState();
+    int8_t currentState = *params[0];
+    int8_t offset = *params[1];
+
+    switch (currentState) {
+        case 1:
+            stateCount[0]++; 
+            break; 
+        case (1 + lead): 
+            stateCount[1]++; 
+            break; 
+        case (1 + (lead*2)):
+            stateCount[2]++; 
+            break; 
+    }
+
+    motorOut((currentState - offset + lead + 6) % 6);
 }
 
+//Main
+int main() {
 
-//~~~~~Decode messages to print on serial port~~~~~
-void commOutFn() {
-    while(1) {
-        osEvent newEvent = outMessages.get();
-        message_t *pMessage = (message_t*)newEvent.value.p;
+    // std::ios::sync_with_stdio(false);
+    Comm comm_plz;
+
+    // comm_plz.pc.printf("%s\n", "do i work bruh" ); // using printf of class is calm
+    SHA256 Miner;
+
+    uint8_t sequence[] = {0x45,0x6D,0x62,0x65,0x64,0x64,0x65,0x64,
+                          0x20,0x53,0x79,0x73,0x74,0x65,0x6D,0x73,
+                          0x20,0x61,0x72,0x65,0x20,0x66,0x75,0x6E,
+                          0x20,0x61,0x6E,0x64,0x20,0x64,0x6F,0x20,
+                          0x61,0x77,0x65,0x73,0x6F,0x6D,0x65,0x20,
+                          0x74,0x68,0x69,0x6E,0x67,0x73,0x21,0x20,
+                          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+                          0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+    uint64_t* key = (uint64_t*)((int)sequence + 48);
+    uint64_t* nonce = (uint64_t*)((int)sequence + 56);
+    uint8_t hash[32];
+    uint32_t length64 = 64;
+    uint32_t hashCounter = 0;
+    Timer timer;
+
+    float dutyC = 1; // 100%
+    float mtrPeriod = 2e-3; // motor period
+
+    pwmCtrl.period(mtrPeriod);
+    pwmCtrl.pulsewidth(mtrPeriod*dutyC);
+
+    comm_plz.start_comm();
 
-        //Case switch to choose serial output based on incoming message
-        switch(pMessage->code) {
-            case Msg_motorState:
-            pc.printf("The motor is currently in state %x\n\r", pMessage->data);
-                break;
-            case Msg_hashRate:
-            pc.printf("Mining at a rate of %.2f Hash/s\n\r", (int32_t)pMessage->data);
-                break;
-            case Msg_nonceMatch:
-                 pc.printf("Nonce found: %x\n\r", pMessage->data);
-                break;
-            case Msg_keyAdded:
-                pc.printf("New key added:\t0x%016x\n\r", pMessage->data);
-                break;
-            case Msg_torque:
-                pc.printf("Motor torque set to:\t%d\n\r", pMessage->data);
-                break;
-            case Msg_velocityIn:
-                pc.printf("Target velocity set to:\t%.2f\n\r", targetVel);
-                break;
-            case Msg_velocityOut:
-                pc.printf("Current Velocity:\t%.2f\n\r", \
-                    (float)((int32_t)pMessage->data / 6));
-                break;
-            case Msg_positionIn:
-                pc.printf("Target rotation set to:\t%.2f\n\r", \
-                    (float)((int32_t)pMessage->data / 6));
-                break;
-            case Msg_positionOut:
-            pc.printf("Current position:\t%.2f\n\r", \
-           (float)((int32_t)pMessage->data / 6));
-                break;
-            case Msg_error:
-                pc.printf("Debugging position:%x\n\r", pMessage->data);
-                break;
-            default:
-            pc.printf("Unknown Error. Data: %x\n\r", pMessage->data);
-                break;
-        }
-        outMessages.free(pMessage);
-    }
-}
+    // Motor States
+    int8_t orState = 0;    //Rotot offset at motor state 0
+    int8_t currentState = 0;    //Rotot offset at motor state 0
+    int8_t stateList[6];    //Rotot offset at motor state 0
+    //Run the motor synchronisation
+    orState = motorHome();
+
+    theStates[0] = orState;
+    theStates[1] = (orState + lead) % 6;
+    theStates[2] = (orState + (lead*2)) % 6;
+
+    // Add callbacks
+    // I1.fall(&stateUpdate);
+    // I2.fall(&stateUpdate);
+    // I3.fall(&stateUpdate);
+    int8_t* params[2];
+    params[0] = &currentState;
+    params[1] = &orState;
 
+    I1.fall(callback(&stateUpdate,params));
+    I2.fall(callback(&stateUpdate,params));
+    I3.fall(callback(&stateUpdate,params));
 
-//~~~~~~~~~Put message in Mail queue~~~~~~~~~~~
-void putMessage(MsgCode code, uint32_t data){
-    message_t *pMessage = outMessages.alloc();
-    pMessage->code = code;
-    pMessage->data = data;
-    outMessages.put(pMessage);
-}
+    I1.rise(callback(&stateUpdate,params));
+    I2.rise(callback(&stateUpdate,params));
+    I3.rise(callback(&stateUpdate,params));
 
+    // Push motor to move
+    currentState = readRotorState();
+    motorOut((currentState-orState+lead+6)%6); // We push it digitally
 
-//~~~~Receive & decode serial input command~~~~~
-void commInFn() {
-    while (1) {
-        osEvent newEvent = inCharQ.get();
-        uint8_t newChar = *((uint8_t*)(&newEvent.value.p));
-        pc.putc(newChar);
-        if(cmdIndx >= MAXCMDLENGTH){            //Make sure there is no overflow in comand.
-            cmdIndx = 0;
-            putMessage(Msg_error, 1);
-        }
-        else{
-            if(newChar != '\r'){                //While the command is not over, 
-                newCmd[cmdIndx] = newChar;      //save input character and
-                cmdIndx++;                      //advance index
-            }
-            else{
-                newCmd[cmdIndx] = '\0';         //When the command is finally over,
-                cmdIndx = 0;                    //reset index and
-                cmdParser();                    //parse the command for decoding.
-            }
-        }
-    }
-}
+    // pc.printf("Rotor origin: %x\n\r",orState);
+    // orState is subtracted from future rotor state inputs to align rotor and motor states
+    // intState = readRotorState();
+    //if (intState != intStateOld) {
+    //     pc.printf("old:%d \t new:%d \t next:%d \n\r",intStateOld, intState, (intState-orState+lead+6)%6);
+    //     intStateOld = intState;
+    //     motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
+    // }
 
+    dutyC = 0.8;
+    pwmCtrl.pulsewidth(mtrPeriod*dutyC);
 
 
-//~~~~~~~~~~~~~Decode the command~~~~~~~~~~~
-void cmdParser(){
-        switch(newCmd[0]) {
-                case 'K':
-                        newKey_mutex.lock();                        //Ensure there is no deadlock
-                        sscanf(newCmd, "K%x", &newKey);             //Find desired the Key code
-                        putMessage(Msg_keyAdded, newKey);           //Print it out
-                        newKey_mutex.unlock();                      
-                        break;
-                case 'V':
-                        sscanf(newCmd, "V%f", &targetVel);          //Find desired the target velocity
-                        putMessage(Msg_velocityIn, targetVel);      //Print it out
-                        break;
-                case 'R':
-                        sscanf(newCmd, "R%f", &targetRot);          //Find desired target rotation
-                        putMessage(Msg_positionIn, targetRot);      //Print it out
-                        break;
-                case 'T':
-                        sscanf(newCmd, "T%d", &motorPower);         //Find desired target torque
-                        putMessage(Msg_torque, motorPower);         //Print it out
-                        break;
-                default: break;
+    // Keep the program running indefinitely
+    timer.start();          // start timer
+    int stateCount = 0;
+    while (1) {
+        // pc.printf("Current:%d \t Next:%d \n\r", currentState, (currentState-orState+lead+6)%6);
+        comm_plz.newKey_mutex.lock();
+        *key = comm_plz.newKey;
+        comm_plz.newKey_mutex.unlock();
+        Miner.computeHash(hash, sequence, length64);
+        hashCounter++;
+        if ((hash[0]==0) && (hash[1]==0)){
+            comm_plz.putMessage((Comm::msgType)7, *nonce);
         }
-}
-
-
-//~~~~~~~~~~~~~Serial ISR~~~~~~~~~~~~
-void serialISR() {
-    uint8_t newChar = pc.getc();
-    inCharQ.put((void*)newChar);
-}
-
-
-//~~~~~~ISR triggered by Ticker~~~~~~
-void motorCtrlTick(){
-    motorCtrlT.signal_set(0x1);                            //Set signal to motor control thread which carries out calculations to avoid CPU blocking
-}
-
-
-//~~~~~~~~~~~~~Motor control function  with proportional controller~~~~~~~~~~~
-void motorCtrlFn() {
 
-    //~~~~~~~~~~~~~Variables~~~~~~~~~~~~~~~~
-    Ticker motorCtrlTicker;                             //Ticker to ba attached to callback function
-    int32_t velocity;                                   //Variable for local velocity calculation
-    int32_t locMotorPos;                                //Local copy of motor position
-    static int32_t oldMotorPos = 0;                     //Old motor position used for calculations
-    static uint8_t motorCtrlCounter = 0;                //Counter to be reset every 10 iterations to get velocity calculation in seconds
-    int32_t torque;                                     //Local variable to set motor torque
-    float sError;                                       //Velocity error between target and reality
-    float rError;                                       //Rotation error between target and reality
-    static float rErrorOld;                             //Old rotation error used for calculation
-
-    //~~~Controller constants~~~~
-    int32_t Kp1=22;                                     //Proportional controller constants 
-    int32_t Kp2=22;                                     //Calculated by trial and error to give optimal accuracy  
-    float   Kd=15.5;    
-       
-
-    //Attach ticker to callback function that will run every 100 ms
-    motorCtrlTicker.attach_us(&motorCtrlTick,100000);
-
+        // Try a new nonce
+        (*nonce)++;
 
-
-    while(1) {
-        motorCtrlT.signal_wait(0x1);                    // Wait for thread signal.
-
-        //Initial velocity calculation and report
-        locMotorPos = motorPos;                         //Read global variable motorPos which is updated by interrupt and store it in local variable
-        velocity = (locMotorPos - oldMotorPos) * 10;    //Proceed with calculation
-        oldMotorPos = locMotorPos;                      //Update old motor position
-        motorCtrlCounter++;                             //Advance counter 
-        if (motorCtrlCounter >= 10) {                   //Every 10th iteration
-            motorCtrlCounter = 0;                       //Reset counter
-            putMessage(Msg_velocityOut, velocity);      //Report the current velocity
-            putMessage(Msg_positionOut, locMotorPos);   //Report the current position
-        }
-        /*
-        //~~~~~Speed controller~~~~~~
-        sError = (targetVel * 6) - abs(velocity);        //Read global variable targetVel updated by interrupt and calculate error between target and reality
-        int32_t Ys;                                      //Initialise controller output Ys  
-        if (sError == -abs(velocity)) {                  //Check if user entered V0, 
-            Ys = MAXPWM;                                 //and set the output to maximum as specified
+        if (stateCount<6){
+            stateList[stateCount] = currentState;
+            stateCount++;
         }
         else {
-            Ys = (int)(Kp1 * sError);                    //If the user didn't enter V0 implement controller transfer function: Ys = Kp * (s -|v|) where,
-        }                                                //Ys = controller output, Kp = prop controller constant, s = target velocity and v is the measured velocity
-        
-        //~~~~~Rotation control~~~~~~
-        rError = targetRot - (locMotorPos/6);            //Read global variable targetRot updated by interrupt and calculate the rotation error. 
-        int32_t Yr;                                      //Initialise controller output Yr
-        Yr = Kp2*rError + Kd*(rError - rErrorOld);       //Implement controller transfer function Ys= Kp*Er + Kd* (dEr/dt)        
-        rErrorOld = rError;                              //Update rotation error
-        if(rError < 0){                                  //Use the sign of the error to set controller wrt direction of rotation
-            Ys = -Ys;                               
+            //pc.printf("states");
+            //for(int i = 0; i < 6; ++i)
+            //pc.printf("%02i,", stateList[i]);
+            //pc.printf("\n\r");
+            stateCount = 0;
         }
 
-        if((velocity>=0 && Ys<Yr) || (velocity<0 && Ys>Yr)){        //Choose Ys or Yr based on distance from target value so that it takes 
-            torque = Ys;                                            //appropriate steps in the right direction to reach target value
-        }
-        else{
-            torque = Yr;
-        }
-        if(torque < 0){                                             //Variable torque cannot be negative since it sets the PWM  
-            torque = -torque;                                       //Hence we make the value positive, 
-            lead = -2;                                              //and instead set the direction to the opposite one
+        // Per Second i.e. when greater or equal to 1
+        if (timer.read() >= 1){
+            comm_plz.putMessage((Comm::msgType)5, hashCounter);
+            //pc.printf("HashRate = %02u \n\r",hashCounter);
+            hashCounter=0;
+            timer.reset();
         }
-        else{
-            lead = 2;
-        }
-        if(torque > MAXPWM){                                        //In case the calculated PWM is higher than our maximum 50% allowance,
-            torque = MAXPWM;                                        //Set it to our max.
-        }   
-        motorPower = torque;                                        //Lastly, update global variable motorPower which is updated by interrupt  
-        */      
     }
-}
+}
\ No newline at end of file