✅ Práctica 37
- ▷ #TSCLab #TCLab #ESP32 #Arduino #Control #MACI
- Repositories
- When using this resource, please cite the original publication:
En el siguiente blog se presenta la trigésima cuarta práctica del laboratorio de control de temperatura y velocidad de un motor.
Objetivo general:
- Realizar multitareascon los dos núcleos del ESP-32.
Materiales:
- TSC-Lab
Introducción:
El ESP-32 posee 2 microprocesadores Xtensa LX6 de 32 bits denominados núcleo 0 y núcleo 1. Por defecto, el código se ejecuta en el núcleo 1. Sin embargo, en la presente práctica de laboratorio se aprenderá a utilizar el doble núcleo del ESP-32 realizando una multitarea, trabajando con la planta de temperatura y velocidad del motor a la vez.
Procedimiento:
Nota: se asume que todas las librerías han sido previamente instaladas.
- Copiar y cargar el siguiente código al TSC-Lab.
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 34 ***************************** | |
This practice is about use of two core esp-32 | |
By: Kevin E. Chica O | |
More information: https://tsc-lab.blogspot.com/ | |
*/ | |
//temperature | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
// GPIO pin 0 is set as OneWire bus | |
OneWire ourWire1(0); | |
//GPIO pin 4 is set as OneWire bus | |
OneWire ourWire2(4); | |
//A variable or object is declared for our sensor 1 | |
DallasTemperature sensors1(&ourWire1); | |
//A variable or object is declared for our sensor 2 | |
DallasTemperature sensors2(&ourWire2); | |
//set parameters | |
int period_temp = 12; //medium period | |
int freq_temp = 2000; // sampling time | |
//motor | |
//initial setting for data acquisition | |
int dutyCycleInitial = 255; | |
int dutyCycleFinish = 0; | |
int period = 13000; | |
int cycles = 10; | |
//separador library | |
#include <Separador.h> | |
Separador s; | |
//motor | |
int motor1Pin1 = 33; | |
int motor1Pin2 = 25; | |
int enable1Pin = 32; | |
int motor_status = 255; | |
// Setting PWM properties | |
const int freq = 30000; | |
const int pwmChannel = 0; | |
const int resolution = 8; | |
//move | |
String move_motor = "counterclockwise"; | |
int encoder = 27; | |
void motor( void *pvParameters ); | |
void RPM( void *pvParameters ); | |
void readTemperature( void *pvParameters ); | |
volatile int counter = 0; | |
void interruption() // Function that runs during each interrupt | |
{ | |
counter++; | |
} | |
void setup() { | |
Serial.begin(115200); | |
//begin temperature sensor | |
sensors1.begin(); //Sensor 1 starts | |
sensors2.begin(); //Sensor 2 starts | |
// 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( | |
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 1 | |
xTaskCreatePinnedToCore( | |
readTemperature | |
, "rTemp" // 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) | |
, 0); // core 0 | |
} | |
void loop() { | |
} | |
void motor( void *pvParameters ) { | |
while (1) { | |
if (cycles > 0) { | |
digitalWrite(motor1Pin1, HIGH); | |
digitalWrite(motor1Pin2, LOW); | |
ledcWrite(pwmChannel, dutyCycleInitial); | |
motor_status = 255; | |
vTaskDelay(period); | |
digitalWrite(motor1Pin1, HIGH); | |
digitalWrite(motor1Pin2, LOW); | |
ledcWrite(pwmChannel, dutyCycleFinish); | |
motor_status = 0; | |
vTaskDelay(period); | |
cycles--; | |
} | |
} | |
} | |
void RPM( void *pvParameters ) { | |
while (1) { | |
vTaskDelay(999); | |
Serial.print(counter * 60); | |
Serial.print(","); | |
Serial.println(motor_status); | |
counter = 0; | |
} | |
} | |
void readTemperature( void *pvParameters ) { | |
while (1) { | |
uint32_t timer = period_temp * 60000L; | |
for ( uint32_t tStart = millis(); (millis() - tStart) < timer; ) { | |
//The command is sent to read the temperature | |
sensors1.requestTemperatures(); | |
//Obtain the temperature in ºC of sensor 1 | |
float temp1 = sensors1.getTempCByIndex(0); | |
//The command is sent to read the temperature | |
sensors2.requestTemperatures(); | |
//Obtain the temperature in ºC of sensor 2 | |
float temp2 = sensors2.getTempCByIndex(0); | |
//print to display the temperature change | |
Serial.println(); | |
Serial.print(temp1); | |
Serial.print(","); | |
Serial.println(temp2); | |
delay(freq_temp); | |
} | |
} | |
} |
Repositories: https://github.com/vasanza/TSC-Lab/tree/main/Practice34
En el monitor serial se verá algo parecido a esto:
Comments
Post a Comment