// Pin tanımlamaları
const int motor1Pin1 = 28; // Motor 1, sürücü pini 1 (IN1)
const int motor1Pin2 = 29; // Motor 1, sürücü pini 2 (IN2)
const int motor1Pin3 = 2; // Motor 1, sürücü pini 3 (ENA)
const int motor2Pin1 = 30; // Motor 2, sürücü pini 1 (IN3)
const int motor2Pin2 = 31; // Motor 2, sürücü pini 2 (IN4)
const int motor2Pin3 = 3; // Motor 2, sürücü pini 3 (ENB)
// Encoder tanımlamaları
const int encoder1PinA = 22; // Motor 1, encoder pin A
const int encoder1PinB = 23; // Motor 1, encoder pin B
const int encoder2PinA = 24; // Motor 2, encoder pin A
const int encoder2PinB = 25; // Motor 2, encoder pin B
// PID parametreleri
const float Kp = 0.1; // P kontrol parametresi
const float Ki = 0.01; // I kontrol parametresi
const float Kd = 0.01; // D kontrol parametresi
// Hedef hızlar
const int targetSpeed1 = 150; // Motor 1 hedef hızı (PWM değeri)
const int targetSpeed2 = 150; // Motor 2 hedef hızı (PWM değeri)
// PID değişkenleri
float lastError1 = 0;
float integral1 = 0;
float lastError2 = 0;
float integral2 = 0;
void setup() {
// Pin modları ayarlama
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(encoder1PinA, INPUT_PULLUP);
pinMode(encoder1PinB, INPUT_PULLUP);
pinMode(encoder2PinA, INPUT_PULLUP);
pinMode(encoder2PinB, INPUT_PULLUP);
// Başlangıç değerleri
analogWrite(motor1Pin1, 0);
analogWrite(motor1Pin2, 0);
analogWrite(motor2Pin1, 0);
analogWrite(motor2Pin2, 0);
// Seri haberleşme başlatma
Serial.begin(9600);
}
void loop() {
// Hız ve yön hesaplamaları
int motor1Speed = calculateMotorSpeed(1, targetSpeed1);
int motor2Speed = calculateMotorSpeed(2, targetSpeed2);
// Motorları hareket ettir
moveMotor(1, motor1Speed);
moveMotor(2, motor2Speed);
// Hızları, darbe sayılarını ve açıları seri port üzerinden yazdır
printMotorData();
delay(100); // Hareket hızı için uygun bir gecikme süresi belirleyebilirsiniz
}
void moveMotor(int motorNum, int speed) {
int directionPin1, directionPin2;
if (speed > 0) {
directionPin1 = HIGH;
directionPin2 = LOW;
} else if (speed < 0) {
directionPin1 = LOW;
directionPin2 = HIGH;
speed = -speed;
} else {
directionPin1 = LOW;
directionPin2 = LOW;
}
if (motorNum == 1) {
digitalWrite(motor1Pin1, directionPin1);
digitalWrite(motor1Pin2, directionPin2);
analogWrite(motor1Pin3, speed);
} else if (motorNum == 2) {
digitalWrite(motor2Pin1, directionPin1);
digitalWrite(motor2Pin2, directionPin2);
analogWrite(motor2Pin3, speed);
}
}
int calculateMotorSpeed(int motorNum, int targetSpeed) {
// Encoder bilgilerini oku
int pulseCount = 0;
if (motorNum == 1) {
pulseCount = getMotorPulseCount(encoder1PinA, encoder1PinB);
} else if (motorNum == 2) {
pulseCount = getMotorPulseCount(encoder2PinA, encoder2PinB);
}
// Hız ve hata hesaplamaları
float speed = getMotorSpeed(pulseCount);
float error = targetSpeed - speed;
// PID kontrolü
float controlSignal = Kp * error + Ki * integral1 + Kd * (error - lastError1);
integral1 += error;
lastError1 = error;
// PWM değeri olarak dönüştür
int pwmValue = map(abs(controlSignal), 0, 255, 0, 255);
if (pwmValue > 255) {
pwmValue = 255;
}
// Hız ve yönü döndür
return controlSignal > 0 ? pwmValue : -pwmValue;
}
void printMotorData() {
// Motor 1 ve Motor 2 hızlarını, darbe sayılarını ve açılarını seri port üzerinden yazdır
Serial.print("Motor 1: ");
printMotorInfo(encoder1PinA, encoder1PinB);
Serial.print("Motor 2: ");
printMotorInfo(encoder2PinA, encoder2PinB);
Serial.println();
}
void printMotorInfo(int pinA, int pinB) {
int pulseCount = getMotorPulseCount(pinA, pinB);
float speed = getMotorSpeed(pulseCount);
float angle = getMotorAngle(pulseCount);
Serial.print("Hız: ");
Serial.print(speed);
Serial.print(" darbe/saniye, Darbe Sayısı: ");
Serial.print(pulseCount);
Serial.print(", Açı: ");
Serial.print(angle);
Serial.print(" derece\t");
}
int getMotorPulseCount(int pinA, int pinB) {
static int previousPinAState = LOW;
static int pulseCount = 0;
int currentPinAState = digitalRead(pinA);
int currentPinBState = digitalRead(pinB);
if (currentPinAState != previousPinAState) {
if (currentPinBState == LOW) {
pulseCount++;
} else {
pulseCount--;
}
}
previousPinAState = currentPinAState;
return pulseCount;
}
float getMotorSpeed(int pulseCount) {
const float pulsesPerSecond = pulseCount / 1.0;
return pulsesPerSecond;
}
float getMotorAngle(int pulseCount) {
const float pulsesPerRevolution = 64.0; // Bir devirdeki darbe sayısı
const float gearRatio = 50.0; // Dişli oranı (gerekirse güncelle)
const float degreesPerPulse = 360.0 / (pulsesPerRevolution * gearRatio);
return pulseCount * degreesPerPulse;
}
with this code, I am driving 2 engines smoothly using the l298n engine driver, but no matter how hard I tried to drive 4 engines, I couldn't come to a conclusion using the master slave connection, can you help me?