▷ Practice 37: Using two core of ESP-32
⭐⭐⭐⭐⭐ Practice 37: Using two core of ESP-32
- ➡️ #TSCLab #TCLab #ESP32 #Arduino #Control #PWM #PID #Matlab #RPM #HTTP #MQTT #OTA #Telegram #FireBase #NodeRed
- ✅ Source TSC-LAB: www.tsc-lab.blogspot.com
- ✅Github Repositories
- ⭐When using this resource, please cite the original publication:
General objective:
- Multitask with the two cores of the ESP32.
Materials:
- TSC-Lab
Introduction:
The ESP-32 has 2 32-bit Xtensa LX6 microprocessors called core 0 and core 1. By default, the code runs on core 1. However, in this laboratory practice, you will learn how to utilize the dual-core of the ESP-32 by performing multitasking, working with both the temperature plant and the motor speed simultaneously.
Steps:
Note: It is assumed that all libraries have been previously installed.
1- Copy and upload the following code to the 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); | |
} | |
} | |
} |
⭐ Practice repository: https://github.com/vasanza/TSC-Lab/tree/main/Practice34
2- In the serial monitor, you will see something similar to this:
Read related topics
- ✅ USB DATA ACQUISITION - OPEN LOOP (Temperature Control Lab)
- Initial setups and tests
- Ambient temperature reading using sensor 1 and 2
- Activation of Transistor 1 and Reading of temperature sensor 1 and 2
- Activation of Transistor 2 and Reading of temperature sensor 1 and 2
- Activation of Transistor 1 and 2, also Reading of temperature sensor 1 and 2
- ✅ USB DATA ACQUISITION - OPEN LOOP (Speed Control Lab)
- Initial setups and tests (ON/OFF)
- Speed control using PWM
- Motor direction control and Speed control
- Encoder Implementation (RPM)
- Data acquisition with square velocity input
- ✅ MATLAB DATA ACQUISITION (USB)
- ✅ SYSTEM IDENTIFICATION (Matlab)
- ✅ SISO PID CONTROLLER DESIGN (Matlab)
- ✅ CLOSED-LOOP WITH CONTROLLER IN MATLAB (*)
- ✅ IOT PLATFORMS (*)
- Wifi connection
- ThingSpeak (HTTP)
- FireBase (HTTP)
- Firebase data collection (HTTP)
- Over-The-Air programming (OTA)
- NodeRed (USB)
- NodeRed (Wifi)
- NodeRed (HTTP) + Telegram
- NodeRed (HTTP) + ThingSpeak
- ✅ MQTT DATA ACQUISITION
- ✅ CLOSED-LOOP WITH CONTROLLER IN ESP32
- ✅ MORE POWERFUL APPLICATIONS
Comentarios
Publicar un comentario