Init

Dependents:   JetflyerMotorController

Files at this revision

API Documentation at this revision

Comitter:
skrickl
Date:
Thu Jul 13 13:42:42 2017 +0000
Parent:
0:375efcba6fef
Commit message:
bla

Changed in this revision

Brake.cpp Show annotated file Show diff for this revision Revisions of this file
Brake.h Show annotated file Show diff for this revision Revisions of this file
Break.cpp Show diff for this revision Revisions of this file
Break.h Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Brake.cpp	Thu Jul 13 13:42:42 2017 +0000
@@ -0,0 +1,121 @@
+#include "Brake.h"
+
+Brake::Brake() : _brakeButton(PIN_BUTTON_BRAKE), _pwm(PIN_PWM_BRAKE),
+                 _dir(PIN_DIR_BRAKE)
+{    
+    _pwm.period_ms(20); //needs to be changed!!!
+    Timer t1;  
+    Timer t2;  
+    t1.start();
+    percentage_old=0;
+     int state_brakeButton_old=0;
+    enable_drive =0;
+    direction=1; // 1 forward, -1 backward
+}
+
+Brake::~Brake()
+{
+    
+}
+
+void Brake::calibrateServo()
+{
+ 
+    
+}
+
+
+void Brake::brake(int percentage, int speed)
+{
+    
+
+    if(percentage != percentage_old)
+        {
+            enable_drive =1;
+            
+            if(percentage_old < percentage)
+            {
+                direction = 1; // forward   
+            }
+            else
+            {
+                 direction = -1;  // backward
+            }
+            
+        }
+      
+////////////
+
+if( state_brakeButton_old != _dir)
+{
+  calculated_position=0; // recalibrate every time it switch state changes  
+}
+
+////////////////
+
+    if( enable_drive == 1)
+    {
+        if( percentage == 0)
+        {
+            drive_homeposition();
+        }
+        else
+        {
+            
+            drive_brake(direction, speed);
+            diff_time = t1.read_ms() - ref_time;
+                   
+            
+            calculated_position = calculated_position + (((((float)speed)/100.0f)*MAX_SPEED_BRAKE* (((float)diff_time)/1000.0)*direction))/(MAX_ROTATIONS_FOR_BRAKEING);
+            
+            //maximum speed 5 rpm
+            
+            if( abs(calculated_position - percentage) < HYSTERESIS)
+            {
+                enable_drive =0;
+            }
+        }
+
+    }
+    
+  
+if(diff_time > 600000) // After 10 Minutes Timer is Reset, avoid overflow
+{
+  t1.reset();  
+}    
+
+state_brakeButton_old = _dir;
+ref_time = t1.read_ms();
+}
+
+int Brake::drive_homeposition()
+{
+    if( _brakeButton ) // check if 1 or 0 is engaged
+    {
+        drive_brake(-1, 100.0);
+    }
+    else
+    {
+         drive_brake(1, 0); // home position reached
+    }
+    
+    
+}
+int Brake::drive_brake(int direction, int _speed)
+{
+    _pwm.write(( float(abs(_speed)))/100.0f);
+   
+   if(direction==1)
+   {
+    _dir = 1; // engage brakes
+   }
+   else
+   {
+      _dir = 0; // release brakes
+   }
+}
+
+bool Brake::getBrakeing()
+{
+    return _brakeing;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Brake.h	Thu Jul 13 13:42:42 2017 +0000
@@ -0,0 +1,52 @@
+#include "mbed.h"
+#include "Controller.h"
+
+#ifndef BRAKE_H
+#define BRAKE_H
+
+#define PIN_BUTTON_BRAKE p22 //or whatever
+#define PIN_PWM_BRAKE p21 //or whatever
+#define PIN_DIR_BRAKE p23 //or whatever
+
+#define CALIBRATION_FACTOR 200 // Steps per Rotation
+
+#define MAX_SPEED_BRAKE     3 // 3 rotations per second
+#define MAX_ROTATIONS_FOR_BRAKEING 10.0 //still needs to 
+#define HYSTERESIS 5
+
+class Brake
+{
+    public:
+    
+    Brake();
+    ~Brake();
+    
+    void calibrateServo();
+    bool getBrakeing();
+    void brake(int percantage, int speed);
+        
+    private:
+    
+    int drive_homeposition(void);
+    int drive_brake(int _percentage, int _speed);
+    bool _brakeing;
+    DigitalIn _brakeButton;
+    PwmOut _pwm;
+    DigitalOut _dir;
+    float _percantage;
+    bool brake_switch;
+    int ref_time;
+    int cur_time;
+    int percentage_old;
+    int enable_drive;
+    int calculated_position;
+    int direction;
+    int diff_time;
+    int state_brakeButton_old;
+    
+  
+    Timer t1; 
+    Timer t2; 
+};
+
+#endif
\ No newline at end of file