technical-info icon indicating copy to clipboard operation
technical-info copied to clipboard

Real eXample: Switching from Init Phase into Data Phase

Open marcrupprath opened this issue 5 years ago • 3 comments

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

marcrupprath avatar Jun 20 '20 16:06 marcrupprath

The code we use in Pybricks is here.

The high level sequence is this:

  1. Set the baud rate to 115200
  2. Within 100ms of powering on a sensor, send a baud rate request (CMD_SPEED) of 115200 to the sensor
  3. If an ACK is received within the 100ms, then stay at 115200, otherwise switch to 2400 baud rate.
  4. Watch for CMD_TYPE to identify the beginning of the "info" sequence
  5. Read all of the info messages that are received.
  6. The final message of the "info" sequence should be ACK.
  7. Reply with ACK.
  8. Change to the baud rate the sensor gave during the info sequence.
  9. Read the "data" messages from the sensor
  10. 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:

dlech avatar Jun 20 '20 19:06 dlech

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

PetervdPerk avatar Aug 08 '22 18:08 PetervdPerk

But I'm receiving the following frames 0xc0 0x00 0x3f constantly.

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).

dlech avatar Aug 08 '22 19:08 dlech