Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

  1. Lightbulb Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    Hello

    Many people out there want to control a Dynamixel servo with a Teensy.
    Dynamixel servos are very powerful and precise.
    You will not find any other servo with that precision for that price.

    The Teensy 3.1 has a much more powerful processor than many Arduino boards.
    It is so fast that it has no problem to drive the Dynamixel at 1 mega baud (which is the factory default).

    But if you search on Google how to drive a Dynamixel you find some posts on PJRC.com and on Trossenrobotics.com
    but NOT EVEN ONE forum discussion offers a working code to set the Teensy into tristate mode.

    It is the opposite: You find misleading postings where people use an external 74HC241 chip to make the Teensy 3-state.
    But this is nonsense.
    You do NOT need any external chips!
    You can connect the Dynamixel directly to the Teensy.

    The AX12 uses RS485 at TTL level which means that it's signals are between 0 V and 5 V.
    The Teensy 3.1 sends signals between 0 V and 3.3 V.
    But this is no problem: The comunication works perfectly.

    The CPU of the Teensy 3.1 offers the functionality to switch the TX pin of the UART into Tristate mode.
    The CPU is able to drive a RS485 half-duplex bus without any external chips.

    But how do I do this in a sketch ?
    This important funcionality is missing in the Teensy core library.
    And if you search in internet which CPU registers must be manipulated to make the TX pin tristate, you will waste hours without success.

    For that reason I post an approved solution here that I tested on Teensy 3.1 with a Dynamixel AX12 servo running at 1 Mega Baud.
    _______________________________________________

    Here come 3 functions that switch the TX pins of Serial1, Serial2 and Serial3 between tristate mode and normal mode:

    Code:
    void serial1_enable_TX_tristate(bool bEnable)
    {
        if (bEnable) CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_MUX(3); // set Open Drain Enabled
        else         CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // set Slew Rate Enabled
    }
    
    void serial2_enable_TX_tristate(bool bEnable)
    {
        if (bEnable) CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_MUX(3); // set Open Drain Enabled
        else         CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // set Slew Rate Enabled
    }
    
    void serial3_enable_TX_tristate(bool bEnable)
    {
        if (bEnable) CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_ODE | PORT_PCR_MUX(3); // set Open Drain Enabled
        else         CORE_PIN8_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3); // set Slew Rate Enabled
    }
    In tristate mode the pull-up resistor in the CPU is enabled.
    In normal mode the CPU pulls the pin directly to GND and +3.3 V.

    I tested these functions on Teensy 3.1 but I suppose they will also work on the other Teensy boards.

    I hope that PJRC will add these 3 functions to the core library and add the corresponding functions in HardwareSerial.h.

    ________________________________________________

    And here come 2 more functions that you write into your sketch:

    Code:
    // Open Serial1 at 1 mega baud and in tristate mode
    void Dynamixel_Serial1_Open()
    {
        Serial1.begin(1000000, SERIAL_8N1);
        
        serial1_enable_TX_tristate(true);
    }
    
    // Send the instruction packet to the Dynamixel servo and then switch the TX pin back to tristate
    void Dynamixel_Serial1_Write(byte* data, int count)
    {
        serial1_enable_TX_tristate(false);            
    
        for (int i=0; i<count; i++)
        { 
            Serial1.write(data[i]);
        }
    
        Serial1.flush(); // waits until the last data byte has been sent
        serial1_enable_TX_tristate(true);            
    }
    Isn't that super easy ?

    IMPORTANT:
    While sending data the TX pin is NOT anymore in tristate mode.
    The CPU sends data in normal mode (pin switched to digital output) and pulls the data line ACTIVELY up and down.
    This is required to allow high speed like 1 mega baud.

    The internal pull up is only in use while the Teensy is waiting for a servo response.

    ____________________________________________

    How do you check that tristate is working correctly ?
    Obvioulsy you need an oscilloscope to check that.

    Then you connect a 22k resistor from Tx to ground.
    The internal pull up resistor in the CPU is also 22k Ohm.
    So in tristate mode the TX pin will be at approx 3.3V / 2 = 1.7 V.

    With this code:

    Code:
    void loop()
    {
        byte data[] = { 0x00 };
        Dynamixel_Serial1_Write(data, 1);
        delay(1);
    }
    you will see on the oscilloscope:

    Click image for larger version. 

