2024-06-25 15:23:50 +02:00
|
|
|
// Librairies utilisées dans le code :
|
2024-06-25 11:19:53 +02:00
|
|
|
#include <AsyncTCP.h>
|
|
|
|
#include <ESPAsyncWebSrv.h>
|
|
|
|
#include <WiFi.h>
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Définition des ports pour contrôler les moteurs
|
|
|
|
// Moteur gauche :
|
2024-06-25 11:19:53 +02:00
|
|
|
#define IN1a 27
|
|
|
|
#define IN2a 14
|
|
|
|
#define IN3a 12
|
|
|
|
#define IN4a 13
|
2024-06-25 15:23:50 +02:00
|
|
|
// Moteur droit :
|
2024-06-25 11:19:53 +02:00
|
|
|
#define IN1b 5
|
|
|
|
#define IN2b 18
|
|
|
|
#define IN3b 19
|
|
|
|
#define IN4b 21
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Paramètres de connexion wifi :
|
|
|
|
const char *ssid = "ESP32";
|
|
|
|
const char *password = "test12345";
|
2024-06-25 11:19:53 +02:00
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Temps d'attente entre deux impulsions :
|
2024-06-25 11:19:53 +02:00
|
|
|
int delayTime = 3;
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Variables de commande :
|
|
|
|
bool commandRunning = false; // Une commande doit être exécutée
|
|
|
|
bool stopCommand = false; // La commande doit être stoppée
|
|
|
|
String globalCommand; // Nom de la commande
|
|
|
|
int globalValue; // Valeur (nombre de pas à effectuer)
|
2024-06-25 11:19:53 +02:00
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Création du serveur asynchrone :
|
2024-06-25 11:19:53 +02:00
|
|
|
AsyncWebServer server(80);
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Code exécuté au démarrage (paramétrage) :
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
// On indique que les ports de contrôle des moteurs sont des sorties (output)
|
2024-06-25 11:19:53 +02:00
|
|
|
pinMode(IN1a, OUTPUT);
|
|
|
|
pinMode(IN2a, OUTPUT);
|
|
|
|
pinMode(IN3a, OUTPUT);
|
|
|
|
pinMode(IN4a, OUTPUT);
|
|
|
|
|
|
|
|
pinMode(IN1b, OUTPUT);
|
|
|
|
pinMode(IN2b, OUTPUT);
|
|
|
|
pinMode(IN3b, OUTPUT);
|
|
|
|
pinMode(IN4b, OUTPUT);
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
Serial.begin(115200); // Démarrage d'une communication Série avec l'ordinateur s'il est connecté
|
|
|
|
connectWiFi(); // Connexion au wifi
|
|
|
|
serverConfig(); // Configuration du serveur
|
|
|
|
server.begin(); // Démarrage du serveur
|
2024-06-25 11:19:53 +02:00
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Code exécuté en boucle jusqu'à l'extinction du robot
|
|
|
|
void loop()
|
|
|
|
{
|
2024-06-25 17:32:09 +02:00
|
|
|
// Ne rien faire si aucune commande en cours
|
|
|
|
if (!commandRunning)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Arrêter le robot si une commande est interrompue
|
2024-06-25 17:34:41 +02:00
|
|
|
// ou si le nombre de pas a été effectué
|
|
|
|
if (stopCommand || globalValue <= 0)
|
2024-06-25 15:23:50 +02:00
|
|
|
{
|
2024-06-25 17:32:09 +02:00
|
|
|
Serial.println("stop");
|
|
|
|
commandRunning = false;
|
|
|
|
stopCommand = false;
|
|
|
|
stopMotors();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sinon executer la commande en cours
|
|
|
|
chooseCommand(); // Appeler la fonction "chooseCommand()"
|
|
|
|
if (globalValue > 0)
|
|
|
|
{ // S'il reste des pas à effectuer alors on l'affiche et on enlève un pas
|
|
|
|
Serial.print(globalCommand);
|
|
|
|
Serial.print(": ");
|
|
|
|
Serial.println(globalValue);
|
|
|
|
globalValue--;
|
|
|
|
}
|
2024-06-25 11:19:53 +02:00
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Configuration du serveur asynchrone
|
|
|
|
void serverConfig()
|
|
|
|
{
|
|
|
|
server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request)
|
|
|
|
{
|
2024-06-25 11:19:53 +02:00
|
|
|
String command;
|
|
|
|
float value;
|
2024-06-25 17:57:54 +02:00
|
|
|
|
|
|
|
// Parse request params and update command and value
|
|
|
|
parseRequestParams(request, command, value);
|
|
|
|
|
2024-06-25 11:19:53 +02:00
|
|
|
Serial.print(command);
|
|
|
|
Serial.print(": ");
|
|
|
|
Serial.println(value);
|
2024-06-25 15:23:50 +02:00
|
|
|
request->send(200, "text/plain", command); });
|
2024-06-25 11:19:53 +02:00
|
|
|
}
|
2024-06-25 17:57:54 +02:00
|
|
|
|
|
|
|
void parseRequestParams(AsyncWebServerRequest &request, String &command, int &value)
|
|
|
|
{
|
|
|
|
// Stop here with error message if wrong command
|
|
|
|
if (!request->hasParam("command"))
|
|
|
|
{
|
2024-06-25 17:58:50 +02:00
|
|
|
command = "No command provided";
|
2024-06-25 17:57:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update command from params
|
|
|
|
command = request->getParam("command")->value();
|
|
|
|
|
|
|
|
// Update value from params and use "0" as default
|
|
|
|
value = request->hasParam("value") ? request->getParam("value")->value().toFloat() : 0;
|
|
|
|
|
|
|
|
requestCheck(command, value);
|
|
|
|
}
|
2024-06-25 11:19:53 +02:00
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Fonction pour convertir une longueur en cm vers un nombre de pas moteur.
|
|
|
|
int convertLengthToSteps(float length)
|
|
|
|
{
|
|
|
|
float result = length * 512 / (4 * 3.1415);
|
2024-06-25 11:19:53 +02:00
|
|
|
return int(result);
|
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// fonction pour convertir une rotation en degrés vers un nombre de pas moteur
|
|
|
|
int convertRotToSteps(int rotation)
|
|
|
|
{
|
|
|
|
int result = convertLengthToSteps(rotation * 3.1415 / 180 * 7.8);
|
2024-06-25 11:19:53 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
// Lorsqu'une commande arrive au serveur, cette fonction permet
|
|
|
|
void requestCheck(String command, float value)
|
|
|
|
{
|
|
|
|
if (command == "forward")
|
|
|
|
{
|
|
|
|
globalCommand = "forward";
|
|
|
|
commandRunning = true;
|
|
|
|
stopCommand = false;
|
|
|
|
globalValue = convertLengthToSteps(value);
|
2024-06-25 11:19:53 +02:00
|
|
|
}
|
2024-06-25 15:23:50 +02:00
|
|
|
else if (command == "backward")
|
|
|
|
{
|
|
|
|
globalCommand = "backward";
|
|
|
|
commandRunning = true;
|
|
|
|
stopCommand = false;
|
|
|
|
globalValue = convertLengthToSteps(value);
|
2024-06-25 11:19:53 +02:00
|
|
|
}
|
2024-06-25 15:23:50 +02:00
|
|
|
else if (command == "left")
|
|
|
|
{
|
|
|
|
globalCommand = "left";
|
|
|
|
commandRunning = true;
|
|
|
|
stopCommand = false;
|
|
|
|
globalValue = convertRotToSteps(value);
|
2024-06-25 11:19:53 +02:00
|
|
|
}
|
2024-06-25 15:23:50 +02:00
|
|
|
else if (command == "right")
|
|
|
|
{
|
|
|
|
globalCommand = "right";
|
|
|
|
commandRunning = true;
|
|
|
|
stopCommand = false;
|
|
|
|
globalValue = convertRotToSteps(value);
|
2024-06-25 11:19:53 +02:00
|
|
|
}
|
2024-06-25 15:23:50 +02:00
|
|
|
else if (command == "stop")
|
|
|
|
{
|
|
|
|
globalCommand = "stop";
|
|
|
|
commandRunning = true;
|
|
|
|
stopCommand = true;
|
2024-06-25 11:19:53 +02:00
|
|
|
}
|
2024-06-25 15:23:50 +02:00
|
|
|
if (value == 0)
|
|
|
|
{
|
2024-06-25 11:19:53 +02:00
|
|
|
globalValue = 474;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void connectWiFi()
|
|
|
|
{
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
|
|
WiFi.softAP(ssid, password);
|
|
|
|
Serial.print("[+] AP Created with IP Gateway ");
|
|
|
|
Serial.println(WiFi.softAPIP());
|
|
|
|
Serial.println("");
|
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
void chooseCommand()
|
|
|
|
{
|
|
|
|
if (globalCommand == "forward")
|
|
|
|
{
|
2024-06-25 11:19:53 +02:00
|
|
|
forward();
|
|
|
|
}
|
2024-06-25 15:23:50 +02:00
|
|
|
else if (globalCommand == "left")
|
|
|
|
{
|
2024-06-25 11:19:53 +02:00
|
|
|
left();
|
|
|
|
}
|
2024-06-25 15:23:50 +02:00
|
|
|
else if (globalCommand == "right")
|
|
|
|
{
|
2024-06-25 11:19:53 +02:00
|
|
|
right();
|
|
|
|
}
|
2024-06-25 15:23:50 +02:00
|
|
|
else if (globalCommand == "backward")
|
|
|
|
{
|
2024-06-25 11:19:53 +02:00
|
|
|
backward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 17:20:53 +02:00
|
|
|
void writeMotor(uint8_t left, uint8_t right)
|
|
|
|
{
|
|
|
|
digitalWrite(IN4a, left & 0b1000);
|
|
|
|
digitalWrite(IN3a, left & 0b0100);
|
|
|
|
digitalWrite(IN2a, left & 0b0010);
|
|
|
|
digitalWrite(IN1a, left & 0b0001);
|
|
|
|
digitalWrite(IN4b, right & 0b1000);
|
|
|
|
digitalWrite(IN3b, right & 0b0100);
|
|
|
|
digitalWrite(IN2b, right & 0b0010);
|
|
|
|
digitalWrite(IN1b, right & 0b0001);
|
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
void forward()
|
|
|
|
{
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b1000, 0b0001);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0100, 0b0010);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0010, 0b0100);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0001, 0b1000);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
void backward()
|
|
|
|
{
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0001, 0b1000);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0010, 0b0100);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0100, 0b0010);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b1000, 0b0001);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
void right()
|
|
|
|
{
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b1000, 0b1000);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0100, 0b0100);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0010, 0b0010);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0001, 0b0001);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
void left()
|
|
|
|
{
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0001, 0b0001);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0010, 0b0010);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0100, 0b0100);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b1000, 0b1000);
|
2024-06-25 11:19:53 +02:00
|
|
|
delay(delayTime);
|
|
|
|
}
|
|
|
|
|
2024-06-25 15:23:50 +02:00
|
|
|
void stopMotors(void)
|
|
|
|
{
|
2024-06-25 17:20:53 +02:00
|
|
|
writeMotor(0b0000, 0b0000);
|
2024-06-25 11:19:53 +02:00
|
|
|
}
|