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

Dependencies:   Crypto

Files at this revision

API Documentation at this revision

Comitter:
adehadd
Date:
Fri Mar 22 16:50:54 2019 +0000
Parent:
48:b2afe48ced0d
Child:
50:d1b983a0dd6f
Commit message:
New communication thread system that works with mutex

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Mar 21 23:54:18 2019 +0000
+++ b/main.cpp	Fri Mar 22 16:50:54 2019 +0000
@@ -93,26 +93,36 @@
 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
 
-Mutex        _newKeyMutex;                               // Restrict access to prevent deadlock.
-bool YES = false;
+#ifndef     MAXCMDLENGTH
+    #define MAXCMDLENGTH 18
+#endif
+
+#ifndef     MAXCMDLENGTH_HALF
+    #define MAXCMDLENGTH_HALF 9
+#endif
+
 class Comm{
 
     public:
 
         volatile bool       _outMining;
         volatile float      _targetVel, _targetRot;
-        volatile char       _notes[9];                                  // Array of actual _notes 
+        volatile char       _notes[MAXCMDLENGTH_HALF];                  // Array of actual _notes 
 
         volatile int8_t     _modeBitField;                              // 0,0,0,... <=> Melody,Torque,Rotation,Velocity
         const    uint8_t    _MAXCMDLENGTH;                              // 
         volatile uint8_t    _inCharIndex, _cmdIndex,
-                            _noteDur[9],_noteLen;                       // Array of note durations
+                            _noteDur[MAXCMDLENGTH_HALF],_noteLen;       // Array of note durations
         volatile uint32_t   _motorTorque;                               // Motor Toque
         volatile uint64_t   _newKey;                                    // hash key
+
         
-
+        char _inCharQ[MAXCMDLENGTH],
+             _newCmd[MAXCMDLENGTH];
+        
         RawSerial           _pc;
-        Thread              _tCommOut;
+        Thread              _tCommOut, _tCommIn;
+        Mutex               _newKeyMutex;                               // Restrict access to prevent deadlock.
         bool                _RUN;
         
         enum msgType      { mot_orState, posIn, velIn, posOut, velOut,
@@ -121,13 +131,14 @@
                             error};
 
         typedef struct    { msgType type;
-                            uint32_t message;} msg;
+                            uint32_t message;} msg; // TODO: Maybe add a thing that stores the newCmd message as well
 
         Mail<msg, 32>       _msgStack;
+        Mail<bool,32>       _msgReceived;
 
 
         //-- Default Constructor With Inheritance From RawSerial Constructor ------------------------------------------------//
