I’m using a Mega with two maxon motors each equipped with an encoder (HEDL 5540 500 CPT, 3 Channels, with Line Driver RS 422). I am connecting the A and B signals from encoder 1 to pins 21 and 22 respectively (pin 21 is interrupt pin 2). Similarly I connect the A and B signals from encoder 2 to pins 20 and 23 (where pin 20 is interrupt pin 3).
I doubt this matters but I am using several other sensors connected to analog inputs and an SPI sensor as well. I’m also printing a string to serial so that I can interface with a Python program running in parallel which parses the serial string and then sends back a string to the arduino with updated variables.
I am using the Encoder.h library to handle the interrupt and count pulses. When I spin the motor slowly by hand through many revolutions the encoder works great and is very accurate. However, when I drive the motor (spinning at ~1000 rpm or higher) the motor count loses counts. I am able to verify this by twisting a string. I start with 0 twists in a string fixed to the motor shaft on one side and a plate on the other side. I spin the motor to say 50 revs and then unwind back to 0 revs. The encoder thinks it is at 0 revs but there are twists remaining in the string meaning it is missing counts somewhere. The mechanical setup of the string is accurate and I am sure nothing funny mechanically is happening. I have spent several weeks trying to figure out this issue. Is the Mega not able to process that many interrupts when the motor starts spinning fast?
I’ve attached some of my setup code below if that might help. Thank you in advance!!!
#include <Encoder.h> #include <SPI.h> // Runtime Options bool DEBUGGER = true; // if true python commands are NOT sent // Define Pin out #define encoder1PinA 21 #define encoder1PinB 22 // 22 #define encoder2PinA 20 #define encoder2PinB 23 // 23 #define motor1_A 2 // CW + #define motor1_B 3 #define motor2_A 5 // CCW + #define motor2_B 6 // #define magEncPin 53 #define pot1Pin 54 // analog pin 0 #define pot2Pin 55 // analog pin 1 #define fsr1Pin 56 // anolog pin 2 #define fsr2Pin 57 // analog pin 3 int CS_PIN = magEncPin; // Options and Parameters #define POT_MIN_VAL 10 #define POT_MAX_VAL 1015 #define BAUD_RATE 115200 #define TIMEOUT 0 // timeout is for Serial.readString() timeout. If its large the control loop becomes very slow #define pauseTime 1 // (us microseconds) change this to increase feedback frequency on AMS5047 encoder #define PID_DELAY 500 // motor PD controller delay time in microseconds #define CPR 2000.0 // counts per turn for maxon motor encoders #define FSR_CALIBRATION_CONSTANT 2.0 //1.81 #define MAX_FSR_FORCE 100 // max fsr force in N #define MAX_MAG_ENC_VAL 16383 //2^14 - 1 #define MAX_NUM_TWISTS 150.0 // 144 (by hand), (115 @12V) #define MAX_PRELOAD 60.0 #define Ks 10 // stiffness gain of twisted string actuator. stiffness of twisted string can be varied/tuned using potentiometer from: -(MAX_NUM_TWISTS)/(2*Ks) < 0 < (MAX_NUM_TWISTS)/(2*Ks). // ...corresponds to the number of twists offset the actuator is from an ideal TSA where the antagonistic strings are perfectly inversed in length // Motor PD Gains #define kp 60.0 // at 12 V power supply to motor driver (40) #define kd -.10 int POT_DEADZONE = 1; // kp/2; float PRELOAD = 3.0; // number of twists of preload // define motor encoder classes Encoder enc1(encoder1PinA, encoder1PinB); Encoder enc2(encoder2PinA, encoder2PinB);