Я работал над строкой, следуя проекту «Робот», но нуждаюсь в помощи, потому что я никогда не работал над такого рода проектом раньше.----parts used-----
1.Arduino uno r3
2.5 sensor array
3.L298N motor driver
4.2 500 Rpm motors
5.arrow shaped chaises
--------------------
< /code>
// === PIN DEFINITIONS ===
#define ENA 5
#define IN1 6
#define IN2 7
#define ENB 9
#define IN3 10
#define IN4 11
int ir[5] = {A0, A1, A2, A3, A4}; // IR sensors from left to right
int sensor[5]; // Digital sensor readings
// === PID PARAMETERS ===
float Kp = 20;
float Ki = 0;
float Kd = 8;
int lastError = 0;
// === MOTOR SPEED CONTROL ===
int baseSpeed = 150;
int maxSpeed = 255;
// === RECOVERY STATE ===
unsigned long lostTime = 0;
bool recovering = false;
// === LINE COLOR DETECTION ===
bool blackLine = true;
int colorThreshold = 500;
// === JUNCTION TRACKING ===
int junctionCount = 0;
// === DEBUG MODE ===
bool debugMode = true;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 5; i++) pinMode(ir, INPUT);
pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
pinMode(ENB, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
calibrateLineColor();
}
void loop() {
readSensors();
if (debugMode) {
Serial.print("Sensors: ");
for (int i = 0; i < 5; i++) Serial.print(sensor);
Serial.println();
}
if (isTjunction()) {
handleJunction();
delay(100);
return;
}
if (isLineLost()) {
recoverLine();
return;
}
recovering = false;
int error = calculateError();
int P = error;
static int I = 0;
int D = error - lastError;
int correction = Kp * P + Ki * I + Kd * D;
lastError = error;
int leftSpeed = constrain(baseSpeed + correction, 0, maxSpeed);
int rightSpeed = constrain(baseSpeed - correction, 0, maxSpeed);
drive(leftSpeed, rightSpeed);
delay(5);
}
// === CALIBRATION ===
void calibrateLineColor() {
long total = 0;
for (int i = 0; i < 5; i++) total += analogRead(ir);
int avg = total / 5;
colorThreshold = avg;
if (avg > 600) {
blackLine = false;
Serial.println("Detected WHITE line on BLACK background");
} else {
blackLine = true;
Serial.println("Detected BLACK line on WHITE background");
}
}
// === SENSOR FUNCTIONS ===
void readSensors() {
for (int i = 0; i < 5; i++) {
int val = analogRead(ir);
sensor = (val > colorThreshold) ^ !blackLine ? 0 : 1;
}
}
int calculateError() {
int weights[5] = {-4, -2, 0, 2, 4};
int sum = 0, count = 0;
for (int i = 0; i < 5; i++) {
if (sensor) {
sum += weights;
count++;
}
}
if (count == 0) return lastError;
return sum / count;
}
bool isLineLost() {
for (int i = 0; i < 5; i++) {
if (sensor) return false;
}
return true;
}
bool isTjunction() {
return (sensor[0] == 1 && sensor[4] == 1);
}
// === JUNCTION HANDLING ===
void handleJunction() {
junctionCount++;
Serial.print("Junction #"); Serial.println(junctionCount);
if (junctionCount == 1) {
smartTurn(-1); // Turn left
} else if (junctionCount == 2) {
smartTurn(1); // Turn right
} else {
goStraight();
}
}
// === TURN BEHAVIOR ===
void smartTurn(int direction) {
while (!sensor[2]) {
if (direction < 0) drive(0, baseSpeed); // Left
else drive(baseSpeed, 0); // Right
readSensors();
}
delay(100);
}
void goStraight() {
drive(baseSpeed, baseSpeed);
delay(200);
}
// === RECOVERY FOR GAPS ===
void recoverLine() {
if (!recovering) {
lostTime = millis();
recovering = true;
}
unsigned long timeLost = millis() - lostTime;
if (debugMode) Serial.println("Recovering...");
if (timeLost < 250) {
drive(baseSpeed, baseSpeed);
} else if (timeLost < 800) {
if (lastError < 0) drive(0, baseSpeed);
else drive(baseSpeed, 0);
} else {
drive(baseSpeed, -baseSpeed); // Spin in place
}
readSensors();
if (!isLineLost()) recovering = false;
}
// === MOTOR CONTROL ===
void drive(int left, int right) {
analogWrite(ENA, abs(left));
analogWrite(ENB, abs(right));
digitalWrite(IN1, left >= 0 ? HIGH : LOW);
digitalWrite(IN2, left >= 0 ? LOW : HIGH);
digitalWrite(IN3, right >= 0 ? HIGH : LOW);
digitalWrite(IN4, right >= 0 ? LOW : HIGH);
}
< /code>
Я также прикрепил изображение типа трека, который я хочу, чтобы он работал
, пожалуйста, помогите, если возможно, < /p>
Помогите мне определить, есть ли какие -либо проблемы, и, возможно, настройка немного < /p>
Подробнее здесь: https://stackoverflow.com/questions/796 ... -me-make-s
Сделал линию после робота, используя помощь в чате GPT, может ли кто -нибудь помочь мне убедиться, что она работает [зак ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение