Skip to content

Commit

Permalink
Logging de cada módulo e interface serial
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrique-BO committed Jun 21, 2022
1 parent c1779ff commit 89cb341
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 82 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"*.tcc": "cpp",
"regex": "cpp"
}
}
22 changes: 0 additions & 22 deletions data/gcode.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
%
(Header)
(Generated by gcodetools from Inkscape.)
(Using default header. To add your own header create file "header" in the output dir.)
M3
(Header end.)
G21 (All units in mm)

(Start cutting path id: rect5810)
(Change tool to Default tool)

G00 Z5.000000
G00 X46.857647 Y218.081856

G01 Z-1.000000 F100.0(Penetrate)
G01 X162.416370 Y218.081856 Z-1.000000 F400.000000
G01 X162.416370 Y117.672590 Z-1.000000
G01 X46.857647 Y117.672590 Z-1.000000
G01 X46.857647 Y218.081856 Z-1.000000
G00 Z5.000000

(End cutting path id: rect5810)


(Footer)
M5
G00 X0.0000 Y0.0000
M2
(Using default footer. To add your own footer create file "footer" in the output dir.)
(end)
%
26 changes: 23 additions & 3 deletions data/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Relé Magritte</title>
<title>Rele Magritte</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Relé Magritte</h1>
<h1>Rele Magritte</h1>

<p>Estado: <strong> %ESTADO%</strong></p>
<!-- <p>Estado: <strong>%ESTADO%</strong></p> -->
<p>Estado: <strong><span id="Estado">...</span></strong></p>

<p><a href="/imprimir"><button class="button">Imprimir</button></a></p>
<p><a href="/cancelar"><button class="button cancelar">Cancelar</button></a></p>
Expand All @@ -23,5 +24,24 @@ <h2>Selecionar novo programa</h2>
<input type="file" name="programa" id="programa" value=""><br>
<button class="button" type="submit">Carregar programa</button>
</form>

<script>
setInterval(function() {
// Call a function repetatively with 2 Second interval
getData();
}, 500); // update rate

function getData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("Estado").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "updateEstado", true);
xhttp.send();
}
</script>
</body>
</html>
8 changes: 4 additions & 4 deletions include/controlador.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ void vTaskControlador(void *param);

class Controlador {
public:
Controlador(int pinStepX, int pinDirX, int pinStepY, int pinDirY, int pinStepZ, int pinDirZ, SensorCurso& sensorCurso1, SensorCurso& sensorCurso2);
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 calibrar();
void origem();
void taskControlar();

bool chegou;
bool chegou = false; // TODO semaforo/mutex

private:
AccelStepper *pStepperX;
AccelStepper *pStepperY;
AccelStepper *pStepperZ;
SensorCurso sensorCurso1;
SensorCurso sensorCurso2;

float speed = 100.0;
bool calibrando = false;
};

extern Controlador controlador;

#endif
16 changes: 8 additions & 8 deletions include/definicoes_sistema.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
#include "maquina_estados.h"

// Pinos dos motores de passo
#define PIN_STEP_X 1
#define PIN_DIR_X 1
#define PIN_STEP_Y 1
#define PIN_DIR_Y 1
#define PIN_STEP_Z 1
#define PIN_DIR_Z 1
#define PIN_STEP_X 16
#define PIN_DIR_X 17
#define PIN_STEP_Y 18
#define PIN_DIR_Y 19
#define PIN_STEP_Z 20
#define PIN_DIR_Z 21

// Pinos dos sensores de fim de curso
#define PIN_SENSOR_1 1
#define PIN_SENSOR_2 1
#define PIN_SENSOR_1 4
#define PIN_SENSOR_2 5

#endif
7 changes: 5 additions & 2 deletions include/interface_wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include "ESPAsyncWebServer.h"

#define SSID "abcabc"
#define PASSWORD "abcabc"
#define SSID "Magdolna"
#define PASSWORD "41308133"


