Advertisement
microrobotics

Pololu Romi Encoder Pair Kit

Apr 24th, 2023
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu Romi Encoder Pair Kit is designed to measure the rotation speed and direction of the motors on the Romi Chassis. The encoders consist of two parts: a magnetic disc with 12 equally spaced magnetic poles and a magnetic encoder sensor. To read the encoder values, you can use an Arduino and interrupts.
  3.  
  4. Here's an example code to read the encoder values from a Pololu Romi Encoder Pair Kit:
  5.  
  6. This code is for an Arduino Mega. If you are using an Arduino Uno or similar boards, you'll need to use pins 2 and 3 for both encoders, as they have only two interrupt pins. In that case, you will need to modify the interrupt service routine to handle both encoders.
  7.  
  8. Before using this code, make sure to connect the encoder output pins to the appropriate interrupt pins on your Arduino board. Also, connect the ground and Vcc pins of the encoders to the ground and appropriate voltage supply on your Arduino board.
  9.  
  10. Upload the code to your Arduino, and open the Serial Monitor. The encoder counts for both the left and right motors will be displayed. These counts can be used to calculate the rotation speed and direction of the motors, and can be helpful in applications like odometry, speed control, and position control.
  11. */
  12.  
  13. // Encoder pins connected to the Arduino interrupt pins
  14. const int encoderPinA1 = 2; // Left motor encoder A pin
  15. const int encoderPinB1 = 3; // Left motor encoder B pin
  16. const int encoderPinA2 = 18; // Right motor encoder A pin (Interrupt pin on Arduino Mega)
  17. const int encoderPinB2 = 19; // Right motor encoder B pin (Interrupt pin on Arduino Mega)
  18.  
  19. // Encoder counts
  20. volatile long leftEncoderCount = 0;
  21. volatile long rightEncoderCount = 0;
  22.  
  23. // Interrupt service routines
  24. void leftEncoderISR() {
  25.   if (digitalRead(encoderPinA1) == digitalRead(encoderPinB1)) {
  26.     leftEncoderCount++;
  27.   } else {
  28.     leftEncoderCount--;
  29.   }
  30. }
  31.  
  32. void rightEncoderISR() {
  33.   if (digitalRead(encoderPinA2) == digitalRead(encoderPinB2)) {
  34.     rightEncoderCount--;
  35.   } else {
  36.     rightEncoderCount++;
  37.   }
  38. }
  39.  
  40. void setup() {
  41.   Serial.begin(9600);
  42.  
  43.   // Set encoder pins as inputs
  44.   pinMode(encoderPinA1, INPUT);
  45.   pinMode(encoderPinB1, INPUT);
  46.   pinMode(encoderPinA2, INPUT);
  47.   pinMode(encoderPinB2, INPUT);
  48.  
  49.   // Enable pull-up resistors
  50.   digitalWrite(encoderPinA1, HIGH);
  51.   digitalWrite(encoderPinB1, HIGH);
  52.   digitalWrite(encoderPinA2, HIGH);
  53.   digitalWrite(encoderPinB2, HIGH);
  54.  
  55.   // Attach interrupts
  56.   attachInterrupt(digitalPinToInterrupt(encoderPinA1), leftEncoderISR, CHANGE);
  57.   attachInterrupt(digitalPinToInterrupt(encoderPinA2), rightEncoderISR, CHANGE);
  58. }
  59.  
  60. void loop() {
  61.   // Print the encoder counts to the serial monitor
  62.   Serial.print("Left Encoder: ");
  63.   Serial.print(leftEncoderCount);
  64.   Serial.print("\tRight Encoder: ");
  65.   Serial.println(rightEncoderCount);
  66.  
  67.   delay(500);
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement