Real eXample: Switching from Init Phase into Data Phase
Hello, besides the fact that i have studied some documents i got still stuck on how two tswitch from Init phase into data phase when using a sensor (Motor encoder or light / color sensor. To kake a long stoty short:
Dooes someone have a "real world" example code or code snippet how to handle the serial communication with a sensor.
Would be nice if it will be written in C. Frameworks like Arduino or MBed are welcome, too
Thanks in advance
Marc
The code we use in Pybricks is here.
The high level sequence is this:
- Set the baud rate to 115200
- Within 100ms of powering on a sensor, send a baud rate request (CMD_SPEED) of 115200 to the sensor
- If an ACK is received within the 100ms, then stay at 115200, otherwise switch to 2400 baud rate.
- Watch for CMD_TYPE to identify the beginning of the "info" sequence
- Read all of the info messages that are received.
- The final message of the "info" sequence should be ACK.
- Reply with ACK.
- Change to the baud rate the sensor gave during the info sequence.
- Read the "data" messages from the sensor
- At the same time send commands to set the mode (or multi-mode) as needed.
This is from memory, so trust the code over my memory :smile:
Hi @dlech,
Thanks for the explanation, it got me to step 9 for my personal project.
Where I'm trying to read out the angular data of a control+ motor
Data rate switch is working and I'm getting response from NACK.
But I'm receiving the following frames 0xc0 0x00 0x3f constantly.
My guess I've to set the mode described in step 10 but are there any examples of those?
Thanks,
Peter
But I'm receiving the following frames
0xc0 0x00 0x3fconstantly.
This is expected: see https://github.com/pybricks/technical-info/blob/master/uart-protocol.md#data-messages
To set the mode, use CMD_SELECT: https://github.com/pybricks/technical-info/blob/master/uart-protocol.md#cmd_select
In Pybricks, we have found the position and speed data to be a bit buggy so we now use the CALIB mode (4) to get the "raw" absolute position and do our own speed estimate. Before that we would use a mode combo to get the POS, SPEED and ABS modes all a the same time (0x5C, 0x23, 0x00, 0x10, 0x20, 0x30, 0x00, 0x00, 0x00, 0x80).