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

Dependencies:   Crypto

Files at this revision

API Documentation at this revision

Comitter:
iachinweze1
Date:
Wed Mar 20 08:30:45 2019 +0000
Parent:
37:71adabab284a
Child:
39:05a021718517
Commit message:
19/03

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Mar 18 20:04:34 2019 +0000
+++ b/main.cpp	Wed Mar 20 08:30:45 2019 +0000
@@ -92,10 +92,12 @@
         volatile uint8_t cmdIndx;
         volatile uint8_t inCharQIdx;
     
-        volatile float motorPower; // motor toque
+        volatile uint16_t motorPower; // motor toque
         volatile float targetVel;
         volatile float targetRot;
     
+        volatile bool outMining;
+
         enum msgType {motorState, posIn, velIn, posOut, velOut,
     
             hashRate, keyAdded, nonceMatch,
@@ -175,9 +177,17 @@
                     putMessage(posIn, targetRot);      //Print it out
                     break;
                 case 'T': //(MsgChar[torque])://
-                    sscanf(newCmd, "T%d", &motorPower);         //Find desired target torque
+                    sscanf(newCmd, "T%u", &motorPower);         //Find desired target torque
                     putMessage(torque, motorPower);         //Print it out
                     break;
+                case 'M': //(MsgChar[torque])://
+                    int8_t miningTest;
+                    sscanf(newCmd, "M%d", &miningTest);         //Find desired target torque
+                    if (miningTest == 1)
+                        outMining = true;
+                    else
+                        outMining = false;
+                    break;
                 default: break;
             }
         }
@@ -194,12 +204,17 @@
                         pc.printf("\r>%s< The motor is currently in state %x\n\r", inCharQ, pMessage->message);
                         break;
                     case hashRate:
-                        pc.printf("\r>%s< Mining: %.4u Hash/s\r", inCharQ, (uint32_t) pMessage->message);
-                        returnCursor();
+                        if (outMining) {
+                            pc.printf("\r>%s< Mining: %.4u Hash/s\r", inCharQ, (uint32_t) pMessage->message);
+                            returnCursor();
+                            outMining = false;
+                        }
                         break;
                     case nonceMatch:
-                        pc.printf("\r>%s< Nonce found: %x\r", inCharQ, pMessage->message);
+                        // if (outMining) {
+                        pc.printf("\r>%s< Nonce found: %x\n\r", inCharQ, pMessage->message);
                         returnCursor();
+                        // }
                         break;
                     case keyAdded:
                         pc.printf("\r>%s< New Key Added:\t0x%016x\n\r", inCharQ, pMessage->message);
@@ -271,7 +286,7 @@
     
             inCharQIdx = 0;
             // inCharQIdx = MAXCMDLENGTH-1;
-    
+            outMining = false;
             pc.attach(callback(this, &Comm::serialISR));
     
             // Thread t_comm_in(osPriorityAboveNormal, 1024);
@@ -352,39 +367,40 @@
         //Run the motor synchronisation
         
         float dutyC;     // 1 = 100%
-        float mtrPeriod; // motor period
+        uint16_t mtrPeriod; // motor period
         uint8_t stateCount[3];  // State Counter
         uint8_t theStates[3];   // The Key states
     
         Thread t_motor_ctrl;    // Thread for motor Control
         
-        float MAXPWM;
+        uint16_t MAXPWM_PRD;
     
     public:
     
-        Motor() : t_motor_ctrl(osPriorityAboveNormal, 1024)
+        Motor() : t_motor_ctrl(osPriorityAboveNormal2, 1024)
         {
             // Set Power to maximum to drive motorHome()
-            dutyC = 1;
-            mtrPeriod = 2e-3; // motor period
-            pwmCtrl.period(mtrPeriod);
-            pwmCtrl.pulsewidth(mtrPeriod*dutyC);
+            dutyC = 1.0f;
+            mtrPeriod = 2e3; // motor period
+            pwmCtrl.period_us(mtrPeriod);
+            pwmCtrl.pulsewidth_us(mtrPeriod);
     
             orState = motorHome();             //Rotot offset at motor state 0
             currentState = readRotorState();   //Current Rotor State
             // stateList[6] = {0,0,0, 0,0,0};     //All possible rotor states stored
             lead = 2;  //2 for forwards, -2 for backwards
     
-            theStates[0] = orState;
-            theStates[1] = (orState + lead) % 6;
-            theStates[2] = (orState + (lead*2)) % 6;
+            // It skips the origin state and it's 'lead' increments?
+            theStates[0] = orState +1;
+            theStates[1] = (orState + lead) % 6 +1;
+            theStates[2] = (orState + (lead*2)) % 6 +1;
         
             stateCount[0] = 0; stateCount[1] = 0; stateCount[2] = 0;
             
             p_comm = NULL; // null pointer for now
             _RUN = false;
             
-            MAXPWM = mtrPeriod*dutyC;
+            MAXPWM_PRD = 2e3;
     
         }
     
@@ -404,8 +420,8 @@
     
             // Default a lower duty cylce
             dutyC = 0.8;
-            pwmCtrl.period(mtrPeriod);
-            pwmCtrl.pulsewidth(mtrPeriod*dutyC);
+            pwmCtrl.period_us((uint16_t)mtrPeriod);
+            pwmCtrl.pulsewidth_us((uint16_t)mtrPeriod*dutyC);
     
              p_comm = comm;
             _RUN = true;
@@ -413,8 +429,7 @@
             // Start motor control thread
             t_motor_ctrl.start(callback(this, &Motor::motorCtrlFn));
 
-            MAXPWM = mtrPeriod*dutyC;
-    
+            p_comm->pc.printf("origin=%i, theStates=[%i,%i,%i]\n", orState, theStates[0], theStates[1], theStates[2]);
 
         }
     
@@ -450,7 +465,7 @@
         int8_t motorHome() {
             //Put the motor in drive state 0 and wait for it to stabilise
             motorOut(0);
-            wait(2.0);
+            wait(3.0);
     
             //Get the rotor state
             return readRotorState();
@@ -492,9 +507,9 @@
 
             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
-            float torque;                                     //Local variable to set motor torque
+            // 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
+            int16_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
@@ -534,9 +549,9 @@
                 sError = (p_comm->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
+                    Ys = MAXPWM_PRD;                                 //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 = (int32_t)(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~~~~~~
@@ -559,13 +574,14 @@
                 } 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.
+                if(torque > MAXPWM_PRD){                                        //In case the calculated PWM is higher than our maximum 50% allowance,
+                    torque = MAXPWM_PRD;                                        //Set it to our max.
                 }   
 
-                pwmCtrl.pulsewidth(torque);
-                p_comm->motorPower = torque;                                        //Lastly, update global variable motorPower which is updated by interrupt        
-                p_comm->pc.printf("%f, %i, %i \r", torque, Ys, Yr);
+                pwmCtrl.pulsewidth_us(p_comm->motorPower);
+                // pwmCtrl.write((float)(p_comm->motorPower/MAXPWM_PRD)); 
+                // p_comm->motorPower = torque;                                        //Lastly, update global variable motorPower which is updated by interrupt        
+                // p_comm->pc.printf("\t\t\t\t\t\t %i, %i, %i \r", torque, Ys, Yr);
                 //p_comm->pc.printf("%u,%u,%u,%u. %.6i \r", iterElementMax, cpyStateCount[0],cpyStateCount[1],cpyStateCount[2], (totalDegrees*10));
             }
         }