diff --git a/include/battery.hpp b/include/battery.hpp deleted file mode 100644 index 927f7a6..0000000 --- a/include/battery.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef BATTERY_HPP -#define BATTERY_HPP - -#include -#include "pins.h" - -namespace Battery{ - - float map_float(float x, float in_min, float in_max, float out_min, float out_max); - void measure(double *voltage); - -} - -#endif \ No newline at end of file diff --git a/include/pins.h b/include/pins.h index 87308e7..ff9821e 100644 --- a/include/pins.h +++ b/include/pins.h @@ -13,9 +13,6 @@ #define BIN1_PIN D3 #define BIN2_PIN D2 -//**Battery pin** -#define BATT A0 - //**Radio pins** #define CE_PIN 0 //RX #define CS_PIN RX //TX diff --git a/platformio.ini b/platformio.ini index 48870c1..6eae856 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,6 +15,4 @@ framework = arduino lib_deps = ; name-based (built-in library in framework) nrf24/RF24@^1.4.2 -;monitor_speed = 9600 -upload_port = /dev/ttyUSB0 -; upload_port = /dev/ttyUSB* +monitor_speed = 115200 \ No newline at end of file diff --git a/src/battery.cpp b/src/battery.cpp deleted file mode 100644 index f28b254..0000000 --- a/src/battery.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include - -namespace Battery{ - - float map_float(float x, float in_min, float in_max, float out_min, float out_max) { - const float dividend = out_max - out_min; - const float divisor = in_max - in_min; - const float delta = x - in_min; - - return (delta * dividend + (divisor / 2)) / divisor + out_min; - } - - /* - Lê a entrada analógica A0 e retorna a tensão na bateria - 3.3V 1023 - 2.96V x - */ - - void measure(double *voltage){ - int sensorValue = analogRead(BATT); - Serial.print("ANALOGREAD:");Serial.println(sensorValue); - //double voltage_read = sensorValue * (3.2 / 1023); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3.2V): - //*voltage =voltage_read * (7.75 / 2.906); - *voltage = sensorValue * 0.0094 - 0.349; - } -/* -void measure(float *voltage){ - // read the input on analog pin 0: - int sensorValue = analogRead(BATT); - // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 3.2V): - float voltage_read = sensorValue * (2.96 / 917.6); - *voltage = voltage_read * (8.5 / 2.96); - - Serial.print("A0 CONTÉM[");Serial.print(sensorValue);Serial.println("]"); - Serial.print("voltagem:[");Serial.print(voltage_read);Serial.println("]V"); - Serial.println("#####"); - //voltage = map_float(readValue, 0, 1023, 0, 100); - } - */ -} - diff --git a/src/main.cpp b/src/main.cpp index c07b848..3d6fd94 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,26 +1,58 @@ #include +#include +#include #define WEMOS_DEBUG false -#define ROBOT_NUMBER 0 +#define ROBOT_NUMBER 1 #include "radio.hpp" #include "motor.hpp" #include "waves.hpp" -#include "battery.hpp" +typedef struct dataStruct +{ + int16_t id; + int16_t vl; + int16_t vr; +} dataStruct; -Radio::dataStruct vel; +//Cria uma struct_message chamada myData +dataStruct vel; + +volatile static uint32_t lastReceived; + +double vl = 0; +double vr = 0; + +//Funcao de Callback executada quando a mensagem for recebida +void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) +{ + memcpy(&vel, incomingData, sizeof(vel)); + if(vel.id == ROBOT_NUMBER){ + vl = vel.vl; + vr = vel.vr; + lastReceived = micros(); + } +} void setup() { #if WEMOS_DEBUG - Serial.begin(9600); + Serial.begin(115200); while(!Serial); delay(1000); Serial.println("START"); #endif - Radio::setup(ROBOT_NUMBER, 3); + + WiFi.mode(WIFI_STA); + if (esp_now_init() != 0) { + Serial.println("Erro ao inicializar o ESP-NOW"); + return; + } + esp_now_set_self_role(ESP_NOW_ROLE_SLAVE); + esp_now_register_recv_cb(OnDataRecv); + Motor::setup(); - pinMode(BATT,INPUT); + lastReceived = micros(); } @@ -30,21 +62,21 @@ void loop() { //=========Radio=============== // Velocidades a serem lidas do rádio, são estáticas de modo que se Radio::receiveData não receber nada, mantém-se a velocidade anterior - static double vl; - static double vr; - Radio::receiveData(&vl, &vr); + // static double vl; + // static double vr; + // Radio::receiveData(&vl, &vr); + // Serial.println("###################"); + // Serial.println("Radio:"); + // Serial.print("vl: ");Serial.print(vl);Serial.print("\tvr: ");Serial.println(vr); + // Serial.println("###################"); + //=========End Radio=========== + + //=========Wifi=============== Serial.println("###################"); Serial.println("Radio:"); Serial.print("vl: ");Serial.print(vl);Serial.print("\tvr: ");Serial.println(vr); Serial.println("###################"); - //=========End Radio=========== - - //=========Bateria=============== - static double voltage; - Battery::measure(&voltage); - Serial.println("Bateria:"); - Serial.print("Tensão aproximada: ");Serial.println(voltage); - //=========End Bateria=========== + //=========End Wifi=========== //=========Motor=============== Motor::move(0, 100); @@ -59,17 +91,10 @@ void loop() { //=========End Motor=========== delay(500); #else - // Velocidades a serem lidas do rádio, são estáticas de modo que se Radio::receiveData não receber nada, mantém-se a velocidade anterior - static double vl; - static double vr; - - // Lê velocidade do rádio - Radio::receiveData(&vl, &vr); - // Rádio foi perdido, mais de 2s sem mensagens - if(Radio::isRadioLost()){ + if((micros() - lastReceived) > RADIO_THRESHOLD){ // Rádio foi disconectado, mais de 5s sem mensagens - if(Radio::isRadioDisconnected()) + if((micros() - lastReceived) > RADIO_RESET_THRESHOLD) ESP.restart(); vr = 0; vl = Waves::sine_wave();