Simon Krogedal / Karbot_wheel_control

Dependencies:   mbed ros_lib_melodic

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MotorControl.h Source File

MotorControl.h

Go to the documentation of this file.
00001 /**
00002  * @file MotorControl.h
00003  *
00004  * Class to control a motor with an encoder, inherits from the encoder class.
00005  *
00006  * @author Simon Krogedal
00007  *
00008  * @version 0.1
00009  */
00010 
00011 #ifndef KARBOT_MOTOR_CONTROL_H
00012 #define KARBOT_MOTOR_CONTROL_H
00013 
00014 /* Karbot motor control class
00015  * Written by Simon Krogedal
00016  * 27/05/21
00017  * Team 9 4th Year project
00018  * 
00019  * for NUCLEO-F401RE
00020  * 
00021  */
00022  
00023  #include "encoder.h"
00024  #include "motor.h"
00025 
00026  /** Motor Controller Class
00027  *
00028  * This class controls the speed of an individual motor, helping the system keep
00029  * symmetric with asymmetric components. It is based on the encoder class
00030  * and contains a pointer to a motor object. With two of these the motion
00031  * controller can drive the robot to the desired points.
00032  *
00033  * @author Simon Krogedal
00034  *
00035  * Written by Simon Krogedal
00036  *
00037  *
00038  * Team 9 4th Year project
00039  * 
00040  *
00041  * for NUCLEO-F401RE
00042  * 
00043  * @version 0.1
00044  *
00045  */
00046 class MotorControl : public encoder {
00047     
00048     private:
00049     
00050         motor*      mot;            // motor object
00051         
00052         double      Kp,             // Proportional gain
00053                     Ti,             // Integral gain time
00054                     r_speed,        // Speed set point
00055                     r_clicks,       // Same but in the "encoder language"
00056                     max_speed,      // The max speed of the motor (not used)
00057                     output,         // PWM output duty cycle
00058                     prev_error;     // Previous error, used for intergral control
00059                     
00060         bool        dir,            // Drivign direction (not used)
00061                     max_out;        // Flag triggered when output is saturated (not used)
00062         
00063         /** Get the current tracking error
00064          * @return Current tracking error
00065          */
00066         double getError(void);
00067         
00068         /// Control algorithm, called intermittedly by ticker object
00069         void algorithm(void);
00070     
00071     public:
00072         /** Creates an instance
00073          *
00074          * @param EncoderChanA Encoder channel A pin, set up using internal pullup
00075          * @param EncoderChanB Encoder channel B pin, set up using internal pullup
00076          * @param CPR Encoder counts per revolution
00077          * @param side Left side or right side motor, defines whether counter-clockwise rotation is forwards (left) or backwards (right) (when looking at the robot from outside)
00078          * @param period Sampling period for control algorithm and speed calculation from encoder readings
00079          * @param Motor Pointer to the motor object to be controlled
00080          * @param MaxSpeed Maximum speed of the motor, no set points above this value will be accepted
00081          * @param kp Proportional control gain
00082          * @param ti Integral control time
00083          * @param diameter Wheel diameter in meters
00084          */
00085         MotorControl(PinName EncoderChanA, PinName EncoderChanB, int CPR, encoder::Side side, double period, motor* Motor, double MaxSpeed, double kp, double ti, double diameter);
00086         
00087         /** Set the speed set point of the controller
00088          * This can be done while driving or while stopped, though the update will only be reflected while driving.
00089          * @param speed Speed set point in m/s
00090          */
00091         void setSpeed(double speed);
00092         
00093         /** Start driving
00094          * Attaches a ticker object to the control algorithm, running it at the frequency specified in the constructor
00095          */
00096         void drive(void);
00097         
00098         /** Stops the control algorithm
00099          * Stops the motors and detaches the ticker callback
00100          */
00101         void stop(void);
00102         
00103         /** Drives the motor in open loop
00104          * No ticket object is attaced, the motors are just activated.
00105          * Note that this also means new speed set points will not be passed to the motor unless this function is called again!
00106          */
00107         void driveManual(void);
00108         
00109         /** Sample the encoders to read speed value
00110          * This function does not return anything, the speed must then be obtained through getSpeed()
00111          */
00112         void samplecall(void);
00113         
00114         /** Set proportional gain
00115          * This function is useful for Ziegler-Nichols tuning
00116          *
00117          * @param k Controller proportional gain
00118          */
00119         void setK(double k);        // Set gain (used for Z-N tuning
00120         
00121         //void start(void);           // This function is overridden to avoid bugs
00122         
00123         /** Returns the current duty cycle, useful for debugging
00124          * @return Current duty cycle
00125          */
00126         double getOutput(void);     // Returns current duty cycle
00127         
00128         /** Checks whether the output saturation flag is set
00129          *
00130          * @return True: Output is saturated
00131          * @return False: Output is not saturated
00132          */
00133         bool checkFlag(void);       // Check whether max out flag is set
00134 };
00135 
00136 #endif