Advertisement
microrobotics

Pololu JRK 12V12 motor controller

Apr 21st, 2023
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. To control a motor using the Pololu JRK 12V12 motor controller, you can use the following code as a starting point:
  3.  
  4. This code uses the SoftwareSerial library to communicate with the JRK 12V12 motor controller over a serial connection. The setTargetSpeed() and setAcceleration() functions send the appropriate command bytes to the controller to set the desired speed and acceleration values, while the getCurrentSpeed() and getCurrentPosition() functions read the current speed and position values from the controller.
  5.  
  6. To use this code, you will need to connect the JRK 12V12 motor controller to your Arduino as follows:
  7.  
  8. 1. Connect the TX pin on the JRK 12V12 motor controller to the RX pin (pin 10) on the Arduino.
  9. 2. Connect the RX pin on the JRK 12V12 motor controller to the TX pin (pin 11) on the Arduino.
  10. 3. Connect the GND pin on the JRK 12V12 motor controller to the GND pin on the Arduino.
  11. 4. Connect the motor power supply to the VIN and GND pins on the JRK 12V12 motor controller.
  12.  
  13. Connect the motor to the OUTA and OUTB pins on the JRK 12V12 motor controller.
  14. Note that you will need to adjust the values passed to the setTargetSpeed() and setAcceleration() functions to suit your specific motor and application. The speed value should be in the range of 0 to 4000 (where 0 is full stop and 4000 is full speed), while the acceleration value should be in the range of 0 to 32000 (where 0 is no acceleration and 32000 is maximum acceleration).
  15.  
  16. Also, keep in mind that the JRK 12V12 motor controller provides a variety of other features and settings that can be configured through the serial interface, such as feedback control, PID tuning, input mapping, and more. Please refer to the JRK 12V12 user's guide and the Pololu website for more information and examples.
  17. */
  18.  
  19. #include <SoftwareSerial.h>
  20.  
  21. SoftwareSerial jrkSerial(10, 11); // RX, TX pins
  22.  
  23. void setup() {
  24.   Serial.begin(9600);
  25.   jrkSerial.begin(9600);
  26. }
  27.  
  28. void loop() {
  29.   // Set the target speed and acceleration/deceleration values
  30.   setTargetSpeed(200); // Set the speed to 200 (0 to 4000 range)
  31.   setAcceleration(200); // Set the acceleration to 200 (0 to 32000 range)
  32.  
  33.   // Read the current speed and position values
  34.   int currentSpeed = getCurrentSpeed();
  35.   int currentPosition = getCurrentPosition();
  36.  
  37.   // Print the values to the serial monitor
  38.   Serial.print("Speed: ");
  39.   Serial.print(currentSpeed);
  40.   Serial.print(" | Position: ");
  41.   Serial.println(currentPosition);
  42.  
  43.   delay(500);
  44. }
  45.  
  46. void setTargetSpeed(int speed) {
  47.   // Set the target speed value (0 to 4000 range)
  48.   jrkSerial.write(0xAA); // Device Command byte
  49.   jrkSerial.write(0xC0); // Set Target command byte
  50.   jrkSerial.write(speed & 0x1F); // Low 5 bits of speed
  51.   jrkSerial.write(speed >> 5 & 0x7F); // High 7 bits of speed
  52. }
  53.  
  54. void setAcceleration(int acceleration) {
  55.   // Set the acceleration value (0 to 32000 range)
  56.   jrkSerial.write(0xAA); // Device Command byte
  57.   jrkSerial.write(0xE0); // Set Acceleration command byte
  58.   jrkSerial.write(acceleration & 0x7F); // Low 7 bits of acceleration
  59.   jrkSerial.write(acceleration >> 7 & 0x7F); // High 7 bits of acceleration
  60. }
  61.  
  62. int getCurrentSpeed() {
  63.   // Read the current speed value (0 to 4000 range)
  64.   jrkSerial.write(0xAA); // Device Command byte
  65.   jrkSerial.write(0xA1); // Get Target/Speed command byte
  66.   delay(10);
  67.   int response = jrkSerial.read();
  68.   int speed = jrkSerial.read() + (response & 0x1F) * 256;
  69.   return speed;
  70. }
  71.  
  72. int getCurrentPosition() {
  73.   // Read the current position value (0 to 4095 range)
  74.   jrkSerial.write(0xAA); // Device Command byte
  75.   jrkSerial.write(0xA2); // Get Position command byte
  76.   delay(10);
  77.   int response = jrkSerial.read();
  78.   int position = jrkSerial.read() + (response & 0x0F) * 256;
  79.   return position;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement