Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real eXample: Switching from Init Phase into Data Phase #4

Open
marcrupprath opened this issue Jun 20, 2020 · 3 comments
Open

Real eXample: Switching from Init Phase into Data Phase #4

marcrupprath opened this issue Jun 20, 2020 · 3 comments

Comments

@marcrupprath
Copy link

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

@dlech
Copy link
Member

dlech commented Jun 20, 2020

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 😄

@PetervdPerk
Copy link

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

@dlech
Copy link
Member

dlech commented Aug 8, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants