Skip to content

Commit

Permalink
remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
gTorrez committed Jan 26, 2023
1 parent 0735a9b commit 28690fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
40 changes: 15 additions & 25 deletions comm/reciever/reciever.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <WiFi.h>


//pin definitions
//definição dos pins
#define PWMA 19
#define PWMB 27
#define A1 5
Expand All @@ -12,19 +12,16 @@
#define B2 26
#define stby 33

// This is de code for the board that is in robots
float v_l, v_a;
int robot_id = 9;
int id;
int first_mark, second_mark;

float v_l, v_a;

const byte numChars = 64;
char commands[numChars];
char tempChars[numChars];

int robot_id = 0;
int id;
float robot_vl;
float robot_va;

typedef struct struct_message{
char message[64];
Expand All @@ -40,6 +37,7 @@ void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len)
strcpy(commands, rcv_commands.message);
}


void motor_R(int speedR) { // se o valor for positivo gira para um lado e se for negativo troca o sentido
if (speedR > 0) {
digitalWrite(A1, 1);
Expand All @@ -50,6 +48,8 @@ void motor_R(int speedR) { // se o valor for positivo gira para um lado e se for
}
ledcWrite(1, abs( speedR));
}


void motor_L(int speedL) {
if (speedL > 0) {
digitalWrite(B1, 1);
Expand All @@ -61,6 +61,7 @@ void motor_L(int speedL) {
ledcWrite(2, abs( speedL));
}


void motors_control(float linear, float angular) {
angular = pid(angular, - get_theta_speed());

Expand Down Expand Up @@ -116,49 +117,38 @@ void setup() {
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start());
ESP_ERROR_CHECK( esp_wifi_set_channel(14, WIFI_SECOND_CHAN_NONE));
// esp_wifi_set_max_tx_power(84);

// ESP_ERROR_CHECK
// WiFi.mode(WIFI_STA);
// esp_wifi_set_channel(14, WIFI_SECOND_CHAN_NONE);
ESP_ERROR_CHECK( esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE));

if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}

// esp_wifi_set_channel(CHANNEL, WIFI_SECOND_CHAN_NONE);

esp_now_register_recv_cb(OnDataRecv);

// configuração mpu

mpu_init();
}


void loop() {
second_mark = millis();

strcpy(tempChars, commands);
strcpy(tempChars, commands); // necessário para proteger a informação original
parseData();

v_l = robot_vl;
v_a = robot_va;


if (second_mark - first_mark > 500) {
v_l = 0.00;
v_a = 0.00;
}


motors_control(v_l, v_a); //aplica os valores para os motores
}


void parseData(){
char * strtokIndx; // this is used by strtok() as an index
char * strtokIndx;

strtokIndx = strtok(tempChars, ",");

Expand All @@ -167,9 +157,9 @@ void parseData(){

if(id == robot_id){
strtokIndx = strtok(NULL, ",");
robot_vl = atof(strtokIndx);
v_l = atof(strtokIndx);
strtokIndx = strtok(NULL, ",");
robot_va = atof(strtokIndx);
v_a = atof(strtokIndx);
strtokIndx = strtok(NULL, ",");
}

Expand Down
29 changes: 9 additions & 20 deletions comm/sender/sender.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#include <WiFi.h>


// This is the code for the board that is connected to PC

// MAC Adress de cada uma das placas que receberao comandos
// MAC Adress genérico para enviar os dados no canal selecionado
uint8_t broadcast_adr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

//==============
Expand All @@ -24,34 +22,28 @@ struct_message commands;

//==============


esp_now_peer_info_t peerInfo;

void setup() {
Serial.begin(115200);

esp_netif_init();
esp_event_loop_create_default();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
esp_wifi_set_storage(WIFI_STORAGE_RAM);
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_start();
esp_wifi_set_channel(14, WIFI_SECOND_CHAN_NONE);
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE));
esp_wifi_set_max_tx_power(84);

// ESP_ERROR_CHECK
// WiFi.mode(WIFI_STA);
// esp_wifi_set_channel(14, WIFI_SECOND_CHAN_NONE);

if (esp_now_init() != ESP_OK)
{
Serial.println("Error initializing ESP-NOW");
return;
}


//
memcpy(peerInfo.peer_addr, broadcast_adr, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Expand All @@ -66,9 +58,6 @@ void setup() {
void loop() {
recvWithStartEndMarkers();
if (newData == true){
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
strcpy(commands.message, receivedChars);
sendData();
newData = false;
Expand All @@ -85,7 +74,7 @@ void recvWithStartEndMarkers(){
char in;

while (Serial.available()){
// Message format:
// Formato da mensagem::
// <[id1],[v_l1],[v_a1],[id2],[v_l2],[v_a2],[id3],[v_l3],[v_a3]>
in = Serial.read();

Expand Down

0 comments on commit 28690fb

Please sign in to comment.