Modules | Enumerations | Functions
Encoders

Functions which handle the wheel encoders. More...

Modules

 Advanced informations
 

Enumerations

enum  eEncoderSelectMask
 clusters bit masks for the requests of the encoders More...
 
enum  eEncoderGet
 clusters select values for reading data from the encoders More...
 

Functions

void encoders_clear (void)
 Clears the encoder values. More...
 

Request Funktions

void encoders_request (eEncoderSelectMask bitmask, eRequestType request)
 Requests new data from the encoders. More...
 
eEncoderSelectMask encoders_received (eEncoderSelectMask bitmask)
 Indicates if new data for the selected data blocks were received. More...
 
eEncoderSelectMask encoders_changed (eEncoderSelectMask bitmask)
 Indicates if new data changed the selected data blocks. More...
 

Access Funktions

int32_t encoders_get (eEncoderGet select)
 Returns the last stored version of the selected value. More...
 

Detailed Description

Functions which handle the wheel encoders.

The encoders are part of the driving module. All actions, like reading their current value or reseting them, involve internal TWI communication. This may take some milliseconds (delay time).

After requesting kEncoderSelectAbs the encoder values can be regularly read with encoders_get().

In a similar way the speed of the wheels can be measured by requesting kEncoderSelectSpeed. The speed of the wheels is calculated within the drive module of the TUC-Bot. In order to allow the maximum possible precision, there are two ways of doing that:

The clearing of the encoder values is done by calling encoders_clear(). The last stored values can be read afterwards with encoders_get().

snippets

  1. Reading the latest absolute encoder values. The request function will wait until new encoder data is received. This might take some milliseconds.
  2. Reading absolute encoder values. There will be no delay during the execution in the main loop. However, the returned encoder values might be outdated by some milliseconds.
    // init
    ...
    // within main loop
    int32_t left = encoders_get(kEncoderGetLeft );
    int32_t right = encoders_get(kEncoderGetRight);
  3. Clear encoders and return their last values. This might take some milliseconds.
    Do not read the current absolute encoder values instead. They are most likely outdated.
    // reset encoders and store last values

example

/*******************************************************************************
* examples/encoderSimple.c *
* ======================== *
* *
* Version: 1.1.4 *
* Date : 09.11.17 *
* Author : Peter Weissig *
*******************************************************************************/
//*********************************<Included files>*****************************
#include <tucbot/tucbot.h>
//*********************************<Methods>************************************
void testEncoder(void);
int main(void);
//*********************************[testEncoder]********************************
void testEncoder(void) {
lcdclr();
lcdstr_p(PSTR("Encoder" ));
lcdxy(0,1);
lcdstr_p(PSTR("L:##### R:#####"));
while (1) {
mdelay(100);
mdelay(400);
} else if (buttons_get(kButtonRight)) {
mdelay(100);
mdelay(400);
}
mdelay(250);
}
}
}
//*********************************[main]***************************************
int main (void) {
init_tucbot(0xFF);
testEncoder();
return (0);
}

Enumeration Type Documentation

◆ eEncoderGet

clusters select values for reading data from the encoders

See also
encoders_get() and encoders_clear()
Enumerator
kEncoderGetLeft 

constant (1): left encoder (32-bit)

kEncoderGetRight 

constant (2): right encoder (32-bit)

kEncoderGetSpeedLeft 

constant (3): left speed in ticks/s

kEncoderGetSpeedRight 

constant (4): right speed in ticks/s

kEncoderGetClearedLeft 

constant (5): left encoder before it was cleared (16-bit)

kEncoderGetClearedRight 

constant (6): right encoder before it was cleared (16-bit)

◆ eEncoderSelectMask

clusters bit masks for the requests of the encoders

See also
encoders_request(), encoders_received() and encoders_changed()
Enumerator
kEncoderSelectNone 

bit mask (0x00): select no data block

kEncoderSelectAbs 

bit mask (0x01): select absolute encoder values (32-bit)
see also sMD_Encoder

kEncoderSelectSpeed 

bit mask (0x02): select wheel speed in ticks per second
see also sMD_SpeedRaw

kEncoderSelectAll 

bit mask (0x03): select all data blocks (except for clear)

Function Documentation

◆ encoders_changed()

