Advertisement
microrobotics

Pololu Jrk 21v3 USB Motor Controller with Feedback

Apr 14th, 2023
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu Jrk 21v3 USB Motor Controller with Feedback is a versatile motor controller that supports various communication protocols like TTL serial, I2C, and USB. In this example, I'll provide you with an Arduino code to control a motor using the TTL serial interface. Note that this code assumes you are using an Arduino Uno or a similar board.
  3.  
  4. First, connect the motor controller to the Arduino as follows:
  5.  
  6. TX (Arduino) to RX (Motor Controller)
  7. RX (Arduino) to TX (Motor Controller)
  8. GND (Arduino) to GND (Motor Controller)
  9. Connect the motor to the motor controller's output and supply power to the motor controller as per your motor's requirements. Additionally, connect the feedback sensor (e.g., a potentiometer) to the motor controller's feedback input.
  10.  
  11. Upload this code to your Arduino. The motor will move to the position 0 for 2 seconds, then move to the position 4095 for 2 seconds. This process will repeat indefinitely. You can change the target positions as needed within the range of 0 to 4095.
  12. */
  13.  
  14. // Pololu Jrk 21v3 USB Motor Controller with Feedback
  15. // Example code for Arduino
  16.  
  17. #include <SoftwareSerial.h>
  18.  
  19. // Motor Controller Pins
  20. const int rxPin = 10; // Connect the RX pin of the motor controller to Digital Pin 10
  21. const int txPin = 11; // Connect the TX pin of the motor controller to Digital Pin 11
  22.  
  23. // Create a SoftwareSerial object for communication with the motor controller
  24. SoftwareSerial jrkSerial(rxPin, txPin);
  25.  
  26. void setup() {
  27.   Serial.begin(9600); // Initialize the serial communication for debugging
  28.   jrkSerial.begin(9600); // Initialize the software serial communication with the motor controller
  29. }
  30.  
  31. void loop() {
  32.   int target = 0; // Set target position (0-4095)
  33.   setMotorTarget(target);
  34.   delay(2000); // Wait for 2 seconds
  35.  
  36.   target = 4095; // Set target position (0-4095)
  37.   setMotorTarget(target);
  38.   delay(2000); // Wait for 2 seconds
  39. }
  40.  
  41. void setMotorTarget(int target) {
  42.   // Ensure target is within allowed range
  43.   if (target < 0) {
  44.     target = 0;
  45.   } else if (target > 4095) {
  46.     target = 4095;
  47.   }
  48.  
  49.   // Send "Set Target" command (0xA3) followed by two data bytes (target split into two bytes)
  50.   jrkSerial.write(0xA3);
  51.   jrkSerial.write(target & 0x1F);
  52.   jrkSerial.write((target >> 5) & 0x7F);
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement