Leds work simultanously. Version 3.1

Dependencies:   HCSR

Dependents:   bertl_led bertl_led bertl_led bertl_led ... more

Fork of Bertl by Bertl_Team_PE

BULME HTL Graz-Gösting

FSST - Hardwarenahe Programmierung

Created by Prof.Dr. Franz Pucher

Inhalt

Inhalt

Start mit folgendem Code in main.cpp eines neuen Projektes mbed_blinky:

#include "mbed.h"
#include "const.h"
#include "Robot.h"

Robot bertl;
    
int main()
{
    bertl.NibbleLeds(0x0F);
    wait(1);
    bertl.NibbleLeds(0x00);
    
    while(1)
    {
        if(bertl.IsButtonPressed(BTN_BL))
        {
            bertl.TurnLedOn(LED_BL1);
        }
        if(bertl.IsButtonPressed(BTN_BR))
        {
            bertl.TurnLedOff(LED_BL1);
        }
    }
}
Committer:
bulmecisco
Date:
Fri May 08 08:39:12 2015 +0000
Revision:
15:43d6a7e6e64a
Parent:
13:3ce84646fd74
Child:
16:9ec833d98d0d
Version 3.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bulmecisco 0:66e9a0afcbd6 1 /***********************************
bulmecisco 13:3ce84646fd74 2 name: ur_Bertl.h Version: 3.0
bulmecisco 13:3ce84646fd74 3 class Bertl included
bulmecisco 0:66e9a0afcbd6 4 author: PE HTL BULME
bulmecisco 0:66e9a0afcbd6 5 email: pe@bulme.at
bulmecisco 4:76acfddc26fb 6 WIKI: https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/
bulmecisco 0:66e9a0afcbd6 7 description:
bulmecisco 4:76acfddc26fb 8 Definition portion of the class ur_Bertl The Robot
bulmecisco 12:cedc088eaf05 9 Step wise turns on left and rigth
bulmecisco 7:e7f74f072564 10 int ReturnButtonPressed() added which returns the int value of button pressed
bulmecisco 0:66e9a0afcbd6 11 ***********************************/
bulmecisco 0:66e9a0afcbd6 12 #include "mbed.h"
bulmecisco 12:cedc088eaf05 13 #include "const.h"
bulmecisco 0:66e9a0afcbd6 14
bulmecisco 0:66e9a0afcbd6 15 #ifndef UR_BERTL_H
bulmecisco 0:66e9a0afcbd6 16 #define UR_BERTL_H
bulmecisco 0:66e9a0afcbd6 17
bulmecisco 0:66e9a0afcbd6 18 #define LEFTSENSOR P1_12
bulmecisco 0:66e9a0afcbd6 19 #define RIGHTSENSOR P1_13
bulmecisco 0:66e9a0afcbd6 20 /********************************************//**
bulmecisco 4:76acfddc26fb 21 name: ur_Bertl.h \n
bulmecisco 13:3ce84646fd74 22 version: 3.0 \n
bulmecisco 13:3ce84646fd74 23 class Bertl included \n
bulmecisco 4:76acfddc26fb 24 author:PE HTL BULME \n
bulmecisco 4:76acfddc26fb 25 email: pe@bulme.at \n
bulmecisco 4:76acfddc26fb 26 WIKI: https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/wiki/Homepage \n
bulmecisco 4:76acfddc26fb 27 description:
bulmecisco 0:66e9a0afcbd6 28 Definition and documentation portion of the class ur_Bertl The Robot.
bulmecisco 0:66e9a0afcbd6 29
bulmecisco 0:66e9a0afcbd6 30 ***********************************************/
bulmecisco 0:66e9a0afcbd6 31 /**
bulmecisco 0:66e9a0afcbd6 32 @code
bulmecisco 0:66e9a0afcbd6 33 //
bulmecisco 0:66e9a0afcbd6 34 @endcode
bulmecisco 0:66e9a0afcbd6 35
bulmecisco 0:66e9a0afcbd6 36 Example motor sensor test:
bulmecisco 0:66e9a0afcbd6 37 @code
bulmecisco 0:66e9a0afcbd6 38 #include "mbed.h"
bulmecisco 0:66e9a0afcbd6 39 #include "ur_Bertl.h"
bulmecisco 0:66e9a0afcbd6 40 #include "const.h"
bulmecisco 0:66e9a0afcbd6 41
bulmecisco 0:66e9a0afcbd6 42 int main()
bulmecisco 0:66e9a0afcbd6 43 {
bulmecisco 0:66e9a0afcbd6 44 ur_Bertl karel(LEFTSENSOR); // RIGHTSENSOR
bulmecisco 0:66e9a0afcbd6 45
bulmecisco 0:66e9a0afcbd6 46 while(true) {
bulmecisco 0:66e9a0afcbd6 47 karel.NibbleLeds(karel.Read());
bulmecisco 0:66e9a0afcbd6 48 }
bulmecisco 0:66e9a0afcbd6 49 }
bulmecisco 0:66e9a0afcbd6 50 @endcode
bulmecisco 0:66e9a0afcbd6 51
bulmecisco 0:66e9a0afcbd6 52 Example moving the robot around:
bulmecisco 0:66e9a0afcbd6 53 @code
bulmecisco 0:66e9a0afcbd6 54 #include "mbed.h"
bulmecisco 0:66e9a0afcbd6 55 #include "ur_Bertl.h"
bulmecisco 0:66e9a0afcbd6 56 #include "const.h"
bulmecisco 0:66e9a0afcbd6 57
bulmecisco 0:66e9a0afcbd6 58 int main()
bulmecisco 0:66e9a0afcbd6 59 {
bulmecisco 0:66e9a0afcbd6 60 ur_Bertl karel;
bulmecisco 0:66e9a0afcbd6 61
bulmecisco 0:66e9a0afcbd6 62 while(karel.WaitUntilButtonPressed()){}
bulmecisco 0:66e9a0afcbd6 63 //karel.Move();
bulmecisco 0:66e9a0afcbd6 64 karel.TurnLeft();
bulmecisco 0:66e9a0afcbd6 65 karel.ShutOff();
bulmecisco 0:66e9a0afcbd6 66 }
bulmecisco 0:66e9a0afcbd6 67 @endcode
bulmecisco 0:66e9a0afcbd6 68
bulmecisco 0:66e9a0afcbd6 69 Example LEDs:
bulmecisco 0:66e9a0afcbd6 70 @code
bulmecisco 0:66e9a0afcbd6 71 #include "mbed.h"
bulmecisco 0:66e9a0afcbd6 72 #include "ur_Bertl.h"
bulmecisco 0:66e9a0afcbd6 73 #include "const.h"
bulmecisco 0:66e9a0afcbd6 74
bulmecisco 0:66e9a0afcbd6 75 int main()
bulmecisco 0:66e9a0afcbd6 76 {
bulmecisco 0:66e9a0afcbd6 77 ur_Bertl karel;
bulmecisco 0:66e9a0afcbd6 78
bulmecisco 0:66e9a0afcbd6 79 while(karel.WaitUntilButtonPressed()){}
bulmecisco 0:66e9a0afcbd6 80
bulmecisco 0:66e9a0afcbd6 81 karel.TurnLedOn(LED_FL1 | LED_FR1); // see const.h
bulmecisco 0:66e9a0afcbd6 82 wait(1);
bulmecisco 0:66e9a0afcbd6 83 karel.TurnLedOn(0xFF); // or use hex
bulmecisco 0:66e9a0afcbd6 84 wait(1);
bulmecisco 0:66e9a0afcbd6 85 karel.RGBLed(1,0,0); // red
bulmecisco 0:66e9a0afcbd6 86 wait(1);
bulmecisco 0:66e9a0afcbd6 87 karel.RGBLed(0,1,0); // green
bulmecisco 0:66e9a0afcbd6 88 wait(1);
bulmecisco 0:66e9a0afcbd6 89 karel.RGBLed(0,0,1); // blue
bulmecisco 0:66e9a0afcbd6 90 karel.BlueLedsON();
bulmecisco 0:66e9a0afcbd6 91 karel.NibbleLeds(karel.Read());
bulmecisco 0:66e9a0afcbd6 92 wait(1);
bulmecisco 0:66e9a0afcbd6 93 karel.BlueLedsOFF();
bulmecisco 0:66e9a0afcbd6 94 karel.TurnLedOff(0xFF);
bulmecisco 0:66e9a0afcbd6 95 karel.ShutOff();
bulmecisco 0:66e9a0afcbd6 96 }
bulmecisco 0:66e9a0afcbd6 97 @endcode
bulmecisco 4:76acfddc26fb 98 Example IF/ELSE Commands (update ur_Bertl 2.0 from https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/
bulmecisco 4:76acfddc26fb 99 @code
bulmecisco 12:cedc088eaf05 100 /*Sorry, but there are Javascript problems with this code
bulmecisco 4:76acfddc26fb 101
bulmecisco 4:76acfddc26fb 102 int main()
bulmecisco 4:76acfddc26fb 103 {
bulmecisco 4:76acfddc26fb 104 ur_Bertl karel;
bulmecisco 4:76acfddc26fb 105
bulmecisco 4:76acfddc26fb 106 while( karel.WaitUntilButtonPressed() ) {}
bulmecisco 4:76acfddc26fb 107 if( karel.NextToABeeper()) {
bulmecisco 4:76acfddc26fb 108 karel.PickBeeper();
bulmecisco 4:76acfddc26fb 109 karel.NibbleLeds(karel.AnyBeeperInBag()); //show number of beepers in bag on 4 yellow Leds
bulmecisco 4:76acfddc26fb 110 }
bulmecisco 4:76acfddc26fb 111 wait(1);
bulmecisco 4:76acfddc26fb 112 if( karel.AnyBeeperInBag() ) {
bulmecisco 4:76acfddc26fb 113 karel.PutBeeper();
bulmecisco 4:76acfddc26fb 114 karel.NibbleLeds(karel.AnyBeeperInBag());
bulmecisco 4:76acfddc26fb 115 }
bulmecisco 4:76acfddc26fb 116 wait(1);
bulmecisco 4:76acfddc26fb 117 if( karel.FrontIsClear() )
bulmecisco 4:76acfddc26fb 118 karel.Move();
bulmecisco 4:76acfddc26fb 119 else
bulmecisco 4:76acfddc26fb 120 karel.TurnLeft();
bulmecisco 4:76acfddc26fb 121
bulmecisco 4:76acfddc26fb 122 karel.ShutOff();
bulmecisco 4:76acfddc26fb 123 }
bulmecisco 4:76acfddc26fb 124
bulmecisco 4:76acfddc26fb 125
bulmecisco 4:76acfddc26fb 126 @endcode
bulmecisco 0:66e9a0afcbd6 127 */
bulmecisco 0:66e9a0afcbd6 128 class ur_Bertl
bulmecisco 0:66e9a0afcbd6 129 {
bulmecisco 0:66e9a0afcbd6 130 protected:
bulmecisco 4:76acfddc26fb 131 int beepersInBag; /**< how many beepers does the robot have in his bag;\n you can show it with: karel.NibbleLeds(karel.AnyBeeperInBag())*/
bulmecisco 0:66e9a0afcbd6 132 char cmd[3]; /**< I2C command */
bulmecisco 0:66e9a0afcbd6 133 int16_t btns; /**< which button is pressed */
bulmecisco 0:66e9a0afcbd6 134 InterruptIn _interrupt; /**< interrupted used*/
bulmecisco 0:66e9a0afcbd6 135 volatile int _count; /**< values of motor sensor*/
bulmecisco 0:66e9a0afcbd6 136
bulmecisco 0:66e9a0afcbd6 137 /**
bulmecisco 0:66e9a0afcbd6 138 protected methodes for internal purposes only
bulmecisco 0:66e9a0afcbd6 139 */
bulmecisco 0:66e9a0afcbd6 140 void increment(); /**< ISR to increment sensor values of motor */
bulmecisco 0:66e9a0afcbd6 141 bool backIsClear(); /**< don't now for what */
bulmecisco 0:66e9a0afcbd6 142 bool frontButtonPressed();/**< TRUE if a a button on the front of Robot is pressed else FALSE */
bulmecisco 4:76acfddc26fb 143 int bottomIsBlack(); /**< check line sensor; returns BCD value */
bulmecisco 0:66e9a0afcbd6 144 void error(); /**< Error: stops the robot and all LEDs are blinking*/
bulmecisco 0:66e9a0afcbd6 145
bulmecisco 0:66e9a0afcbd6 146 public:
bulmecisco 0:66e9a0afcbd6 147 ur_Bertl(); /**< default constructor; you have to define constants in config.h such as SPEED, DISTANCE or ANGLE*/
bulmecisco 0:66e9a0afcbd6 148 ur_Bertl(PinName pin); /**< parameterized constructor; on what pin should the interrupt work; SPEED, DISTANCE or ANGLE have to bee defined in config.h */
bulmecisco 0:66e9a0afcbd6 149
bulmecisco 12:cedc088eaf05 150 //void Move(); /**< Robot moves one turn as much as the constant DISTANCE; if one of the buttons fire --> Error()*/
bulmecisco 15:43d6a7e6e64a 151 void Move(); /**< Robot moves one turn as much as the constant DISTANCE; if one of the buttons fire --> Error()*/
bulmecisco 0:66e9a0afcbd6 152 void TurnLeft(); /**< Robot turns left as much as the constant ANGLE*/
bulmecisco 0:66e9a0afcbd6 153 void PutBeeper(); /**< if Robot has any Beepers in his bag he can put one or more Beeper; if not --> Error(()*/
bulmecisco 4:76acfddc26fb 154 void PickBeeper(); /**< if Robot stands on a black item he can pick one or more Beeper (max. 15 --> Error()); if not --> Error()*/
bulmecisco 15:43d6a7e6e64a 155 void TurnOff(); /**< turnes the robot off */
bulmecisco 4:76acfddc26fb 156 bool WaitUntilButtonPressed(); /**< wait until any button is pressed at the robot */
bulmecisco 1:fafbac0ba96d 157 bool FrontIsClear(); /**< returns a boolean value true if front is free; if not false */
bulmecisco 4:76acfddc26fb 158 bool NextToABeeper(); /**< returns a boolean value true if the robot is on a black place or line; if not --> false */
bulmecisco 7:e7f74f072564 159 bool IsButtonPressed(const int btn); /**< returns true if Button btn is pressed, else false */
bulmecisco 7:e7f74f072564 160 int ReturnButtonPressed(); /**< returns the int value of button pressed */
bulmecisco 4:76acfddc26fb 161 int AnyBeeperInBag(); /**< returns an int value (if > 0 equal true) how many beepers in bag; if zero --> false */
bulmecisco 4:76acfddc26fb 162 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())*/
bulmecisco 0:66e9a0afcbd6 163 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 */
bulmecisco 0:66e9a0afcbd6 164 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 */
bulmecisco 0:66e9a0afcbd6 165 void RGBLed(bool red, bool green, bool blue); /**<RGB Led with red, green and blue component of the Color */
bulmecisco 0:66e9a0afcbd6 166 void BlueLedsOFF(); /**< OFF all blue LEDs which are on the same Port 1_28 */
bulmecisco 0:66e9a0afcbd6 167 void BlueLedsON(); /**< ON all blue LEDs which are on the same Port 1_28 */
bulmecisco 0:66e9a0afcbd6 168 int Read();
bulmecisco 0:66e9a0afcbd6 169 };
bulmecisco 12:cedc088eaf05 170 /********************************************//**
bulmecisco 12:cedc088eaf05 171 name: Bertl.h \n
bulmecisco 13:3ce84646fd74 172 version: 3.0 \n
bulmecisco 12:cedc088eaf05 173 author:PE HTL BULME \n
bulmecisco 12:cedc088eaf05 174 email: pe@bulme.at \n
bulmecisco 12:cedc088eaf05 175 WIKI: https://developer.mbed.org/teams/BERTL_CHEL_18/code/ur_Bertl/wiki/Homepage \n
bulmecisco 12:cedc088eaf05 176 description:
bulmecisco 12:cedc088eaf05 177 Definition and documentation portion of the class Bertl The Robot.
bulmecisco 12:cedc088eaf05 178
bulmecisco 12:cedc088eaf05 179 ***********************************************/
bulmecisco 12:cedc088eaf05 180
bulmecisco 12:cedc088eaf05 181 /**
bulmecisco 12:cedc088eaf05 182 @code
bulmecisco 12:cedc088eaf05 183 //
bulmecisco 12:cedc088eaf05 184 @endcode
bulmecisco 12:cedc088eaf05 185
bulmecisco 12:cedc088eaf05 186 Example motor sensor test:
bulmecisco 12:cedc088eaf05 187 @code
bulmecisco 12:cedc088eaf05 188 #include "mbed.h"
bulmecisco 12:cedc088eaf05 189 #include "ur_Bertl.h"
bulmecisco 12:cedc088eaf05 190 #include "const.h"
bulmecisco 12:cedc088eaf05 191
bulmecisco 12:cedc088eaf05 192 int main()
bulmecisco 12:cedc088eaf05 193 {
bulmecisco 12:cedc088eaf05 194 ur_Bertl karel(LEFTSENSOR); // RIGHTSENSOR
bulmecisco 12:cedc088eaf05 195
bulmecisco 12:cedc088eaf05 196 while(true) {
bulmecisco 12:cedc088eaf05 197 karel.NibbleLeds(karel.Read());
bulmecisco 12:cedc088eaf05 198 }
bulmecisco 12:cedc088eaf05 199 }
bulmecisco 12:cedc088eaf05 200 @endcode
bulmecisco 12:cedc088eaf05 201
bulmecisco 12:cedc088eaf05 202 Example follow a line:
bulmecisco 12:cedc088eaf05 203 @code
bulmecisco 12:cedc088eaf05 204 #include "mbed.h"
bulmecisco 12:cedc088eaf05 205 #include "ur_Bertl.h"
bulmecisco 12:cedc088eaf05 206 #include "const.h"
bulmecisco 12:cedc088eaf05 207
bulmecisco 12:cedc088eaf05 208 int main()
bulmecisco 12:cedc088eaf05 209 {
bulmecisco 12:cedc088eaf05 210 Bertl karel;
bulmecisco 12:cedc088eaf05 211
bulmecisco 12:cedc088eaf05 212 while(karel.WaitUntilButtonPressed())
bulmecisco 12:cedc088eaf05 213 {
bulmecisco 12:cedc088eaf05 214 karel.NibbleLeds(karel.GetLineValues());
bulmecisco 12:cedc088eaf05 215 switch(karel.GetLineValues())
bulmecisco 12:cedc088eaf05 216 {
bulmecisco 12:cedc088eaf05 217 case 0x00:
bulmecisco 12:cedc088eaf05 218 karel.TurnLeftStep(50);
bulmecisco 12:cedc088eaf05 219 break;
bulmecisco 12:cedc088eaf05 220 case 0x03: case 0x01: case 0x07:
bulmecisco 12:cedc088eaf05 221 karel.TurnLeftStep(20);
bulmecisco 12:cedc088eaf05 222 break;
bulmecisco 12:cedc088eaf05 223 case 0x0E: case 0x0A: case 0x08:
bulmecisco 12:cedc088eaf05 224 karel.TurnRigthStep(20);
bulmecisco 12:cedc088eaf05 225 break;
bulmecisco 12:cedc088eaf05 226 default:
bulmecisco 12:cedc088eaf05 227 karel.Move(20);
bulmecisco 12:cedc088eaf05 228 break;
bulmecisco 12:cedc088eaf05 229 }
bulmecisco 12:cedc088eaf05 230 if(!karel.FrontIsClear())
bulmecisco 12:cedc088eaf05 231 karel.TurnLeftStep(1000);
bulmecisco 12:cedc088eaf05 232 }
bulmecisco 12:cedc088eaf05 233 }
bulmecisco 12:cedc088eaf05 234 @endcode
bulmecisco 12:cedc088eaf05 235 */
bulmecisco 12:cedc088eaf05 236 class Bertl : public ur_Bertl
bulmecisco 12:cedc088eaf05 237 {
bulmecisco 12:cedc088eaf05 238 protected:
bulmecisco 12:cedc088eaf05 239 public:
bulmecisco 12:cedc088eaf05 240 //Bertl(); /**< default constructor; you have to define constants in config.h such as SPEED, DISTANCE or ANGLE*/
bulmecisco 13:3ce84646fd74 241 int Move(int move = MOVE); /**< Robot moves one turn as much as the constant DISTANCE; if one of the buttons fire --> Error()*/
bulmecisco 13:3ce84646fd74 242 int MoveBackwards(int move = MOVE); /**< Robot moves as much back as the constant DISTANCE; if one of the buttons fire --> Error()*/
bulmecisco 12:cedc088eaf05 243 void TurnRigth(); /**< Robot turns rigth as much as the constant ANGLE*/
bulmecisco 12:cedc088eaf05 244 void TurnLeftStep(int step=STEPTIME); /**< Robot turns left for a short time defined in STEPTIME */
bulmecisco 12:cedc088eaf05 245 void TurnRigthStep(int step=STEPTIME); /**< Robot turns rigth for a short time defined in STEPTIME */
bulmecisco 12:cedc088eaf05 246 uint8_t GetLineValues(); /**< in the lower 4 bit are the values of the line sensor */
bulmecisco 13:3ce84646fd74 247 void RGBLed(bool red, bool green, bool blue); /**<RGB Led with red, green and blue component of the Color */
bulmecisco 12:cedc088eaf05 248 };
bulmecisco 0:66e9a0afcbd6 249 #endif