-        Comm(): _pc(SERIAL_TX, SERIAL_RX), _tCommOut(osPriorityAboveNormal, 1024), _MAXCMDLENGTH(18){
+        Comm(): _pc(SERIAL_TX, SERIAL_RX), _tCommOut(osPriorityNormal, 1024), _tCommIn(osPriorityAboveNormal, 1024), _MAXCMDLENGTH(MAXCMDLENGTH){
 
             _cmdIndex     = 0;
             _inCharIndex  = 0;
@@ -139,7 +150,7 @@
 
             _modeBitField = 0x01;                                       // Default velocity mode
 
-            _pc.printf("\n\r%s\n\r>", "Welcome" );                    // Welcome                                       
+            _pc.printf("\n\r%s %d\n\r>", "Welcome", _MAXCMDLENGTH );                    // Welcome                                       
             for (int i = 0; i < _MAXCMDLENGTH; ++i)                     // Reset buffer
                 _inCharQ[i] = (char)'.';                                // If a null terminator is printed Mbed prints 'Embedded Systems are fun and do awesome things!'
 
@@ -147,24 +158,27 @@
             sprintf(_inCharQ, "%s", _inCharQ);                          // Handling of the correct string
             strncpy(_newCmd, _inCharQ, _MAXCMDLENGTH);
 
-            _pc.printf("%s\n\r", _inCharQ);
-            _pc.putc('<');
-            _pc.attach(callback(this, &Comm::serialISR));
+            _pc.printf("%s\n\r<", _inCharQ);
+            
         }
 
-        //-- Interrupt Service Routine for Serial Port and Character Queue Handling -----------------------------------------//
-        void serialISR() {
-            if (_pc.readable()) {
-                char newChar = _pc.getc();
+        void commInFn() {
+            _pc.attach(callback(this, &Comm::serialISR));
+            char newChar;
 
+            while (_RUN) {
+                osEvent newEvent = _msgReceived.get();                  // Waits forever until it receives a thing
+                _msgReceived.free((bool *)newEvent.value.p);
+                
                 if (_inCharIndex == (_MAXCMDLENGTH)) {
                     _inCharQ[_MAXCMDLENGTH] = '\0';                      // Force the string to have an end character
                     putMessage(error, 1);
                     _inCharIndex = 0;                                   // Reset buffer index
                 }
                 else{
-                    if(newChar != '\r'){                                // While the command is not over,
-                        _inCharQ[_inCharIndex] = newChar;                // Save input character and
+                    newChar = _inCharQ[_inCharIndex];
+
+                    if(newChar != '\r'){               // While the command is not over,
                         _inCharIndex++;                                 // Advance index
                         _pc.putc(newChar);
                     }
@@ -174,15 +188,28 @@
 
                         for (int i = 0; i < _MAXCMDLENGTH; ++i)         // Reset buffer
                             _inCharQ[i] = ' ';
-
+                        
                         _inCharIndex = 0;                               // Reset index  
-                        YES = true;
-                        // cmdParser();                                    // Parse the command for decoding
+                        cmdParser();
+
+                        // _tCommIn.signal_wait(0x01);
                     }
                 }
             }
         }
 
+        //-- Interrupt Service Routine for Serial Port and Character Queue Handling -----------------------------------------//
+        void serialISR() {
+            if (_pc.readable()) {
+                char newChar = _pc.getc();
+                _inCharQ[_inCharIndex] = newChar;                       // Save input character
+
+                bool *new_msg = _msgReceived.alloc();
+                *new_msg = true;
+                _msgReceived.put(new_msg);
+            }
+        }
+
         //-- Reset Cursor Position ------------------------------------------------------------------------------------------//
         void returnCursor() {
             _pc.putc('>');
@@ -283,7 +310,6 @@
                 _noteLen = len;
                 return true;
             }
-
             else {
                 return false;
             }   
@@ -293,9 +319,6 @@
         void commOutFn() {
             while (_RUN) {
 
-                if (YES){ // waiiiiiittt
-                cmdParser();                                    // Parse the command for decoding
-
                 osEvent newEvent = _msgStack.get();
                 msg *pMessage = (msg *) newEvent.value.p;
 
@@ -356,11 +379,7 @@
                         break;
                 }
 
-
-
                 _msgStack.free(pMessage);
-                YES = false;
-                }
             }
         }
 
@@ -376,12 +395,12 @@
         void start_comm(){
             _RUN = true;
             _tCommOut.start(callback(this, &Comm::commOutFn));
+            _tCommIn.start(callback(this, &Comm::commInFn));
         }
 
-        char _newCmd[];                                                 // Unallocated must be defined at the bottom of the class
-        static char _inCharQ[];
+        
 };
-char Comm::_inCharQ[] = {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\0'}; // Static member must be defined outside class
+// char Comm::_inCharQ[] = {'.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','.','\0'}; // Static member must be defined outside class
 //Mutex Comm::_newKeyMutex;
 
 class Motor {
@@ -412,7 +431,7 @@
             pwmCtrl.pulsewidth_us(mtrPeriod);
 
             _orState       = motorHome();                               // Rotor offset at motor state 0
-            _currentState  = readRot_orState();                         // Current Rotor State
+            _currentState  = readRotorState();                         // Current Rotor State
             _lead = 2;                                                  // 2 for forwards, -2 for backwards
 
             _theStates[0]  = _orState +1;                               // Initialise repeatable states, for us this is state 1
@@ -477,7 +496,7 @@
         }
 
         //-- Inline Conversion of Photointerrupts to a Rotor State ----------------------------------------------------------//
-        inline int8_t readRot_orState() {
+        inline int8_t readRotorState() {
             return stateMap[I1 + 2*I2 + 4*I3];
         }
 
@@ -487,12 +506,12 @@
             motorOut(0);                                                //Put the motor in drive state 0 and wait for it to stabilise
             wait(3.0);
 
-            return readRot_orState();                                   //Get the rotor state
+            return readRotorState();                                   //Get the rotor state
         }
 
         //-- Motor State Log, Circumvents Issues From Occasionally Skipping Certain States ----------------------------------//
         void stateUpdate() { // () { // **params
-            _currentState = readRot_orState();                          // Get current state
+            _currentState = readRotorState();                          // Get current state
 
             if (_currentState == _theStates[0])                         // Cumulative state counter (1 for our group's motor)
                 _stateCount[0]++;
@@ -553,6 +572,7 @@
 
             while (_RUN) {
                 _tMotorCtrl.signal_wait((int32_t)0x1);
+                // _pComm->_tCommIn.signal_set(0x01);
 
                 core_util_critical_section_enter();                     // Access shared variables here
                     cpyModeBitfield = _pComm->_modeBitField;
@@ -589,7 +609,7 @@
                     // Ts = controller output, Kp = prop controller constant, s = target velocity and v is the measured velocity
 
                     // Check if user entered V0 and set the output to maximum as specified
-                    sError == -abs(cur_speed) ? Ts = _MAXPWM_PRD : \     
+                    sError == -abs(cur_speed) ? Ts = _MAXPWM_PRD : \
                                                 Ts = (Kp1 * sError);    // If the user didn't enter V0 implement controller transfer function: 
                                                                         
                                                                                                                   
@@ -669,9 +689,9 @@
         //try{
 
             // Mutex For Access Control
-            _newKeyMutex.lock();
+            comm_port._newKeyMutex.lock();
             *key = comm_port._newKey;
-            _newKeyMutex.unlock();
+            comm_port._newKeyMutex.unlock();
 
             // Compute Hash and Counter
             miner.computeHash(hash, sequence, length64);