refactor(server): ♻️ move global const to config header and use #define instead of memory const

This commit is contained in:
Julien Oculi 2024-06-26 12:27:50 +02:00
parent 93922dc387
commit 5bf06a4e94
3 changed files with 92 additions and 96 deletions

View file

@ -1,21 +1,9 @@
//Définition des ports pour controler les moteurs #include "config.h"
//Moteur gauche :
#define IN1a 27
#define IN2a 14
#define IN3a 12
#define IN4a 13
//Moteur droit :
#define IN1b 5
#define IN2b 18
#define IN3b 19
#define IN4b 21
//Temps d'attente entre deux impulsions : // Code éxécuté au démarrage (paramétrage) :
int delayTime = 3; void setup()
{
//Code éxécuté au démarrage (paramétrage) : // On indique que les ports de controle des moteurs sont des sorties (output)
void setup() {
//On indique que les ports de controle des moteurs sont des sorties (output)
pinMode(IN1a, OUTPUT); pinMode(IN1a, OUTPUT);
pinMode(IN2a, OUTPUT); pinMode(IN2a, OUTPUT);
pinMode(IN3a, OUTPUT); pinMode(IN3a, OUTPUT);
@ -26,38 +14,40 @@ void setup() {
pinMode(IN3b, OUTPUT); pinMode(IN3b, OUTPUT);
pinMode(IN4b, OUTPUT); pinMode(IN4b, OUTPUT);
Serial.begin(115200); // Démarrage d'une communication Série avec l'ordinateur s'il est connecté
Serial.begin(115200); //Démarrage d'une communication Série avec l'ordinateur s'il est connecté
} }
//Code éxécuté en boucle jusqu'à l'extinction du robot // Code éxécuté en boucle jusqu'à l'extinction du robot
void loop() { void loop()
//Exemple de commande : {
for (int i=0; i<50; i++){ // Exemple de commande :
for (int i = 0; i < 50; i++)
{
forward(); forward();
} }
int rotation_steps = convertRotToSteps(46); int rotation_steps = convertRotToSteps(46);
for (int i=0; i<rotation_steps; i++){ for (int i = 0; i < rotation_steps; i++)
{
left(); left();
} }
} }
// Fonction pour convertir une longueur en cm vers un nombre de pas moteur.
int convertLengthToSteps(float length)
//Fonction pour convertir une longueur en cm vers un nombre de pas moteur. {
int convertLengthToSteps(float length){ float result = length * 512 / (4 * 3.1415);
float result = length*512/(4*3.1415);
return int(result); return int(result);
} }
//fonction pour convertir une rotation en degrés vers un nombre de pas moteur // fonction pour convertir une rotation en degrés vers un nombre de pas moteur
int convertRotToSteps(int rotation){ int convertRotToSteps(int rotation)
int result = convertLengthToSteps(rotation*3.1415/180*7.8); {
int result = convertLengthToSteps(rotation * 3.1415 / 180 * 7.8);
return result; return result;
} }
void forward()
void forward(){ {
digitalWrite(IN4a, HIGH); digitalWrite(IN4a, HIGH);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -66,7 +56,7 @@ void forward(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH); digitalWrite(IN1b, HIGH);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, HIGH); digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -75,7 +65,7 @@ void forward(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH); digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH); digitalWrite(IN2a, HIGH);
@ -84,7 +74,7 @@ void forward(){
digitalWrite(IN3b, HIGH); digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -93,10 +83,11 @@ void forward(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
} }
void backward(){ void backward()
{
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -105,7 +96,7 @@ void backward(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH); digitalWrite(IN2a, HIGH);
@ -114,7 +105,7 @@ void backward(){
digitalWrite(IN3b, HIGH); digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, HIGH); digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -123,7 +114,7 @@ void backward(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH); digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, HIGH); digitalWrite(IN4a, HIGH);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -132,10 +123,11 @@ void backward(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH); digitalWrite(IN1b, HIGH);
delay(delayTime); delay(DELAY_TIME);
} }
void right(){ void right()
{
digitalWrite(IN4a, HIGH); digitalWrite(IN4a, HIGH);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -144,7 +136,7 @@ void right(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, HIGH); digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -153,7 +145,7 @@ void right(){
digitalWrite(IN3b, HIGH); digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH); digitalWrite(IN2a, HIGH);
@ -162,7 +154,7 @@ void right(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH); digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -171,10 +163,11 @@ void right(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH); digitalWrite(IN1b, HIGH);
delay(delayTime); delay(DELAY_TIME);
} }
void left(){ void left()
{
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -183,7 +176,7 @@ void left(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH); digitalWrite(IN1b, HIGH);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH); digitalWrite(IN2a, HIGH);
@ -192,7 +185,7 @@ void left(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH); digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, HIGH); digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -201,7 +194,7 @@ void left(){
digitalWrite(IN3b, HIGH); digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
digitalWrite(IN4a, HIGH); digitalWrite(IN4a, HIGH);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -210,11 +203,11 @@ void left(){
digitalWrite(IN3b, LOW); digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
delay(delayTime); delay(DELAY_TIME);
} }
void stopMotors(void)
void stopMotors(void) { {
digitalWrite(IN4a, LOW); digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW); digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW); digitalWrite(IN2a, LOW);
@ -225,4 +218,3 @@ void stopMotors(void) {
digitalWrite(IN2b, LOW); digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW); digitalWrite(IN1b, LOW);
} }

