fork of what I have been writing
Revision 13:f6e37c21d31d, committed 2020-03-09
- Comitter:
- le1917
- Date:
- Mon Mar 09 17:19:39 2020 +0000
- Parent:
- 12:38afe92e67d0
- Commit message:
- Thread start stopping not working;
Changed in this revision
--- a/ES_CW2_Starter_STARFISH/MotorControl.h Mon Mar 09 15:02:47 2020 +0000 +++ b/ES_CW2_Starter_STARFISH/MotorControl.h Mon Mar 09 17:19:39 2020 +0000 @@ -57,7 +57,6 @@ const float kd_pos = 0; const float pwm_period =0.25f; -int target_velocity = 30; float target_position = 500; // GLOBAL VARIABLES MOTOR // @@ -223,8 +222,9 @@ } } -void motorCtrlFn() +void motorCtrlFn( float* NewSpeed) { + float target_velocity = *NewSpeed; // motor controller variables float current_position = 0; int previous_position = 0; @@ -233,6 +233,7 @@ uint32_t last_time_MtrCtlr; float pos_error_old; int i = 0; + float velocity_integral=0; float velocity_derivative=0;
--- a/ES_CW2_Starter_STARFISH/SerialCommunication.h Mon Mar 09 15:02:47 2020 +0000 +++ b/ES_CW2_Starter_STARFISH/SerialCommunication.h Mon Mar 09 17:19:39 2020 +0000 @@ -4,9 +4,6 @@ RawSerial pc(USBTX, USBRX); char command_category; Mail<uint8_t, 8> inCharQ; -float NewRotation; -float NewSpeed; -uint64_t NewKey; // Serial port methods @@ -25,18 +22,19 @@ { case 'R': // Rotation command - R-?\d{1,s4}(\.\d)? - Rotation_mutex.lock(); + NewMotorCommand_mutex.lock(); sscanf(serial_buffer.c_str(), "%c %f", &command_category, &NewRotation); pc.printf("You have a new rotation: %f \r\n", NewRotation); - Rotation_mutex.unlock(); + NewMotorCommand_mutex.unlock(); break; case 'V': // Speed command - V\d{1,3}(\.\d)? - Speed_mutex.lock(); + NewMotorCommand_mutex.lock(); sscanf(serial_buffer.c_str(), "%c %f", &command_category, &NewSpeed); pc.printf("You have a new speed: %f \r\n", NewSpeed); - Speed_mutex.unlock(); + NewSpeed_flag = true; + NewMotorCommand_mutex.unlock(); // to implement ! break; @@ -54,7 +52,7 @@ pc.printf("You have entered a new melody: --> TO IMPLEMENT <--- \r\n"); Music_mutex.unlock(); // to implement ! - + default: printf("Input value out of format - please try again! \r\n"); }
--- a/ES_CW2_Starter_STARFISH/main.cpp Mon Mar 09 15:02:47 2020 +0000 +++ b/ES_CW2_Starter_STARFISH/main.cpp Mon Mar 09 17:19:39 2020 +0000 @@ -3,11 +3,27 @@ #include "rtos.h" #include <stdlib.h> #include <string> -#include "mutexes.h" + +Mutex NewKey_mutex; +Mutex NewMotorCommand_mutex; +Mutex Music_mutex; + +// Global Variable Shared between threads +float NewRotation; +float NewSpeed; +uint64_t NewKey; + +bool NewSpeed_flag; +bool NewRotation_flag; + #include "SerialCommunication.h" #include "CryptoMining.h" #include "MotorControl.h" + + + + // Declaration of threads Thread thread_crypto; Thread thread_processor; @@ -16,23 +32,46 @@ Timer timer_nonce; uint32_t previous_time; - //Main int main() { pc.attach(&serialISR); - thread_motorCtrl.start(motorCtrlFn); - // Initialize threads and timers timer_nonce.start(); thread_crypto.start(thread_crypto_print); thread_processor.start(thread_processor_callback); + //NewSpeed = 30; + //thread_motorCtrl.start(&NewSpeed, motorCtrlFn); + uint8_t hash[32]; while (1) { + //Luccas' Code until we can move rest into seperate thread + + NewMotorCommand_mutex.lock(); + + pc.printf("Thread Actual %d\r\n", thread_motorCtrl.get_state()); + pc.printf("Thread state Expected %d\r\n",Thread::Running); + + + + if(NewSpeed_flag && thread_motorCtrl.get_state() != Thread::Deleted){ + pc.printf("Ending Previous Thread"); + thread_motorCtrl.terminate(); + } + + if(NewSpeed_flag){ + pc.printf("starting thread"); + thread_motorCtrl.start(&NewSpeed, motorCtrlFn); + NewSpeed_flag = false; + } + + NewMotorCommand_mutex.unlock(); + + wait(1); + // Set main as lowest priority thread - NewKey_mutex.lock(); *key = NewKey; NewKey_mutex.unlock();
--- a/ES_CW2_Starter_STARFISH/mutexes.h Mon Mar 09 15:02:47 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ -Mutex NewKey_mutex; -Mutex Speed_mutex; -Mutex Music_mutex; -Mutex Rotation_mutex;