Bonjour je suis actuellement en train de réaliser un drone bicoptère avec une carte Nodemcu ESP8266, un driver 2 moteurs et un capteur MPU6050. J'ai écrit un code pour pouvoir contrôler mon drone avec mon téléphone et pour que le drone vole en se stabilisant après chaque requête. Mais voilà un problème se pose à la ligne 74 PIDTimer.setInterval(Periode_Echantillonnage, Gerer_Robot);
et à la ligne 137 PIDTimer.run();
.
Voilà la capture du rapport de bug et mon code en entier.
`#include <ESP8266WiFi.h>
#include <Servo.h>
#include <Wire.h>
#include <MPU6050.h>
#include <SimpleTimer.h>
WiFiClient client;
WiFiServer server(81);
const char *ssid = "XXX";
const char *password = "XXX";
// Création du timer PIDTimer
SimpleTimer PIDTimer;
// Définition de la fréquence d'échantillonnage du timer PIDTimer
const int Periode_Echantillonnage = 1500;
// Création d'une instance mpu
MPU6050 mpu;
// Angles mesurés par le gyroscope
float angle_x_gyro = 0.0;
float angle_y_gyro = 0.0;
// Angles mesurés par l'accéléromètre
float angle_x = 0.0;
float angle_y = 0.0;
// Données brutes fournies par mpu
int16_t ax, ay, az;
int16_t gx, gy, gz;
// Constante de converion des radians en degrés
const float RADIANS_TO_DEGREES = 57.295779513;
// Définition des offsets das angles X et Y
float Offset_angle_x = 0.0; // A mesurer
float Offset_angle_y = 0.0; // A mesurer
Servo Servo_1;
Servo Servo_2;
#define Servo1 10 //broche servo 1 S3
#define Servo2 9 //broche servo 2 S2
int Min = 500;
int Max = 2000;
int AIN1 = 16; //broche 1 moteur 1 D0
int AIN2 = 5; //broche 2 moteur 1 D1
int PWMA = 4; //broche puissance A D2
int PWMB = 0; //broche de puissance B D3
int BIN1 = 2; //broche 1 moteur 2 D4
int BIN2 = 14; //broche 2 moteur 2 D5
int RC1; // rapport cyclique PWMA entre 5 et 255
int RC2; // rapport cyclique PWMB entre 5 et 255
//*************************
int RC; // rapport cyclique
float angle_3points = 0.0;
float _3points = 0.0;
//*************************
void setup() {
// Initialisation I2C
Wire.begin();
// Initialisation du mpu
mpu.initialize();
//Affectation de la procédure Gerer_Robot au PIDTimer
PIDTimer.setInterval(Periode_Echantillonnage, Gerer_Robot);
//ouverture des broches
pinMode(Servo1, OUTPUT);
pinMode(Servo2, OUTPUT);
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(PWMA,OUTPUT);
pinMode(PWMB,OUTPUT);
pinMode(BIN1,OUTPUT);
pinMode(BIN2,OUTPUT);
Connexion_Reseau();
server.begin();
delay(1000);
//partie qui va etre suprime sur le code final
//verif des servos
Servo_1.attach(Servo1, Min, Max);
Servo_2.attach(Servo2, Min, Max);
Servo_1.write(0);
Servo_2.write(0);
delay(1000);
Servo_1.write(60);
Servo_2.write(60);
delay(500);
Servo_1.write(0);
Servo_2.write(0);
delay(1000);
Servo_1.write(-60);
Servo_2.write(-60);
delay(1000);
Servo_1.write(0);
Servo_2.write(0);
delay(1000);
//verif des moteurs
digitalWrite(AIN1,0); //maj du moteur1 dans le sens 1
digitalWrite(AIN2,1);
digitalWrite(BIN1,0); //maj du moteur2 dans le sens 1
digitalWrite(BIN2,1);
delay(10);
RC1 = 5; //maj des rapports cycliques a 5 et test des echelles de puissance
RC2 = 5;
while (RC <= 255) {
analogWrite(PWMA, RC1);
analogWrite(PWMB, RC2);
RC1 = RC1 + 25;
RC2 = RC2 + 25;
delay(500);
}
RC1 = 5; //maj des rapports cycliques a 5
RC2 = 5;
}
void loop() {
// Exécution du PIDTimer
PIDTimer.run();
Stabilisateur(); // stabilisation du drone
}
void Connexion_Reseau(){
WiFi.disconnect();
delay(1000);
WiFi.begin(ssid,password);
while ((!(WiFi.status() == WL_CONNECTED))){
delay(300);
}
}
void Traiter_Requete(){
WiFiClient client = server.available();
if (client) {
String Requete_Client = client.readStringUntil('\r');
client.flush(); // On efface la requête du client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<meta chaset=\"utf-8\">");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"stylesheet\" href=\"style.css\">");
client.println("<style type=\"text/css\">");
client.println("f1 { color: #000FFF;font-family: Helvetica, sans-serif; font-size: 24px;}");
client.println("</style>");
client.println("<title>Oblivion</title>");
client.println("</head>");
client.println("<body>");
client.println("<center>");
client.println("<p align=\"center\"><a href=\"/LACET_GAUCHE\"><button style=\"width: 240px; height: 60px;\"><f1><strong>L GAUCHE</strong></f1></button></a> <p align=\"center\"><a href=\"/PLUS\"><button style=\"width: 240px; height: 60px;\"><f1><strong>+</strong></f1></button></a></p> <a href=\"/LACET_DROIT\"><button style=\"width: 240px; height: 60px;\"><f1><strong>L DROIT</strong></f1></button></a></p>");
client.println("<p align=\"center\"><a href=\"/AVANCE\"><button style=\"width: 240px; height: 60px;\"><f1><strong>AVANCE</strong></f1></button></a></p>");
client.println("<p align=\"center\"><a href=\"/RECULE\"><button style=\"width: 240px; height: 60px;\"><f1><strong>RECULE</strong></f1></button></a></p>");
client.println("<p align=\"center\"><a href=\"/ROULI_GAUCHE\"><button style=\"width: 240px; height: 60px;\"><f1><strong>R GAUCHE</strong></f1></button></a> <p align=\"center\"><a href=\"/MOINS\"><button style=\"width: 240px; height: 60px;\"><f1><strong>-</strong></f1></button></a></p> <a href=\"/ROULI_DROIT\"><button style=\"width: 240px; height: 60px;\"><f1><strong>R DROIT</strong></f1></button></a></p>");
client.println("</center>");
client.println("</body>");
client.println("</html>");
if(Requete_Client.indexOf("GET AVANCE") >= 0){
Servo_1.write(30); // valeurs a determiner
Servo_2.write(30);
RC1 = RC1 + 10; // valeurs a determiner
RC2 = RC2 + 10;
angle_3points = angle_3points + 30; // correction du stabilisateur
}
if(Requete_Client.indexOf("GET RECULE") >= 0){
Servo_1.write(-30); // valeurs a determiner
Servo_2.write(-30);
RC1 = RC1 + 10; // valeurs a determiner
RC2 = RC2 + 10;
angle_3points = angle_3points + 30; // correction du stabilisateur
}
if(Requete_Client.indexOf("GET ROULI_DROIT") >= 0){
Servo_1.write(0); // valeurs a determiner
Servo_2.write(0);
RC1 = RC1 + 10; // valeurs a determiner
RC2 = RC2 - 10;
angle_3points = angle_3points + _3points; // correction du stabilisateur
}
if(Requete_Client.indexOf("GET ROULI_GAUCHE") >= 0){
Servo_1.write(0); // valeurs a determiner
Servo_2.write(0);
RC1 = RC1 - 10; // valeurs a determiner
RC2 = RC2 + 10;
angle_3points = angle_3points + _3points; // correction du stabilisateur
}
if(Requete_Client.indexOf("GET LACET_DROIT") >= 0){
Servo_1.write(-30); // valeurs a determiner
Servo_2.write(30);
RC1 = RC1;
RC2 = RC2;
angle_3points = angle_3points + _3points; // correction du stabilisateur
}
if(Requete_Client.indexOf("GET LACET_GAUCHE") >= 0){
Servo_1.write(30); // valeurs a determiner
Servo_2.write(-30);
RC1 = RC1; // valeurs a determiner
RC2 = RC2;
angle_3points = angle_3points + _3points; // correction du stabilisateur
}
if(Requete_Client.indexOf("GET PLUS") >= 0){
RC1 = RC1 + 25;
RC2 = RC2 + 25;
angle_3points = angle_3points + _3points; // correction du stabilisateur
}
if(Requete_Client.indexOf("GET MOINS") >= 0){
RC1 = RC1 - 25;
RC2 = RC2 - 25;
angle_3points = angle_3points + _3points; // correction du stabilisateur
}
}
}
void Gerer_Robot(){
// Acquisition des données brutes
//mpu.getMotion6(&ax, &ay, &gx, &gy);
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Application du gain
// Application du gain
float gyro_x = gx / 131.0;
float gyro_y = gy / 131.0;
float accel_x = ax;
float accel_y = ay;
float accel_z = az;
// Calcul des angles mesurés par l'accéléromètre
float angle_x_accel = atan(accel_y/sqrt(pow(accel_x,2) + pow(accel_z,2)))RADIANS_TO_DEGREES + Offset_angle_x ;
float angle_y_accel = atan(-1accel_x/sqrt(pow(accel_y,2) + pow(accel_z,2)))*RADIANS_TO_DEGREES + Offset_angle_y;
// Calcul des angles mesurés par le gyroscope
float dt = Periode_Echantillonnage / 1000.0;
angle_x_gyro = gyro_xdt + angle_x;
angle_y_gyro = gyro_ydt + angle_y;
// Application du filtre complémentaire
const float alpha = 0.96;
angle_x = alpha * angle_x_gyro + (1.0 - alpha) * angle_x_accel ;
angle_y = alpha * angle_y_gyro + (1.0 - alpha) * angle_y_accel ;
}
void Stabilisateur(){
while (angle_x = 0){
if (angle_x >= 0){
// correction avec les moteurs ou servos
Servo_1.write(+1);// toutes la valeurs suivantes sont a changées
Servo_2.write(+1);
RC1 = RC1 + 10;
RC2 = RC2 + 10;
}
if (angle_x <= 0){
// correctiin avec les moteurs ou servos
Servo_1.write(-+1);
Servo_2.write(-+1);
RC1 = RC1 + 10;
RC2 = RC2 + 10;
}
}
while (angle_y = 0){
if (angle_y >= 0){
// correction avec les moteurs ou servos
Servo_1.write(+1);
Servo_2.write(-+1);
RC1 = RC1 + 5;
RC2 = RC2 + 5;
}
if (angle_y <= 0){
// correction avec les moteurs ou servos
Servo_1.write(-+1);
Servo_2.write(+1);
RC1 = RC1 + 5;
RC2 = RC2 + 5;
}
}
}
`