Filip Putz / Bertl

Dependencies:   HCSR

Fork of Bertl by Franz Pucher

Files at this revision

API Documentation at this revision

Comitter:
bulmecisco
Date:
Tue Apr 07 12:30:39 2015 +0000
Parent:
3:01b183fe8b41
Child:
5:6b667e2cb800
Commit message:
Predicats added, such as FrontIsClear(), NextToABeeper(), AnyBeepersInBeeperBag(), ...

Changed in this revision

config.h Show annotated file Show diff for this revision Revisions of this file
const.h Show annotated file Show diff for this revision Revisions of this file
ur_Bertl.cpp Show annotated file Show diff for this revision Revisions of this file
ur_Bertl.h Show annotated file Show diff for this revision Revisions of this file
--- a/config.h	Thu Mar 26 13:06:00 2015 +0000
+++ b/config.h	Tue Apr 07 12:30:39 2015 +0000
@@ -1,8 +1,8 @@
 /*! \file config.h
 \brief A Documented file.
-* name:   config.h    Version: 1.1
-* author: PE HTL BULME
-* email:  pe@bulme.at
+* name:   config.h    Version: 1.1 \n
+* author: PE HTL BULME \n
+* email:  pe@bulme.at \n
 * description:
 *    Wiring for ur_Bertl The Robot
 */
--- a/const.h	Thu Mar 26 13:06:00 2015 +0000
+++ b/const.h	Tue Apr 07 12:30:39 2015 +0000
@@ -1,8 +1,8 @@
 /*! \file const.h
 \brief A Documented file.
-* name:   const.h    Version: 1.0
-* author: PE HTL BULME
-* email:  pe@bulme.at
+* name:   const.h    Version: 1.0 \n
+* author: PE HTL BULME \n
+* email:  pe@bulme.at \n
 * description:
 *    Constants for ur_Bertl The Robot
 */
@@ -19,7 +19,7 @@
 /*! \var int DISTANCE
 \brief one wheel turn is appr. 24 ticks.
 \warning maybe the number has to be adjusted */
-const int DISTANCE = 24;    /*  one wheel turn is appr. 24 ticks */
+const int DISTANCE = 12;    /*  one wheel turn is appr. 24 ticks */
 /*! \var int ANGLE
 \brief 12 is nearly 90 degree turn left.
 \warning maybe the number has to be adjusted */
--- a/ur_Bertl.cpp	Thu Mar 26 13:06:00 2015 +0000
+++ b/ur_Bertl.cpp	Tue Apr 07 12:30:39 2015 +0000
@@ -1,11 +1,11 @@
 /***********************************
-name:   ur_Bertl.cpp    Version: 1.1
+name:   ur_Bertl.cpp    Version: 2.0
 author: PE HTL BULME
 email:  pe@bulme.at
+WIKI:   https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/
 description:
-    Implementation portion of class ur_Bertl The Robot 
-    Blue LED and RGB LED test added
-      
+        Implementation portion of class ur_Bertl The Robot 
+        boolean commands added for if/else, while, ...      
 ***********************************/
 #include "mbed.h"
 #include "config.h"
@@ -14,7 +14,7 @@
 // Constructor
 ur_Bertl::ur_Bertl() : _interrupt(P1_12)            // left sensor P1_13
 {
-    i2c.frequency(40000);                           // I2C Frequenz 40kHz
+    i2c.frequency(40000);                           // I2C init
     char init1[2] = {0x6, 0x00};
     char init2[2] = {0x7, 0xff};
     i2c.write(0x40, init1, 2);
@@ -27,7 +27,7 @@
 
 ur_Bertl::ur_Bertl(PinName pin) :  _interrupt(pin)  // create the InterruptIn on the pin specified to Counter
 {
-    i2c.frequency(40000);                           // I2C Frequenz 40kHz
+    i2c.frequency(40000);                           // I2C init
     char init1[2] = {0x6, 0x00};
     char init2[2] = {0x7, 0xff};
     i2c.write(0x40, init1, 2);
@@ -38,7 +38,6 @@
     beepersInBag = 0;
 }
 
-
 // Pulblic methodes
 void ur_Bertl::Move()
 {
@@ -80,6 +79,8 @@
         beepersInBag++;
     else 
         error();   
+    if(beepersInBag > 16)
+        error();
 }
 
 void ur_Bertl::TurnLeft()
@@ -105,36 +106,13 @@
     wait_ms(250);           // only to step the robot
 }
 