View file

@ -2,28 +2,7 @@
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <WiFi.h> #include <WiFi.h>
#include "config.h"
// Définition des ports pour contrôler les moteurs
// Moteur gauche :
#define IN1a 27
#define IN2a 14
#define IN3a 12
#define IN4a 13
// Moteur droit :
#define IN1b 5
#define IN2b 18
#define IN3b 19
#define IN4b 21
// Paramètres de connexion wifi :
// const char *ssid = "ESP32";
// const char *password = "test12345";
// For simulator only
const char *ssid = "Wokwi-GUEST";
const char *password = "";
// Temps d'attente entre deux impulsions :
int delayTime = 3;
// Variables de commande : // Variables de commande :
struct command_t struct command_t
@ -174,7 +153,7 @@ void connectWiFi()
{ {
// For simulator only // For simulator only
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
if (WiFi.waitForConnectResult() != WL_CONNECTED) if (WiFi.waitForConnectResult() != WL_CONNECTED)
{ {
Serial.printf("WiFi Failed!\n"); Serial.printf("WiFi Failed!\n");
@ -186,7 +165,7 @@ void connectWiFi()
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
// WiFi.mode(WIFI_AP); // WiFi.mode(WIFI_AP);
// WiFi.softAP(ssid, password); // WiFi.softAP(WIFI_SSID, WIFI_PASSWORD);
// Serial.print("[+] AP Created with IP Gateway "); // Serial.print("[+] AP Created with IP Gateway ");
// Serial.println(WiFi.softAPIP()); // Serial.println(WiFi.softAPIP());
// Serial.println(""); // Serial.println("");
@ -227,49 +206,49 @@ void writeMotor(uint8_t left, uint8_t right)
void forward() void forward()
{ {
writeMotor(0b1000, 0b0001); writeMotor(0b1000, 0b0001);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0100, 0b0010); writeMotor(0b0100, 0b0010);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0010, 0b0100); writeMotor(0b0010, 0b0100);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0001, 0b1000); writeMotor(0b0001, 0b1000);
delay(delayTime); delay(DELAY_TIME);
} }
void backward() void backward()
{ {
writeMotor(0b0001, 0b1000); writeMotor(0b0001, 0b1000);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0010, 0b0100); writeMotor(0b0010, 0b0100);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0100, 0b0010); writeMotor(0b0100, 0b0010);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b1000, 0b0001); writeMotor(0b1000, 0b0001);
delay(delayTime); delay(DELAY_TIME);
} }
void right() void right()
{ {
writeMotor(0b1000, 0b1000); writeMotor(0b1000, 0b1000);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0100, 0b0100); writeMotor(0b0100, 0b0100);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0010, 0b0010); writeMotor(0b0010, 0b0010);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0001, 0b0001); writeMotor(0b0001, 0b0001);
delay(delayTime); delay(DELAY_TIME);
} }
void left() void left()
{ {
writeMotor(0b0001, 0b0001); writeMotor(0b0001, 0b0001);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0010, 0b0010); writeMotor(0b0010, 0b0010);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b0100, 0b0100); writeMotor(0b0100, 0b0100);
delay(delayTime); delay(DELAY_TIME);
writeMotor(0b1000, 0b1000); writeMotor(0b1000, 0b1000);
delay(delayTime); delay(DELAY_TIME);
} }
void stopMotors(void) void stopMotors(void)

25
server/src/config.h Normal file
View file

@ -0,0 +1,25 @@
/************************************
Fichier de configuration du robot
************************************/
// Définition des ports pour contrôler les moteurs
// Moteur gauche :
#define IN1a 27
#define IN2a 14
#define IN3a 12
#define IN4a 13
// Moteur droit :
#define IN1b 5
#define IN2b 18
#define IN3b 19
#define IN4b 21
// Paramètres de connexion wifi :
// #define WIFI_SSID "ESP32"
// #define WIFI_PASSWORD "test12345"
// For simulator only
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Temps d'attente entre deux impulsions :
#define DELAY_TIME 3