Good day! I'm currently working on a project which involves writing to and reading from a Dynamixel XL-430W-250T via Python. My current setup involves a Dynamixel Motor Shield which I've connected to my PC via an Arduino Uno and USB 2.0. I've supplemented the connection with an LN-101 Serial USB Downloader, as Dynamixels notoriously clog the serial ports of the USB 2.0, which results in garbage being output to the serial monitor.
My python script encodes a string and writes it to the Arduino via the LN-101, which it then reads and writes back to the computer so as to "verify" the command. The arduino script utilizes the Dynamixel Shield library.
Python script:
import os
import time
import re
import serial
import sys
import msvcrt
USB2 = serial.Serial(port = 'COM12', timeout=None)
USB2.baudrate = 115200
USB2.bytesize = serial.EIGHTBITS
USB2.parity = serial.PARITY_NONE
USB2.stopbits = serial.STOPBITS_ONE
LN101 = serial.Serial(port = 'COM32', timeout=None)
LN101.baudrate = 115200
LN101.bytesize = serial.EIGHTBITS
LN101.parity = serial.PARITY_NONE
LN101.stopbits = serial.STOPBITS_ONE
while check == False:
toard = ("{}\0".format(string))
LN101.reset_output_buffer()
LN101.write(str.encode(toard))
toard = toard.rstrip("\0")
time.sleep(2)
if LN101.in_waiting > 0:
comcheck = str(LN101.readline().decode('utf-8'))
comcheck = comcheck.rstrip("\n")
Arduino code:
#include <DynamixelShield.h>
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_MEGA2560)
#include <SoftwareSerial.h>
SoftwareSerial soft_serial(7, 8); // DYNAMIXELShield UART RX/TX
#define DEBUG_SERIAL soft_serial
#elif defined(ARDUINO_SAM_DUE) || defined(ARDUINO_SAM_ZERO)
#define DEBUG_SERIAL SerialUSB
#else
#define DEBUG_SERIAL Serial
#endif
DynamixelShield dxl;
const uint8_t DXL_ID = 1;
const float DXL_PROTOCOL_VERSION = 2.0, dtime = 100;
const int maxVelval = 330;
unsigned long tstart;
int vel, goalVelval, sep1, sep2, c=0;
long int pos_i, pos_c, pos_f, desrotval, zero;
float pos, dpos, tpos, pos_i_rel, veldegmsec, delaymsec;
String Sdir, Svel, Spos, Spos2, Fpos, SZero, toard, fromard, comcheck, check, btoard, tora;
using namespace ControlTableItem;
void setup() {
DEBUG_SERIAL.begin(115200);
Serial.begin(115200);
dxl.begin(57600);
dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION);
dxl.ping(DXL_ID);
dxl.torqueOff(DXL_ID);
dxl.setOperatingMode(DXL_ID, OP_EXTENDED_POSITION);
dxl.torqueOn(DXL_ID);
}
void loop() {
comcheck = "False";
tora = "Recieved\n";
fromard = "";
toard = "";
while (comcheck == "False") {
while (DEBUG_SERIAL.available() == 0) {}
toard = DEBUG_SERIAL.readStringUntil('\0');
toard.trim(); //
btoard = toard;
fromard = toard += "\n";
DEBUG_SERIAL.write(fromard.c_str());
delay(100);
}
The code works just fine when I'm utilizing an Uno; however, if I try to switch the board to a Mega, the serial communication over the LN-101 doesn't seem to work (from what I can tell, the arduino never reads the initial command written by the computer).
Troubleshooting thus far:
- The baudrate is the same for both the uno and mega
- Everything is mounted correctly on the Arduino
- The board, LN-101 and USB 2.0 seem to be working just fine
- The arduino seems to write to the com port just fine; it just never receives anything from python for whatever reason
Could I please get some help in resolving this?