Serial.write truncating output

I have some Dynamixels and up to this point I have been using a laptop running PUREBASIC to experiment with them and defined over 30 command strings. I am now trying to duplicate the functions using an R4. Some like:

LEDOn:
Data.a $FF, $FF, $05, $04, $03, $19, $01, $D9

would be easy to program using the shield libraries, However some like:

Swish1:
Data.a $FF, $FF, $FE, $22, $83, $1E, $04, $02, $31, $04, $50, $00, $03, $0C, $06, $50, $00, $04, $0C, $06, $50, $00, $0A, $0C, $0A, $50, $00, $0B, $0C, $06, $50, $00, $0C, $0C, $06, $50, $00, $9D

would be very time consuming to decode and implement (I’m almost 70 and I don’t know if I would last that long).

The PUREBASIC program reads the array and writes the output to the serial port connected to the motors.

I tried to convert the LEDOn function but I couldn’t get it to work.

#include <DynamixelShield.h>

#define DEBUG_SERIAL Serial
// Dynamixel is Serial1

const uint8_t DXL_ID = 1;
const float DXL_PROTOCOL_VERSION = 1.0;

DynamixelShield dxl;

byte LEDOn[] = { 0xff, 0x80, 0x7f, 0x04, 0x03, 0x19, 0x01, 0xD9 };

int x;

using namespace ControlTableItem;

void setup() {

DEBUG_SERIAL.begin(9600);
dxl.begin(57600);
dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION);
}

void loop() {

DEBUG_SERIAL.println("LEDOn");

for (byte i = 0; i < 8; i = i + 1) {

DEBUG_SERIAL.print("print i = ");
DEBUG_SERIAL.println(i);

x = LEDOn[i];
DEBUG_SERIAL.print("print x = ");
DEBUG_SERIAL.println(x);

DEBUG_SERIAL.println(x, BIN);
 DEBUG_SERIAL.println();
DEBUG_SERIAL.println();

}

delay(5000000);
}

The issue, I think, is that the output of any value lower than 0x80 is truncated to significant digits. The LEDOn string in this example was modified to show this behavior. Here is the output:

print i = 0
print x = 255
11111111 <--- 8 bits

print i = 1
print x = 128
10000000 <--- 8 bits

print i = 2
print x = 127
1111111 <--- 7 bits

A couple of questions:

Is what I am seeing on line 3 of each output actually was is being written to the port?

If so is there a “reasonable” solution?

Thanks for any guidance.

Update to try to post sketch correctly.

#include <DynamixelShield.h>

#define DEBUG_SERIAL Serial
// Dynamixel is Serial1

const uint8_t DXL_ID = 1;
const float DXL_PROTOCOL_VERSION = 1.0;

DynamixelShield dxl;

byte LEDOn[] = { 0xff, 0x80, 0x7f, 0x04, 0x03, 0x19, 0x01, 0xD9 };

int x;

using namespace ControlTableItem;

void setup() {

DEBUG_SERIAL.begin(9600);
dxl.begin(57600);
dxl.setPortProtocolVersion(DXL_PROTOCOL_VERSION);
}

void loop() {

DEBUG_SERIAL.println("LEDOn");

for (byte i = 0; i < 8; i = i + 1) {

DEBUG_SERIAL.print("print i = ");
DEBUG_SERIAL.println(i);

x = LEDOn[i];
DEBUG_SERIAL.print("print x = ");
DEBUG_SERIAL.println(x);

DEBUG_SERIAL.println(x, BIN);
DEBUG_SERIAL.println();
DEBUG_SERIAL.println();

}

delay(5000000);
}

It is a known behavior that when printing a number via

DEBUG_SERIAL.println(x, BIN);

then all leading zeroes are not printed. Hence 1111111 is the expected output for the number 0x7f / 127.

The solution is to print all bits manually:

1 Like

I am not sure whether you are just testing the code logic at the moment, but there seem to be no statements actually sending bytes to the Dynamixel port as yet?

BitSeeker,

Thank you for the response. Yes, I had skinnied the sketch down to only what I thought would show me what was going on. In the full sketch I had:

Serial1.write(LEDOn[i]); within the loop. When it didn't work i stripped it out for testing.