-bool ur_Bertl::WaitUntilButtonPressed()
-{
-    char cmd[3];
-    int16_t btns;
-    bool wert;
-
-    RGB_blue=RGB_red=RGB_green=0;
-    cmd[0] = 0x06;
-    cmd[1] = 0x00;
-    i2c.write(addr, cmd, 2); 
-
-    cmd[0]=0x01;
-    i2c.write(addr, cmd, 1);
-    i2c.read(addr|1, cmd, 1);
-    btns = cmd[0];
-    if( btns & (0xFF))
-        wert = false;
-    else
-        wert = true;
-    DEBUG_PRINT("\right\nWERT: %d \right\n", wert);
-    return wert;
-}
-
 void ur_Bertl::ShutOff()
 {
     MotorR_FORWARD = MotorL_FORWARD = 0;    // motor OFF
     MotorR_EN=MotorL_EN=0;                  // motor disable
 }
 
-// LEDs methodes
+// Public LEDs methodes
 
 void ur_Bertl::BlueLedsON()
 {
@@ -191,61 +169,6 @@
     NibbleLEDs = value%16;
 }
 
-//-----------------INTERNAL USE ONLY ----------------------------
-void ur_Bertl::error()
-{
-    int wait = 500;
-    MotorR_FORWARD = MotorL_FORWARD = 0;    // both motor off
-    MotorR_REVERSE = MotorL_REVERSE = 0;    // both motor off
-    MotorR_EN=MotorL_EN=0;
-    while(1) {
-        TurnLedOff(0xFF);
-        LED_D10 = LED_D11 = LED_D12 = LED_D13 = 0;
-        LED_blue=1;
-        RGB_blue=RGB_green=RGB_red=1;
-        wait_ms(wait);
-        TurnLedOn(0xFF);
-        LED_D10 = LED_D11 = LED_D12 = LED_D13 = 1;
-        LED_blue=0;
-        RGB_blue=RGB_green=1;RGB_red=0;
-        wait_ms(wait);
-    }
-}
-
-bool ur_Bertl::frontButtonPressed()
-{
-    char cmd[3];            // array for I2C
-    int16_t btns;
-    bool wert;
-
-    cmd[0] = 0x06;
-    cmd[1] = 0x00;
-    i2c.write(addr, cmd, 2); 
-
-    cmd[0]=0x01;
-    i2c.write(addr, cmd, 1);
-    i2c.read(addr|1, cmd, 1);
-    btns = cmd[0];
-    if( btns & (BTN_FL|BTN_FM|BTN_FR|BTN_FRR|BTN_FLL))
-        wert = true;
-    else
-        wert = false;
-    DEBUG_PRINT("WERT: %d", wert);
-    return wert;
-}
-
-// ISR
-
-void ur_Bertl::increment()
-{
-    _count++;
-}
-
-int ur_Bertl::Read()
-{
-    return _count;
-}
-
 //----------------------------------------------------------------------
 
 bool ur_Bertl::FrontIsClear()
@@ -282,10 +205,32 @@
 #endif
 }
 
-/*
+bool ur_Bertl::WaitUntilButtonPressed()
+{
+    char cmd[3];
+    int16_t btns;
+    bool wert;
+
+    RGB_blue=RGB_red=RGB_green=0;
+    cmd[0] = 0x06;
+    cmd[1] = 0x00;
+    i2c.write(addr, cmd, 2); 
+
+    cmd[0]=0x01;
+    i2c.write(addr, cmd, 1);
+    i2c.read(addr|1, cmd, 1);
+    btns = cmd[0];
+    if( btns & (0xFF))
+        wert = false;
+    else
+        wert = true;
+    DEBUG_PRINT("\right\nWERT: %d \right\n", wert);
+    return wert;
+}
+
 bool ur_Bertl::NextToABeeper()
 {
-    if (BottomIsBlack())
+    if (bottomIsBlack())
         return true;
     else 
         return false;
@@ -299,6 +244,32 @@
         return 0;
 }
 
+void ur_Bertl::MoveBackwards()
+{
+    int count = _count;
+    //wait_ms(250);                         // waite until Bertl stops
+    MotorR_EN=MotorL_EN=1;                  // both motor ENABLE
+    MotorR_REVERSE = MotorL_REVERSE = 1;    // both motor backwards ON
+    while(_count < count+DISTANCE) {
+        if(!backIsClear())
+            break;
+        DEBUG_PRINT("count: %d _count: %d", count, _count);
+    }
+    MotorR_REVERSE = MotorL_REVERSE = 0;    // both motor off
+    MotorR_EN=MotorL_EN=0;
+    wait_ms(250);
+}
+
+// Protected methodes
+
+int ur_Bertl::bottomIsBlack()
+{
+    int detect;
+    
+    detect = linesensor;
+    return detect;
+}
+
 bool ur_Bertl::backIsClear()
 {
     char cmd[3];            // array for I2C
@@ -321,27 +292,58 @@
     return wert;
 }
 
-int ur_Bertl::BottomIsBlack()
+bool ur_Bertl::frontButtonPressed()
 {
-    int detect;
-    
-    detect = linesensor;
-    return detect;
+    char cmd[3];            // array for I2C
+    int16_t btns;
+    bool wert;
+
+    cmd[0] = 0x06;
+    cmd[1] = 0x00;
+    i2c.write(addr, cmd, 2); 
+
+    cmd[0]=0x01;
+    i2c.write(addr, cmd, 1);
+    i2c.read(addr|1, cmd, 1);
+    btns = cmd[0];
+    if( btns & (BTN_FL|BTN_FM|BTN_FR|BTN_FRR|BTN_FLL))
+        wert = true;
+    else
+        wert = false;
+    DEBUG_PRINT("WERT: %d", wert);
+    return wert;
 }
 
-void ur_Bertl::MoveBackwards()
+//-----------------INTERNAL USE ONLY ----------------------------
+void ur_Bertl::error()
 {
-    int count = _count;
-    //wait_ms(250);                         // waite until Bertl stops
-    MotorR_EN=MotorL_EN=1;                  // both motor ENABLE
-    MotorR_REVERSE = MotorL_REVERSE = 1;    // both motor backwards ON
-    while(_count < count+DISTANCE) {
-        if(!backIsClear())
-            break;
-        DEBUG_PRINT("count: %d _count: %d", count, _count);
-    }
+    int wait = 500;
+    MotorR_FORWARD = MotorL_FORWARD = 0;    // both motor off
     MotorR_REVERSE = MotorL_REVERSE = 0;    // both motor off
     MotorR_EN=MotorL_EN=0;
-    wait_ms(250);
+    while(1) {
+        TurnLedOff(0xFF);
+        LED_D10 = LED_D11 = LED_D12 = LED_D13 = 0;
+        LED_blue=1;
+        RGB_blue=RGB_green=RGB_red=1;
+        wait_ms(wait);
+        TurnLedOn(0xFF);
+        LED_D10 = LED_D11 = LED_D12 = LED_D13 = 1;
+        LED_blue=0;
+        RGB_blue=RGB_green=1;RGB_red=0;
+        wait_ms(wait);
+    }
 }
-*/
+
+// ISR
+
+void ur_Bertl::increment()
+{
+    _count++;
+}
+
+int ur_Bertl::Read()
+{
+    return _count;
+}
+
--- a/ur_Bertl.h	Thu Mar 26 13:06:00 2015 +0000
+++ b/ur_Bertl.h	Tue Apr 07 12:30:39 2015 +0000
@@ -1,11 +1,11 @@
 /***********************************
-name:   ur_Bertl.h      Version: 1.0
+name:   ur_Bertl.h      Version: 2.0
 author: PE HTL BULME
 email:  pe@bulme.at
+WIKI:   https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/
 description:
-    Definition portion of the class ur_Bertl The Robot
-
-
+        Definition portion of the class ur_Bertl The Robot
+        boolean commands added for if/else, while, ...
 ***********************************/
 #include "mbed.h"
 
@@ -15,11 +15,12 @@
 #define LEFTSENSOR P1_12
 #define RIGHTSENSOR P1_13
 /********************************************//**
- name:   ur_Bertl.h 
- version: 1.0
- author: PE HTL BULME.
- email:  pe@bulme.at
- description:
+ name:   ur_Bertl.h \n
+ version: 2.0 \n
+ author:PE HTL BULME \n
+ email: pe@bulme.at \n
+ WIKI:  https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/wiki/Homepage \n
+ description: 
     Definition and documentation portion of the class ur_Bertl The Robot.
    
 ***********************************************/
