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

Dependencies:   mbed ros_lib_melodic

src/MotorControl.h

Committer:
krogedal
Date:
2021-05-31
Revision:
3:4b6080e86761
Parent:
0:441289ea4e29
Child:
5:44b2454a5eea

File content as of revision 3:4b6080e86761:

#ifndef KARBOT_MOTOR_CONTROL_H
#define KARBOT_MOTOR_CONTROL_H

/* Karbot motor control class
 * Written by Simon Krogedal
 * 27/05/21
 * Team 9 4th Year project
 * 
 * for NUCLEO-F401RE
 * 
 */
 
 #include "mbed.h"
 #include "encoder.h"
 #include "motor.h"

/* 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 object
        
        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);      // Calculate current error
        
        void algorithm(void);       // Control algorithm
    
    public:
        // 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 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