Skip to content

Commit

Permalink
Correção em carregar programa e ajuste da interação InterpretadorG-Co…
Browse files Browse the repository at this point in the history
…ntrolador
  • Loading branch information
Henrique-BO committed Jun 25, 2022
1 parent 90bda3a commit a4bdb9c
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 88 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

include/credenciais.h
14 changes: 11 additions & 3 deletions data/gcode.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
G01 X100.00 Y0 Z0 F1000
G01 X250.00 Y0 Z0 F1000
G01 X200.00 Y0 Z0 F1000
G00 X0 Y0 Z0 F1000
G00 X5 Y0 Z0

G01 X5 Y0 Z-30
G01 X5 Y-5 Z-30
G01 X50 Y-5 Z-30
G01 X50 Y-50 Z-30
G01 X5 Y-5 Z-30

G01 X5 Y-5 Z0
G00 X0 Y0 Z0
10 changes: 6 additions & 4 deletions include/controlador.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

#include "AccelStepper.h"
#include "sensor_curso.h"
#include "GCodeParser.h"

// Velocidade máxima de deslocamento
#define MAX_SPEED 30000.0

// Resolução de passo de cada eixo
#define STEPS_POR_MM_X 4.750893824 // 2pi/200*R
#define STEPS_POR_MM_Y 4.486397268
#define STEPS_POR_MM_Z 1.0
#define Z_CANETA_BAIXA -30

// Margem de caibração no eixo X
// Margem de caibração no eixo X (em steps)
#define MARGEM_X 10

// Período da Task do controlador
Expand All @@ -25,7 +26,8 @@ class Controlador {
public:
Controlador(int pinStepX, int pinDirX, int pinStepY, int pinDirY, int pinStepZ, int pinDirZ);
void iniciarControlador();
void enviarComando(int G, float X, float Y, float Z);
// void enviarComando(int G, float X, float Y, float Z);
void enviarComando(GCodeParser *pGCode);
void calibrar();
void cancelar();
void origem();
Expand All @@ -37,7 +39,7 @@ class Controlador {
AccelStepper *pStepperZ;

// velocidade de deslocamento
float speed = 5000.0;
float speed = 50.0; // TODO calcular velocidade efetiva

// flags de controle
bool calibrando = false;
Expand Down
2 changes: 2 additions & 0 deletions include/interface_wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class InterfaceWiFi {
static String processor(const String& var);
static void handleEstado(AsyncWebServerRequest *request);

bool carregando = false;

private:
AsyncWebServer server;
};
Expand Down
25 changes: 25 additions & 0 deletions include/maquina_estados.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ typedef enum Acao {
A05
} Acao;

const String estados_str[] = {
"Idle",
"Imprimindo",
"Calibrando"
};

const String eventos_str[] = {
"Nenhum evento",
"Calibrar",
"Imprimir",
"Cancelar",
"Terminado",
"Origem",
"Carregar"
};

const String acoes_str[] = {
"Nenhuma ação",
"A01",
"A02",
"A03",
"A04",
"A05"
};

/**
* @brief Struct que armazena o próximo estado e a ação a ser realizada
*
Expand Down
42 changes: 32 additions & 10 deletions src/controlador.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Controlador::Controlador(int pinStepX, int pinDirX, int pinStepY, int pinDirY, i
pStepperX->setMaxSpeed(MAX_SPEED);
pStepperY->setMaxSpeed(MAX_SPEED);
pStepperZ->setMaxSpeed(MAX_SPEED);

pStepperY->setPinsInverted(true, false, false);
}

void Controlador::iniciarControlador()
Expand All @@ -34,11 +36,31 @@ void Controlador::iniciarControlador()
);
}

void Controlador::enviarComando(int G, float X, float Y, float Z)
// void Controlador::enviarComando(int G, float X, float Y, float Z)
void Controlador::enviarComando(GCodeParser *pGCode)
{
int stepsX = (int) round(X * STEPS_POR_MM_X);
int stepsY = (int) round(Y * STEPS_POR_MM_Y);
int stepsZ = (int) round(Z * STEPS_POR_MM_Z);
static int G = -1;
static int stepsX, stepsY, stepsZ;

if (pGCode->HasWord('G')) {
G = pGCode->GetWordValue('G');
}
if (pGCode->HasWord('X')) {
int X = pGCode->GetWordValue('X');
stepsX = (int) round(X * STEPS_POR_MM_X);
}
if (pGCode->HasWord('Y')) {
int Y = pGCode->GetWordValue('Y');
stepsY = (int) round(Y * STEPS_POR_MM_Y);
}
if (pGCode->HasWord('Z')) {
int Z = pGCode->GetWordValue('Z');
if (Z >= 0) {
stepsZ = 0;
} else {
stepsZ = Z_CANETA_BAIXA;
}
}

Serial.printf("[Controlador] G%d stepsX=%d stepsY=%d stepsZ=%d\n", G, stepsX, stepsY, stepsZ);

Expand All @@ -57,7 +79,7 @@ void Controlador::enviarComando(int G, float X, float Y, float Z)
pStepperY->moveTo(stepsY);
pStepperZ->moveTo(stepsZ);

// Velocidades para trajetoria reta XY
// Velocidades para trajetoria reta no plano XY
float dist = sqrt(pow(stepsX - pStepperX->currentPosition(),2) +
pow(stepsY - pStepperY->currentPosition(),2)
);
Expand All @@ -68,6 +90,7 @@ void Controlador::enviarComando(int G, float X, float Y, float Z)
pStepperY->setSpeed(speedY);
pStepperZ->setSpeed(speed);
}
// TODO arcos

mover = true;
}
Expand All @@ -81,18 +104,19 @@ void Controlador::calibrar()

Serial.println("[Controlador] Iniciando calibração");
pStepperX->setSpeed(-speed);
pStepperY->setSpeed(-speed);
pStepperZ->setSpeed(-speed);
}

void Controlador::cancelar()
{
// TODO levantar caneta
movendo = false;
flag_cancelar = true;
Serial.println("[Controlador] Cancelando movimento");
}

void Controlador::taskControlar()
{
// TODO quando terminar jogar o papel pra fora
xSemaphoreGive(xSemaphoreControlador);

TickType_t xLastWakeTime;
Expand All @@ -119,8 +143,8 @@ void Controlador::taskControlar()
if (xSemaphoreGive(xSemaphoreControlador) != pdTRUE) {
Serial.println("[Controlador] Falha ao ceder semáforo");
}
Serial.println("[Controlador] Movimento cancelado");
flag_cancelar = false;
Serial.println("[Controlador] Movimento cancelado");
}
if (movendo) {
if ((pStepperX->distanceToGo() != 0) || (pStepperY->distanceToGo() != 0) || (pStepperZ->distanceToGo() != 0)) {
Expand All @@ -146,8 +170,6 @@ void Controlador::taskControlar()
calibrando = false;
}
pStepperX->runSpeed();
pStepperY->runSpeed();
pStepperZ->runSpeed();
}
// Delay para obter frequência constante
vTaskDelayUntil(&xLastWakeTime, xFrequency);
Expand Down
74 changes: 43 additions & 31 deletions src/interface_wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ void InterfaceWiFi::iniciarWiFi()

// Conectar no WiFI
WiFi.begin(SSID, PASSWORD);
Serial.print("[InterfaceWiFi] Conectando no WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("[InterfaceWiFi] Conectando no WiFi...");
Serial.print(".");
}
Serial.print("[InterfaceWiFi] Conectado no IP: ");
Serial.print("\n[InterfaceWiFi] Conectado no IP: ");
Serial.println(WiFi.localIP());

// Página principal
Expand Down Expand Up @@ -66,50 +67,61 @@ void InterfaceWiFi::iniciarWiFi()
void InterfaceWiFi::imprimir()
{
Evento evento = IMPRIMIR;
while(xQueueSendToBack(xQueueEventos, &evento, portMAX_DELAY) != pdTRUE);
if(xQueueSendToBack(xQueueEventos, &evento, portMAX_DELAY) != pdTRUE) {
Serial.println("[InterfaceWiFi] Erro ao enviar evento à fila");
}
}

void InterfaceWiFi::cancelar()
{
Evento evento = CANCELAR;
while(xQueueSendToBack(xQueueEventos, &evento, portMAX_DELAY) != pdTRUE);
}
if(xQueueSendToBack(xQueueEventos, &evento, portMAX_DELAY) != pdTRUE) {
Serial.println("[InterfaceWiFi] Erro ao enviar evento à fila");
}}

void InterfaceWiFi::calibrar()
{
Evento evento = CALIBRAR;
while(xQueueSendToBack(xQueueEventos, &evento, portMAX_DELAY) != pdTRUE);
}
if(xQueueSendToBack(xQueueEventos, &evento, portMAX_DELAY) != pdTRUE) {
Serial.println("[InterfaceWiFi] Erro ao enviar evento à fila");
}}

void InterfaceWiFi::carregarPrograma(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final)
{
if (maquinaEstados.getEstado() == IDLE) {
// https://github.com/smford/esp32-asyncwebserver-fileupload-example/blob/master/example-01/example-01.ino
String logmessage = "[InterfaceWiFi] Client:" + request->client()->remoteIP().toString() + " " + request->url();
Serial.println(logmessage);

if (!index) {
logmessage = "[InterfaceWiFi] Upload Start: " + String(filename);
// open the file on first call and store the file handle in the request object
// request->_tempFile = SPIFFS.open("/" + filename, "w");
request->_tempFile = SPIFFS.open("/gcode.txt", "w");
Serial.println(logmessage);
if (!interfaceWifi.carregando) {
Evento evento = CARREGAR;
if(xQueueSendToBack(xQueueEventos, &evento, portMAX_DELAY) != pdTRUE) {
Serial.println("[InterfaceWiFi] Erro ao enviar evento à fila");
}
}

if (len) {
// stream the incoming chunk to the opened file
request->_tempFile.write(data, len);
logmessage = "[InterfaceWiFi] Writing file: " + String(filename) + " index=" + String(index) + " len=" + String(len);
Serial.println(logmessage);
}
while(!interfaceWifi.carregando) vTaskDelay(pdMS_TO_TICKS(100));

if (final) {
logmessage = "[InterfaceWiFi] Upload Complete: " + String(filename) + ",size: " + String(index + len);
// close the file handle as the upload is now done
request->_tempFile.close();
Serial.println(logmessage);
request->redirect("/");
}
// https://github.com/smford/esp32-asyncwebserver-fileupload-example/blob/master/example-01/example-01.ino
String logmessage = "[InterfaceWiFi] Client: " + request->client()->remoteIP().toString() + " " + request->url();
Serial.println(logmessage);

if (!index) {
logmessage = "[InterfaceWiFi] Upload Start: " + String(filename);
// open the file on first call and store the file handle in the request object
request->_tempFile = SPIFFS.open("/gcode.txt", "w");
Serial.println(logmessage);
}

if (len) {
// stream the incoming chunk to the opened file
request->_tempFile.write(data, len);
logmessage = "[InterfaceWiFi] Writing file: " + String(filename) + " index=" + String(index) + " len=" + String(len);
Serial.println(logmessage);
}

if (final) {
logmessage = "[InterfaceWiFi] Upload Complete: " + String(filename) + ",size: " + String(index + len);
// close the file handle as the upload is now done
request->_tempFile.close();
Serial.println(logmessage);
request->redirect("/");
interfaceWifi.carregando = false;
}
}

Expand Down
20 changes: 4 additions & 16 deletions src/interpretador_g.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void InterpretadorG::cancelar()
{
imprimindo = false;
file.close();
Serial.println("[InterpretadorG] Impressão cancelada");
}

void InterpretadorG::taskExecutar()
Expand All @@ -46,8 +47,7 @@ void InterpretadorG::taskExecutar()
}

while(1) {
if (imprimindo && (xSemaphoreControlador != NULL)) { // estado Imprimindo
// Serial.println("[InterpretadorG] Aguardando semáforo");
if (imprimindo) { // estado Imprimindo
if (xSemaphoreTake(xSemaphoreControlador, (TickType_t) 0) == pdTRUE) {
Serial.println("[InterpretadorG] Semáforo capturado");
if (file.available()) {
Expand All @@ -57,20 +57,8 @@ void InterpretadorG::taskExecutar()
GCode.ParseLine();
Serial.println("[InterpretadorG] Linha lida");

if (GCode.HasWord('G')) {
G = GCode.GetWordValue('G');
}
if (GCode.HasWord('X')) {
X = GCode.GetWordValue('X');
}
if (GCode.HasWord('Y')) {
Y = GCode.GetWordValue('Y');
}
if (GCode.HasWord('Z')) {
Z = GCode.GetWordValue('Z');
}

controlador.enviarComando(G, X, Y, Z);
// controlador.enviarComando(G, X, Y, Z);
controlador.enviarComando(&GCode);

} else { // quando termina a última linha
Serial.println("[InterpretadorG] Programa terminado");
Expand Down
6 changes: 0 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ void setup() {
}

void loop() {
// static int i = 0;
// Serial.print("Hello World! ");
// Serial.print(i++);
// Serial.print("\t");
// Serial.println(sensorCurso1.origem());
// vTaskDelay(pdMS_TO_TICKS(5000));
}


Loading

0 comments on commit a4bdb9c

Please sign in to comment.