Merge branch 'develop' of ssh://git.lab-ouest.org:8022/Epitech/T-IOT-901_convoyor into feat/main-algo
# Conflicts: # config.ini # include/Program.h # lib/NFC/src/NfcReader.cpp # src/Program.cpp
This commit is contained in:
200
src/Program.cpp
200
src/Program.cpp
@ -1,103 +1,159 @@
|
||||
#include "Program.h"
|
||||
#include "Arduino.h"
|
||||
#include "M5LCD.h"
|
||||
#include "DolibarrClient.h"
|
||||
#include "ServoMotorComponent.h"
|
||||
#include "NfcReader.h"
|
||||
#include <Ticker.h>
|
||||
|
||||
uint32_t derniereExecution = 0;
|
||||
const uint32_t intervalle = 1000;
|
||||
std::vector<models::Warehouse> *warehouses;
|
||||
|
||||
int initialize_wifi(WifiConfig wifi) {
|
||||
lcdScreen->add_log("Connecting to the WiFi network...");
|
||||
WiFiClass::mode(WIFI_STA); //Optional
|
||||
WiFi.setSleep(false);
|
||||
WiFi.begin(wifi.ssid, wifi.password);
|
||||
Serial.print("Connecting ");
|
||||
while(WiFiClass::status() != WL_CONNECTED){
|
||||
delay(WAITING_WIFI_DELAY);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("Connected to the WiFi network");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Program::checkNfc() {
|
||||
if (this->nfcReader->IsNfcConnected() && lcdScreen->get_components().nfc != COMPONENT_OK) {
|
||||
lcdScreen->set_nfc_status(COMPONENT_OK);
|
||||
lcdScreen->add_log("NFC component connected !");
|
||||
Serial.println("NFC IS CONNECTED !");
|
||||
} else if (!this->nfcReader->IsNfcConnected() && lcdScreen->get_components().nfc == COMPONENT_OK) {
|
||||
lcdScreen->set_nfc_status(COMPONENT_KO);
|
||||
lcdScreen->add_log("NFC component disconnected !");
|
||||
Serial.println("NFC NOT CONNECTED !");
|
||||
}
|
||||
}
|
||||
|
||||
void Program::checkServo() {
|
||||
if (this->servo->isConnected() && lcdScreen->get_components().servo != COMPONENT_OK) {
|
||||
lcdScreen->set_servo(COMPONENT_OK);
|
||||
lcdScreen->add_log("SERVO component connected !");
|
||||
Serial.println("SERVO IS CONNECTED !");
|
||||
} else if (!this->servo->isConnected() && lcdScreen->get_components().servo == COMPONENT_OK) {
|
||||
lcdScreen->set_servo(COMPONENT_KO);
|
||||
lcdScreen->add_log("SERVO component disconnected !");
|
||||
Serial.println("SERVO NOT CONNECTED !");
|
||||
}
|
||||
}
|
||||
|
||||
void Program::checkWifi() {
|
||||
if (WiFiClass::status() == WL_CONNECTED && lcdScreen->get_components().wifi != COMPONENT_OK) {
|
||||
lcdScreen->add_log("Connected to the WiFi network");
|
||||
lcdScreen->set_wifi_status(COMPONENT_OK);
|
||||
struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN};
|
||||
this->client = new DolibarrClient(dolibarr);
|
||||
warehouses = this->client->list_warehouse();
|
||||
if (warehouses != nullptr) {
|
||||
lcdScreen->add_log("Warehouses found !");
|
||||
lcdScreen->set_dolibarr_status(COMPONENT_OK);
|
||||
for (auto &ware : *warehouses) {
|
||||
char buffer[50];
|
||||
sprintf(buffer, "+ Warehouse '%s' (%s)", ware.label.c_str(), ware.id.c_str());
|
||||
lcdScreen->add_log(buffer);
|
||||
}
|
||||
} else {
|
||||
lcdScreen->add_log("Warehouse not found");
|
||||
lcdScreen->set_dolibarr_status(COMPONENT_KO);
|
||||
}
|
||||
} else if (WiFiClass::status() != WL_CONNECTED && lcdScreen->get_components().wifi == COMPONENT_OK) {
|
||||
lcdScreen->add_log("Wifi signal lost, reconnecting...");
|
||||
lcdScreen->set_wifi_status(COMPONENT_KO);
|
||||
lcdScreen->set_dolibarr_status(COMPONENT_KO);
|
||||
struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD};
|
||||
initialize_wifi(wifi_c);
|
||||
}
|
||||
}
|
||||
|
||||
Program::Program() {
|
||||
Serial.begin(MONITOR_SPEED);
|
||||
struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD};
|
||||
struct DolibarrConfig dolibarr = {DOLIBARR_URL, DOLIBARR_API_TOKEN};
|
||||
initialize_wifi(wifi_c);
|
||||
this->client = new DolibarrClient(dolibarr);
|
||||
|
||||
// initialisation GRBL
|
||||
Wire.begin();
|
||||
delay(1000);
|
||||
lcdScreen = new M5LCD();
|
||||
lcdScreen->add_log("Initialize M5LCD component....");
|
||||
this->nfcReader = new NfcReader(NFC_ADDR);
|
||||
this->servo = new ServoMotorComponent(2, 1, 1);
|
||||
this->grbl = new GRBL(STEPMOTOR_I2C_ADDR);
|
||||
Wire.begin(21, 22);
|
||||
grbl->init(STEPER_SPEED, STEPER_PAS, STEPER_ACC);
|
||||
|
||||
this->NFC = new NfcReader(NFC_ADDR);
|
||||
struct WifiConfig wifi_c = {WIFI_SSID, WIFI_PASSWORD};
|
||||
initialize_wifi(wifi_c);
|
||||
}
|
||||
|
||||
void Program::loop() {
|
||||
// if(this->grbl->)
|
||||
// this->grbl->mouveForward(5); //TODO: réglé pour qu'il avance de la bonne distance
|
||||
// String nfcIn = "0"; // TODO: si le lecteur envoie qqc
|
||||
// //TODO: refrech servo
|
||||
// if(nfcIn !="0"){
|
||||
// //envoyer sur dolibard
|
||||
// this->NfcIDs->push_back(nfcIn);
|
||||
// }
|
||||
// if(this->NfcIDs->size() > 0){
|
||||
// }
|
||||
lcdScreen->update();
|
||||
|
||||
//algo V1
|
||||
//je lis la valeur NFC
|
||||
String nfcId = this->NFC->ReadNfc();
|
||||
uint32_t maintenant = millis();
|
||||
if (maintenant - derniereExecution >= intervalle) {
|
||||
this->checkServo();
|
||||
this->checkNfc();
|
||||
this->checkWifi();
|
||||
derniereExecution = maintenant;
|
||||
}
|
||||
this->servo->refresh();
|
||||
|
||||
String nfcId = this->nfcReader->ReadNfc();
|
||||
//si qqc
|
||||
if(nfcId != "0"){
|
||||
if (lcdScreen->get_components().wifi != COMPONENT_OK) {
|
||||
lcdScreen->add_log("Wifi not connected !");
|
||||
lcdScreen->set_nfc_message("Cannot send wifi request !");
|
||||
return;
|
||||
}
|
||||
//j'arrète le stepper
|
||||
//this->grbl->stop(); //FIXME: implemente
|
||||
//j'affiche le tag lue
|
||||
Serial.print("new colis in comming : ");
|
||||
Serial.println(nfcId);
|
||||
//je demande a dolibard ou j'envoie
|
||||
Serial.println("Demande a dolibard");
|
||||
delay(500);// TODO: remove mock
|
||||
//j'envoie au servo la commande
|
||||
Serial.print("Servo : ");
|
||||
Serial.println("Droite");
|
||||
delay(500);// TODO: remove mock
|
||||
//j'avance le moteur jusqu'a X (a definir)
|
||||
lcdScreen->add_log("new colis detected: ");
|
||||
lcdScreen->add_log(nfcId.c_str());
|
||||
lcdScreen->set_nfc_message(nfcId.c_str());
|
||||
auto product = this->client->get_product_by_factory_id(nfcId.c_str());
|
||||
Serial.printf("Product: %s\n%s", product.label.c_str(), product.accountancy_code_sell_export.c_str());
|
||||
lcdScreen->set_product_label(product.label);
|
||||
lcdScreen->set_product_id(product.id);
|
||||
/*if (product.fk_default_warehouse == product.accountancy_code_sell_export) {
|
||||
Serial.printf("Product already in the good warehouse !\n");
|
||||
return;
|
||||
}*/
|
||||
auto ware = std::find_if(warehouses->begin(), warehouses->end(), [&product](models::Warehouse w) {return w.id == product.accountancy_code_sell_export;});
|
||||
if (ware.base() == nullptr) {
|
||||
Serial.printf("Warehouse not found !\n");
|
||||
return;
|
||||
}
|
||||
Serial.printf("Product need to go to warehouse %s\n", ware->label.c_str());
|
||||
this->client->create_movement(models::CreateProductStock{
|
||||
product.id,
|
||||
product.fk_default_warehouse,
|
||||
"-1"
|
||||
});
|
||||
if (this->client->create_movement(models::CreateProductStock{
|
||||
product.id,
|
||||
ware->id,
|
||||
"1"
|
||||
}) == 0) {
|
||||
Serial.printf("Movement created !\n");
|
||||
} else {
|
||||
Serial.printf("Movement cannot be created !\n");
|
||||
}
|
||||
if (ware->id == "1") {
|
||||
this->servo->setDesiredPosition(Position::RIGHT);
|
||||
lcdScreen->set_servo_message("RIGHT");
|
||||
} else if (ware->id == "2") {
|
||||
this->servo->setDesiredPosition(Position::LEFT);
|
||||
lcdScreen->set_servo_message("LEFT");
|
||||
} else if (ware->id == "3") {
|
||||
this->servo->setDesiredPosition(Position::MIDDLE);
|
||||
lcdScreen->set_servo_message("MIDDLE");
|
||||
}
|
||||
this->grbl->mouveForward(CONVOYER_LEN);
|
||||
}else{
|
||||
//si rien
|
||||
//je check si le stepper est en iddle
|
||||
} else {
|
||||
if(this->grbl->isIddle()){
|
||||
//si oui j'avance de 5 mm
|
||||
this->grbl->mouveForward(5);
|
||||
}
|
||||
}
|
||||
//j'update la position du servo
|
||||
//this->servo->refresh();
|
||||
//idée affichage:
|
||||
// waitting packet
|
||||
// new packet detected : ID
|
||||
// check to dolibarr
|
||||
// DOLIBARR ID found stockage in progress
|
||||
// stoage done
|
||||
// ERR DOLIBARR ID not found
|
||||
|
||||
//idée algo v1.5 (ajou petite gestion d'erreur)
|
||||
//je lis la valeur NFC
|
||||
//si qqc
|
||||
//je demande a dolibard ou j'envoie
|
||||
//j'envoie au servo la commande
|
||||
//j'avance le moteur jusqu'a X (a definir)
|
||||
//si rien j'avance le moteur (5mm)
|
||||
//si le lecteur de sortie voi pas de colis
|
||||
//on affiche une erreur de colis non lue
|
||||
//si un seul colis
|
||||
//on dit que le colis est bien trié (avec porte de sortie)
|
||||
//si il y a 2 colis (ou plus)
|
||||
//on affiche une erreur de bourrage
|
||||
|
||||
//idée algo v2
|
||||
//utiliser une pile
|
||||
//utiliser le 2nd lecteur pour vidé la pile
|
||||
//gestion d'erreur complete avec celle du dessus plus celle de la pile
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user