encoderComplex.c
/*******************************************************************************
* examples/encoderComplex.c *
* ========================= *
* *
* Version: 1.1.5 *
* Date : 09.11.17 *
* Author : Peter Weissig *
* *
* If you are changing this file, you may also consider to update *
* demos/diagnosticTool/diagnosticTool.c *
*******************************************************************************/
//*********************************<Included files>*****************************
#include <tucbot/tucbot.h>
//*********************************<Methods>************************************
void testEncoder(void);
int main(void);
//*********************************[testEncoder]********************************
void testEncoder(void) {
uint8_t menu_pos = 0;
uint8_t flag_redraw_menu = 0xFF;
int8_t speed_l = 0, speed_r = 0;
uint8_t bumpers;
lcdclr();
lcdstr_p(PSTR("Encoder - " ));
while (1) {
// redraw menu
if (flag_redraw_menu) {
flag_redraw_menu = 0x00;
lcdxy(10,0);
switch (menu_pos) {
case 0:
lcdstr_p(PSTR("Ticks"));
break;
case 1:
lcdstr_p(PSTR("Clear"));
break;
default:
lcdstr_p(PSTR("Speed"));
menu_pos = 2;
break;
}
lcdxy(0,1); lcdstr_p(PSTR("L:##### R:##### "));
mdelay(400);
}
// check if buttons were pushed
// middle button --> increase menu
mdelay(100);
menu_pos++;
if (menu_pos > 2) {menu_pos = 0;}
flag_redraw_menu = 0xFF;
} else {
// right button --> clear encoder
mdelay(100);
mdelay(400);
}
// update display depending on menu position
switch (menu_pos) {
case 0 :
lcdxy( 7,1); lcdout(' '); // in case encoder < -9999
lcdxy( 2,1);
lcdxy(15,1); lcdout(' '); // in case encoder < -9999
lcdxy(10,1);
mdelay(250);
}
break;
case 1 :
lcdxy( 7,1); lcdout(' '); // in case encoder < -9999
lcdxy( 2,1);
lcdxy(15,1); lcdout(' '); // in case encoder < -9999
lcdxy(10,1);
mdelay(250);
}
break;
default:
lcdxy( 7,1); lcdout(' '); // in case encoder < -9999
lcdxy( 2,1);
lcdxy(15,1); lcdout(' '); // in case encoder < -9999
lcdxy(10,1);
}
mdelay(250);
break;
}
}
// change motor speed through bumpers
bumpers = bumpers_get(kBumperAll);
if (bumpers & kBumperBackLeft ) { speed_l+= 10;}
if (bumpers & kBumperFrontLeft ) { speed_l-= 10;}
if (bumpers & kBumperBackRight ) { speed_r+= 10;}
if (bumpers & kBumperFrontRight) { speed_r-= 10;}
if (speed_l < -70) {speed_l = -70;}
if (speed_l > 70) {speed_l = 70;}
if (speed_r < -70) {speed_r = -70;}
if (speed_r > 70) {speed_r = 70;}
mdelay(250);
}
}
//*********************************[main]***************************************
int main (void) {
init_tucbot(0xFF);
testEncoder();
return (0);
}
leds_set
void leds_set(eLedMask bitmask, uint8_t bool)
Sets the selected leds on or off.
encoders_changed
eEncoderSelectMask encoders_changed(eEncoderSelectMask bitmask)
Indicates if new data changed the selected data blocks.
buttons_get
eButtonMask buttons_get(eButtonMask bitmask)
Returns the last stored value of the selected buttons.
kBumperBackLeft
@ kBumperBackLeft
bit mask (0x04): the back left bumper
Definition: bumpers.h:36
kButtonMiddle
@ kButtonMiddle
bit mask (0x08): middle button
Definition: buttons.h:35
kEncoderSelectSpeed
@ kEncoderSelectSpeed
Definition: encoders.h:121
kModeStudent
@ kModeStudent
constant (1): robot is in student mode
Definition: mode.h:31
kMotorSetRight
@ kMotorSetRight
bit mask (0x02): right motor
Definition: motors.h:45
kLedOn
@ kLedOn
constant (0xFF): turn leds on
Definition: leds.h:63
kLedRight
@ kLedRight
bit mask (0x04): right led
Definition: leds.h:46
kEncoderGetSpeedLeft
@ kEncoderGetSpeedLeft
constant (3): left speed in ticks/s
Definition: encoders.h:185
kButtonRight
@ kButtonRight
bit mask (0x10): right button
Definition: buttons.h:37
kEncoderSelectAbsAndClear
@ kEncoderSelectAbsAndClear
Definition: encoders.h:159
kEncoderGetClearedRight
@ kEncoderGetClearedRight
constant (6): right encoder before it was cleared (16-bit)
Definition: encoders.h:191
buttons_request
void buttons_request(eRequestType request)
Requests new data from the buttons.
kEncoderSelectAbs
@ kEncoderSelectAbs
Definition: encoders.h:118
kBumperFrontRight
@ kBumperFrontRight
bit mask (0x02): the front right bumper
Definition: bumpers.h:34
int16tostr
void int16tostr(void *out, int16_t number, uint8_t digits)
Converts an integer to a string and passes it to an output function.
encoders_clear
void encoders_clear(void)
Clears the encoder values.
kRequestContinuous
@ kRequestContinuous
constant (3): request continuous updates
Definition: update.h:37
kBumperAll
@ kBumperAll
bit mask (0x0F): all bumpers
Definition: bumpers.h:40
kLedMiddle
@ kLedMiddle
bit mask (0x08): middle led
Definition: leds.h:44
kLedOff
@ kLedOff
constant (0x00): turn leds off
Definition: leds.h:61
kEncoderGetSpeedRight
@ kEncoderGetSpeedRight
constant (4): right speed in ticks/s
Definition: encoders.h:187
lcdclr
void lcdclr(void)
Clears the display.
lcdxy
void lcdxy(uint8_t x, uint8_t y)
Moves the cursor to a postion on the display.
kRequestSingle
@ kRequestSingle
constant (1): request a single update
Definition: update.h:33
bumpers_request
void bumpers_request(eRequestType request)
Requests new data from the bumpers.
mode_set
void mode_set(eMode mode)
Sets the mode of the TUC-Bot.
encoders_get
int32_t encoders_get(eEncoderGet select)
Returns the last stored version of the selected value.
kEncoderGetRight
@ kEncoderGetRight
constant (2): right encoder (32-bit)
Definition: encoders.h:183
kEncoderGetLeft
@ kEncoderGetLeft
constant (1): left encoder (32-bit)
Definition: encoders.h:181
motors_set
void motors_set(eMotorSetMask select, int8_t power)
Sets the motor power of the selected motor(s).
lcdstr_p
void lcdstr_p(const char *send_data)
Writes a string located in the flash to the display.
kMotorSetLeft
@ kMotorSetLeft
bit mask (0x01): left motor
Definition: motors.h:43
encoders_request
void encoders_request(eEncoderSelectMask bitmask, eRequestType request)
Requests new data from the encoders.
lcdout
void lcdout(uint8_t data)
Writes one byte to the display.
kBumperBackRight
@ kBumperBackRight
bit mask (0x08): the back right bumper
Definition: bumpers.h:38
init_tucbot
void init_tucbot(uint8_t enable_interrupts)
Initiates the TUC-Bot.
mdelay
void mdelay(uint16_t mseconds)
Waits for the given time in milliseconds.
kEncoderGetClearedLeft
@ kEncoderGetClearedLeft
constant (5): left encoder before it was cleared (16-bit)
Definition: encoders.h:189
bumpers_get
eBumperMask bumpers_get(eBumperMask bitmask)
Returns the last stored value of the masked bumpers.
kBumperFrontLeft
@ kBumperFrontLeft
bit mask (0x01): the front left bumper
Definition: bumpers.h:32