I am using a Raspberry Pi 3, B+ pertinent libraries are WiringPi, with WiringSerial. Code is written in c.
EDIT: the Serial connection is handled through GPIO 14(Tx) & 15(Rx), 5v (Tx) is stepped down through 165 ohm resistor and, a 3.3v Zener Diode.
The following code is what I use to retrieve and and convert Raw Encoder Count to 16bit value.
int16_t sensors_left_encoder_init() {
int16_t left_encoder_start = 0;
char buffer_l[2];
int i = 0;
serialFlush(fd);
serialPutchar(fd, 142); // Request sensor packet
serialPutchar(fd, 43); // Left encoder request
do {
buffer_l[i] = serialGetchar(fd);
printf(" -> Left Serial Value: %d\n", buffer_l[i]);
i++;
} while (serialDataAvail(fd));
left_encoder_start = (int16_t)(((buffer_l[0]) & 0xFF) << 8 | (buffer_l[1]) & 0xFF);
printf(" -> Left Encoder Start: %" PRId16 "\n", left_encoder_start);
return left_encoder_start;
}
int16_t sensors_right_encoder_init() {
int16_t right_encoder_start = 0;
unsigned char buffer_r[2];
int i = 0;
serialFlush(fd);
serialPutchar(fd, 142); // Request sensor packet
serialPutchar(fd, 44); // Left encoder request
do {
buffer_r[i] = serialGetchar(fd);
printf(" -> Right Serial Value: %d\n", buffer_r[i]);
i++;
} while (serialDataAvail(fd));
right_encoder_start = (int16_t)(((buffer_r[0]) & 0xFF) << 8 | (buffer_r[1]) & 0xFF);
printf(" -> Right Encoder Start: %" PRId16 "\n", right_encoder_start);
return right_encoder_start;
}
I pulled the battery, ran the code and the first run produced:
-> Left Serial Value: 0
-> Left Serial Value: 1
-> Left Encoder Start: 1
-> Right Serial Value: 0
-> Right Serial Value: 2
-> Right Encoder Start: 2
However the second run through produced this:
-> Left Serial Value: 0
-> Left Serial Value: 3
-> Left Encoder Start: 3
-> Right Serial Value: 0
-> Right Serial Value: 4
-> Right Encoder Start: 4
I have a feeling its how serialGetchar() is receiving the serial output from the interface, but I am not 100%.
From reading the wheel encoder topics on here, and the OI Specs, I understand that the raw values should not change unless a drive command was issued to the interface.