En el siguiente blog se presenta la octava práctica y corresponde a la tercera del laboratorio de control de velocidad.
Objetivo general:
- Controlar el funcionamiento del motor DC con PWM para aumentar o bajar la velocidad y su giro usando comandos.
Objetivos específicos:
- Comparar los resultados de movimiento con diferentes parámetros.
Materiales:
- PCB de Temperature Control Lab (TSC-Lab)
Introducción:
Una vez entendido que enviar señales de High y Low al motor lo puede poner en marcha y que PWM es posible ver el aumento y disminución del mismo. Ahora, en esta práctica con los conocimientos adquiridos de las prácticas anteriores, mediante comandos ingresados al monitor serie se controlará la velocidad y sentido de giro del motor.
Procedimiento:
Se asume que la placa del ESP-32 ha sido previamente instalada en el IDE de Arduino. Además en esta práctica se utiliza la librería separador, la cual se puede descargar aquí.
Importante: se realizó una pequeña variación con respecto al código anterior en la parte del setup(), concretamente al llamar las tareas, se cambió el método xTaskCreate( ... ) por xTaskCreatePinnedToCore ( ... ), ya que al usar tareas más complejas se generaba un conclifcto entre los dos núcleos del ESP-32, generando auto-resets inesperados, por lo que con el último método se especifica que las tareas se realicen en el núcleo 1.
- Copiar el código en el IDE de Arduino:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
****************************** TSC-Lab ******************************* | |
***************************** PRACTICE 8 ***************************** | |
This practice is about motor direction control and Speed control | |
By: Kevin E. Chica O | |
More information: https://tsc-lab.blogspot.com/ | |
*/ | |
//separador library | |
#include <Separador.h> | |
Separador s; | |
//motor | |
int motor1Pin1 = 33; | |
int motor1Pin2 = 25; | |
int enable1Pin = 32; | |
// Setting PWM properties | |
const int freq = 30000; | |
const int pwmChannel = 0; | |
const int resolution = 8; | |
int dutyCycle = 0; | |
//move | |
String move_motor = "counterclockwise"; | |
void motor( void *pvParameters ); | |
void comands( void *pvParameters ); | |
void setup() { | |
Serial.begin(115200); | |
// sets the pins as outputs: | |
pinMode(motor1Pin1, OUTPUT); | |
pinMode(motor1Pin2, OUTPUT); | |
pinMode(enable1Pin, OUTPUT); | |
// configure LED PWM functionalitites | |
ledcSetup(pwmChannel, freq, resolution); | |
// attach the channel to the GPIO to be controlled | |
ledcAttachPin(enable1Pin, pwmChannel); | |
xTaskCreatePinnedToCore( | |
motor | |
, "MotorDC" // Descriptive name of the function (MAX 8 characters) | |
, 2048 // Size required in STACK memory | |
, NULL // INITIAL parameter to receive (void *) | |
, 1 // Priority, priority = 3 (configMAX_PRIORITIES - 1) is the highest, priority = 0 is the lowest. | |
, NULL // Variable that points to the task (optional) | |
, 1); // core 1 | |
xTaskCreatePinnedToCore( | |
comands | |
, "Comands" // Descriptive name of the function (MAX 8 characters) | |
, 2048 // Size required in STACK memory | |
, NULL // INITIAL parameter to receive (void *) | |
, 1 // Priority, priority = 3 (configMAX_PRIORITIES - 1) is the highest, priority = 0 is the lowest. | |
, NULL // Variable that points to the task (optional) | |
, 1); //core 1 | |
} | |
void loop() { | |
} | |
void motor( void *pvParameters ) { | |
while (1) { | |
if (move_motor == "clockwise") { | |
// Move the DC motor forward at maximum speed | |
digitalWrite(motor1Pin1, LOW); | |
digitalWrite(motor1Pin2, HIGH); | |
ledcWrite(pwmChannel, dutyCycle); | |
} else if (move_motor == "counterclockwise") { | |
// Stop the DC motor | |
digitalWrite(motor1Pin1, HIGH); | |
digitalWrite(motor1Pin2, LOW); | |
ledcWrite(pwmChannel, dutyCycle); | |
} | |
} | |
} | |
void comands(void *pvParameters) { | |
while (1) { | |
if (Serial.available()) { | |
String string = Serial.readStringUntil('\n'); | |
String pwm = s.separa(string, '_', 0); | |
if (string == "clockwise") { | |
Serial.println("Motor move on clockwise"); | |
move_motor = "clockwise"; | |
} else if (string == "counterclockwise") { | |
Serial.println("Motor move on counterclockwise"); | |
move_motor = "counterclockwise"; | |
} | |
else if (pwm == "pwm") { | |
dutyCycle = s.separa(string, '_', 1).toInt(); | |
} | |
} | |
} | |
} |
Repositories: https://github.com/vasanza/TSC-Lab/tree/main/Practice8
- Cargar el código a la placa.
- Observar el movimiento del motor.
Nota: el código de programación por defecto viene con el motor detenido, para observar el movimiento se deberá cambiar el valor del duty cycle, también mediante es posible cambiar el sentido de giro. Todo esto se lo realiza con los siguientes comandos:
- Para cambiar el duty cycle se debe enviar el comando: "pwm_X", donde "X" es el valor PWM que va de 0 a 255.
- Para hacer girar el motor en sentido horario, se debe enviar el comando: "clockwise".
- Para hacer girar el motor en sentido antihorario, se debe enviar el comando: "counterclockwise"
OJO: los comandos van sin las comillas.
Comments
Post a Comment