Getting a path and speed value from the encoder

hello everyone. i need to get location and speed data from the encoders using a 4 pololu motor, I ran this code that I found for a single motor, but since we use 4 motors, the L298N motor driver gets involved.how do I include the engine driver in this code.Also how can i run 4 motors with equal speed without pid.

// Motor encoder output pulses per 360 degree revolution (measured manually)
#define ENC_COUNT_REV 620
 
// Encoder output to Arduino Interrupt pin. Tracks the pulse count.
#define ENC_IN_RIGHT_A 2
 
// Other encoder output to Arduino to keep track of wheel direction
// Tracks the direction of rotation.
#define ENC_IN_RIGHT_B 4
 
// True = Forward; False = Reverse
boolean Direction_right = true;
 
// Keep track of the number of right wheel pulses
volatile long right_wheel_pulse_count = 0;
 
// One-second interval for measurements
int interval = 1000;
  
// Counters for milliseconds during interval
long previousMillis = 0;
long currentMillis = 0;
 
// Variable for RPM measuerment
float rpm_right = 0;
 
// Variable for angular velocity measurement
float ang_velocity_right = 0;
float ang_velocity_right_deg = 0;
 
const float rpm_to_radians = 0.10471975512;
const float rad_to_deg = 57.29578;
 
void setup() {
 
  // Open the serial port at 9600 bps
  Serial.begin(9600); 
 
  // Set pin states of the encoder
  pinMode(ENC_IN_RIGHT_A , INPUT_PULLUP);
  pinMode(ENC_IN_RIGHT_B , INPUT);
 
  // Every time the pin goes high, this is a pulse
  attachInterrupt(digitalPinToInterrupt(ENC_IN_RIGHT_A), right_wheel_pulse, RISING);
   
}
 
void loop() {
 
  // Record the time
  currentMillis = millis();
 
  // If one second has passed, print the number of pulses
  if (currentMillis - previousMillis > interval) {
 
    previousMillis = currentMillis;
 
    // Calculate revolutions per minute
    rpm_right = (float)(right_wheel_pulse_count * 60 / ENC_COUNT_REV);
    ang_velocity_right = rpm_right * rpm_to_radians;   
    ang_velocity_right_deg = ang_velocity_right * rad_to_deg;
     
    Serial.print(" Pulses: ");
    Serial.println(right_wheel_pulse_count);
    Serial.print(" Speed: ");
    Serial.print(rpm_right);
    Serial.println(" RPM");
    Serial.print(" Angular Velocity: ");
    Serial.print(rpm_right);
    Serial.print(" rad per second");
    Serial.print("\t");
    Serial.print(ang_velocity_right_deg);
    Serial.println(" deg per second");
    Serial.println();
 
    right_wheel_pulse_count = 0;
   
  }
}
 
// Increment the number of pulses by 1
void right_wheel_pulse() {
   
  // Read the value for the encoder for the right wheel
  int val = digitalRead(ENC_IN_RIGHT_B);
 
  if(val == LOW) {
    Direction_right = false; // Reverse
  }
  else {
    Direction_right = true; // Forward
  }
   
  if (Direction_right) {
    right_wheel_pulse_count++;
  }
  else {
    right_wheel_pulse_count--;
  }
}

motors used in the project:131:1 Metal Gearmotor 37Dx73L mm 24V with 64 CPR Encoder (Helical Pinion)
motor driver ; L298N
In the project, 2 arduino megas were used for 4 motors, 4 motors are controlled by master-slave connection.

Use some other control algorithm than PID.

can you make a suggestion please?

My suggestion would be to use PID. Please explain why don't you want to do that, given that it is simple and works well.

I do not agree that PID is really simple.

not a good idea to use a master-slave configuration as this complicates things more than nescessary. You will have to exchange encoder-data very fast to keep the motors in sync.

If you plan to drive the motors in opposite direction for turning / rotating this means the wheels are slipping and then any precision between number of encoder-pulses and physically moved distance is gone.

If you want best possible precision for determing position the wheels must avoid any slipping.
Write an overview about your project what you want to do all in all.
Depending on what that is different approaches might be better suited.

best regards Stefan

Then why not suggest to the OP a simpler procedure to control the motor speed?

A simpler way would be to use stepper-motors. But this would require to buy different components and more power-consumption.

Depending on what the overall / final purpose of this project is maybe using ultrasonic sensors or lidar is better suited. But as the TO has not posted such details it is unclear

Wheels driven by steppers do not slip?

Yes of course the wheels can slip too. Though my estimation is that with stepper-motors
it will be easier to avoid slipping because controlling the rpm is easier than with PID.

Using closed loop servo-motors might offer the best correction as a high sophisticated position-control realised inside the servo-driver would be easy to use because the control-details are done inside the servo-controller. But such systems cost a lot more.

Anyway the TO should post a description why knowing the position (at what precision) is nescessary

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.