UART half duplex for Dynamixel servos

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

UART half duplex for Dynamixel servos

Post by OutoftheBOTS_ » Mon Feb 28, 2022 4:14 am

I thinking about using Dynamixels Servos on some of my micropython projects.

The Dynamixel servos are what many would call a smart servo where the servo has an MCU on it that controls the servo including all PID speed and position and torque control and a master just tells the dynamixel what it wants it to do via a single wire half duplex UART protocol.

They dynamixel actually has a STM32F103 processor on it that receives the half duplex UART coms and carries out the commands.

Robotis that make dynamixel servos does document the protocol of the packet that is sent over half duplex UART and does provide a SDK for using it from a PC via a USB to UART converter or from a RPi or similar.

I have also used the dynamixels directly off a RPi UART but this requires for me to build some hard circuits to convert the full duplex to half duplex as RPi doesn't have hardware half duplex as well as modifying the setup of the RPi to increase the UART clock source so that I can the 1Mbp that the dynamixles talk on.

I would like to be able to drive the dynamixel directly from the UART of a micropython board.
How can 1 set the UART to hardware half duplex??
Is there a way to do it fro within the UART module or will I need to change the needed in the UART control register?
half duplex.PNG
half duplex.PNG (56.45 KiB) Viewed 4828 times

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: UART half duplex for Dynamixel servos

Post by pythoncoder » Mon Feb 28, 2022 11:01 am

I'd consider an external tri-state gate.
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART half duplex for Dynamixel servos

Post by dhylands » Mon Feb 28, 2022 7:01 pm

Depending the MCU being used, sometimes you don't need any extra hardware.

I have a driver, which you can find over here:
https://github.com/dhylands/bioloid3/bl ... rt_port.py

which should work on the STM32F4 series and allows you to the the Tx to the Rx and use that as the line that goes to the servos. You may still want an I2C style (bidirectional) voltage converter if you have any long cable runs, although it should work fine with the 3.3v control for short runs.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: UART half duplex for Dynamixel servos

Post by OutoftheBOTS_ » Wed Mar 02, 2022 12:32 am

dhylands wrote:
Mon Feb 28, 2022 7:01 pm
Depending the MCU being used, sometimes you don't need any extra hardware.

I have a driver, which you can find over here:
https://github.com/dhylands/bioloid3/bl ... rt_port.py
Thanks

Code: Select all

        # Set HDSEL (bit 3) in CR3 - which puts the UART in half-duplex
        # mode. This connects Rx to Tx internally, and only enables the
        # transmitter when there is data to send.
        stm.mem16[self.uart_base + stm.USART_CR3] |= (1 << 3)
Here's where you put it in half duplex.
which should work on the STM32F4 series and allows you to the the Tx to the Rx and use that as the line that goes to the servos. You may still want an I2C style (bidirectional) voltage converter if you have any long cable runs, although it should work fine with the 3.3v control for short runs.
Yes that's right the dynamixel is a 5v data line. Is the UART pin the the STM32 5v tolerant as the dynamixel sends back 5v

Did you also need a pull-up on the data line or did the STM32F4 pull-up the line when idle??

Also did you just use the CPython SDK here https://github.com/ROBOTIS-GIT/Dynamixe ... ter/python just with your UART handler or did you write your own SDK for constructing the packets to send and recieve??
Last edited by OutoftheBOTS_ on Wed Mar 02, 2022 12:40 am, edited 1 time in total.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: UART half duplex for Dynamixel servos

Post by OutoftheBOTS_ » Wed Mar 02, 2022 12:39 am

pythoncoder wrote:
Mon Feb 28, 2022 11:01 am
I'd consider an external tri-state gate.
The timing of trying to switch the tri-state logic gate using another GPIO pin is near impossile to do, other have tried that from the RPi UART.

