Lib for Bulme Bertl

Dependents:   BertlPingPong BertlTemplate LineSensTest MotorTest2 ... more

Files at this revision

API Documentation at this revision

Comitter:
hollegha2
Date:
Thu Nov 19 08:11:19 2015 +0000
Parent:
6:78243412d2b3
Commit message:
V3.0

Changed in this revision

Bertl14.cpp Show annotated file Show diff for this revision Revisions of this file
Bertl14.h Show annotated file Show diff for this revision Revisions of this file
BertlObjects.h Show annotated file Show diff for this revision Revisions of this file
--- a/Bertl14.cpp	Thu Mar 05 17:39:16 2015 +0000
+++ b/Bertl14.cpp	Thu Nov 19 08:11:19 2015 +0000
@@ -2,160 +2,218 @@
 #include "mbed.h"
 #include "Bertl14.h"
 
+// V 3.0
+
 Motor::Motor(PinName pwm, PinName fwd, PinName rev) :
-_pwm(pwm), _fwd(fwd), _rev(rev) 
+    _pwm(pwm), _fwd(fwd), _rev(rev)
 {
-	_pwm.period(0.001); _pwm=0;
-	_fwd=0; _rev=0;
+    _pwm.period(0.001);
+    _pwm=0;
+    _fwd=0;
+    _rev=0;
+    _running=0;
+}
+
+void Motor::SetBrake(int aOnOff)
+{
+    _pwm=0;
+    if( aOnOff ) {
+        _fwd=0;
+        _rev=0;
+    } else {
+        _fwd=1;
+        _rev=1;
+    }
 }
 
 void Motor::SetPow(float aPow)
 {
-	if( aPow>=0.0 ) {
-		_fwd=1; _rev=0;
-		_pwm = aPow;
-	}
-	else {
-		_fwd=0; _rev=1;
-		_pwm = -aPow;
-	}
+    if( aPow==0 ) {
+        _pwm=0;
+        _running=0;
+        return;
+    }
+    if( aPow>=0.0 ) {
+        _fwd=1;
+        _rev=0;
+        _pwm = aPow;
+    } else {
+        _fwd=0;
+        _rev=1;
+        _pwm = -aPow;
+    }
+    _running=1;
+}
+
+void Motor::SetPow2(float aPow)
+{
+    float pow;
+    if( aPow==0 ) {
+        _pwm=0;
+        _running=0;
+        return;
+    }
+    if( aPow>=0.0 ) {
+        _fwd=1;
+        _rev=0;
+        pow = aPow;
+    } else {
+        _fwd=0;
+        _rev=1;
+        pow = -aPow;
+    }
+    if( !_running && (pow<0.3) ) {
+        _pwm = 0.3;
+        wait_ms(20); // 50
+    }
+    _pwm = pow;
+    _running = 1;
 }
 
 BertlDrive::BertlDrive(PinName pwm, PinName fwd, PinName rev, PinName encoder) :
-Motor(pwm,fwd,rev) , _enc(encoder)
+    Motor(pwm,fwd,rev) , _enc(encoder)
 {
-	encCnt = 0;
+    encCnt = 0;
 }
 
 void BertlDrive::Init()
 {
-	_enc.rise(this, &BertlDrive::EncoderISR);
-	_enc.fall(this, &BertlDrive::EncoderISR);
+    _enc.rise(this, &BertlDrive::EncoderISR);
+    _enc.fall(this, &BertlDrive::EncoderISR);
 }
 
 void BertlDrive::EncoderISR()
 {
-	encCnt++;
+    encCnt++;
 }
 
 
-PortEx::PortEx() : 
-_i2c(p28,p27), _p6Event(p6)
+
+PortEx::PortEx() :
+    _i2c(p28,p27), _p6Event(p6)
 {
-	btns=btnEvent=0;
-	useISR = 1;
+    btns=btnEvent=0;
+    useISR = 1;
 }
 
 void PortEx::Init()
 {
-	char cmd[4];
-	_i2c.frequency(100000);
-	wait(0.01);
-  // Port0 Config  Port0 Out    Port1 In
-	cmd[0]=0x06;     cmd[1]=0x00; cmd[2]=0xFF;
-	_i2c.write(DEV, cmd, 3, false);
-	SetLedPort(0);
-	_p6Event.fall(this, &PortEx::p6ISR);
+    char cmd[4];
+    _i2c.frequency(100000);
+    wait(0.01);
+    // Port0 Config  Port0 Out    Port1 In
+    cmd[0]=0x06;
+    cmd[1]=0x00;
+    cmd[2]=0xFF;
+    _i2c.write(DEV, cmd, 3, false);
+    SetLedPort(0);
+    _p6Event.fall(this, &PortEx::p6ISR);
 }
 
 void PortEx::p6ISR()
 {
-	if( !useISR )
-		return;
-	int16_t prev = btns;
-  ReadButtons();
-	if( !btns )
-		btns = prev;
-	else
-		btnEvent = 1;
+    if( !useISR )
+        return;
+    int16_t prev = btns;
+    ReadButtons();
+    if( !btns )
+        btns = prev;
+    else
+        btnEvent = 1;
 }
 
 void PortEx::SetLedPort(uint8_t aBitPattern)
 {
-	char cmd[4];
-	cmd[0]=2; cmd[1]=~aBitPattern;
-	_i2c.write(DEV, cmd, 2, false);
+    char cmd[4];
+    cmd[0]=2;
+    cmd[1]=~aBitPattern;
+    _i2c.write(DEV, cmd, 2, false);
 }
 
 void PortEx::SetLeds(uint8_t aBitPattern)
 {
-	_currLeds |= aBitPattern;
-	SetLedPort(_currLeds);
+    _currLeds |= aBitPattern;
+    SetLedPort(_currLeds);
 }
 
 void PortEx::ToggleLeds(uint8_t aBitPattern)
 {
-	_currLeds ^= aBitPattern;
-	SetLedPort(_currLeds);
+    _currLeds ^= aBitPattern;
+    SetLedPort(_currLeds);
 }
 
 void PortEx::ClearLeds()
 {
-	_currLeds=0; SetLedPort(0);
+    _currLeds=0;
+    SetLedPort(0);
 }
 
 
 void PortEx::ReadButtons()
 {
-	char cmd[4];
-	cmd[0]=1;
-	_i2c.write(DEV, cmd, 1, true);
-	_i2c.read(DEV|1, cmd, 1, false);
-	btns = cmd[0];
+    char cmd[4];
+    cmd[0]=1;
+    _i2c.write(DEV, cmd, 1, true);
+    _i2c.read(DEV|1, cmd, 1, false);
+    btns = cmd[0];
 }
 
 void PortEx::WaitUntilButtonPressed()
 {
-	int prev = useISR;
-	useISR = 0;
-	btns = 0;
-	while(1) {
-		ReadButtons();
-		if( btns )
-			break;
-		wait(0.01);
-	}
-	btns=btnEvent=0;
-	useISR = prev;
+    int prev = useISR;
+    useISR = 0;
+    btns = 0;
+    while(1) {
+        ReadButtons();
+        if( btns )
+            break;
+        wait(0.01);
+    }
+    btns=btnEvent=0;
+    useISR = prev;
 }
 
 void PortEx::WaitUntilFrontButtonPressed()
 {
-	int prev = useISR;
-	useISR = 0;
-	btns = 0;
-	while(1) {
-		ReadButtons();
-		if( IsAnyFrontButton() )
-			break;
-		wait(0.01);
-	}
-	btns=btnEvent=0;
-	useISR = prev;
+    int prev = useISR;
+    useISR = 0;
+    btns = 0;
+    while(1) {
+        ReadButtons();
+        if( IsAnyFrontButton() )
+            break;
+        wait(0.01);
+    }
+    btns=btnEvent=0;
+    useISR = prev;
 }
 
 
 
 UsDistSens::UsDistSens(PinName pinTrigger, PinName pinEcho) :
-trigger(pinTrigger),echo(pinEcho)
+    trigger(pinTrigger),echo(pinEcho)
 {
-	echo.rise(this, &UsDistSens::RisingISR);
-	echo.fall(this, &UsDistSens::FallingISR);
+    echo.rise(this, &UsDistSens::RisingISR);
+    echo.fall(this, &UsDistSens::FallingISR);
 }
 
 void UsDistSens::StartMeas()
 {
-	trigger=1; wait_us(12); trigger=0;
-	stw.start();
+    trigger=1;
+    wait_us(12);
+    trigger=0;
+    stw.start();
 }
 
 void UsDistSens::RisingISR()
-{ stw.reset(); }
+{
+    stw.reset();
+}
 
 void UsDistSens::FallingISR()
 {
-	dist=stw.read_us();
-	distCM = (float)dist*(343.2E-4/2.0);
+    dist=stw.read_us();
+    distCM = (float)dist*(343.2E-4/2.0);
 }
 
 
--- a/Bertl14.h	Thu Mar 05 17:39:16 2015 +0000
+++ b/Bertl14.h	Thu Nov 19 08:11:19 2015 +0000
@@ -2,25 +2,32 @@
 #ifndef Bertl14_h
 #define Bertl14_h
 
