Matthieu Rousseau / Motor
Committer:
Matthieu_
Date:
Wed Nov 06 14:22:03 2019 +0000
Revision:
0:863eca4e24ff
First Commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Matthieu_ 0:863eca4e24ff 1 #ifndef L298HBridge_H
Matthieu_ 0:863eca4e24ff 2 #define L298HBridge_H
Matthieu_ 0:863eca4e24ff 3
Matthieu_ 0:863eca4e24ff 4 #include "mbed.h"
Matthieu_ 0:863eca4e24ff 5 #include <string>
Matthieu_ 0:863eca4e24ff 6 #include <vector>
Matthieu_ 0:863eca4e24ff 7 using namespace std;
Matthieu_ 0:863eca4e24ff 8
Matthieu_ 0:863eca4e24ff 9 /** Class library for a L298 H-Bridge.
Matthieu_ 0:863eca4e24ff 10 *
Matthieu_ 0:863eca4e24ff 11 * Example:
Matthieu_ 0:863eca4e24ff 12 * @code
Matthieu_ 0:863eca4e24ff 13 * #include "mbed.h"
Matthieu_ 0:863eca4e24ff 14 * #include "L298HBridge.h"
Matthieu_ 0:863eca4e24ff 15 *
Matthieu_ 0:863eca4e24ff 16 * L298HBridge dcmotor(PA_5, PA_6, PA_10); //Create a L298HBridge object with enable pin on PA5, Fwd pin or PA6 and Rev pin on PA10
Matthieu_ 0:863eca4e24ff 17 *
Matthieu_ 0:863eca4e24ff 18 * int main()
Matthieu_ 0:863eca4e24ff 19 * {
Matthieu_ 0:863eca4e24ff 20 * while(1)
Matthieu_ 0:863eca4e24ff 21 * {
Matthieu_ 0:863eca4e24ff 22 * dcmotor.Fwd(); //setting the motor to spin forward
Matthieu_ 0:863eca4e24ff 23 * dcmotor.SetSpeed(0.5); //setting the speed the motor will spin
Matthieu_ 0:863eca4e24ff 24 * }
Matthieu_ 0:863eca4e24ff 25 * }
Matthieu_ 0:863eca4e24ff 26 * @endcode
Matthieu_ 0:863eca4e24ff 27 */
Matthieu_ 0:863eca4e24ff 28
Matthieu_ 0:863eca4e24ff 29 class L298HBridge {
Matthieu_ 0:863eca4e24ff 30 public:
Matthieu_ 0:863eca4e24ff 31 /** Create a L298HBridge object connected to the specified pins. Once created, The pins still need
Matthieu_ 0:863eca4e24ff 32 * to be set to a direction and also the speed via PWM.
Matthieu_ 0:863eca4e24ff 33 * @param ENpin PwmOut compatible pin used to set the speed of the motor.
Matthieu_ 0:863eca4e24ff 34 * @param FWDpin Used to spin the motor in the forward direction.
Matthieu_ 0:863eca4e24ff 35 * @param REVDpin Used to spin the motor in the Reverse direction.
Matthieu_ 0:863eca4e24ff 36 * @param chanA digital interrupt to trigger the speed calculation.
Matthieu_ 0:863eca4e24ff 37 * @param chanB digital input to get the sens of rotation.
Matthieu_ 0:863eca4e24ff 38 */
Matthieu_ 0:863eca4e24ff 39 L298HBridge(PinName ENpin, PinName FWDpin, PinName REVpin, PinName chanApin, PinName chanBpin);
Matthieu_ 0:863eca4e24ff 40
Matthieu_ 0:863eca4e24ff 41 /** Setting the DC motor to spin in the forward direction.
Matthieu_ 0:863eca4e24ff 42 * @param
Matthieu_ 0:863eca4e24ff 43 * None
Matthieu_ 0:863eca4e24ff 44 */
Matthieu_ 0:863eca4e24ff 45 void Fwd();
Matthieu_ 0:863eca4e24ff 46
Matthieu_ 0:863eca4e24ff 47 /** Setting the DC motor to spin in the Revers direction.
Matthieu_ 0:863eca4e24ff 48 * @param
Matthieu_ 0:863eca4e24ff 49 * None
Matthieu_ 0:863eca4e24ff 50 */
Matthieu_ 0:863eca4e24ff 51 void Rev();
Matthieu_ 0:863eca4e24ff 52
Matthieu_ 0:863eca4e24ff 53 /** Stopping the motor.
Matthieu_ 0:863eca4e24ff 54 * @param
Matthieu_ 0:863eca4e24ff 55 * None
Matthieu_ 0:863eca4e24ff 56 */
Matthieu_ 0:863eca4e24ff 57 void Stop();
Matthieu_ 0:863eca4e24ff 58
Matthieu_ 0:863eca4e24ff 59 /** Set the speed of the motor.
Matthieu_ 0:863eca4e24ff 60 * @param
Matthieu_ 0:863eca4e24ff 61 * PWMPercentage, variable to set te speed of the motor (any value from 0.1 - 1).
Matthieu_ 0:863eca4e24ff 62 * @return
Matthieu_ 0:863eca4e24ff 63 * None
Matthieu_ 0:863eca4e24ff 64 */
Matthieu_ 0:863eca4e24ff 65 void SetSpeed(float PWMPercentage);
Matthieu_ 0:863eca4e24ff 66
Matthieu_ 0:863eca4e24ff 67 /** Get the speed of the motor
Matthieu_ 0:863eca4e24ff 68 * @param
Matthieu_ 0:863eca4e24ff 69 * None
Matthieu_ 0:863eca4e24ff 70 * @return
Matthieu_ 0:863eca4e24ff 71 * Speed, the current speed of the motor
Matthieu_ 0:863eca4e24ff 72 */
Matthieu_ 0:863eca4e24ff 73 float GetSpeed(int index);
Matthieu_ 0:863eca4e24ff 74
Matthieu_ 0:863eca4e24ff 75 /** Set the speed (RPM) of the motor
Matthieu_ 0:863eca4e24ff 76 * @param
Matthieu_ 0:863eca4e24ff 77 * SpeedRPM, the Speed in RPM the motor must reach
Matthieu_ 0:863eca4e24ff 78 * @return
Matthieu_ 0:863eca4e24ff 79 * None
Matthieu_ 0:863eca4e24ff 80 */
Matthieu_ 0:863eca4e24ff 81 void SetSpeedA(float SpeedRPM);
Matthieu_ 0:863eca4e24ff 82
Matthieu_ 0:863eca4e24ff 83 /** change the parameters of control
Matthieu_ 0:863eca4e24ff 84 * @param
Matthieu_ 0:863eca4e24ff 85 * GainProp, the value of the propotionnal gain used in the asservissment
Matthieu_ 0:863eca4e24ff 86 * GainInt, the value of the integral gain used in the asservissment
Matthieu_ 0:863eca4e24ff 87 * @return
Matthieu_ 0:863eca4e24ff 88 * None
Matthieu_ 0:863eca4e24ff 89 */
Matthieu_ 0:863eca4e24ff 90 void SetAsser(float GainProp, float GainInt);
Matthieu_ 0:863eca4e24ff 91
Matthieu_ 0:863eca4e24ff 92 /** Calculate the speed of the motor
Matthieu_ 0:863eca4e24ff 93 * @param
Matthieu_ 0:863eca4e24ff 94 * None
Matthieu_ 0:863eca4e24ff 95 * @return
Matthieu_ 0:863eca4e24ff 96 * None
Matthieu_ 0:863eca4e24ff 97 */
Matthieu_ 0:863eca4e24ff 98 void rising();
Matthieu_ 0:863eca4e24ff 99
Matthieu_ 0:863eca4e24ff 100 /** Return the calcul ated input
Matthieu_ 0:863eca4e24ff 101 * @param
Matthieu_ 0:863eca4e24ff 102 * None
Matthieu_ 0:863eca4e24ff 103 *@ @return
Matthieu_ 0:863eca4e24ff 104 * percent, the last output calulated by the PI
Matthieu_ 0:863eca4e24ff 105 */
Matthieu_ 0:863eca4e24ff 106 float GetU();
Matthieu_ 0:863eca4e24ff 107
Matthieu_ 0:863eca4e24ff 108 /** Return the state
Matthieu_ 0:863eca4e24ff 109 * @param
Matthieu_ 0:863eca4e24ff 110 * None
Matthieu_ 0:863eca4e24ff 111 *@ @return
Matthieu_ 0:863eca4e24ff 112 * state, 1 fwd, 2 rev, 0 stop
Matthieu_ 0:863eca4e24ff 113 */
Matthieu_ 0:863eca4e24ff 114 string state();
Matthieu_ 0:863eca4e24ff 115
Matthieu_ 0:863eca4e24ff 116
Matthieu_ 0:863eca4e24ff 117 private:
Matthieu_ 0:863eca4e24ff 118 PwmOut _en;
Matthieu_ 0:863eca4e24ff 119 DigitalOut _fwd, _rev;
Matthieu_ 0:863eca4e24ff 120 InterruptIn _chanA;
Matthieu_ 0:863eca4e24ff 121 DigitalIn _chanB;
Matthieu_ 0:863eca4e24ff 122 float _currentSpeed;
Matthieu_ 0:863eca4e24ff 123 float _lastAUp;
Matthieu_ 0:863eca4e24ff 124 Timer _Time;
Matthieu_ 0:863eca4e24ff 125 float _gainProp;
Matthieu_ 0:863eca4e24ff 126 float _gainInt;
Matthieu_ 0:863eca4e24ff 127 float _maxSpeed;
Matthieu_ 0:863eca4e24ff 128 float _lastOut;
Matthieu_ 0:863eca4e24ff 129
Matthieu_ 0:863eca4e24ff 130 int _rpm_index;
Matthieu_ 0:863eca4e24ff 131 };
Matthieu_ 0:863eca4e24ff 132 static std::vector<float> rpm;
Matthieu_ 0:863eca4e24ff 133 static int count_ini = 10;
Matthieu_ 0:863eca4e24ff 134
Matthieu_ 0:863eca4e24ff 135 #endif