Master slave arduino

// 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?

i only see code for 2 motors

they want me to make a master and slave connection with this code I will use this code for the master arduino at the same time I need to use this code in the slave arduino

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

who is they?

what is the master/slave interface between two(?) Unos?

looks like the posted code uses an encode to measure motor speed and used to maintain some specified speed. are the two motors in that code running independent of one another?

if so, why is there a need for any interface between 2 unos when presumably all 4 motors run independent of one another

i'm missing something

i have to drive 4 motors with 2 arduino mega. to do this, I need to establish a master and slave connection between 2 arduino mega after connecting master and slave, I will move the motors using a joystick.First of all, I have to drive 4 engines in the same direction.

ok

  1. are you satisfied with the code you posted to driver 2 motors?

  2. what is the electrical interface you plan to use between the arduinos guessing Unos. Unos have just a single serial I/F

  3. what type of commands to you expect to need? do you need to just drive all motors forward/reverse at some speed, do you have separate speeds for each motor or are there some higher level commands for presumably what some robot needs to do (see joystick below)


i don't believe your code to measure motor speed using an encoder is correct. pulse counts need to be continuously detected, not just during some function call.

determining speed from pulse counts also requires knowing the time over which those pulses occurred.

i found that interrupts are needed to monitor encoders even when turned by hand

looks like your attempting to use PID. the Kd error term, the delta needs to be divided by dT, the time between the PID updates. the integral term is multiplied by dT.

i won't be surprised if you say that part of the code hasn't been debugged yet or was provided to you.

which leads to the question about the joystick control. is it crudely just indicating the direction and a scaled PWM value to the motors or is it a desired speed and turn rate and other code calculates the speed and direction of each motor?

before you repeat yourself .. i know you need a master/slave connect .. so answering the questions would save time

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