Getting an MC variable into a program variable

Suppose in a computer program I send
&hAA, 11, &hA5
to the jrk motor controller, which is supposed to read the feedback position, 0 to 4092. After doing that, where is the number, how do I get it into a variable of the computer program?

Hello. Your serial command is incorrect. If you are using the Pololu protocol (commands that start with 0xAA), then the command byte (the third byte) must have its most significant byte cleared.

Your program should look something like this, in pseudocode:

comPort.writeThreeBytes(0xAA, 11, 0x25);
bytes = comPort.readTwoBytesBlocking();
feedback = bytes[0] + 256*bytes[1];

In fact, we have example code that looks a lot like that in the jrk user’s guide in the “Using the Serial Interface” section. I recommend reading that entire section before proceeding.


Why is there a discrepancy between these three numbers:
jrk configuration utility “Manually set target”: 400 (for example)
jrk configuration utility “Scaled feedback”: … 434 (actually unscaled)
The program below: … 413

 Local hComm As Dword
 Local t As Single, receive$, position As Long

' open COM port
 hComm = FreeFile
 Comm Open "COM5" As #hComm

' tell jrk to furnish feedback position
 Comm Send #hComm, Chr$(&hAA, 11, &h25) 

' wait until it's done
 t = Timer + 1
  If Timer > t Then Print "Comm port timed out." : End
 Loop Until Comm(#hComm, RxQue) >= 2

' read feedback position
 Comm Recv #hComm, 2, receive$
 position = Asc(receive$,1) + Asc(receive$,2)*256
 Print position

The “Manually set target” box is just a box that you can use to set the target; it will not be affected by the Set Target command you are sending from your program. To see the actual Target, look in the upper-left corner of the jrk configuration utility. You can alse see Scaled Feedback in the same spot.

The Target and the Scaled Feedback are two different numbers. The Target comes from the input source (in this case, your program), while the Feedback is a measurement of the current position of your system. The main point of the jrk’s PID algorithm is that it will try to drive the motor to make the Scaled Feedback equal to the Target, but whether it can actually do that and the accuracy that it achieves are determined by a lot of factors, such as:

  • the physical setup of your system
  • the PID parameters you chose, and how will you tuned them.

With properly tuned PID constants I can usually get the Target and PID constants within 10 or 15 of eachother. Please see the “Setting Up Your System” section of the jrk user’s guide.