Revision 0:09cac0d76c5e, committed 2016-03-07
- Comitter:
- DomiSukic
- Date:
- Mon Mar 07 11:51:28 2016 +0000
- Commit message:
Changed in this revision
bertl.cpp | Show annotated file Show diff for this revision Revisions of this file |
bertl.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bertl.cpp Mon Mar 07 11:51:28 2016 +0000 @@ -0,0 +1,109 @@ +/**************************** +Bertl engine&I2C Lib. rework + +2.2.2016 + +by Dominik Sukic +with help from Kevin Heinrich +*****************************/ + +#include <mbed.h> +#include "bertl.h" +// Define Motor Routines +DigitalOut engineLR(P1_0); //IN1, EP10, MG1 engine-Pin 2 +DigitalOut engineLF(P1_1); //IN2, EP11, MG1 engine-Pin 1 +PwmOut engineLEN(p34); //EN1, P34 +DigitalOut engineRR(P1_4); //IN4, EP13, MG2 engine-Pin 2 +DigitalOut engineRF(P1_3); //IN3, EP14, MG2 engine-Pin 1 +PwmOut engineREN(p36); //EN2, P36 + +// Define PC9555 Routines +I2C pc9555(P0_5, P0_4);// SDA, SCL + +//Start PC9555 Routines +void bertl_PC9555_init() +{ + char data[2]; + // I2C-Initialisierung + pc9555.frequency(PC9555_FREQUENCY); + // Port 0 = Leds, Port 1 = Switches + // Adresse, R/W# = 0, Config PORT 0 (6) = 0x00(= Output), Stop + data[0] = PC9555_PORT0_DIRCONFIG; + data[1] = 0x00; + pc9555.write(PC9555_ADDR_W, data, 2); + // Adresse, R/W# = 0, Config PORT 1 (7) = 0xFF(= Input), Stop + data[0] = PC9555_PORT1_DIRCONFIG; + data[1] = 0xFF; + pc9555.write(PC9555_ADDR_W, data, 2); +} + +void bertl_PC9555_leds(unsigned char leds) +{ + char data[2]; //Two Bytes for Transmitt via I2C + // Send leds to PORT0 of PC9555 via I2C: + // Start, Address, RW# = 0, CMD PC9555_PORT0_OUT, leds, STOP + data[0] = PC9555_PORT0_OUT; + data[1] = ~leds; //bitwise inversion since Hardware is switched on with 0 (inverse logic) + pc9555.write(PC9555_ADDR_W, data, 2); +} + +unsigned char bertl_PC9555_switches() +{ + char taster; + + pc9555.start(); // Start PC9555 to write the Adress + pc9555.write(PC9555_ADDR_W); // Write the WRITE-Adress + pc9555.write(0x01); // To define the Switches + pc9555.start(); // Start PC9555 again to read the Values + pc9555.write(PC9555_ADDR_R); // Set the READ_bit + taster = pc9555.read(0); // Read the Value of the Switches + pc9555.stop(); // Stop the I2C + + return taster; // Return the value + +} +// END PC9555 Routines + +//Experimental +bool checkbitFront() +{ + bool Frontbuttons = CHECK_BIT(bertl_PC9555_switches(), 7) | CHECK_BIT(bertl_PC9555_switches(), 6) | CHECK_BIT(bertl_PC9555_switches(), 2) | CHECK_BIT(bertl_PC9555_switches(), 0); //Checks if a bit of the front buttons is set + + return Frontbuttons; //returns true if set +} +bool checkbitBack() +{ + bool Backbuttons = CHECK_BIT(bertl_PC9555_switches(), 5) | CHECK_BIT(bertl_PC9555_switches(), 4) | CHECK_BIT(bertl_PC9555_switches(), 1); //Checks if a bit of the back buttons is set + + return Backbuttons; //returns true if set +} + +// Begin Motor Routines +void bertl_engine(int left, int right) +{ + // If the left engines-value is greater than 0, the left engine turn FORWARD + if(left > 0) { + engineLF=1; + engineLR=0; + } + // Or if the left engines-value is less than 0, the left engine turn REVERSE + else if(left < 0) { + engineLF=0; + engineLR=1; + left = left * (-1); // For PWM the Value have to be positive + } + // If the right engines-value is greater than 0, the right engine turn FORWARD + if(right > 0) { + engineRF=1; + engineRR=0; + } + // Or if the right engines-value is less than 0, the right engine turn REVERSE + else if(right < 0) { + engineRF=0; + engineRR=1; + right = right * (-1); // For PWM the Value have to be positive + } + // Or if the right- or/and the left engines value equals 0, the ENABLE-Pin turn off + engineLEN=(left/255.0); + engineREN=(right/255.0); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bertl.h Mon Mar 07 11:51:28 2016 +0000 @@ -0,0 +1,58 @@ +#include <mbed.h> +//Checkbit-Defines +#define CHECK_BIT(var,pos) ((var) & (1<<(pos))) //Checks if bits are set Input: Variable to chaeck adn the position of the bit to check +//LED-Defines +#define LED_OFF 0x00 // 0b0000 0000, All Leds are off +#define LED_FL_WHITE 0x01 // 0b0000 0001, BERTL FORWARD Left-Side WHITE-LED +#define LED_FL_YELLOW 0x02 // 0b0000 0010, BERTL FORWARD Left-Side YELLOW-LED +#define LED_FR_WHITE 0x04 // 0b0000 0100, BERTL FORWARD Right-Side WHITE-LED +#define LED_FR_YELLOW 0x08 // 0b0000 1000, BERTL FORWARD Right-Side YELLOW-LED +#define LED_BL_RED 0x10 // 0b0001 0000, BERTL BACKWARD Left-Side RED-LED +#define LED_BL_YELLOW 0x20 // 0b0010 0000, BERTL BACKWARD Left-Side YELLOW-LED +#define LED_BR_RED 0x40 // 0b0100 0000, BERTL BACKWARD Right-Side RED-LED +#define LED_BR_YELLOW 0x80 // 0b1000 0000, BERTL BACKWARD Right-Side YELLOW-LED +#define LED_ON 0xFF // 0b1111 1111, All Leds are on + +#define LED_R_YELLOW (LED_FR_YELLOW|LED_BR_YELLOW) // 0b1000 1000, BERTL Right Side, YELLOW-LEDs are on +#define LED_L_YELLOW (LED_FL_YELLOW|LED_BL_YELLOW) // 0b0010 0010, BERTL Left Side, YELLOW-LEDs are on +#define LED_F_WHITE (LED_FL_WHITE|LED_FR_WHITE) // 0b0000 0101, BERTL Forward, WHITE-LEDs are on +#define LED_B_RED (LED_BL_RED|LED_BR_RED) // 0b0101 0000, BERTL Backward, RED LEDs are on +#define LED_B_ALL (LED_B_RED|LED_BL_YELLOW|LED_BR_YELLOW) // 0b1111 0000, BERTL Backward, All LEDs are on +#define LED_F_ALL (LED_F_WHITE|LED_FL_YELLOW|LED_FR_YELLOW) // 0b0000 1111, BERTL Forward, All LEDs are on + +// Define PC9555 Routines +#define PC9555_ADDR_W 0x40 //A2 = A1 = A0 = 0, WRITE-ADRESS +#define PC9555_ADDR_R 0x41 // READ-ADRESS, to set the Read-bit +#define PC9555_FREQUENCY 100000 // fI2C in Hz + +// PC9555 Commands +#define PC9555_PORT0_IN (0) +#define PC9555_PORT1_IN (1) +#define PC9555_PORT0_OUT (2) +#define PC9555_PORT1_OUT (3) +#define PC9555_PORT0_INV (4) +#define PC9555_PORT1_INV (5) +#define PC9555_PORT0_DIRCONFIG (6) +#define PC9555_PORT1_DIRCONFIG (7) + +// Define Motor Routines +#define ENGINE_LEFT_BACKWARD P1_0 //IN1, EP10, MG1A => MG1 engine-Pin 2, left_Reverse +#define ENGINE_LEFT_FORWARD P1_1 //IN2, EP11, MG1B => MG1 engine-Pin 1, left_Forward +#define ENGINE_ENABLE_LEFT P1_15 //EN1, P34, left_ENABLE +#define ENGINE_RIGHT_REVERSE P1_4 //IN4, EP13, MG2A => MG2 engine-Pin 2, right_Reverse +#define ENGINE_RIGHT_FORWARD P1_3 //IN3, EP14, MG2B => MG2 engine-Pin 1, right_Forward +#define ENGINE_ENABLE_RIGHT P0_21 //EN2, P36, right_ENABLE +#define MAX_PWM 255 //Define the MAX Value of the PWM + +// Motor Routines +void bertl_engine(int left, int right); +void bertl_engine_test(); + +// PC9555 routines +void bertl_PC9555_init(); +void bertl_PC9555_leds(unsigned char leds); +unsigned char bertl_PC9555_switches(); + +//Checkbit Routines + bool checkbitFront(); + bool checkbitBack();