Bonjour à tous,
J'ai un robot constitué d'un "4wd robot" d'une platine "motor shield" et d'un "Ping Ultrasonic Range Finder".
Le système fonctionne normalement, c'est-à-dire qu'il reconnait l'obstacle, recule, vire à droite et ainsi de suite pendant environ 1 minute, puis se met à tourner sur 360° par tranche de 90° continuellement !
Les 4 moteurs sont alimentés par un accu de 9 volts ainsi que la carte arduino.
Pourriez-vous m'aider à resoudre ce petit problème
Ci-joint le code:
#include <AFMotor.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
const int pingPin = 19;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
motor1.setSpeed(220); // Set the speed for the motors (255 is the maximum)
motor2.setSpeed(220);
motor3.setSpeed(220);
motor4.setSpeed(220);
}
float ping () // This is the code that runs the PING)) Sensor
{
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
return inches;
}
void forward() // This function moves the wheels forward
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void backward() // This function moves the wheels backward
{
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void haltMotors() // This function stops each motor (It is better to stop the motors before changing direction.)
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void turnRight() // This function turns the robot right.
{
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(BACKWARD);
}
void loop()
{
long duration, inches; // Declare the variable, duration and inches.
duration = pulseIn(pingPin, HIGH);
inches = ping(); // Set the inches variable to the float returned by the ping() function.
while (inches >8){ // While the robot is 8 inches away from an object.
inches = ping();
forward(); // Move the robot forward.
}
haltMotors(); // Stop the motors for 2 seconds, before changing direction.
delay(2000);
while (inches < 10){ // Until the robot is 10 inches away from the object, go backward.
inches = ping();
backward(); // Move the robot backward.
}
turnRight(); // Once the robot is done moving backward, turn the robot right.
delay(1000); // Note! Change this value (greater or smaller) to adjust how far the robot turns right.
haltMotors();
delay(2000);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