@@ -90,11 +91,40 @@
    karel.ShutOff();
 } 
 @endcode
+Example IF/ELSE Commands (update ur_Bertl 2.0 from https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/
+@code
+/* Sorry, but there are Javascript problems with this code
+
+int main()
+{
+    ur_Bertl karel;
+
+    while( karel.WaitUntilButtonPressed() ) {}
+    if( karel.NextToABeeper()) {
+        karel.PickBeeper();
+        karel.NibbleLeds(karel.AnyBeeperInBag());   //show number of beepers in bag on 4 yellow Leds
+    }
+    wait(1);
+    if( karel.AnyBeeperInBag() ) {
+        karel.PutBeeper();
+        karel.NibbleLeds(karel.AnyBeeperInBag());  
+    }
+    wait(1);
+    if( karel.FrontIsClear() )
+        karel.Move();
+    else
+        karel.TurnLeft();
+
+    karel.ShutOff();
+}
+
+
+@endcode
  */
 class ur_Bertl
 {
 protected:
-    int beepersInBag;       /**< how many beepers does the robot have in his bag*/
+    int beepersInBag;       /**< how many beepers does the robot have in his bag;\n you can show it with: karel.NibbleLeds(karel.AnyBeeperInBag())*/
     char cmd[3];            /**< I2C command */
     int16_t btns;           /**< which button is pressed */
     InterruptIn _interrupt; /**< interrupted used*/
@@ -106,6 +136,7 @@
     void increment();       /**< ISR to increment sensor values of motor */
     bool backIsClear();     /**< don't now for what */
     bool frontButtonPressed();/**< TRUE if a a button on the front of Robot is pressed else FALSE */
+    int bottomIsBlack();    /**< check line sensor; returns BCD value */
     void error();           /**< Error: stops the robot and all LEDs are blinking*/
 
 public:
@@ -115,16 +146,19 @@
     void Move();                /**< Robot moves one turn as much as the constant DISTANCE; if one of the buttons fire --> Error()*/      
     void TurnLeft();            /**< Robot turns left as much as the constant ANGLE*/      
     void PutBeeper();           /**< if Robot has any Beepers in his bag he can put one or more Beeper; if not --> Error(()*/  
-    void PickBeeper();          /**< if Robot stands on a black item he can pick one or more Beeper (max. 16 --> Error()); if not --> Error()*/   
+    void PickBeeper();          /**< if Robot stands on a black item he can pick one or more Beeper (max. 15 --> Error()); if not --> Error()*/   
+    void ShutOff();             /**<  turnes the robot off */
+    void MoveBackwards();       /**< Robot moves as much back as the constant DISTANCE; if one of the buttons fire --> Error()*/
+    bool WaitUntilButtonPressed();  /**< wait until any button is pressed at the robot */
     bool FrontIsClear();        /**< returns a boolean value true if front is free; if not false */ 
-    void NibbleLeds(int value); /**< methode for the 4 (half byte) yellow LEDs at the back left side */
+    bool NextToABeeper();       /**< returns a boolean value true if the robot is on a black place or line; if not --> false */ 
+    int AnyBeeperInBag();       /**< returns an int value (if > 0 equal true) how many beepers in bag; if zero --> false */ 
+    void NibbleLeds(int value); /**< methode for the 4 (half byte) yellow LEDs at the back left side; ie. you can show how many beeper a robot has in his bag with: karel.NibbleLeds(karel.AnyBeeperInBag())*/
     void TurnLedOn(int16_t led);/**< turns the specified one or more LEDs ON; description and name in const.h, such as LED_FL1 = 0x01; front LED white */
     void TurnLedOff(int16_t led);/**< turns the specified one or more LEDs OFF; description and name in const.h, such as LED_FL1 = 0x01; front LED white */
-    bool WaitUntilButtonPressed();  /**< wait until any button is pressed at the robot */
     void RGBLed(bool red, bool green, bool blue); /**<RGB Led with red, green and blue component of the Color */
     void BlueLedsOFF();         /**< OFF all blue LEDs which are on the same Port 1_28 */   
     void BlueLedsON();          /**< ON all blue LEDs which are on the same Port 1_28 */   
-    void ShutOff();             /**<  turnes the robot off */
     int Read();
 };
 #endif
\ No newline at end of file