PM2_Lib

Dependencies:   LSM9DS1 RangeFinder FastPWM

Revision:
10:fe74e8909d3f
Parent:
9:583dbd17e0ba
Child:
19:518ed284d98b
--- a/SpeedController.cpp	Tue Aug 31 15:38:44 2021 +0000
+++ b/SpeedController.cpp	Thu Feb 10 12:04:36 2022 +0000
@@ -1,7 +1,5 @@
 #include "SpeedController.h"
 
-using namespace std;
-
 const float SpeedController::TS = 0.001f;                       // period of 1 ms
 const float SpeedController::LOWPASS_FILTER_FREQUENCY = 100.0f; // given in [rad/s]
 const float SpeedController::MIN_DUTY_CYCLE = 0.02f;            // minimum duty-cycle
@@ -10,15 +8,15 @@
 SpeedController::SpeedController(float counts_per_turn, float kn, float max_voltage, FastPWM& pwm, EncoderCounter& encoderCounter) : pwm(pwm), encoderCounter(encoderCounter), thread(osPriorityHigh, 4096)
 {
     this->counts_per_turn = counts_per_turn;
-    this->kn = kn;
-    this->kp = 0.1f;
+    setFeedForwardGain(kn);
+    setSpeedCntrlGain(0.1f);
     this->max_voltage = max_voltage;
 
-    // Initialisieren der PWM Ausgaenge
-    pwm.period(0.00005); // PWM Periode von 50 us
-    pwm.write(0.5);      // Duty-Cycle von 50%
+    // initialise pwm
+    pwm.period(0.00005); // pwm period of 50 us
+    pwm.write(0.5);      // duty-cycle of 50%
 
-    // Initialisieren von lokalen Variabeln
+    // initialise
     previousValueCounter = encoderCounter.read();
     speedFilter.setPeriod(TS);
     speedFilter.setFrequency(LOWPASS_FILTER_FREQUENCY);
@@ -26,64 +24,69 @@
     actualSpeed = 0.0f;
     // actualAngle = 0.0f;
 
-    // Starten des periodischen Tasks
+    // set up thread
     thread.start(callback(this, &SpeedController::run));
-    ticker.attach(callback(this, &SpeedController::sendThreadFlag), TS);
-}
-
-SpeedController::SpeedController(float counts_per_turn, float kn, float kp, float max_voltage, FastPWM& pwm, EncoderCounter& encoderCounter) : pwm(pwm), encoderCounter(encoderCounter), thread(osPriorityHigh, 4096)
-{
-    this->counts_per_turn = counts_per_turn;
-    this->kn = kn;
-    this->kp = kp;
-    this->max_voltage = max_voltage;
-
-    // Initialisieren der PWM Ausgaenge
-    pwm.period(0.00005); // PWM Periode von 50 us
-    pwm.write(0.5);      // Duty-Cycle von 50%
-
-    // Initialisieren von lokalen Variabeln
-    previousValueCounter = encoderCounter.read();
-    speedFilter.setPeriod(TS);
-    speedFilter.setFrequency(LOWPASS_FILTER_FREQUENCY);
-    desiredSpeed = 0.0f;
-    actualSpeed = 0.0f;
-    // actualAngle = 0.0f;
-
-    // Starten des periodischen Tasks
-    thread.start(callback(this, &SpeedController::run));
-    ticker.attach(callback(this, &SpeedController::sendThreadFlag), TS);
+    ticker.attach(callback(this, &SpeedController::sendThreadFlag), std::chrono::microseconds{static_cast<long int>(1.0e6f * TS)});
 }
 
 SpeedController::~SpeedController()
 {
-    ticker.detach(); // Stoppt den periodischen Task
+    ticker.detach();
 }
 
+/**
+ * Sets the desired speed in RPM (rotations per minute).
+ */
 void SpeedController::setDesiredSpeedRPM(float desiredSpeed)
 {
     this->desiredSpeed = desiredSpeed;
 }
 
+/**
+ * Reads the speed in RPM (rotations per minute).
+ * @return actual speed in RPM.
+ */
 float SpeedController::getSpeedRPM()
 {
     return actualSpeed;
 }
 
+/**
+ * Sets the desired speed in RPS (rotations per second).
+ */
 void SpeedController::setDesiredSpeedRPS(float desiredSpeed)
 {
     this->desiredSpeed = desiredSpeed*60.0f;
 }
 
+/**
+ * Reads the speed in RPS (rotations per second).
+ * @return actual speed in RPS.
+ */
 float SpeedController::getSpeedRPS()
 {
     return actualSpeed/60.0f;
 }
 
+/**
+ * Sets the feed-forward gain.
+ */
+void SpeedController::setFeedForwardGain(float kn)
+{
+    this->kn = kn;
+}
+
+/**
+ * Sets the gain of the speed controller (p-controller).
+ */
+void SpeedController::setSpeedCntrlGain(float kp)
+{
+    this->kp = kp;
+}
+
 void SpeedController::run()
 {
     while(true) {
-
         // wait for the periodic signal
         ThisThread::flags_wait_any(threadFlag);
 
@@ -102,7 +105,6 @@
         if (dutyCycle < MIN_DUTY_CYCLE) dutyCycle = MIN_DUTY_CYCLE;
         else if (dutyCycle > MAX_DUTY_CYCLE) dutyCycle = MAX_DUTY_CYCLE;
         pwm.write(static_cast<double>(dutyCycle));
-
     }
 }