96 lines
2.8 KiB
C++
96 lines
2.8 KiB
C++
/*
|
|
Name: ESP_Serial.ino
|
|
Author: Zendion
|
|
Description: Controla o motor de passo de acordo com a entrada do usuário via Serial.
|
|
Exemplo de protocolo: 360;0;300;1;1
|
|
A configuração do protocolo é: Angulo, em três dígitos ; Sentido (0 para Horário, 1 para Antihorário) ; RPM, em três dígitos ; Delay antes de executar o comando, em segundos ; Delay após executar o comando, em segundos
|
|
*/
|
|
|
|
#include<zendion_stepper.h>
|
|
|
|
TipoPasso TipoDePasso = PassoCheio;
|
|
#define PinoENA 12
|
|
#define PinoDIR 14
|
|
#define PinoSTP 27
|
|
const int PPR = 12800;
|
|
|
|
// MP1( ENA, DIR, STP )
|
|
Z_STEPPER MP1(PinoENA, PinoDIR, PinoSTP);
|
|
|
|
void ReceiverTask(void *pvParameters);
|
|
TaskHandle_t ReceiverTaskHandle = NULL;
|
|
|
|
String Protocolo = "";
|
|
int TamanhoProtocolo = 12;
|
|
|
|
void setup() {
|
|
// Inicia a conexão Serial
|
|
Serial.begin(115200);
|
|
|
|
// MP1.Configure( RPM, TIPO DE PASSO, PASSOS POR REVOLUCAO )
|
|
MP1.Configure(300, (int)TipoDePasso, PPR);
|
|
// MP1.Init( Debug )
|
|
MP1.Init(false);
|
|
|
|
// Cria a tarefa verificadora do recebimento de dados pela porta serial
|
|
xTaskCreatePinnedToCore(ReceiverTask, "ReceiverTask", 4000, NULL, 24, &ReceiverTaskHandle, 0);
|
|
}
|
|
|
|
void ReceiverTask(void *pvParameters) {
|
|
while (1)
|
|
{
|
|
if (Serial.available() > TamanhoProtocolo)
|
|
VerificaSerial();
|
|
|
|
vTaskDelay(1);
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
MP1.Loop();
|
|
}
|
|
|
|
void VerificaSerial() {
|
|
// ENQUANTO HOUVER DADOS PARA SEREM LIDOS NO BUFFER DE ENTRADA -- 360;0;300;1;1
|
|
Protocolo = "";
|
|
while (Serial.available()) {
|
|
char Com = (char)Serial.read();
|
|
Protocolo += Com;
|
|
}
|
|
|
|
Serial.println(Protocolo);
|
|
|
|
int Angulo = ((String)Protocolo[0] + (String)Protocolo[1] + (String)Protocolo[2]).toInt();
|
|
Sentido _Sentido = (Sentido)(((String)Protocolo[4]).toInt());
|
|
int RPM = ((String)Protocolo[6] + (String)Protocolo[7] + (String)Protocolo[8]).toInt();
|
|
int DelayAntes = ((String)Protocolo[10]).toInt() * 1000;
|
|
int DelayDepois = ((String)Protocolo[12]).toInt() * 1000;
|
|
|
|
Serial.print("Angulo ");
|
|
Serial.print(Angulo);
|
|
Serial.print("º no sentido ");
|
|
Serial.print((String)_Sentido);
|
|
Serial.print(" com RMP de ");
|
|
Serial.print(RPM);
|
|
Serial.print(", com DelayAntes = ");
|
|
Serial.print(DelayAntes);
|
|
Serial.print(" e DelayDepois = ");
|
|
Serial.println(DelayDepois);
|
|
|
|
AcionaMotor(Angulo, _Sentido, RPM, DelayAntes, DelayDepois, false);
|
|
|
|
LimparSerial();
|
|
}
|
|
|
|
void AcionaMotor(int Angulo, Sentido _Sentido, int RPM, int DelayAntes, int DelayDepois, bool Forcar) {
|
|
MP1.Configure(RPM, (int)TipoDePasso, PPR);
|
|
// MP1.Set ( ANGULO, SENTIDO, DELAY ANTES DE ATUAR O MOTOR, DELAY APOS ATUAR O MOTOR, FORCAR NOVA EXECUCAO DO COMANDO );
|
|
MP1.Set(Angulo, _Sentido, DelayAntes, DelayDepois, Forcar);
|
|
}
|
|
|
|
void LimparSerial() {
|
|
while (Serial.available()) {
|
|
char Com = (char)Serial.read();
|
|
Protocolo += Com;
|
|
}
|
|
} |