✅ Práctica 9
▷ #TSCLab #TCLab #ESP32 #Arduino #Control #MACI
En el siguiente blog se presenta la novena práctica y corresponde a la cuarta del laboratorio de control de velocidad.
Objetivo general:
- Visualizar las RPM del motor en base a la PWM asignada.
Objetivos específicos:
- Comparar los resultados de movimiento con diferentes parámetros.
Materiales:
- PCB de Temperature Control Lab (TSC-Lab)
Introducción:
En la práctica anterior se puso en marcha en motor asignándole un valor PWM, además brindaba la opción de escoger el sentido de gira. Ahora, en el presente laboartorio se implementa un encoder óptico, que mediante el código de programación en el IDE de Arduino, permite saber a cuantas revoluciones por minuto gira el motor. Es importante mencionar que en esta ocasión se hará uso de interrupciones, cualquier pin GPIO del ESP-32 puede ser utilizado con interrupción, en este caso se ha escogido el 27, pin en el cual está conectado el encoder óptico.
Procedimiento:
Se asume que la placa del ESP-32 ha sido previamente instalada en el IDE de Arduino.
- 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 9 ***************************** | |
This practice is about encoder Implementation (RPM) | |
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"; | |
int encoder = 27; | |
void motor( void *pvParameters ); | |
void comands( void *pvParameters ); | |
//void enviar( void *pvParameters ); | |
void RPM( void *pvParameters ); | |
volatile int counter = 0; | |
void interruption() // Function that runs during each interrupt | |
{ | |
counter++; | |
} | |
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); | |
attachInterrupt(encoder, interruption, RISING); | |
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 | |
xTaskCreatePinnedToCore( | |
RPM | |
, "RPM" // 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 0 | |
} | |
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(); | |
} | |
} | |
} | |
} | |
void RPM( void *pvParameters ) { | |
while (1) { | |
vTaskDelay(999); | |
Serial.print(counter*60); | |
Serial.println(" RPM"); | |
counter = 0; | |
} | |
} |
Repositories: https://github.com/vasanza/TSC-Lab/tree/main/Practice9
- Cargar el código a la placa.
- Observar el movimiento del motor.
Nota: los comandos son los mismos que en la práctica 8, la diferencia radica en que ahora se mostrará a cuantas revoluciones gira el motor.
Comments
Post a Comment