Matthieu Rousseau / Motor

Files at this revision

API Documentation at this revision

Comitter:
Matthieu_
Date:
Wed Nov 06 14:22:03 2019 +0000
Commit message:
First Commit;

Changed in this revision

Motor.cpp Show annotated file Show diff for this revision Revisions of this file
Motor.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.cpp	Wed Nov 06 14:22:03 2019 +0000
@@ -0,0 +1,138 @@
+#include "Motor.h"
+#include "mbed.h"
+ 
+
+ 
+L298HBridge::L298HBridge(PinName ENpin, PinName FWDpin, PinName REVpin, PinName chanApin, PinName chanBpin) : _en(ENpin), _fwd(FWDpin), _rev(REVpin), _chanA(chanApin), _chanB(chanBpin) 
+{
+    _fwd = 0;
+    _rev = 0;
+    _en = 0.0;
+    
+    _Time.start(); //start timer
+    _currentSpeed = 0.0; //the motor is stopped
+    _lastAUp = 0.0;
+    
+     _rpm_index = rpm.size();
+    rpm.push_back(0);
+        
+
+    _chanA.rise(callback(this, &L298HBridge::rising)); //attach the function
+    
+    
+    
+    _gainProp = 1;
+    _gainInt = 0.1;
+    
+    _maxSpeed = 290.0; //RPM
+    _lastOut = 0;
+   
+    
+    }
+ 
+void L298HBridge::Fwd() 
+{    
+    _fwd = 1;
+    _rev = 0;
+}
+
+void L298HBridge::Rev() 
+{
+    _fwd = 0;
+    _rev = 1;
+}
+
+void L298HBridge::Stop() 
+{
+    _fwd = 1;
+    _rev = 1;
+    
+    
+}
+
+
+ 
+void L298HBridge::SetSpeed(float PWMPercentage) 
+{
+    printf("%d", count_ini);
+    if(PWMPercentage>0){
+        Fwd();
+        _en = PWMPercentage;
+    }
+    else if(PWMPercentage<0){
+        Rev();
+        _en = -PWMPercentage;
+    }
+    else{
+        Stop();
+    }
+}
+
+void L298HBridge::rising(){
+       float tempTime= _Time.read();
+       if(_chanB){
+        _currentSpeed =  60.0f/(11.0f*(tempTime - _lastAUp))/45.0f;
+        }
+        else{
+            this->_currentSpeed = -60.0f/(11.0f*(tempTime - _lastAUp))/45.0f;
+            
+        }
+        rpm[_rpm_index] = _currentSpeed;
+        //test = _currentSpeed;
+        
+        
+        /*
+        if (fabs(_currentSpeed) < 35.0f){
+            _currentSpeed = 0.0;    
+        }*/
+        //printf("C %d\n\r", _rpm_index);
+       _lastAUp = tempTime;   
+       
+       //printf("update\r\n");
+       }
+       
+float L298HBridge::GetSpeed(int index){
+       return rpm[rpm.size() - index];
+       
+}
+/*       
+void L298HBridge::SetAsser(float GainProp, float GainInt){
+       _gainProp = GainProp;
+       _gainInt = GainInt;
+       }
+       
+void L298HBridge::SetSpeedA(float SpeedRPM){
+    float error = SpeedRPM - _currentSpeed;
+    float commande = error * _gainProp ;
+    if (commande >0 ){
+        this->Fwd();
+        this->SetSpeed(12/fabs(commande));
+    } 
+    else{
+        this->Rev();
+        this->SetSpeed(12/fabs(commande));
+    }
+    _lastOut = commande;     
+    }
+*/    
+float L298HBridge::GetU(){
+   return _en;
+   } 
+
+string L298HBridge::state(){
+    if(_fwd  and not(_rev)){
+        return  "fwd";
+        }
+    else if (_rev and not(_fwd)){
+        return "rev";
+        }
+    else if (_rev == _fwd){
+        return "stop";   
+
+    }
+    else {
+        return "error";
+        }
+    }
+
+       
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.h	Wed Nov 06 14:22:03 2019 +0000
@@ -0,0 +1,135 @@
+#ifndef L298HBridge_H
+#define L298HBridge_H
+ 
+#include "mbed.h"
+#include <string>
+#include <vector>
+using namespace std;
+ 
+/** Class library for a L298 H-Bridge.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "L298HBridge.h"
+ *
+ * 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
+ *
+ * int main() 
+ * {
+ *    while(1)
+ *    {
+ *          dcmotor.Fwd();               //setting the motor to spin forward
+ *          dcmotor.SetSpeed(0.5);       //setting the speed the motor will spin
+ *    }    
+ * }
+ * @endcode
+ */
+ 
+class L298HBridge {
+  public:
+    /** Create a L298HBridge object connected to the specified pins. Once created, The pins still need
+    * to be set to a direction and also the speed via PWM.
+    * @param ENpin PwmOut compatible pin used to set the speed of the motor.
+    * @param FWDpin Used to spin the motor in the forward direction.
+    * @param REVDpin Used to spin the motor in the Reverse direction.
+    * @param chanA digital interrupt to trigger the speed calculation.
+    * @param chanB digital input to get the sens of rotation.
+    */
+    L298HBridge(PinName ENpin, PinName FWDpin, PinName REVpin, PinName chanApin, PinName chanBpin);
+    
+    /** Setting the DC motor to spin in the forward direction.
+    * @param 
+    *     None
+    */
+    void Fwd();
+    
+    /** Setting the DC motor to spin in the Revers direction.
+    * @param 
+    *     None
+    */
+    void Rev();
+    
+     /** Stopping the motor.
+    * @param 
+    *     None
+    */
+    void Stop();
+    
+     /** Set the speed of the motor.
+    * @param 
+    *     PWMPercentage, variable to set te speed of the motor (any value from 0.1 - 1).
+    * @return
+    *     None
+    */
+    void SetSpeed(float PWMPercentage);
+    
+    /** Get the speed of the motor
+    * @param
+    *   None
+    * @return
+    *   Speed, the current speed of the motor
+    */
+    float GetSpeed(int index);
+    
+    /** Set the speed (RPM) of the motor
+    * @param
+    *   SpeedRPM, the Speed in RPM the motor must reach 
+    * @return
+    *  None
+    */
+    void SetSpeedA(float SpeedRPM);
+    
+    /** change the parameters of control
+    * @param
+    *   GainProp, the value of the propotionnal gain used in the asservissment
+    *   GainInt, the value of the integral gain used in the asservissment
+    * @return
+    *   None
+    */
+    void SetAsser(float GainProp, float GainInt);
+    
+    /** Calculate the speed of the motor 
+    * @param
+    *   None
+    * @return
+    *   None
+    */
+    void rising();
+    
+    /** Return the calcul  ated input
+    * @param
+    *   None
+    *@ @return
+    *   percent, the last output calulated by the PI
+    */
+    float GetU();
+    
+    /** Return the state
+    * @param
+    *   None
+    *@ @return
+    *   state, 1 fwd, 2 rev, 0 stop
+    */
+    string state();
+    
+ 
+  private:
+    PwmOut _en;
+    DigitalOut _fwd, _rev;
+    InterruptIn _chanA;
+    DigitalIn _chanB;
+    float _currentSpeed;
+    float _lastAUp;
+    Timer _Time;
+    float _gainProp;
+    float _gainInt;
+    float _maxSpeed;
+    float _lastOut;
+    
+    int _rpm_index;
+};
+static std::vector<float> rpm;
+static int count_ini = 10;
+
+#endif