Go Down

Topic: Example sketch for serial communication with jrk21v3 motor controller (Read 1 time) previous topic - next topic

solhelpr

I wasted a lot of time trying to figure out arduino serial communication with this pololu jrk21v3 and i finally got it working so I figured I'd post a rough sketch on here so that people in the future can use it as reference.  

Code: [Select]
/*
* Motor control setup for pololu jrk21v3 with Arduino UNO R3, verified using linear actualtor LACT2P
*
* Pololu jrk config utility in Serial mode using UART detect baud rate interface.
* starting with the default configuration settings for LACT2P linear actuators provided on the pololu website
*
* pin 8 connected to jrk pin Rx
* jrk grnd connected to arduino ground
*/


#include <SoftwareSerial.h>
SoftwareSerial mySerial(7,8); // RX, TX, plug your control line into pin 8 and connect it to the RX pin on the JRK21v3

int myTarget = 0; // target position, 0-4095 is the range of the JRK21V3 controller.

//stuff used for input from pc
char buffer[5] ;
int pointer = 0;
byte inByte = 0;



// announcer for PC Serial output
void announcePos(int (position)) {
  Serial.print("positiion set to ");
  Serial.println(position);
  Serial.flush();
}




//sets the new target for the JRK21V3 controller, this uses pololu high resulution protocal
void Move(int x) {
  word target = x;  //only pass this ints, i tried doing math in this and the remainder error screwed something up
  mySerial.write(0xAA); //tells the controller we're starting to send it commands
  mySerial.write(0xB);   //This is the pololu device # you're connected too that is found in the config utility(converted to hex). I'm using #11 in this example
  mySerial.write(0x40 + (target & 0x1F)); //first half of the target, see the pololu jrk manual for more specifics
  mySerial.write((target >> 5) & 0x7F);   //second half of the target, " " "





void setup()
{
  mySerial.begin(9600);
  Serial.begin(9600);
  Serial.println("Initialized");
  Serial.flush();// Give reader a chance to see the output.

  int myTarget = 0; //the health level at any point in time
  Serial.println("Enter '#' and 4 digit position level (#0000-#4095)");

}

   

void loop()
{

  if (Serial.available() >0) {
   // read the incoming byte:
   
   inByte = Serial.read();
   delay(10);
   
   // If the marker's found, next 4 characters are the position
   if (inByte == '#') {
   

     while (pointer < 4) { // accumulate 4 chars
        buffer[pointer] = Serial.read(); // store in the buffer
        pointer++; // move the pointer forward by 1
      }
      Serial.flush();
      //translating into an int
      myTarget=(buffer[0]-48)*1000+(buffer[1]-48)*100+(buffer[2]-48)*10+(buffer[3]-48);
      pointer =0;
   }
   
   
   //makes sure the target is within the bounds
   if (myTarget < 0){
      myTarget = 0;
      }
   else if (myTarget > 4095){
      myTarget=4095;
      }
     
   Move(myTarget); 
   announcePos(myTarget);


  }
}

markvonrose

This is a perfect example for this and it works perfectly. Thank you. A couple things to add that I messed up (as I'm new to this) that maybe could help others:
1. Make sure you set the SoftwareSerial TX and Rx values correct. Triple check. On the Uno the TX pin number is greater than the RX pin number, but on Megas the TX pin number is less than RX. Rookie mistake, but I overlooked this at first when I was just switching out pin numbers for what was on my board.
2. The TX pin should go the RX pin on the motor controller, and the RX pin should attach to the TX pin.
3. Make sure the Serial Interface in the JRK configuration utility is set to UART with a fixed baud rate that matches what is in your sketch.

I banged my head on the wall for 30minutes having messed each of these up. Thanks again to solhelpr.


Pototo_5

Hello

I'm currently working with the JRK21V3, and a Concentric LACT6P-12V-20 Linear Actuator with Feedback: 6" Stroke, 12V, 0.5"/s. I looked at your code and I thought it was going to work but when I upload the code to the Arduino nothing happens.  My connections are as follows I have the Arduino UNO Pin 8  connected to the RX pin of the JRK21V3 and Pin GND of the Arduino connected to the JRK21V3 GND, as instructed on the code. Then I followed the connections of the JRK21V3 and the   LACT6P based on Pololu's web cite link(http://www.pololu.com/picture/view/0J3409). Now when I run the code I open the Serial Monitor and enter #1234 or any number between 0-4095 and the  LACT6P doesn't move at all.  I was successfully able to move the  LACT6P using the USB method, and unsuccessful, using the analog method and Arduino method. The analog method is using a potentiometer connected to the  JRK21V3 RX, 5V(out), and GND pins.

Please Help is needed
Thanks

solhelpr

I don't see anything wrong with what you've posted, I could take some guesses I suppose.

Have you got sufficient power hooked up to the large blue connector (I used a 12v 3Amp wall-wort power supply, don't use the arduino 5v pin it is way too weak) ? Have you verified your configuration is set to UART detect baud rate? have you got your arduino software set to 9600 baud rate when you connect? Have you tried running your setup while the jrk configuration utility is running and are you sure you have the correct device# selected? Have you tried clearing all the errors that pop up in the config utility?

Pototo_5

OK I got it to work  :D :D :D :D :D :smiley-mr-green: :smiley-mr-green: :smiley-mr-green: :smiley-mr-green: :smiley-mr-green: XD XD XD XD XD XD XD XD XD

So my problem was that I didn't set in Pololu Jrk Configuration Utility, Serial interface to "UART, fixed baud rate to 9600", I had it set to "USB Dual Port"

Thank you so much for your help.

Here is how to fix this issue I had
Step 1: First you have to download the  Pololu Jrk Configuration Utility here is the link: http://www.pololu.com/docs/0J38/3.a

Step 2: Connect your Jrk 21v3 USB Motor Controller using the USB set up : (http://www.pololu.com/picture/view/0J2080)

Step 3: Once the  Pololu Jrk Configuration Utility recognizes your Jrk 21v3  you will have to do the following(http://www.pololu.com/picture/view/0J1729)

Step 4:  In the "Input" tab, under the "Serial Interface" group the default is set to "USB Dual Port", uncheck it and set it to "UART, fixed baud rate 9600". Then you click on the "Apply settings to device" bottom on the bottom right corner.

Step 5: Disconnect your USB, and connect the Arduino, run the program, and it will work  :)


Go Up