eEncoderSelectMask encoders_changed ( eEncoderSelectMask  bitmask)

Indicates if new data changed the selected data blocks.

Parameters
bitmaskbit mask of the selected data blocks - see also eEncoderSelectMask
The parameter is used to only return the value of the masked changed-bits. All other values(bits) are set to 0.
Returns
bitfield of the selected data blocks - see also eEncoderSelectMask
If a data block is changed its related bit will be 1, otherwise 0.
If only one data block is selected the return value can be interpreted as a boolean expression.
See also
encoders_received(), encoders_request(), encoders_get()
Examples
encoderComplex.c, and encoderSimple.c.

◆ encoders_clear()

void encoders_clear ( void  )

Clears the encoder values.

This function waits until the encoder values within the drive module were cleared.

The last values of the encoders, before clearing, are stored in data block sMD_EncoderClear and can be read afterwards with encoders_get():

Involved data blocks are:
sMD_EncoderClear

Note
This function does not update the current absolute encoder values within sMD_Encoder. This must be separably requested, if needed.
Keep also in mind, that all data blocks are independent. Therefore, reading the current absolute encoder values right after clearing them, will most likely return old values, instead of zeros.
Examples
encoderComplex.c, and encoderSimple.c.

◆ encoders_get()

int32_t encoders_get ( eEncoderGet  select)

Returns the last stored version of the selected value.

This function relies on the internal data blocks sMD_Encoder, sMD_SpeedRaw and sMD_EncoderClear - for new data use encoders_request() or encoders_clear().

Parameters
selectvalue of the selected data - see also eEncoderGet
Returns
32-bit integer
select decription result
kEncoderGetLeft
kEncoderGetRight
current encoder value in ticks 32-bit
kEncoderGetSpeedLeft
kEncoderGetSpeedRight
current encoder speed in ticks/s 16-bit
kEncoderGetClearedLeft
kEncoderGetClearedRight
last encoder value before reset in ticks 16-bit
See also
encoders_request() and encoders_clear()
Examples
encoderComplex.c, and encoderSimple.c.

◆ encoders_received()

eEncoderSelectMask encoders_received ( eEncoderSelectMask  bitmask)

Indicates if new data for the selected data blocks were received.

Parameters
bitmaskbit mask of the selected data blocks - see also eEncoderSelectMask
The parameter is used to only return the value of the masked received-bits. All other values(bits) are set to 0.
Returns
bitfield of the selected data blocks - see also eEncoderSelectMask
If a data block is received its related bit will be 1, otherwise 0.
If only one data block is selected the return value can be interpreted as a boolean expression.
See also
encoders_changed(), encoders_request(), encoders_get()

◆ encoders_request()

void encoders_request ( eEncoderSelectMask  bitmask,
eRequestType  request 
)

Requests new data from the encoders.

For details on the request schema see The general request.

Involved data blocks are:
sMD_Encoder and sMD_SpeedRaw

Parameters
bitmaskbit mask of the selected data blocks - see also eEncoderSelectMask
requestSee also eRequestType
See also
encoders_get()
Examples
encoderComplex.c, and encoderSimple.c.
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.
kButtonMiddle
@ kButtonMiddle
bit mask (0x08): middle button
Definition: buttons.h:35
kLedOn
@ kLedOn
constant (0xFF): turn leds on
Definition: leds.h:63
kLedRight
@ kLedRight
bit mask (0x04): right led
Definition: leds.h:46
kButtonRight
@ kButtonRight
bit mask (0x10): right button
Definition: buttons.h:37
kEncoderGetClearedRight
@ kEncoderGetClearedRight
constant (6): right encoder before it was cleared (16-bit)
Definition: encoders.h:191
kRequestWait
@ kRequestWait
constant (2): request a single update and wait until it is done
Definition: update.h:35
buttons_request
void buttons_request(eRequestType request)
Requests new data from the buttons.
kEncoderSelectAbs
@ kEncoderSelectAbs
Definition: encoders.h:118
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
kLedMiddle
@ kLedMiddle
bit mask (0x08): middle led
Definition: leds.h:44
kLedOff
@ kLedOff
constant (0x00): turn leds off
Definition: leds.h:61
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.
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
lcdstr_p
void lcdstr_p(const char *send_data)
Writes a string located in the flash to the display.
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.
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