Code conversion

This may be a simple problem, but I have limited programming experience so please keep responses kind.

I have some Dynamixels and up to this point I have been playing with them using a laptop running PUREBASIC connected via a U2D2. I have defined over 30 command strings that I now want to implement on the R4. The PUREBASIC program reads an array and writes the output to the serial port connected to the motors.

I am now trying to duplicate the functions using an UNO R4 WiFi and attached Dynamixel-shield. Some like:

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

PrintN ("LEDOn")
WriteSerialPortData(0, ?LEDOn, ?LEDOnEnd - ?LEDOn)

would be easy to duplicate 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 (I should have kept notes) and implement (I’m almost 70 and I don’t know if I would last that long) using the available libraries. I used the LEDOn, and similar LEDOff strings for testing and have tried as many things as I could find reference to, but none have worked.

I am using an MX-28, UNO R4 WiFi and Dynamixel shield.

Does anyone have a suggestion on how to send the command strings to the Dynamixels without using the libraries?

Thank you in advance for any assistance you can provide.

I think the first step is to describe all the acronyms that are likely new to the forum, like Dynamixels U2D2 etc.
Then a high level problem statement. Let's see how that goes OR maybe there is another helper who knows your system.

I do not know your system, but it looks like you are looking for sending bytes over Serial connection somewhere.

I would try something like this:

uint8_t LEDOn[] = { 0xFF, 0xFF, 0x05, 0x04, 0x03, 0x19, 0x01, 0xDD };
for (uint8_t i=0; i < sizeof(LEDOn)/sizeof(LEDOn[0]); i++) {
  Serial.write(LEDOn[i]);
}
sizeof(LEDOn)/sizeof(LEDOn[0]) is lenght of LEDOn array (and is calculated by compiler once when compiling)

Check your Basic program to see how to compute the checksum for the message. That will also have to be done on your Arduino.

1 Like

Untested code:


uint8_t LEDOn[] = { 0xFF, 0xFF, 0x05, 0x04, 0x03, 0x19, 0x01, 0xDD };
uint8_t Swish1[] = {
  0xFF, 0xFF, 0xFE, 0x22, 0x83, 0x1E, 0x04, 0x02, 0x31, 0x04, 0x50, 0x00,
  0x03, 0x0C, 0x06, 0x50, 0x00,
  0x04, 0x0C, 0x06, 0x50, 0x00,
  0x0A, 0x0C, 0x0A, 0x50, 0x00,
  0x0B, 0x0C, 0x06, 0x50, 0x00,
  0x0C, 0x0C, 0x06, 0x50, 0x00, 0x9D
};

void setup() {
  Serial.begin(115200);      //for serial monitor
  Serial1.begin(115200);      //for motors; use whatever baud rate you used before, we don't know what it is
  Serial.println("LED ON");
  sendit(LEDOn, sizeof(LEDOn));   //sizeof simplified in this context is acceptable, as we have declared the buffer as uint8_t
  delay(2000);
  Serial.println("Swish 1");
  sendit(Swish1, sizeof(Swish1)); //sizeof simplified in this context is acceptable, as we have declared the buffer as uint8_t
}
void loop() {
  //presumably, you'll be doing some stuff here
}

void sendit(uint8_t * buf, uint16_t buflen) {
  for (uint16_t i = 0; i < buflen; i++)     Serial1.write(buf[i]);
}

I formatted the Swish1 array somewhat strangely mostly to show you what you can do with care in C.
Hope that helps. As I see Paul has pointed out, there's more to it, but there's no reason computing the checksum couldn't be done in sendit().

what is the interface to the Dynamixel, Wifi, Serial, ??

how do you intend to control your code on the Arduino(?) via the serial/wifi interface?

without which libraries? WiFi?

The for loop is not necessary, write() will work with a uint8_t array directly:

  uint8_t LEDOn[] = { 0xFF, 0xFF, 0x05, 0x04, 0x03, 0x19, 0x01, 0xDD };
  Serial.write(LEDOn, sizeof(LEDOn) / sizeof(LEDOn[0]));
1 Like

Except that, if a checksum needs to be created and added, the data will need iterating anyway. Meh.

There has been some previous discussion on how handy BASIC's "DATA" and "READ" statements can be in situations like this, and how it might be possible to implement something more similar in Arduino's C/C++ language. (It'd be particularly useful when porting code from things like the BASIC Stamp that was historically used for many of the prop-like devices that are now turning to Arduino.)

But no one has ever implemented such a thing (I guess largely because using a bare C-style array is not THAT much more difficult?)