Skip to content

Commit

Permalink
Merge pull request #4 from unball/teste_espnow
Browse files Browse the repository at this point in the history
Última versão funcional do Firmware com ESP Now
  • Loading branch information
adornelas authored Jul 30, 2023
2 parents 873c943 + 9f1ef65 commit abeae34
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 87 deletions.
14 changes: 0 additions & 14 deletions include/battery.hpp

This file was deleted.

3 changes: 0 additions & 3 deletions include/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 0 additions & 41 deletions src/battery.cpp

This file was deleted.

77 changes: 51 additions & 26 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <espnow.h>

#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();

}

Expand All @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit abeae34

Please sign in to comment.