Name:	Tristate Rs485.png 
Views:	1075 
Size:	14.8 KB 
ID:	6496

    As you see there is a short delay between switching to normal mode and the Start bit
    and another delay between the stop bit and switching back to tristate mode.
    This delay is about 2 µs on Teensy 3.1 running at a CPU clock of 96 MHz.
    During this delay the TX line is HIGH.
    __________________________________________

    And how do you connect the Dynamixel ?

    The Dynamixel should be connected via a 100 Ohm resistor as a protection.
    In case that both (Teensy and Dynamixel) send at the same time it protects the output pins.
    But this will only happen if there is something wrong in your sketch.
    Normally it will never happen that both send at the same time.

    Apart from that normally the CPU pins survive shortcuts for a short time.

    Click image for larger version. 

Name:	DynamixelWithTeensy.png 
Views:	2655 
Size:	50.4 KB 
ID:	6497
    ________________________________________

    What can you do if it does not work ?

    1.) When you power up the AX12 servo the red LED at the bottom side must flash up once for one second.
    If it does not, check the 12 V supply.

    2.) The factory default of Dynamixel is 1 mega baud.
    If you have changed the baudrate of the servo and forgot it, then you have a problem.
    You will have to try all possible baud rates.

    3.) The factory default is servo ID = 1.
    If you have changed the servo ID and forgot it, that's no problem:
    Send a PING packet to the broadcast ID and the servo will respond with it's ID.
    Obviously you must connect only ONE servo at the same time.

    4.) Check the signal with an oscilloscope.

    5.) If you have doubts that the failure might be caused by a lack of your electronic / sketch programming skills,
    the bullet proof solution is to buy a USB2Dynamixel adapter and connect the servo directly to the PC.
    This allows to test the servo without Teensy.

    ________________________________________

    Delays

    After sending an instruction packet to the servo you will normally immediately read the returned Status packt.
    With the servo register "Return Delay Time" you can define the delay before the servo responds with it's status packet.
    The Teensy takes only approx 2 µs to switch back to tristate.
    Nevertheless I recommend to do your first tests with the register "Return Delay Time" at it's maximum value.
    Later, when all works correctly, you can fine tune this register (if you really need so much speed).

    ________________________________________

    Echoed data

    As the Tx and Rx pins are tied together you will first receive an echo of the instruction packet that you have sent.
    Behind that echo you will receive the response from the servo.
    This is very useful because you can prove in your code that all sent bytes are echoed correctly.
    If they are not, this is a severe error.
    This allows to detect for example a shortcut of the RS485 data line to ground.

    The Teensy uses a FIFO RX buffer of default = 64 byte for Serial1, Serial2 and Serial3.
    You can change these defaults in the files serial1.c, serial2.c and serial3.c

    Normally the Status packet is very short and the RX buffer is big enough to receive it entirely.
    But if you read ALL servo registers at once with the instruction READ DATA, the RX data will be = (Echo + Status + All servo registers) which may be longer than 64 bytes.
    If you run at 1 mega baud and do not start immediately to read the response you may lose data.
    You can either increase the RX buffer size or not read all registers at once.

    Elmü
    Last edited by Elmue; 03-11-2016 at 02:03 PM.

  2. #2
    Join Date
    Sep 2010
    Location
    ಠ_ಠ
    Posts
    2,317
    Images
    27
    Rep Power
    283

    Re: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    No. They do not use RS-485 at TTL levels. The AX-, XL-, and MX-T (3-pin) servos use a simple half-duplex UART at 0~5V and does not necessarily require external hardware if the pins are 5V-tolerant (ATmega-based arbotix does this; simply short TX and RX, then disable RX during TX operations). The RX-, EX-, MX-R (4-pin) servos use RS-485 and do absolutely require an RS-485 transceiver connected to the microcontroller's UART.
    Please pardon the pedantry... and the profanity... and the convoluted speech pattern...
    "You have failed me, Brain!"
    [git][mech][hack]
    gives free advice only on public threads

  3. #3

    Re: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    I think it's very misleading to say "RS485 at TTL level" -- RS485 is by definition differential signaling. The MX-T/AX Dynamixels use single-ended simplex TTL UART.

    Also, the claim that AX are "fine" with 3.3V levels is actually not strictly true. I have a bot where I drive AX-12 servos with a Teensy (four individually steered wheels on a rover.) I had a 47K pull-up to 5V on the bus while not being driven, which just kept it floating at 3.3V. However, when transmitting, some servos would miss the start byte at times. When I changed the pull-up to 10K (which makes the bus float at about 3.8V) those problems went away.

    Separately, the long wire runs of TTL busses have significant inductance, and can easily pick up EMI / spikes. Wiring them straight into a microcontroller serial bus is certainly possible, but is also a bit dangerous. You want to at least put some resistors in front -- 27 Ohms is better than nothing (that's what I do on the Onyx Fire boards.)
    The buffering of a 74CHT125 is super helpful from a robustness point of view, though, and using a DIP chip that can be replaced makes it even better. I'd rather replace a $0.50 buffer than a $19 Teensy board... Check: Walgreens Ad Preview and Target Ad Preview on Weekly Ads.

  4. Re: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    tician:
    I'm talking about the AX12 which uses RS-485 at TTL levels.
    Obvioulsy this does not apply to the 4 pin servos.



    jwatte:
    I cannot confirm your epxerience.
    I use various AX12 servos in several projects where they run in production every day since many years at 1 mega baud and without external pull up resistor and without external buffer chip.

    There is NO problem with the 3.3V signal for the AX12.
    Here it works without any problems.

    I suppose the reason for your problem is that you use another configuration.
    Do you use an open collector bus ?

    If you have read my article thoroughly you will have noticed that whenever the Teensy TRANSMITS data it's TX pin is NOT anymore in tristate mode. The Teensy pulls the data ACTIVELY to ground and +3.3V.

    It would be a big nonsense to pull the line up with a 47k resistor while the CPU is pulling the line ACTIVELY up and down.
    Also the Dynamixel pulls the line ACTIVELY up and down when it sends data.
    Such a high resistor would simply do nothing.

    I suppose in your project you have used a pure open collector bus.
    In this case it is completely correct that you collect spikes and that you need an external pull up.
    But you would be better to use a lower value than 47k if you want to run at 1 mega baud.
    Generally it is a very bad idea to use open collector buses with longer cables and high frequencies.
    I don't use open collector buses at all.

    All my data is sent in "normal" mode where the CPU pulls the data line up and down actively.
    The internal pull up resistor is ONLY used to hold the data line up while NEITHER the Teensy NOR the Dynamixel are sending data. The internal pull up of 22k is turned off while the Teensy sends data because the TX pin is switched to normal output mode.


    Please read my article thoroughly.
    Last edited by Elmue; 03-11-2016 at 08:05 PM.

  5. #5
    Join Date
    Sep 2010
    Location
    ಠ_ಠ
    Posts
    2,317
    Images
    27
    Rep Power
    283

    Re: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    RS-485 is the electrical specification for the differential signaling interface used on RX-, EX, and MX-R series servos (4-pins). RS-485 is not the communication protocol (dynamixel). RS-485 is not the electrical specification for the interface of the microcontroller (UART). RS-485 is not used on any AX servos (3-pins) in any way. Neither is RS-232 (another electrical specification) used in any dynamixel servos. RS-232 was used on some older Robotis controllers (CM-5, CM-510) through an audio cable connector to communicate with the PC, but nowhere else.

    AX servos use ATmega88 microcontrollers that are powered off a 5V regulator and a 74HC126D (currently from NXP in my AX-12A) for interfacing to the data pin of the DXL buss. Even if the data pin is actively pulled up to 3.3V, this may not be sufficient for reliable communications at larger cable lengths or in noisy environments (SN74HC126 has 3.15V minimum voltage for input HIGH when powered from 4.5V).
    Please pardon the pedantry... and the profanity... and the convoluted speech pattern...
    "You have failed me, Brain!"
    [git][mech][hack]
    gives free advice only on public threads

  6. #6

    Re: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    FYI - I have been using Teensy 3.1s for awhile now without external hardware, using the ability to put the USARTS into half duplex.
    You connect the TX pin to the AX BUSS and you use the direction flag to say if the pin is input or output.

    However as mentioned the signal is 3.3vs. I have not noticed issues with this, but that is not to say there is not an issue. That is why on a few of my new boards I put in some level shifting hardware to get this to 5 volts. One of these boards does not have a direction bit, but uses a different method, which I do plan on trying out soon. I added this to make it safe to use a Teensy LC instead, which is not 5v tolerant.

    My latest board I added a chip that I will use a direction pin to control input or output. I am actually in the process of building this board now (slowly). Hoping to use the stuff Paul (PJRC) built in to the Serial class (transmitterEnable), to automatically control when we are in input or output mode.

  7. Re: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    Hello Kurt

    You can use external chips and level shifting.
    But if you use a Teensy 3.1 nothing of that is required.

    A logical low is always 0 V which both chips will understand.
    A logical high is for MOSFET chips mostly at the half of the VDD voltage.

    5 / 2 = 2,5 V for 5V chips.
    3.3 / 2 = 1.7 V for 3.3 V chips.

    So the 5V chip will recognize 3.3V as high because it is clearly above 2.5 V.
    And the 3.3 V chip will obviously recognize 5V as high.

    With a 100 Ohm protection resistor you are done!
    But you are always free to make things more complicated than necessary.

    I have tied a lot of 3.3V chips with 5V chips and never had any problem.

  8. #8
    Join Date
    Sep 2010
    Location
    ಠ_ಠ
    Posts
    2,317
    Images
    27
    Rep Power
    283

    Re: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    Quote Originally Posted by Elmue View Post
    A logical low is always 0 V which both chips will understand.
    A logical high is for MOSFET chips mostly at the half of the VDD voltage.

    5 / 2 = 2,5 V for 5V chips.
    3.3 / 2 = 1.7 V for 3.3 V chips.

    So the 5V chip will recognize 3.3V as high because it is clearly above 2.5 V.
    And the 3.3 V chip will obviously recognize 5V as high.
    Having a single transition point from LOW to HIGH at exactly 0.5*VDD is not common or robust. For relative/proportional inputs, 0.3*VDD is the most common maximum voltage for input LOW and 0.7*VDD is the most common minimum voltage for input HIGH. Everything between these two voltages is indeterminate and the better inputs are designed to internally produce the last valid state while receiving indeterminate levels until the other threshold is breached (latching inputs). Some devices may be a bit more tolerant of crap inputs (0.4*VDD for LOW, 0.6*VDD for HIGH), some devices may have an IO_REF pin to set the input/output voltage level with an external source, some devices may be designed specifically for working with lower voltage microcontrollers/logic levels by using a fixed-voltage internal reference source (like gate drivers that support 3.3V logic inputs even when powered by 12V), but many more are not. 5V Neopixels being one device not much liking 3.3V inputs.
    Please pardon the pedantry... and the profanity... and the convoluted speech pattern...
    "You have failed me, Brain!"
    [git][mech][hack]
    gives free advice only on public threads

  9. #9

    Re: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    the AX12 which uses RS-485 at TTL levels
    I understood what you meant the first time, and I think that you are wrong in saying that. Repeating it doesn't make it less wrong.

    5 / 2 = 2,5 V for 5V chips.
    That's not what the data sheet says. 0.7 * 5.0V == 3.5V, which is higher than 3.3V.
    So, while it may "work fine" in "practical experience" for "certain cases," you are in fact operating outside the guaranteed-to-work range. You are of course free to ignore this information, just like you are free to ignore my report that, in fact, that matters for real in at least some cases.
    However, when you give advice that "everyone should do this" and "buffers are not needed," I believe you are advocating bad engineering practices, and I will suggest that people who actually care about robustness probably should be doing something more robust.

  10. #10

    Re: Rs485 halfduplex for Dynamixel with Teensy 3.1 without external Buffer chip

    As I mentioned in first reply, I have not confirmed any problems on the AXBUS associated with 3.3vs. HOWEVER: when I see ones like jwatte and tician say something is not a good idea, I take it to heart. And I have had issues with servos resetting their ID's to #1, however again not specifically with Teensy...

    I decided to change my boards, after I ran into the issue that Neopixels did not work with the 3.3v signal. Although you could get them to work, if the voltage you passed to the Neopixel was 3.3v. Also this was not the first time, I ran into this. Earlier I ran into problems using the earlier version of the Sparfun XBee adapter, with some Atmega based boards, where it simply passed through the TX pin without converting it up to 5V...

    So my newer boards will have some form of level shifting...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. YATB - Yet Another Teensy Board :D
    By KurtEck in forum Arbotix, Microcontrollers, Arduino
    Replies: 126
    Last Post: 12-08-2017, 09:29 AM
  2. PhantomX using a Teensy 3.1
    By KurtEck in forum Humanoids, Walkers & Crawlers
    Replies: 301
    Last Post: 11-17-2016, 02:08 AM
  3. RobotGeek I2C display with a Teensy -- not working
    By jwatte in forum Arbotix, Microcontrollers, Arduino
    Replies: 1
    Last Post: 02-19-2016, 09:19 PM
  4. Just For Fun Arduino Dynamixel RS485 using Max485 module
    By Jim Cheong in forum Arbotix, Microcontrollers, Arduino
    Replies: 0
    Last Post: 10-21-2015, 11:13 AM
  5. Dynamixel MX64T 3pin TTL and 4pin RS485 version
    By navidahmeduom in forum DYNAMIXEL & Robot Actuators
    Replies: 2
    Last Post: 05-27-2015, 06:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •