Library to control the bike (just basic for now)

Dependents:   TORTUGA_BLE

Files at this revision

API Documentation at this revision

Comitter:
ptuytsch
Date:
Mon Jul 18 13:19:33 2016 +0000
Parent:
0:792a8f167ac0
Commit message:
adding full functionallity of tortuga in library (not the most beautiful way)

Changed in this revision

BikeControl.cpp Show annotated file Show diff for this revision Revisions of this file
BikeControl.h Show annotated file Show diff for this revision Revisions of this file
diff -r 792a8f167ac0 -r 39f462024f10 BikeControl.cpp
--- a/BikeControl.cpp	Mon Jul 18 09:25:53 2016 +0000
+++ b/BikeControl.cpp	Mon Jul 18 13:19:33 2016 +0000
@@ -1,16 +1,63 @@
 #include "mbed.h"
 #include "BikeControl.h"
+#include "BikeData.h"
+#include "BatteryState.h"
 
-BikeControl::BikeControl(){
+BikeControl::BikeControl(BikeData* bikeData, BatteryState* trailerBat, BatteryState* bikeBat, BatteryState* auxBat):
+    trailerCtrl(PB_1),
+    motorRightCtrl(PB_15),
+    motorRightCounter(0),
+    motorRightRpm(0),
+    motorLeftCtrl(PB_13),
+    motorLeftCounter(0),
+    motorLeftRpm(0),
+    motorRightHall(PC_13),
+    motorLeftHall(PC_1),
+    
+    //BRAKE
+    brakeFront(PC_10),
+    brakeRear(PC_12),
+    
+    //GENERATOR
+    generatorHallA(PD_2),
+    generatorBrake(PC_9),
+    generatorHallACounter(0),
+    generatorHallARpm(0),
+    generatorHallB(PC_3),
+    generatorHallBCounter(0),
+    generatorHallBRpm(0),
+    
+    //BUTTONS ON STEERING
+    //userButton(USER_BUTTON),
+    buttonGreen(PB_11),
+    buttonRed(PB_12),
+    buttonDirectionRight(PA_15),
+    buttonDirectionLeft(PB_7),
+    
+    //SWITCH
+    switchOn(PC_11),
+    switchWalk(PB_2),
+    
+    //LIGHT
+    lightFront(PB_14),
+    lightBack(PA_11),
+    lightLeft(PC_6),
+    lightRight(PC_8),
+    
+    bd(bikeData),
+    bikeBat(bikeBat),
+    trailerBat(trailerBat),
+    auxBat(auxBat)
+{
         //PullUp on userButton input
-        userButton.mode(PullUp);
+        //userButton.mode(PullUp);
         //PullUp on brake inputs
         brakeFront.mode(PullUp);
         brakeRear.mode(PullUp);
     
         //Call function on rise of interruptpin
-        generatorHallA.rise(&generatorHallAPulsed);
-        generatorHallB.rise(&generatorHallBPulsed);
+        generatorHallA.rise(this, &BikeControl::generatorHallAPulsed);
+        generatorHallB.rise(this, &BikeControl::generatorHallBPulsed);
         generatorBrake.period_us(50);
         generatorBrake.write(0.5f);
         generatorHallACounter=0;
@@ -21,10 +68,10 @@
         //motor
         motorRightCtrl.period_ms(100);
         motorRightCtrl.write(0.0f);
-        motorRightHall.rise(&motorRightPulsed);
+        motorRightHall.rise(this, &BikeControl::motorRightPulsed);
         motorLeftCtrl.period_ms(100);
         motorLeftCtrl.write(0.0f);
-        motorLeftHall.rise(&motorLeftPulsed);
+        motorLeftHall.rise(this, &BikeControl::motorLeftPulsed);
     
         //Button inputs
         buttonGreen.mode(PullUp);
@@ -35,4 +82,169 @@
         //SWITCH
         switchOn.mode(PullUp);
         switchWalk.mode(PullUp);
+        
+        //t.attach(this, &BikeControl::periodicCallback, 0.5);
+    }
+    
+void BikeControl::startControlLoop(){
+    t.attach(this, &BikeControl::periodicCallback, 0.5);
+    }
+    
+void BikeControl::periodicCallback(){
+        //printf("callback\n");
+        generatorHallARpm = generatorHallACounter*2; //60 rising edges/ rotation means Rpm is equal to the counter value in 1 sec
+        generatorHallACounter = 0;
+        generatorHallBRpm = generatorHallBCounter*2; //60 rising edges/ rotation means Rpm is equal to the counter value in 1 sec
+        generatorHallBCounter = 0;
+        motorRightRpm = motorRightCounter*2;
+        motorRightCounter = 0;
+        motorLeftRpm = motorLeftCounter*2;
+        motorLeftCounter = 0;
+        if (generatorHallARpm>0) {
+            printf("RPM generator Hall A = %i\n",generatorHallARpm);
+        }
+        if (generatorHallBRpm>0) {
+            printf("RPM generator Hall B = %i\n",generatorHallBRpm);
+        }
+        if (motorRightRpm>0) {
+            printf("RPM motor Right = %i\n",motorRightRpm);
+        }
+        if (motorLeftRpm>0) {
+            printf("RPM motor Left = %i\n",motorLeftRpm);
+        }
+        //printf("light control\n");
+        //Turning LIGHT to RIGHT
+        if (!buttonDirectionRight) {
+            lightRight=!lightRight;
+        } else {
+            lightRight=0;
+        }
+        //Turning LIGHT to Left
+        if (!buttonDirectionLeft) {
+            lightLeft=!lightLeft;
+        } else {
+            lightLeft=0;
+        }
+        // Check if the Lights need to be on
+        if(!buttonRed) {
+            lightFront=1;
+            if (trailerBat->getBatteryPercentage()<30) {lightBack=!lightBack;}
+            else {lightBack=1;}
+        } else {
+            lightFront=0;
+            if (trailerBat->getBatteryPercentage()<30) {lightBack=!lightBack;}
+            else {lightBack=0;}
+        }
+    }
+    
+void BikeControl::generatorHallAPulsed()
+{
+    generatorHallACounter++;
+}
+
+void BikeControl::generatorHallBPulsed()
+{
+    generatorHallBCounter++;
+}
+
+void BikeControl::motorRightPulsed()
+{
+    motorRightCounter++;
+}
+
+void BikeControl::motorLeftPulsed()
+{
+    motorLeftCounter++;
+}
+    
+void BikeControl::runTestLight()
+{
+    printf("front\n");
+    lightFront =1;
+    //wait(1);
+    for(uint32_t i = 0; i<0xFFFFFF; i++); //WAIT STATEMENT CAUSES FREEZING!!!!! USE FOR LOOP IN STEAD, device can not handle wait and interrupts at same time!
+    lightFront = 0;
+    printf("back\n");
+    lightBack = 1;
+    //wait(1);
+    for(uint32_t i = 0; i<0xFFFFFF; i++);
+    lightBack = 0;
+    printf("left\n");
+    lightLeft = 1;
+    //wait(1);
+    for(uint32_t i = 0; i<0xFFFFFF; i++);
+    lightLeft = 0;
+    printf("right\n");
+    lightRight = 1;
+    //wait(1);
+    for(uint32_t i = 0; i<0xFFFFFF; i++);
+    lightRight = 0;
+    //wait(1);
+}
+
+void BikeControl::checkStatus(){
+        
+        //STATE MACHINE
+        if (!brakeFront || !brakeRear) {
+            if(!brakeFront) {
+                printf("BRAKE front \n");
+            }
+            if(!brakeRear) {
+                printf("BRAKE rear \n");
+            }
+            motorRightCtrl.write(0.0f);
+            motorLeftCtrl.write(0.0f);
+            trailerCtrl=0;
+        } else if (!buttonGreen) {
+            if (motorRightRpm<=61) {// motorRightRpm=61 means 6km/h
+                motorRightCtrl.write(1.0f);
+                motorLeftCtrl.write(1.0f);
+                trailerCtrl=1;
+            } else {
+                if (generatorHallARpm<10) {
+                    motorRightCtrl.write(0.0f);
+                    motorLeftCtrl.write(0.0f);
+                    trailerCtrl=0;
+                } else if(generatorHallARpm<=100) {
+                    motorRightCtrl.write(generatorHallARpm*0.004f);
+                    motorLeftCtrl.write(generatorHallARpm*0.004f);
+                    if (motorRightRpm<190) { //boven de 18km/h enkel de trekker aansturen
+                        trailerCtrl=1;
+                    } else {
+                        trailerCtrl=0;
+                    }
+                } else if (generatorHallARpm>100) {
+                    motorRightCtrl.write(0.4f);
+                    motorLeftCtrl.write(0.4f);
+                    if (motorRightRpm<190) { //boven de 18km/h enkel de trekker aansturen
+                        trailerCtrl=1;
+                    } else {
+                        trailerCtrl=0;
+                    }
+                }
+            }
+        } else if (generatorHallARpm>=0) {
+            if (generatorHallARpm<10) {
+                motorRightCtrl.write(0.0f);
+                motorLeftCtrl.write(0.0f);
+                trailerCtrl=0;
+            } else if(generatorHallARpm<=100) {
+                motorRightCtrl.write(generatorHallARpm*0.004f);
+                motorLeftCtrl.write(generatorHallARpm*0.004f);
+                if (motorRightRpm<190) {
+                    trailerCtrl=1;
+                } else {
+                    trailerCtrl=0;
+                }
+            } else if (generatorHallARpm>100) {
+                motorRightCtrl.write(0.4f);
+                motorLeftCtrl.write(0.4f);
+                if (motorRightRpm<190) {
+                    trailerCtrl=1;
+                } else {
+                    trailerCtrl=0;
+                }
+            }
+        }
+    
     }
\ No newline at end of file
diff -r 792a8f167ac0 -r 39f462024f10 BikeControl.h
--- a/BikeControl.h	Mon Jul 18 09:25:53 2016 +0000
+++ b/BikeControl.h	Mon Jul 18 13:19:33 2016 +0000
@@ -1,14 +1,12 @@
-#ifndef BATTERYSTATE_H
-#define BATTERYSTATE_H
+#ifndef BIKECONTROL_H
+#define BIKECONTROL_H
 #include "mbed.h"
+#include "BikeData.h"
+#include "BatteryState.h"
 
 class BikeControl{
-
-public:
-    Bikecontrol();
     
 protected:
-    
     //State machine states definition
     typedef enum DriveState {
         DRIVE_START=0,
@@ -22,55 +20,67 @@
     //const float WHEEL_CIRCUMFERENCE = 1.66; //Wheel Circumference
     //MOTOR
     //const float GEAR_RATIO = 13.3;     //Motor gear ration
-    
-    //BATTERY
-    AnalogIn batteryVoltageTrailer(PB_0);
-    AnalogIn batteryVoltageBike(PC_5);
-    AnalogIn batteryVoltageAuxiliay(PC_4);
+   
     
     //TRAILER
-    DigitalOut trailerCtrl(PB_1);
+    DigitalOut trailerCtrl;
     
     //MOTOR
-    PwmOut motorRightCtrl(PB_15);
-    InterruptIn motorRightHall(PC_0);
-    float motorRightCounter = 0;
-    float motorRightRpm = 0;
-    PwmOut motorLeftCtrl(PB_13);
-    InterruptIn motorLeftHall(PC_1);
-    float motorLeftCounter = 0;
-    float motorLeftRpm = 0;
+    PwmOut motorRightCtrl;
+    InterruptIn motorRightHall;
+    uint16_t motorRightCounter;
+    uint16_t motorRightRpm;
+    PwmOut motorLeftCtrl;
+    InterruptIn motorLeftHall;
+    uint16_t motorLeftCounter;
+    uint16_t motorLeftRpm;
     
     //BRAKE
-    DigitalIn brakeFront(PC_10);
-    DigitalIn brakeRear(PC_12);
+    DigitalIn brakeFront;
+    DigitalIn brakeRear;
     
     //GENERATOR
-    InterruptIn generatorHallA(PD_2);
-    PwmOut generatorBrake(PC_9);
-    float generatorHallACounter = 0;
-    float generatorHallARpm = 0;
-    InterruptIn generatorHallB(PC_3);
-    float generatorHallBCounter = 0;
-    float generatorHallBRpm = 0;
+    InterruptIn generatorHallA;
+    PwmOut generatorBrake;
+    uint16_t generatorHallACounter;
+    uint16_t generatorHallARpm;
+    InterruptIn generatorHallB;
+    uint16_t generatorHallBCounter;
+    uint16_t generatorHallBRpm;
     
     //BUTTONS ON STEERING
-    DigitalIn userButton(USER_BUTTON);
-    DigitalIn buttonGreen(PB_11);
-    DigitalIn buttonRed(PB_12);
-    DigitalIn buttonDirectionRight(PA_15);
-    DigitalIn buttonDirectionLeft(PB_7);
+    //DigitalIn userButton;
+    DigitalIn buttonGreen;
+    DigitalIn buttonRed;
+    DigitalIn buttonDirectionRight;
+    DigitalIn buttonDirectionLeft;
     
     //SWITCH
-    DigitalIn switchOn(PC_11);
-    DigitalIn switchWalk(PB_2);
+    DigitalIn switchOn;
+    DigitalIn switchWalk;
     
     //LIGHT
-    DigitalOut lightFront(PB_14);
-    DigitalOut lightBack(PA_11);
-    DigitalOut lightLeft(PC_6);
-    DigitalOut lightRight(PC_8);
+    DigitalOut lightFront;
+    DigitalOut lightBack;
+    DigitalOut lightLeft;
+    DigitalOut lightRight;
+    
     
+    void generatorHallAPulsed();
+    void generatorHallBPulsed();
+    void motorRightPulsed();
+    void motorLeftPulsed();
+    void periodicCallback();
+    
+    Ticker t;
+    BikeData *bd;
+    BatteryState *bikeBat, *trailerBat, *auxBat;
+    
+public:
+    BikeControl(BikeData* bikeData, BatteryState* trailerBat, BatteryState* bikeBat, BatteryState* auxBat);
+    void runTestLight();
+    void startControlLoop();
+    void checkStatus();
 };
     
 #endif
\ No newline at end of file