class InterfaceWiFi {
Expand All @@ -16,9 +16,12 @@ class InterfaceWiFi {
void calibrar();
static void carregarPrograma(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final);
static String processor(const String& var);
static void handleEstado(AsyncWebServerRequest *request);

private:
AsyncWebServer server;
};

extern InterfaceWiFi interfaceWifi;

#endif
5 changes: 3 additions & 2 deletions include/interpretador_g.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void vTaskInterpretadorG(void *param);

class InterpretadorG {
public:
InterpretadorG(Controlador& controlador): controlador(controlador){}
InterpretadorG(){}
void iniciarInterpretadorG();
void imprimir();
void cancelar();
Expand All @@ -21,8 +21,9 @@ class InterpretadorG {
private:
File file;
GCodeParser GCode;
Controlador controlador;
bool imprimindo = false;
};

extern InterpretadorG interpretadorG;

#endif
17 changes: 7 additions & 10 deletions include/maquina_estados.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ typedef enum Estado {
*/
typedef enum Evento {
NENHUM_EVENTO,
CALIBRAR, // Botão Calibrar
IMPRIMIR, // Botão Imprimir
CANCELAR, // Botão Cancelar desenho
TERMINADO, // Desenho terminado
ORIGEM, // Caneta na origem
CARREGAR // Botão Carregar programa
CALIBRAR, // 1 Botão Calibrar
IMPRIMIR, // 2 Botão Imprimir
CANCELAR, // 3 Botão Cancelar desenho
TERMINADO, // 4 Desenho terminado
ORIGEM, // 5 Caneta na origem
CARREGAR // 6 Botão Carregar programa
} Evento;

/**
Expand Down Expand Up @@ -69,8 +69,7 @@ typedef struct ProxEstadoAcao {
*/
class MaquinaEstados {
public:
MaquinaEstados(Controlador& controlador, InterpretadorG& interpretadorG):
controlador(controlador), interpretadorG(interpretadorG) {}
MaquinaEstados(){}

/**
* @brief Inicia a Máquina de Estados, criando a matriz de transição de estados e a task do FreeRTOS
Expand Down Expand Up @@ -103,8 +102,6 @@ class MaquinaEstados {

ProxEstadoAcao matrizTransicaoEstados[NUM_ESTADOS][NUM_EVENTOS]; // matriz de transicao de estados
Estado estado; // estado atual
Controlador controlador; // TODO precisa de Controlador& ??
InterpretadorG interpretadorG;
};

/**
Expand Down
3 changes: 3 additions & 0 deletions include/sensor_curso.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ class SensorCurso {
int pin;
};

extern SensorCurso sensorCurso1;
extern SensorCurso sensorCurso2;

#endif
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ lib_deps =
waspinator/AccelStepper@^1.61
tgolla/GCodeParser@^1.3.0
me-no-dev/ESP Async WebServer@^1.2.3
monitor_speed = 115200
7 changes: 5 additions & 2 deletions src/controlador.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

#include "maquina_estados.h"
#include "controlador.h"
#include "sensor_curso.h"

Controlador::Controlador(int pinStepX, int pinDirX, int pinStepY, int pinDirY, int pinStepZ, int pinDirZ, SensorCurso& sensor1, SensorCurso& sensor2):
sensorCurso1(sensorCurso1), sensorCurso2(sensorCurso2) {
Controlador::Controlador(int pinStepX, int pinDirX, int pinStepY, int pinDirY, int pinStepZ, int pinDirZ)
{
pStepperX = new AccelStepper(AccelStepper::DRIVER, pinStepX, pinDirX);
pStepperY = new AccelStepper(AccelStepper::DRIVER, pinStepY, pinDirY);
pStepperZ = new AccelStepper(AccelStepper::DRIVER, pinStepZ, pinDirZ);
Expand All @@ -19,6 +20,8 @@ sensorCurso1(sensorCurso1), sensorCurso2(sensorCurso2) {

void Controlador::iniciarControlador()
{
Serial.println("[Controlador] Iniciando controlador");

calibrando = false;
xTaskCreate(
vTaskControlador,
Expand Down
38 changes: 31 additions & 7 deletions src/interface_wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

void InterfaceWiFi::iniciarWiFi()
{
Serial.println("[InterfaceWiFi] Iniciando interface WiFi");

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

// Página principal
Expand All @@ -30,13 +32,15 @@ void InterfaceWiFi::iniciarWiFi()

// Botão imprimir
server.on("/imprimir", HTTP_GET, [this](AsyncWebServerRequest *request){
Serial.println("[InterfaceWiFi] Botão imprimir");
this->imprimir();
request->redirect("/"); // TODO provavelmente não vai dar tempo de atualizar o estado
});

// Botão cancelar
server.on("/cancelar", HTTP_GET, [this](AsyncWebServerRequest *request){
this->cancelar();
Serial.println("[InterfaceWiFi] Botão cancelar");
request->redirect("/"); // TODO provavelmente não vai dar tempo de atualizar o estado
});

Expand All @@ -51,6 +55,9 @@ void InterfaceWiFi::iniciarWiFi()
request->send(200);
}, this->carregarPrograma);

// AJAX update estado
server.on("/updateEstado", HTTP_GET, handleEstado);

// Inicia o servidor
server.begin();
}
Expand All @@ -77,11 +84,11 @@ void InterfaceWiFi::carregarPrograma(AsyncWebServerRequest *request, String file
{
if (maquinaEstados.getEstado() == IDLE) {
// https://github.com/smford/esp32-asyncwebserver-fileupload-example/blob/master/example-01/example-01.ino
String logmessage = "Client:" + request->client()->remoteIP().toString() + " " + request->url();
String logmessage = "[InterfaceWiFi] Client:" + request->client()->remoteIP().toString() + " " + request->url();
Serial.println(logmessage);

if (!index) {
logmessage = "Upload Start: " + String(filename);
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");
Expand All @@ -91,12 +98,12 @@ void InterfaceWiFi::carregarPrograma(AsyncWebServerRequest *request, String file
if (len) {
// stream the incoming chunk to the opened file
request->_tempFile.write(data, len);
logmessage = "Writing file: " + String(filename) + " index=" + String(index) + " len=" + String(len);
logmessage = "[InterfaceWiFi] Writing file: " + String(filename) + " index=" + String(index) + " len=" + String(len);
Serial.println(logmessage);
}

if (final) {
logmessage = "Upload Complete: " + String(filename) + ",size: " + String(index + len);
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);
Expand All @@ -106,7 +113,7 @@ void InterfaceWiFi::carregarPrograma(AsyncWebServerRequest *request, String file
}

String InterfaceWiFi::processor(const String& var){
Serial.println(var);
Serial.println("[InterfaceWiFi] " + var);
if(var == "ESTADO"){
switch(maquinaEstados.getEstado()) {
case IDLE:
Expand All @@ -118,4 +125,21 @@ String InterfaceWiFi::processor(const String& var){
}
}
return String();
}

void InterfaceWiFi::handleEstado(AsyncWebServerRequest *request)
{
String sEstado;
switch(maquinaEstados.getEstado()) {
case IDLE:
sEstado = "Idle";
break;
case IMPRIMINDO:
sEstado = "Imprimindo";
break;
case CALIBRANDO:
sEstado = "Calibrando";
break;
}
request->send(200, "text/plane", sEstado);
}
Loading

0 comments on commit 89cb341

Please sign in to comment.