I did find this circuit for converting full duplex to half duplex on an AVR reference manual. You would also have to add a level shifter to 3.3v too.
avr.PNG
avr.PNG (68.54 KiB) Viewed 4771 times

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART half duplex for Dynamixel servos

Post by dhylands » Wed Mar 02, 2022 5:36 am

OutoftheBOTS_ wrote:
Wed Mar 02, 2022 12:39 am
pythoncoder wrote:
Mon Feb 28, 2022 11:01 am
I'd consider an external tri-state gate.
The timing of trying to switch the tri-state logic gate using another GPIO pin is near impossile to do, other have tried that from the RPi UART.

I did find this circuit for converting full duplex to half duplex on an AVR reference manual. You would also have to add a level shifter to 3.3v too.
avr.PNG
It's impossible to do from straight python. That's why my UART driver (for the STM32F4) uses inline assembler and its definitely possible to switch things fast enough that way. I haven't tried the Dynamixels with the RPi,

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART half duplex for Dynamixel servos

Post by dhylands » Wed Mar 02, 2022 6:18 am

OutoftheBOTS_ wrote:
Wed Mar 02, 2022 12:32 am
Yes that's right the dynamixel is a 5v data line. Is the UART pin the the STM32 5v tolerant as the dynamixel sends back 5v
Yes the STM32F4 UART lines are 5v tolerant.
Did you also need a pull-up on the data line or did the STM32F4 pull-up the line when idle??
I think I must have had a pullup on the data line since I don't see anything in the code about enabling the GPIO pullup, and I'm not sure if the GPIO pullup would have an impact on the UART.
Also did you just use the CPython SDK here https://github.com/ROBOTIS-GIT/Dynamixe ... ter/python just with your UART handler or did you write your own SDK for constructing the packets to send and recieve??
I have some CPython code here: https://github.com/dhylands/Bioloid which implements a command line that allows you to interact with the servos (or other devices) and it uses a bunch of code I wrote for assembling packets etc (I originally wrote this before Robotis came out with their software). It was also originally written in Python 2 and I just recently updated it to run on Python3) I also have C/C++ code which implements a similar command line interface (this was written before the CPython code) https://github.com/dhylands/projects/tr ... ioloid/cli The Bioloid project has a README.md file, and the C CLI has a bioloid.htm file.

The https://github.com/dhylands/bioloid3 project has some code which will run on micropython. I was working on some code which would accept packets on the USB interface and forward them out the UART. I was using a Espruino Pico board as a bioloid bus interface, and was also adding code to implement my own bioloid devices using MicroPython. The bus.py code has an API for implementing the bioloid commands (i.e. ping, read, write, etc).

My brother and I wrote some C code to implement our own bioloid devices using an AVR here: https://github.com/dhylands/projects/tree/master/avr Look in the directories that start with bioloid-

I'm currently in the process of improving/implementing some C++ code for implementing bioloid devices.

And the bioloid3 project also has this variant: https://github.com/dhylands/bioloid3/bl ... io_port.py which assumes you're using a GPIO line to switch an external device (we typically use an SN74LVC2G241)

The C/C++ code and the Bioloid code were all originally written before MicroPython existed (2009 for the C/C++ code and 2013 for the CPython code). The bioloid3 project was me playing with code which would run under micropython.

Feel free to ask questions.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: UART half duplex for Dynamixel servos

Post by OutoftheBOTS_ » Wed Mar 02, 2022 9:05 am

@dhlands

Have you looked at the the Robotis dynamixel SDK for both their protocol 1.0 and 2.0 it has implementations in many programming languages. I have used their python SDK on both PC and RPi and it has all the needed low level primitive functionality but could do with a high level API wrapper for ease of use.

If you based any of your further work on the official Robotis SDK then this will help you to be compatible with anything anyone else writes.

I brought the Robotis Engineer kit which is the next generation after the bioloid. The next gen servos from the Engineer kit use dynamixel protocol 2.0 and I think the bioloid servos use protocol 1.0

Post Reply