Ya kno it
Motor.h@0:aefd03ad04e6, 2017-10-30 (annotated)
- Committer:
- BramS23
- Date:
- Mon Oct 30 13:50:58 2017 +0000
- Revision:
- 0:aefd03ad04e6
Changed Homing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BramS23 | 0:aefd03ad04e6 | 1 | #include "mbed.h" |
BramS23 | 0:aefd03ad04e6 | 2 | #include "QEI.h" |
BramS23 | 0:aefd03ad04e6 | 3 | #include "PID.h" |
BramS23 | 0:aefd03ad04e6 | 4 | /** Motorclass |
BramS23 | 0:aefd03ad04e6 | 5 | Used to control Servo motors |
BramS23 | 0:aefd03ad04e6 | 6 | */ |
BramS23 | 0:aefd03ad04e6 | 7 | class Motor{ |
BramS23 | 0:aefd03ad04e6 | 8 | public: |
BramS23 | 0:aefd03ad04e6 | 9 | /** Motor class declaration |
BramS23 | 0:aefd03ad04e6 | 10 | @param PWM Pin |
BramS23 | 0:aefd03ad04e6 | 11 | @param Direction Pin |
BramS23 | 0:aefd03ad04e6 | 12 | @param Encoder Pin1 |
BramS23 | 0:aefd03ad04e6 | 13 | @param Encoder Pin2 |
BramS23 | 0:aefd03ad04e6 | 14 | @param Homingswitch |
BramS23 | 0:aefd03ad04e6 | 15 | @param controller Interval |
BramS23 | 0:aefd03ad04e6 | 16 | */ |
BramS23 | 0:aefd03ad04e6 | 17 | Motor(PinName PWM, PinName Direction, PinName Enc1,PinName Enc2,PinName HomingSW,float interval); |
BramS23 | 0:aefd03ad04e6 | 18 | |
BramS23 | 0:aefd03ad04e6 | 19 | /** GotoPos function |
BramS23 | 0:aefd03ad04e6 | 20 | This function Sends the motor to the requested Position. |
BramS23 | 0:aefd03ad04e6 | 21 | This Function needs to be called repeadedly since it also |
BramS23 | 0:aefd03ad04e6 | 22 | triggers one tick of the PID controller |
BramS23 | 0:aefd03ad04e6 | 23 | @param The required Position in radians |
BramS23 | 0:aefd03ad04e6 | 24 | */ |
BramS23 | 0:aefd03ad04e6 | 25 | void GotoPos(float Rad); |
BramS23 | 0:aefd03ad04e6 | 26 | |
BramS23 | 0:aefd03ad04e6 | 27 | /** Outputs the current motor position in radians, while keeping the gear ratio into account |
BramS23 | 0:aefd03ad04e6 | 28 | */ |
BramS23 | 0:aefd03ad04e6 | 29 | float GetPos(); |
BramS23 | 0:aefd03ad04e6 | 30 | |
BramS23 | 0:aefd03ad04e6 | 31 | /** Homes the motor with the homing pin |
BramS23 | 0:aefd03ad04e6 | 32 | @param Direction |
BramS23 | 0:aefd03ad04e6 | 33 | @param PWM value |
BramS23 | 0:aefd03ad04e6 | 34 | @param HomingPosition in radians |
BramS23 | 0:aefd03ad04e6 | 35 | */ |
BramS23 | 0:aefd03ad04e6 | 36 | void Homing(bool direction, float PWM, float HomingPos); |
BramS23 | 0:aefd03ad04e6 | 37 | |
BramS23 | 0:aefd03ad04e6 | 38 | /** Set the Pid values |
BramS23 | 0:aefd03ad04e6 | 39 | @param Pvalue |
BramS23 | 0:aefd03ad04e6 | 40 | @param Ti |
BramS23 | 0:aefd03ad04e6 | 41 | @param Td |
BramS23 | 0:aefd03ad04e6 | 42 | */ |
BramS23 | 0:aefd03ad04e6 | 43 | void SetPID(float P,float Ti,float Td); |
BramS23 | 0:aefd03ad04e6 | 44 | |
BramS23 | 0:aefd03ad04e6 | 45 | /** Set the motor gear ratio |
BramS23 | 0:aefd03ad04e6 | 46 | */ |
BramS23 | 0:aefd03ad04e6 | 47 | void SetGearRatio(float GearRatio); |
BramS23 | 0:aefd03ad04e6 | 48 | |
BramS23 | 0:aefd03ad04e6 | 49 | /** Set the inputlimits, or the allowed Position |
BramS23 | 0:aefd03ad04e6 | 50 | @param InputMinimum |
BramS23 | 0:aefd03ad04e6 | 51 | @param InputMaximum |
BramS23 | 0:aefd03ad04e6 | 52 | */ |
BramS23 | 0:aefd03ad04e6 | 53 | void SetInputLimits(float Imin, float Imax); |
BramS23 | 0:aefd03ad04e6 | 54 | |
BramS23 | 0:aefd03ad04e6 | 55 | /** Set the output Limits of the PWM |
BramS23 | 0:aefd03ad04e6 | 56 | @param OutputMinimum |
BramS23 | 0:aefd03ad04e6 | 57 | @param OutputMaximum |
BramS23 | 0:aefd03ad04e6 | 58 | */ |
BramS23 | 0:aefd03ad04e6 | 59 | void SetOutputLimits(float Omin, float Omax); |
BramS23 | 0:aefd03ad04e6 | 60 | |
BramS23 | 0:aefd03ad04e6 | 61 | /** Stops the motor |
BramS23 | 0:aefd03ad04e6 | 62 | */ |
BramS23 | 0:aefd03ad04e6 | 63 | void Stop(); |
BramS23 | 0:aefd03ad04e6 | 64 | |
BramS23 | 0:aefd03ad04e6 | 65 | private: |
BramS23 | 0:aefd03ad04e6 | 66 | QEI encoder; |
BramS23 | 0:aefd03ad04e6 | 67 | float _GearRatio; |
BramS23 | 0:aefd03ad04e6 | 68 | |
BramS23 | 0:aefd03ad04e6 | 69 | PwmOut MotorThrottle; |
BramS23 | 0:aefd03ad04e6 | 70 | DigitalOut MotorDirection; |
BramS23 | 0:aefd03ad04e6 | 71 | DigitalIn HomingSwitch; |
BramS23 | 0:aefd03ad04e6 | 72 | |
BramS23 | 0:aefd03ad04e6 | 73 | PID controller; |
BramS23 | 0:aefd03ad04e6 | 74 | }; |