Wheel control software for satellite microcontroller running the motors on the Karbor

Dependencies:   mbed ros_lib_melodic

Revision:
3:4b6080e86761
Parent:
0:441289ea4e29
Child:
5:44b2454a5eea
--- a/src/MotorControl.h	Thu May 27 19:26:21 2021 +0000
+++ b/src/MotorControl.h	Mon May 31 16:44:26 2021 +0000
@@ -13,36 +13,44 @@
  #include "mbed.h"
  #include "encoder.h"
  #include "motor.h"
- 
-class MotorControl : public encoder {                                   //controls the speed of an individual motor, helping the system keep symmetric with asymmetric components
+
+/* This class controls the speed of an individual motor, helping the system keep
+ * symmetric with asymmetric components. It is based on the encoder class
+ * and contains a pointer to a motor object. With two of these the motion
+ * controller can drive the robot to the desired points.
+ */
+class MotorControl : public encoder {
     private:
-        motor*      mot;
+        motor*      mot;            // motor object
         
-        double      Kp, Ki, r_speed, r_clicks, max_speed, output, acc_err;
-        bool        dir, max_out;                                       //driving direction and error flag
+        double      Kp,             // Proportional gain
+                    Ti,             // Integral gain time
+                    r_speed,        // Speed set point
+                    r_clicks,       // Same but in the "encoder language"
+                    max_speed,      // The max speed of the motor (not used)
+                    output,         // PWM output duty cycle
+                    prev_error;     // Previous error, used for intergral control
+                    
+        bool        dir,            // Drivign direction (not used)
+                    max_out;        // Flag triggered when output is saturated (not used)
         
-        double getError(void);
+        double getError(void);      // Calculate current error
         
-        void algorithm(void);
+        void algorithm(void);       // Control algorithm
     
     public:
-        MotorControl(PinName a, PinName b, PinName i, int r, bool c, double p, motor* m, double ms, double kp, double ki, double ec);
-        
-        void setSpeed(double s);
-        void drive(void);
-        
-        void stop(void);
-        
-        void driveManual(void);
+        // Constructor takes encoder pins and constants, and a motor object and contorller gains
+        MotorControl(PinName a, PinName b, PinName i, int r, bool c, double p, motor* m, double ms, double kp, double ti, double ec);
         
-        void samplecall(void);
-        
-        void setK(double k);
-
-        void start(void);       //this function is overridden to avoid bugs
-//      double getError(void);
-        double getOutput(void);
-        bool checkFlag(void);
+        void setSpeed(double s);    // Set the speed of the wheel
+        void drive(void);           // Drive at set speet
+        void stop(void);            // Stop motor
+        void driveManual(void);     // Drive at set speed in open loop
+        void samplecall(void);      // Sample encoder and update read speed value
+        void setK(double k);        // Set gain (used for Z-N tuning
+        void start(void);           // This function is overridden to avoid bugs
+        double getOutput(void);     // Returns current duty cycle
+        bool checkFlag(void);       // Check whether max out flag is set
 };
 
 #endif
\ No newline at end of file