Ma première étape pour le bateau est de faire fonctionner la voiture … et ce n'est pas le cas.
Je n'ai qu'une roue qui tourne.
Le servo ne bouge pas ( on l'entend s'initialiser au démarrage).
Je vous joints le schéma de câblage donné sur le site et les photos de mon montage.
Les 2 moteurs fonctionnent et le servo également.
J'ai bien vérifier les contacts électriques.
Pour le circuit imprimé rouge, j'ai bien enlever les 2 cavaliers.
le retour du serial.
J'ai laissé ma main devant le détecteur ultrason, la roue de droite sur la photo est toujours en marche arrière tant que j'ai la main devant puis marche avant quand je la retire
J'ai le retour d'info de rotation du servo mais il ne bouge pas
checking direction
Forward
checking direction
Forward
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Problem Ahead
servo 90
Next step is TurnAround
TurnAround
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Next step is TurnRight
servo 90
TurnRight
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Problem Ahead
servo 90
Next step is TurnAround
TurnAround
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Problem Ahead
servo 90
Next step is TurnAround
TurnAround
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Problem Ahead
servo 90
Next step is TurnAround
TurnAround
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Problem Ahead
servo 90
Next step is TurnAround
TurnAround
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Problem Ahead
servo 90
Next step is TurnAround
TurnAround
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Next step is TurnRight
servo 90
TurnRight
checking direction
Forward
checking direction
Forward
checking direction
Forward
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Problem Ahead
servo 90
Next step is TurnAround
TurnAround
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Problem Ahead
servo 90
Next step is TurnAround
TurnAround
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Problem Ahead
servo 90
Next step is TurnAround
TurnAround
Problem Ahead
checking direction
stopped
servo 180
Problem Ahead
Next step is TurnRight
servo 90
TurnRight
checking direction
Forward
checking direction
Forward
checking direction
Forward
checking direction
Forward
checking direction
Forward
checking direction
Forward
checking direction
Forward
checking direction
Forward
checking direction
Forward
checking direction
Forward
checking direction
Forward
checking direction
Forward
avant de passer définitivement au bateau, j'ai vu un pb sur la voiture ( qui sera aussi sur la bateau).
Le détecteur ultrason n'a que 15° d'angle de détection et il est posé dans l'axe de la voiture.
Donc si un obstacle est situé sur les cotés droit ou gauche il ne sera pas détecté et la voiture va l' heurter.
Sur le site youtube, il donne dans la partie 2 des tests pour chaque élément, entre autre un test du servo qui lui fait faire un balayage.
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Je me disais, quand la voiture va tout droit ,( forward dans le code), on pourrait faire faire au servo un balayage de - 45° à +45°. ainsi l'angle de détection sera plus important.
Excat.
Mais quand il va tout droit, le servo/ultrason reste dans l'axe et ne pivote plus.
donc l'angle de détection vers l'avant n'est aue de 15° ( celui du détecteur).
en faisant un balayage en avançant de +/- 45° il couvre 90° sur l'avant et évite ainsi les obstacles sur au niveau des roues par exemple, obstacles non détectés si le servo restait dans l'axe de la voiture
Donc tu veux adapter le code actuel pour que le servo fasse ses aller / retours pendant que le robot avance ?
Si oui, poste le code actuel pour que j'y jette un œil...
#include <Servo.h>
//https://www.youtube.com/watch?v=ms0C3o407oM
//http://chenthedesignmaker.com/robot-car-part-3-code-preview/
// Pins
#define TRIG_PIN A0
#define ECHO_PIN A1
//Define all the connections maps to the L298N
#define enA 13
#define in1 12
#define in2 11
#define in3 7
#define in4 6
#define enB 5
#define servoPin 2
class Motor {
int enablePin;
int directionPin1;
int directionPin2;
public:
//Method to define the motor pins
Motor(int ENPin, int dPin1, int dPin2) {
enablePin = ENPin;
directionPin1 = dPin1;
directionPin2 = dPin2;
};
//Method to drive the motor 0~255 driving forward. -1~-255 driving backward
void Drive(int speed) {
if (speed >= 0) {
digitalWrite(directionPin1, LOW);
digitalWrite(directionPin2, HIGH);
} else {
digitalWrite(directionPin1, HIGH);
digitalWrite(directionPin2, LOW);
speed = - speed;
}
analogWrite(enablePin, speed);
}
};
Motor leftMotor = Motor(enA, in1, in2);
Motor rightMotor = Motor(enB, in3, in4);
Servo myservo; // create servo object to control a servo
void motorInitiation() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Set initial direction and speed
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
//Variables--------------------------------------------------------------------------
// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;
bool ObsticalAhead = false;
enum Directions { Forward, TurnLeft, TurnRight, TurnAround, Brake};
Directions nextStep = Forward;
unsigned long t1;
unsigned long t2;
unsigned long pulse_width;
float cm;
// servo
int pos = 90;
int sensservo = 1; // <-- augmenter si le mouvement du servo est trop lent
//SETUP--------------------------------------------------------------------------
void setup() {
// The Trigger pin will tell the sensor to range find
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
// We'll use the serial monitor to view the sensor output
Serial.begin(9600);
motorInitiation();
Directions nextStep = Forward;
myservo.attach(servoPin);
myservo.write(pos); // intitialize servo's medium position
delay(2000); // <-- à modifier, pour laisser le servo atteindre la position centrale
}
void loop() {
pos += sensservo;
pos = constrain (pos,0,180);
if (pos >= 180 || pos<= 0) sensservo = - sensservo;
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
checkDistance();
checkDirection();
drive();
}
void checkDistance() {
// Hold the trigger pin high for at least 10 us
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Wait for pulse on echo pin
while ( digitalRead(ECHO_PIN) == 0 );
// Measure how long the echo pin was held high (pulse width)
// Note: the micros() counter will overflow after ~70 min
t1 = micros();
while ( digitalRead(ECHO_PIN) == 1);
t2 = micros();
pulse_width = t2 - t1;
// Calculate distance in centimeters and inches. The constants
// are found in the datasheet, and calculated from the assumed speed
//of sound in air at sea level (~340 m/s).
cm = pulse_width / 58.0;
// Print out results
if ( pulse_width > MAX_DIST ) {
//Serial.println("Out of range");
} else {
//Serial.print(cm);
//Serial.println(" cm \t");
}
// Wait at least 60ms before next measurement
delay(60);
if (cm <= 20) {
ObsticalAhead = true;
Serial.println("Problem Ahead");
}
else {
ObsticalAhead = false;
}
}
void checkDirection() {
Serial.println("checking direction");
if (ObsticalAhead == true) {
nextStep = Brake;
drive();
myservo.write(180); // tell servo to go to position in variable 'pos'
delay(400); // waits 15ms for the servo to reach the position
checkDistance();
if (ObsticalAhead == false) { //if left side is open
nextStep = TurnLeft;
Serial.println("Next step is TurnLeft");
myservo.write(90);//reset servo position
delay(400);
}
else { // left is blocked, now need to look at right
myservo.write(0); // tell servo to go to position in variable 'pos'
delay(800); // waits 15ms for the servo to reach the position
checkDistance();
if (ObsticalAhead == false) { //if right side is open
nextStep = TurnRight;
Serial.println("Next step is TurnRight");
myservo.write(90);//reset servo position
delay(400);
}
else { //right is blocked as well, need to turn around
nextStep = TurnAround;
myservo.write(90);//reset servo position
delay(300);
Serial.println("Next step is TurnAround");
}
}
}
else {
nextStep = Forward; //No obstical ahead
}
}
void drive() {
switch (nextStep) {
case Forward:
leftMotor.Drive(255);
rightMotor.Drive(255);
Serial.println("Forward");
break;
case TurnLeft:
leftMotor.Drive(-255);
rightMotor.Drive(255);
Serial.println(" TurnLeft");
delay(400);
break;
case TurnRight:
leftMotor.Drive(255);
rightMotor.Drive(-255);
Serial.println(" TurnRight");
delay(400);
break;
case TurnAround:
leftMotor.Drive(255);
rightMotor.Drive(-255);
Serial.println(" TurnAround");
delay(600);
break;
case Brake:
leftMotor.Drive(0);
rightMotor.Drive(0);
Serial.println(" stopped");
break;
}
}
Il y a des paramètres à régler :
int sensservo = 1; // <-- augmenter si le mouvement du servo est trop lent
delay(2000); // <-- à modifier, pour laisser le servo atteindre la position centrale
delay(15); // waits 15ms for the servo to reach the position
Je pense que oui, tu as mis un délai max aller / retour de 23 ms. Je pense que pendant ce temps, même avec une vitesse de 10, le servo, s'il bouge au moment de la lecture du capteur US, n'a pas le temps de se déplacer beaucoup...
Exact.
Je me disais pour les fonctions BordureDroite et BordureGauche, il ne faut faire marcher qu'un seul moteur en marche avant et non 1 marche avant et 1 marche arrière, sinon le bateau 'tournerait' sur place.
En clair pour BordureDroite, moteur gauche 255 moteur droit 0.
C'est bien expliqué ?
Salut : je relance le sujet, je n'ai pas pu avancer car je galérais sur un projet pour moi.
Peux-tu me redonner les versions finales de la V1, de l'afficheur et de la V2 (tout ce qu'il faut mettre ensemble) ?
En résumé , garder la V2
Ajouter l'écran ( les températures et la tension)
Ajouter les 2 pompes
Ajouter de la V1, les fonctions BordureDroite et BordureGauche.
Et un plus , m'expliquer comment brancher sur les modules sensor shield et le LN298 ajoutés sur l'Arduino