Jrk motor controllers seem to be ignoring device id

Background:

I have two jrk21v3 firmware rev 1.3 connected up on a “shared” RX line coming out of an arduino. The TX lines from the jrk are not connected. One is set on device id “2”, the other “4”. They are using a pot for analog feedback and are responding properly over usb “manual” control. The PIDs are tuned and it in general seems happy. They are set to be hard coded at 9600baud.

Problem:
It seems that there is either something wrong with my code or a bug in the firmware… as both motor drives respond to commands regardless of the “device ID” I have them set to. Additionally, one of the devices “serial protocol error” bit is set, but it’s still responding to the commands.

Proof of concept:

I would expect this code to set one of the jrks to postition 100, and the other to position 1500. What’s really happening is that they both travel to position 100 and position 1500. Any ideas?

#include <SoftwareSerial.h>

#define rxPin 3
#define txPin 2

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte pinState = 0;

unsigned char focusbuff[6];
int focustarget = 0;

unsigned char zoombuff[6];
int zoomtarget = 0;


void setup() {
  delay(50);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);

}

void loop() {

  focustarget = 100;
  focusbuff[0] = 0xAA; 
  focusbuff[1] = 0x02;
  focusbuff[2] = 0xC0 + (focustarget & 0x1F);
  focusbuff[3] = (focustarget >> 5) & 0x7F;
  for (int i=0; i<4; i++) {
    mySerial.print(focusbuff[i],BYTE);
  }
  delay(3000);
  zoomtarget = 1500; 
  zoombuff[0] = 0xAA; 
  zoombuff[1] = 0x04;
  zoombuff[2] = 0xC0 + (zoomtarget & 0x1F);
  zoombuff[3] = (zoomtarget >> 5) & 0x7F;
  for (int i=0; i<4; i++) {
   // mySerial.print(zoombuff[i],BYTE);
  }
  delay (3000);
}

Hello.

Thank you for the clear description of your system and the problem you are experiencing. You forgot to clear the most significant bit of the command byte. If you change 0xC0 to 0x40 it should work.

–David

oh AWESOME. Thank you. I’ll give that a shot. Page 30 of this manual:

pololu.com/docs/pdf/0J38/jrk … roller.pdf

…Does not make this very clear.

Additionally, I’m not sure why an error in the command byte (when I’m using the pololu protocol) causes every device to respond. While I agree there was an error on my end, I don’t think the device was handling it the way I expect. Then again, it was reporting a protocol error… so meh.

Anyway, thanks again… this is very helpful!

The user’s guide specifies the two different protocols you can use to send a Set Target High Resolution command to the jrk:

Suppose your Arduino incorrectly sends 0xAA, 0x02, 0xC0 + 0x1C, 0x2E. After the jrk with id 2 receives the first two bytes, it expects to receive a command byte with the most-significant bit cleared. Then when it receives 0xC0 + 0x1C instead, it means that the command was interrupted, so that is a serial protocol error. However, the last two bytes are a valid compact protocol command, so both jrks detect that command and respond accordingly.

–David