Hello friends, I have a problem with an ESP32, I have two model Metal Gearbox Para Smart Car Robot(130RPM) motors connected to an ESP32 and a raspberry pi that acts as an on-board computer. As a system requirement, Bluetooth communication must be used between both systems.
The problem is that when I have the ESP32 without connecting to the motors it receives and executes the orders (seen by the console it executes the Serial.println) but if I connect the ESP32 to the motor control board it starts to have a very significant delay, in On certain occasions it does carry out the orders on time (assuming the delay of Bluetooth technology) but sometimes it gets stuck on one of the sent orders.
If someone can give me a hand and if they have any idea how to solve the error. If I'm not wrong, I think the fault is in the motor control.
NOTE: the ESP32 program uses multitasking because it was one of the solutions I tried, but it didn't work
#include <Arduino.h>
#include <BluetoothSerial.h> //libreria Bluetooth
#include <TaskScheduler.h> //libreria del planificador
//***********************************
// INICIALIZACION BLUETOOTH
//***********************************
BluetoothSerial SerialBT;
String comando; //comando con la instrucción del robot
//***********************************
// DEFICION DE FUNCIONES
//***********************************
void recibe_comandos();
void mueve_motores();
//***********************************
// INICIAR MULTITAREA
//***********************************
Scheduler planificador; //definicion del planificador
//Task [nombre_tarea]([tiempo mseg],[veces ejecuta],[funcion ejecuta],[nombre planificador],[bol]);
Task comandoBT(500,TASK_FOREVER,&recibe_comandos,&planificador,true);
Task motores(500,TASK_FOREVER,&mueve_motores,&planificador,true);
//***********************************
// TAREAS PRINCIPALES
//***********************************
void recibe_comandos(){ //funcion que recibe comandos por Bluetooth desde la Raspberry Pi
if (SerialBT.available()) {
//serial.isempty
//While me llegue algo lo meto en una variable
//fuera del while son todos if
comando = SerialBT.readString();
Serial.println(comando);
}//if (SerialBT.available())
}//recibe_comandos()
void mueve_motores(){ //funcion que realiza movimiento de los motores
// switch (comando) {
// case 'f':
// moving_fordward();
// Serial.println("Adelante");
// delay(10);
// break;
// case 'b':
// moving_backward();
// Serial.println("Atras");
// delay(10);
// break;
// case 's':
// stop_motors();
// Serial.println("Stop");
// delay(10);
// break;
// case 'r':
// move_right();
// Serial.println("Stop");
// delay(10);
// break;
// case 'l':
// move_left();
// Serial.println("Izquierda");
// delay(10);
// break;
// }//switch (comando)
if (comando == "f"){ //no necesito el \n
moving_fordward();
Serial.println("Adelante");
//delay(10);
} else if (comando == "b"){
moving_backward();
//Serial.println("Atras");
delay(10);
} else if (comando == "s"){
stop_motors();
Serial.println("Stop");
//delay(10);
} else if (comando == "r"){
move_right();
Serial.println("Derecha");
//delay(10);
} else if (comando == "l"){
move_left();
Serial.println("Izquierda");
//delay(10);
}else{
Serial.println('.');
}//if-else - movimiento
}//mueve_motores()
// ********CONTROL DE MOTORES**********
// Motor1 - Derecha
int motorDchaPin1 = 18;
int motorDchaPin2 = 19;
int enableMotorDcha = 5;
// Motor2 - Izquierda
int motorIzqPin1 = 27;
int motorIzqPin2 = 26;
int enableMotorIZq = 14;
int speedmotors = 200; //velocidad de los motores (0-255)
void setup() {
Serial.begin(115200);
//Inicializacion de pines de salida:
pinMode(motorDchaPin1, OUTPUT);
pinMode(motorDchaPin2, OUTPUT);
pinMode(enableMotorDcha, OUTPUT);
pinMode(motorIzqPin1, OUTPUT);
pinMode(motorIzqPin2, OUTPUT);
pinMode(enableMotorIZq, OUTPUT);
//inicializacion de motores
analogWrite(enableMotorDcha, speedmotors); //establezco la velocidad inicial - motorDcha
analogWrite(enableMotorIZq, speedmotors); //establezco la velocidad inicial - motorIzq
SerialBT.begin("ESP32"); // Inicializa el módulo Bluetooth con el nombre "ESP32"
}//setup
// ********************************
// FUNCIONES DE CONTROL DE MOTORES
// ********************************
void loop() {
//ejecucion del planificador
planificador.execute();
}//loop
// ************************************
// FUNCIONES DE CONTROL DE MOTORES
// ************************************
void moving_fordward(){
digitalWrite(motorDchaPin1, LOW);
digitalWrite(motorDchaPin2, HIGH);
digitalWrite(motorIzqPin1, LOW);
digitalWrite(motorIzqPin2, HIGH);
analogWrite(enableMotorDcha, speedmotors); //establezco la velocidad motorDcha
analogWrite(enableMotorIZq, speedmotors); //establezco la velocidad motorIzq
}//moving_fordward()
void moving_backward(){
digitalWrite(motorDchaPin1, HIGH);
digitalWrite(motorDchaPin2, LOW);
digitalWrite(motorIzqPin1, HIGH);
digitalWrite(motorIzqPin2, LOW);
analogWrite(enableMotorDcha, speedmotors); //establezco la velocidad motorDcha
analogWrite(enableMotorIZq, speedmotors); //establezco la velocidad motorIzq
}//moving_backward()
void stop_motors(){
digitalWrite(motorDchaPin1, LOW);
digitalWrite(motorDchaPin2, LOW);
digitalWrite(motorIzqPin1, LOW);
digitalWrite(motorIzqPin2, LOW);
//NOTA: no necesito establecer velocidad a los motores
}//stop_motors()
void move_right(){
digitalWrite(motorDchaPin1, LOW);
digitalWrite(motorDchaPin2, HIGH);
digitalWrite(motorIzqPin1, HIGH);
digitalWrite(motorIzqPin2, LOW);
// Gira el motor izquierdo a una velocidad mayor que el motor derecho
analogWrite(enableMotorDcha, speedmotors/2); //establezco la velocidad motorDcha
analogWrite(enableMotorIZq, speedmotors/4); //establezco la velocidad motorIzq
}//move_rigth()
void move_left(){
digitalWrite(motorDchaPin1, HIGH);
digitalWrite(motorDchaPin2, LOW);
digitalWrite(motorIzqPin1, LOW);
digitalWrite(motorIzqPin2, HIGH);
// El motor que gira hacia atras lo hace a una velocidad menor
analogWrite(enableMotorDcha, speedmotors/4); //establezco la velocidad motorDcha
analogWrite(enableMotorIZq, speedmotors/2); //establezco la velocidad motorIzq
}//move_left()
