Alexander Doppler
/
Bertl_Test
Bertl
Revision 0:ae8f51bac163, committed 2015-04-23
- Comitter:
- Alexander400
- Date:
- Thu Apr 23 12:04:34 2015 +0000
- Commit message:
- Bertl
Changed in this revision
diff -r 000000000000 -r ae8f51bac163 Bertl_Bewegungen.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Bertl_Bewegungen.cpp Thu Apr 23 12:04:34 2015 +0000 @@ -0,0 +1,8 @@ +#include "Bertl_Bewegungen.h" + +void Bertl_Bewegungen :: TurnRight(){ + + for(int i = 0;i <3; i++){ + TurnLeft(); + } +} \ No newline at end of file
diff -r 000000000000 -r ae8f51bac163 Bertl_Bewegungen.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Bertl_Bewegungen.h Thu Apr 23 12:04:34 2015 +0000 @@ -0,0 +1,11 @@ +#include "ur_Bertl.h" + +#ifndef BERTL_BEWEGUNGEN_H +#define BERTL_BEWEGUNGEN_H +class Bertl_Bewegungen : public ur_Bertl{ + +public: + void TurnRight(); + +}; +#endif \ No newline at end of file
diff -r 000000000000 -r ae8f51bac163 HCSR.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HCSR.lib Thu Apr 23 12:04:34 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/BERTL_CHEL_18/code/HCSR/#92d1f0be3af4
diff -r 000000000000 -r ae8f51bac163 config.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config.h Thu Apr 23 12:04:34 2015 +0000 @@ -0,0 +1,60 @@ +/*! \file config.h +\brief A Documented file. +* name: config.h Version: 1.1 +* author: PE HTL BULME +* email: pe@bulme.at +* description: +* Wiring for ur_Bertl The Robot +*/ + +#include "mbed.h" +#include "hcsr04.h" +#include "const.h" + +#ifndef CONFIG_H +#define CONFIG_H +/*! \def DEBUG +\brief Turns debugging infos off (0) and on (3) which are send to serial pc.uart */ +#define DEBUG 0 +//#define FRONTBUTTON + +BusOut NibbleLEDs(P1_8, P1_9, P1_10, P1_11); /**< 4 yellow LEDs as a bus defined */ +DigitalOut LED_D10(P1_8); /**< wiring first LED_D10 */ +DigitalOut LED_D11(P1_9); /**< wiring second LED_D11 */ +DigitalOut LED_D12(P1_10); /**< wiring thired LED_D12 */ +DigitalOut LED_D13(P1_11); /**< wiring fourth LED_D13 */ + +DigitalOut LED_blue(P1_28); /**< all blue LEDs are on Port 1_28 */ + +DigitalOut RGB_blue(P1_24); /**< RGB blue component of the color uP -> Port 1_24 */ +DigitalOut RGB_red(P1_23); /**< RGB red component of the color uP -> Port 1_23 */ +DigitalOut RGB_green(P1_25); /**< RGB green component of the color uP -> Port 1_25 */ + +//------------------ CHANGE ONLY IF NESSESARY ------------------------------------- + +DigitalOut MotorL_EN(p34); // wiring motor left +DigitalOut MotorL_FORWARD(P1_1); +DigitalOut MotorL_REVERSE(P1_0); + +DigitalOut MotorR_EN(p36); // wiring motor right +DigitalOut MotorR_FORWARD(P1_3); +DigitalOut MotorR_REVERSE(P1_4); + +I2C i2c(p28,p27); +BusIn linesensor(p18, p16, p19, p17); +DigitalIn SensorL(P1_12); /**< motor sensor left on Port P1_12 */ +DigitalIn SensorR(P1_13); /**< motor sensor right on Port P1_13 */ +HCSR04 usensor(p21,p22); // HC-SR04 ultrasonic sensor +PwmOut mg1(P1_15); // PWM out motor left +PwmOut mg2(P0_21); // PWM out motor right + +Serial pc(USBTX, USBRX); // tx, rx + +#if defined(DEBUG) && DEBUG > 0 + #define DEBUG_PRINT(fmt, args...) fprintf(stderr, "DEBUG: %s:%d:%s(): " fmt, \ + __FILE__, __LINE__, __func__, ##args) +#else + #define DEBUG_PRINT(fmt, args...) /* Don't do anything in release builds */ +#endif + +#endif \ No newline at end of file
diff -r 000000000000 -r ae8f51bac163 const.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/const.h Thu Apr 23 12:04:34 2015 +0000 @@ -0,0 +1,52 @@ +/*! \file const.h +\brief A Documented file. +* name: const.h Version: 1.0 +* author: PE HTL BULME +* email: pe@bulme.at +* description: +* Constants for ur_Bertl The Robot +*/ +#ifndef CONST_H +#define CONST_H + +/*! \var int SPEED +\brief Bertl speed 0.2 (slow) - 1.0 (fast). +\warning speed below 0.2 possible the motor won't start */ +const float SPEED = 0.2; /* Bertl speed 0.2 (slow) - 1.0 (fast) */ +/*! \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 */ +/*! \var int ANGLE +\brief 12 is nearly 90 degree turn left. +\warning maybe the number has to be adjusted */ +const int ANGLE = 12; /* nearly 90 degree turn left */ + +const int BTN_FLL = 0x80; // button front left outer +const int BTN_FL = 0x04; // button front left +const int BTN_FM = 0x01; // button front middle +const int BTN_FR = 0x08; // button front right +const int BTN_FRR = 0x40; // button front right outer +const int BTN_BL = 0x10; // button back left +const int BTN_BM = 0x02; // button back middle +const int BTN_BR = 0x20; // button back right + +const int LED_FL1 = 0x01; /**< front LED white */ +const int LED_FL2 = 0x02; /**< front LED yellow */ +const int LED_FR1 = 0x04; /**< front LED white */ +const int LED_FR2 = 0x08; /**< front LED yellow */ +const int LED_ALL_FRONT = 0x0F; + +const int LED_BL1 = 0x20; /**< yellow LED back left outer */ +const int LED_BL2 = 0x10; /**< red LED back left inner */ +const int LED_BR1 = 0x80; /**< yellow LED back right outer */ +const int LED_BR2 = 0x40; /**< red LED back right inner */ +const int LED_ALL_BACK = 0xF0; +const int LED_ALL = 0xFF; + +const int addr = 0x40; // I2C-address PCA9555 +const int PERIOD = 20; // PWM period +const int PULSWIDTH = 5; // PWN pulswidth + + +#endif \ No newline at end of file
diff -r 000000000000 -r ae8f51bac163 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Apr 23 12:04:34 2015 +0000 @@ -0,0 +1,12 @@ +#include "mbed.h" +#include "ur_Bertl.h" +#include "const.h" +#include "Bertl_Bewegungen.h" + + +int main() { + + Bertl_Bewegungen karel; + karel.TurnRight(); + +}
diff -r 000000000000 -r ae8f51bac163 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Apr 23 12:04:34 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac \ No newline at end of file
diff -r 000000000000 -r ae8f51bac163 ur_Bertl.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ur_Bertl.cpp Thu Apr 23 12:04:34 2015 +0000 @@ -0,0 +1,316 @@ +/*********************************** +name: ur_Bertl.cpp Version: 1.1 +author: PE HTL BULME +email: pe@bulme.at +description: + Implementation portion of class ur_Bertl The Robot + Blue LED and RGB LED test added + +***********************************/ +#include "mbed.h" +#include "config.h" +#include "ur_Bertl.h" + +// Constructor +ur_Bertl::ur_Bertl() : _interrupt(P1_12) // left sensor P1_13 +{ + i2c.frequency(40000); // I2C Frequenz 40kHz + mg1 = mg2 = SPEED; + _interrupt.rise(this, &ur_Bertl::increment); // attach increment function of this counter instance ie. motor sensor + _count = 0; + beepersInBag = 0; +} + +ur_Bertl::ur_Bertl(PinName pin) : _interrupt(pin) // create the InterruptIn on the pin specified to Counter +{ + i2c.frequency(40000); // I2C Frequenz 40kHz + mg1 = mg2 = SPEED; + _interrupt.rise(this, &ur_Bertl::increment); // attach increment function of this counter instance ie. motor sensor + _count = 0; + beepersInBag = 0; +} + + +// Pulblic methodes +void ur_Bertl::Move() +{ + int count = _count; + MotorR_EN=MotorL_EN=1; // both motor ENABLE + MotorR_FORWARD = MotorL_FORWARD = 1; // both motor forward ON + + while(_count < count+DISTANCE) { + //if(!FrontIsClear()) // more convenient because there are no accidents :-) + // break; +#ifdef FRONTBUTTON + if(frontButtonPressed()) + error(); +#endif + DEBUG_PRINT("count: %d _count: %d", count, _count); + } + MotorR_FORWARD = MotorL_FORWARD = 0; // both motor off + MotorR_EN=MotorL_EN=0; + wait_ms(250); +} + +void ur_Bertl::PutBeeper() +{ +// wait_ms(500); + if(beepersInBag > 0) + beepersInBag--; + else + error(); +} + +void ur_Bertl::PickBeeper() +{ +// wait_ms(500); + if (linesensor) + beepersInBag++; + else + error(); +} + +void ur_Bertl::TurnLeft() +{ + int count = _count; + MotorR_EN=MotorL_EN=1; // motor left and right ENABLE + + MotorR_FORWARD = MotorL_REVERSE = 1; + while(_count < count+ANGLE) { +#ifdef FRONTBUTTON + if(frontButtonPressed()) // get out if to much problems + error(); +#endif + DEBUG_PRINT("count: %d _count: %d", count, _count); + } + MotorR_FORWARD = MotorL_REVERSE = 0; + MotorR_EN=MotorL_EN=0; + 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 + +void ur_Bertl::BlueLedsON() +{ + LED_blue=0; +} + +void ur_Bertl::BlueLedsOFF() +{ + LED_blue=1; +} + +void ur_Bertl::RGBLed(bool red, bool green, bool blue) +{ + RGB_blue=!blue; + RGB_red=!red; + RGB_green=!green; +} + +void ur_Bertl::TurnLedOn(int16_t led) +{ + char cmd[3]; + + cmd[0] = 0x02; + cmd[1] = ~led; + i2c.write(addr, cmd, 2); + wait(0.5); +} + +void ur_Bertl::TurnLedOff(int16_t led) +{ + char cmd[3]; + + cmd[0] = 0x02; + cmd[1] = led; + i2c.write(addr, cmd, 2); + wait(0.5); +} + +void ur_Bertl::NibbleLeds(int value) +{ + 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() +{ + int dist = 0; + usensor.start(); + wait_ms(10); + dist=usensor.get_dist_cm(); + if(dist < 5) + return false; + else + return true; + DEBUG_PRINT("Distance: %d", dist); + +// if there is no ultra sonic sensor use this - with front buttons + 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)) + wert = false; + else + wert = true; + DEBUG_PRINT("WERT: %d", wert); + return wert; +} + + +bool ur_Bertl::NextToABeeper() +{ + if (BottomIsBlack()) + return true; + else + return false; +} + +int ur_Bertl::AnyBeeperInBag() +{ + if(beepersInBag > 0) + return beepersInBag; + else + return 0; +} + +bool ur_Bertl::backIsClear() +{ + 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_BL|BTN_BM|BTN_BR)) + wert = false; + else + wert = true; + DEBUG_PRINT("WERT: %d", wert); + return wert; +} + +int ur_Bertl::BottomIsBlack() +{ + int detect; + + detect = linesensor; + return detect; +} + +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); +} +*/
diff -r 000000000000 -r ae8f51bac163 ur_Bertl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ur_Bertl.h Thu Apr 23 12:04:34 2015 +0000 @@ -0,0 +1,129 @@ +/*********************************** +name: ur_Bertl.h Version: 1.0 +author: PE HTL BULME +email: pe@bulme.at +description: + Definition portion of the class ur_Bertl The Robot + + +***********************************/ +#include "mbed.h" + +#ifndef UR_BERTL_H +#define UR_BERTL_H + +#define LEFTSENSOR P1_12 +#define RIGHTSENSOR P1_13 +/********************************************//** + name: ur_Bertl.h + version: 1.0 + author: PE HTL BULME. + email: pe@bulme.at + description: + Definition and documentation portion of the class ur_Bertl The Robot. + +***********************************************/ +/** +@code +// +@endcode + +Example motor sensor test: +@code +#include "mbed.h" +#include "ur_Bertl.h" +#include "const.h" + +int main() +{ + ur_Bertl karel(LEFTSENSOR); // RIGHTSENSOR + + while(true) { + karel.NibbleLeds(karel.Read()); + } +} +@endcode + +Example moving the robot around: +@code +#include "mbed.h" +#include "ur_Bertl.h" +#include "const.h" + +int main() +{ + ur_Bertl karel; + + while(karel.WaitUntilButtonPressed()){} + //karel.Move(); + karel.TurnLeft(); + karel.ShutOff(); +} +@endcode + +Example LEDs: +@code +#include "mbed.h" +#include "ur_Bertl.h" +#include "const.h" + +int main() +{ + ur_Bertl karel; + + while(karel.WaitUntilButtonPressed()){} + + karel.TurnLedOn(LED_FL1 | LED_FR1); // see const.h + wait(1); + karel.TurnLedOn(0xFF); // or use hex + wait(1); + karel.RGBLed(1,0,0); // red + wait(1); + karel.RGBLed(0,1,0); // green + wait(1); + karel.RGBLed(0,0,1); // blue + karel.BlueLedsON(); + karel.NibbleLeds(karel.Read()); + wait(1); + karel.BlueLedsOFF(); + karel.TurnLedOff(0xFF); + karel.ShutOff(); +} +@endcode + */ +class ur_Bertl +{ +protected: + int beepersInBag; /**< how many beepers does the robot have in his bag*/ + char cmd[3]; /**< I2C command */ + int16_t btns; /**< which button is pressed */ + InterruptIn _interrupt; /**< interrupted used*/ + volatile int _count; /**< values of motor sensor*/ + + /** + protected methodes for internal purposes only + */ + 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 */ + void error(); /**< Error: stops the robot and all LEDs are blinking*/ + +public: + ur_Bertl(); /**< default constructor; you have to define constants in config.h such as SPEED, DISTANCE or ANGLE*/ + ur_Bertl(PinName pin); /**< parameterized constructor; on what pin should the interrupt work; SPEED, DISTANCE or ANGLE have to bee defined in config.h */ + + 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 NibbleLeds(int value); /**< methode for the 4 (half byte) yellow LEDs at the back left side */ + 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