refactor(server): ♻️ deduplicate code, split logic into headers for readability and add some doc

This commit is contained in:
Julien Oculi 2024-06-26 12:48:07 +02:00
parent 45b7f115a7
commit 1e704fa356
4 changed files with 126 additions and 292 deletions

View file

@ -1,19 +1,12 @@
#include "config.h"
// Libraries locales pour configurer et contrôler le robot.
#include "config.h" // Configuration des entrées/sorties et delais.
#include "controls.h" // Fonctions pour se déplacer.
#include "convert.h" // Utilitaires pour convertir des mesures en steps.
// Code exécuté au démarrage (paramétrage) :
void setup()
{
// On indique que les ports de controle des moteurs sont des sorties (output)
pinMode(IN1a, OUTPUT);
pinMode(IN2a, OUTPUT);
pinMode(IN3a, OUTPUT);
pinMode(IN4a, OUTPUT);
pinMode(IN1b, OUTPUT);
pinMode(IN2b, OUTPUT);
pinMode(IN3b, OUTPUT);
pinMode(IN4b, OUTPUT);
setupPins(); // Configure les pins de l'ESP32
Serial.begin(115200); // Démarrage d'une communication Série avec l'ordinateur s'il est connecté
}
@ -30,191 +23,4 @@ void loop()
{
left();
}
}
// Fonction pour convertir une longueur en cm vers un nombre de pas moteur.
int convertLengthToSteps(float length)
{
float result = length * 512 / (4 * 3.1415);
return int(result);
}
// 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);
return result;
}
void forward()
{
digitalWrite(IN4a, HIGH);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, HIGH);
digitalWrite(IN4b, HIGH);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
}
void backward()
{
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, HIGH);
digitalWrite(IN4b, HIGH);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, HIGH);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH);
delay(DELAY_TIME);
}
void right()
{
digitalWrite(IN4a, HIGH);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, HIGH);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, HIGH);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH);
delay(DELAY_TIME);
}
void left()
{
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, HIGH);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, HIGH);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, HIGH);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, HIGH);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, HIGH);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, HIGH);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
digitalWrite(IN4a, HIGH);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, HIGH);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
delay(DELAY_TIME);
}
void stopMotors(void)
{
digitalWrite(IN4a, LOW);
digitalWrite(IN3a, LOW);
digitalWrite(IN2a, LOW);
digitalWrite(IN1a, LOW);
digitalWrite(IN4b, LOW);
digitalWrite(IN3b, LOW);
digitalWrite(IN2b, LOW);
digitalWrite(IN1b, LOW);
}
}

View file

@ -2,7 +2,11 @@
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <WiFi.h>
#include "config.h"
// Libraries locales pour configurer et contrôler le robot.
#include "config.h" // Configuration des entrées/sorties et delais.
#include "controls.h" // Fonctions pour se déplacer.
#include "convert.h" // Utilitaires pour convertir des mesures en steps.
// Variables de commande :
struct command_t
@ -19,17 +23,7 @@ AsyncWebServer server(80);
// Code exécuté au démarrage (paramétrage) :
void setup()
{
// On indique que les ports de contrôle des moteurs sont des sorties (output)
pinMode(IN1a, OUTPUT);
pinMode(IN2a, OUTPUT);
pinMode(IN3a, OUTPUT);
pinMode(IN4a, OUTPUT);
pinMode(IN1b, OUTPUT);
pinMode(IN2b, OUTPUT);
pinMode(IN3b, OUTPUT);
pinMode(IN4b, OUTPUT);
setupPins(); // Configure les pins de l'ESP32
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
@ -100,20 +94,6 @@ void parseRequestParams(AsyncWebServerRequest *request, String &commandName, flo
requestCheck(commandName, commandValue);
}
// Fonction pour convertir une longueur en cm vers un nombre de pas moteur.
int convertLengthToSteps(float length)
{
float result = length * 512 / (4 * 3.1415);
return int(result);
}
// 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);
return result;
}
// Lorsqu'une commande arrive au serveur, cette fonction permet
void requestCheck(String commandName, float commandValue)
{
@ -189,69 +169,4 @@ void chooseCommand()
{
backward();
}
}
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);
}
void forward()
{
writeMotor(0b1000, 0b0001);
delay(DELAY_TIME);
writeMotor(0b0100, 0b0010);
delay(DELAY_TIME);
writeMotor(0b0010, 0b0100);
delay(DELAY_TIME);
writeMotor(0b0001, 0b1000);
delay(DELAY_TIME);
}
void backward()
{
writeMotor(0b0001, 0b1000);
delay(DELAY_TIME);
writeMotor(0b0010, 0b0100);
delay(DELAY_TIME);
writeMotor(0b0100, 0b0010);
delay(DELAY_TIME);
writeMotor(0b1000, 0b0001);
delay(DELAY_TIME);
}
void right()
{
writeMotor(0b1000, 0b1000);
delay(DELAY_TIME);
writeMotor(0b0100, 0b0100);
delay(DELAY_TIME);
writeMotor(0b0010, 0b0010);
delay(DELAY_TIME);
writeMotor(0b0001, 0b0001);
delay(DELAY_TIME);
}
void left()
{
writeMotor(0b0001, 0b0001);
delay(DELAY_TIME);
writeMotor(0b0010, 0b0010);
delay(DELAY_TIME);
writeMotor(0b0100, 0b0100);
delay(DELAY_TIME);
writeMotor(0b1000, 0b1000);
delay(DELAY_TIME);
}
void stopMotors(void)
{
writeMotor(0b0000, 0b0000);
}
}