-class Motor {
-	public:
-		Motor(PinName pwm, PinName fwd, PinName rev);
-		void SetPow(float aPow);
-	protected:
+// V 3.0
+
+class Motor
+{
+public:
+    Motor(PinName pwm, PinName fwd, PinName rev);
+    void SetBrake(int aOnOff);
+    void SetPow(float aPow);
+    void SetPow2(float aPow);
+protected:
     PwmOut _pwm;
     DigitalOut _fwd;
     DigitalOut _rev;
+    int16_t    _running;
 };
 
-class BertlDrive : public Motor {
-	public:
-		int16_t encCnt;
-	public:
-		BertlDrive(PinName pwm, PinName fwd, PinName rev, PinName encoder);
-		void Init();
-	private:
-		void EncoderISR();
-		InterruptIn _enc;
+class BertlDrive : public Motor
+{
+public:
+    int16_t encCnt;
+public:
+    BertlDrive(PinName pwm, PinName fwd, PinName rev, PinName encoder);
+    void Init();
+private:
+    void EncoderISR();
+    InterruptIn _enc;
 };
 
 
@@ -46,65 +53,72 @@
 const int LED_ALL_BACK = 0xF0;
 
 
-class PortEx {
-	public:
-		// Current State of Buttons is refreshed with ReadButtons()
-		int16_t btns;
-		uint8_t btnEvent;
-		uint8_t useISR;
-	public:
-		PortEx();
-		void Init();
-		
-		void SetLedPort(uint8_t aBitPattern); // NO local Bit-OR
-		void SetLeds(uint8_t aBitPattern);
-		void ToggleLeds(uint8_t aBitPattern);
-		void ClearLeds();
-		
-		void ReadButtons();
-		void WaitUntilButtonPressed();
-		void WaitUntilFrontButtonPressed();
+class PortEx
+{
+public:
+    // Current State of Buttons is refreshed with ReadButtons()
+    int16_t btns;
+    uint8_t btnEvent;
+    uint8_t useISR;
+public:
+    PortEx();
+    void Init();
+
+    void SetLedPort(uint8_t aBitPattern); // NO local Bit-OR
+    void SetLeds(uint8_t aBitPattern);
+    void ToggleLeds(uint8_t aBitPattern);
+    void ClearLeds();
 
-		bool IsButton(int aBitPattern)
-			{ return btns & aBitPattern; }
-		
-		bool IsAnyFrontButton()
-			{ return btns & (BTN_FL|BTN_FM|BTN_FR); }
-		
-		bool IsAnyBackButton()
-			{ return btns & (BTN_BL|BTN_BM|BTN_BR); }
-	private:
-		uint8_t _currLeds;
-		void p6ISR();
-		I2C _i2c;
-		const int DEV = 0x40;
-		InterruptIn _p6Event;
+    void ReadButtons();
+    void WaitUntilButtonPressed();
+    void WaitUntilFrontButtonPressed();
+
+    bool IsButton(int aBitPattern) {
+        return btns & aBitPattern;
+    }
+
+    bool IsAnyFrontButton() {
+        return btns & (BTN_FL|BTN_FM|BTN_FR);
+    }
+
+    bool IsAnyBackButton() {
+        return btns & (BTN_BL|BTN_BM|BTN_BR);
+    }
+private:
+    uint8_t _currLeds;
+    void p6ISR();
+    I2C _i2c;
+    const int DEV = 0x40;
+    InterruptIn _p6Event;
 };
 
 
-class UsDistSens {
-	public:
-		UsDistSens(PinName pinTrigger, PinName pinEcho);
-		void StartMeas();
-	private:
-		void RisingISR();
-		void FallingISR();
-	private:
-		DigitalOut trigger;
-		InterruptIn echo;
-		Timer stw;
-	public:
-		int dist;
-		float distCM;
+class UsDistSens
+{
+public:
+    UsDistSens(PinName pinTrigger, PinName pinEcho);
+    void StartMeas();
+private:
+    void RisingISR();
+    void FallingISR();
+private:
+    DigitalOut trigger;
+    InterruptIn echo;
+    Timer stw;
+public:
+    int dist;
+    float distCM;
 };
 
 #ifndef ANALOGIN_HL
 #define ANALOGIN_HL
-class AnalogInHL : public AnalogIn {
-	public:
-		AnalogInHL(PinName pin) : AnalogIn(pin) { }
-		int Read()
-			{ return read_u16()>>6; }
+class AnalogInHL : public AnalogIn
+{
+public:
+    AnalogInHL(PinName pin) : AnalogIn(pin) { }
+    int Read() {
+        return read_u16()>>6;
+    }
 };
 #endif
 
--- a/BertlObjects.h	Thu Mar 05 17:39:16 2015 +0000
+++ b/BertlObjects.h	Thu Nov 19 08:11:19 2015 +0000
@@ -1,10 +1,10 @@
 
-
+// V3.0
 
 BusOut leds(LED1,LED2,LED3,LED4);
 // DigitalOut ledBlue(P1_28); // 3 blaue LEDs
 
-BertlDrive mL(p34, P1_1, P1_0, P1_12); 
+BertlDrive mL(p34, P1_1, P1_0, P1_12);
 BertlDrive mR(p36, P1_3, P1_4, P1_13);  // changed
 
 PortEx pex;
@@ -12,9 +12,11 @@
 
 void InitBertl()
 {
-	leds=0;
-  mL.Init(); mR.Init(); pex.Init();
-	pex.useISR=0;
+    leds=0;
+    mL.Init();
+    mR.Init();
+    pex.Init();
+    pex.useISR=0;
 }