90
server/src/controls.h Normal file
View file

@ -0,0 +1,90 @@
/*******************************************
Initialisation et fonctions de déplacement
*******************************************/
// Configure les ports de contrôle des moteurs sont des sorties (output)
void setupPins()
{
pinMode(IN1a, OUTPUT);
pinMode(IN2a, OUTPUT);
pinMode(IN3a, OUTPUT);
pinMode(IN4a, OUTPUT);
pinMode(IN1b, OUTPUT);
pinMode(IN2b, OUTPUT);
pinMode(IN3b, OUTPUT);
pinMode(IN4b, OUTPUT);
}
// Applique une configuration aux moteurs.
// Chaque configuration est sur 1 octet.
// ex: writeMotor(0b0010, 0b1100);
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);
}
// Commande pour faire avancer le robot.
void forward()
{
writeMotor(0b1000, 0b0001);
delay(DELAY_TIME);
writeMotor(0b0100, 0b0010);
delay(DELAY_TIME);
writeMotor(0b0010, 0b0100);
delay(DELAY_TIME);
writeMotor(0b0001, 0b1000);
delay(DELAY_TIME);
}
// Commande pour faire reculer le robot.
void backward()
{
writeMotor(0b0001, 0b1000);
delay(DELAY_TIME);
writeMotor(0b0010, 0b0100);
delay(DELAY_TIME);
writeMotor(0b0100, 0b0010);
delay(DELAY_TIME);
writeMotor(0b1000, 0b0001);
delay(DELAY_TIME);
}
// Commande pour faire tourner le robot à droite.
void right()
{
writeMotor(0b1000, 0b1000);
delay(DELAY_TIME);
writeMotor(0b0100, 0b0100);
delay(DELAY_TIME);
writeMotor(0b0010, 0b0010);
delay(DELAY_TIME);
writeMotor(0b0001, 0b0001);
delay(DELAY_TIME);
}
// Commande pour faire tourner le robot à gauche.
void left()
{
writeMotor(0b0001, 0b0001);
delay(DELAY_TIME);
writeMotor(0b0010, 0b0010);
delay(DELAY_TIME);
writeMotor(0b0100, 0b0100);
delay(DELAY_TIME);
writeMotor(0b1000, 0b1000);
delay(DELAY_TIME);
}
// Commande pour arrêter les moteurs du robot.
void stopMotors(void)
{
writeMotor(0b0000, 0b0000);
}

23
server/src/convert.h Normal file
View file

@ -0,0 +1,23 @@
/*********************************************
Utilitaires de conversions physique -> steps
*********************************************/
// Convertit une longueur en cm vers un nombre de pas moteur.
// ex:
// float distance = 22.3; //cm
// int steps = convertLengthToSteps(distance);
int convertLengthToSteps(float length)
{
float result = length * 512 / (4 * 3.1415);
return int(result);
}
// Convertit une rotation en degrés vers un nombre de pas moteur.
// ex:
// float angle = 45; //deg
// int steps = convertRotToSteps(angle);
int convertRotToSteps(int rotation)
{
int result = convertLengthToSteps(rotation * 3.1415 / 180 * 7.8);
return result;
}