Skip to content

Commit

Permalink
add parseData function to receiver file
Browse files Browse the repository at this point in the history
  • Loading branch information
gTorrez committed Jan 25, 2023
1 parent e05fbb0 commit 0735a9b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
3 changes: 1 addition & 2 deletions comm/reciever/pid.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ float pid(float target, float atual){
float output = error * kp ;

return output;

}
}
60 changes: 44 additions & 16 deletions comm/reciever/reciever.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <esp_wifi.h>
#include <WiFi.h>

#define CHANNEL 3

//pin definitions
#define PWMA 19
Expand All @@ -18,25 +17,27 @@ float v_l, v_a;
int first_mark, second_mark;


typedef struct recieved_message {
int id;
float v_l;
float v_a;
} recieved_message;
const byte numChars = 64;
char commands[numChars];
char tempChars[numChars];

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

typedef struct struct_message{
char message[64];
} struct_message;

struct_message rcv_commands;

recieved_message robot;

void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
memcpy(&commands, incomingData, sizeof(commands));
memcpy(&rcv_commands, incomingData, sizeof(rcv_commands));
// Update the structures with the new incoming data
first_mark = millis();

robot.id = commands.id;
robot.v_l = commands.v_l;
robot.v_a = commands.v_a;
Serial.println();
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
Expand Down Expand Up @@ -140,8 +141,11 @@ void setup() {
void loop() {
second_mark = millis();

v_l = robot.v_l;
v_a = robot.v_a;
strcpy(tempChars, commands);
parseData();

v_l = robot_vl;
v_a = robot_va;


if (second_mark - first_mark > 500) {
Expand All @@ -152,3 +156,27 @@ void loop() {

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

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

strtokIndx = strtok(tempChars, ",");

while (strtokIndx != NULL){
id = atoi(strtokIndx);

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

else{
strtokIndx = strtok(NULL, ",");
strtokIndx = strtok(NULL, ",");
strtokIndx = strtok(NULL, ",");
}
}
}
25 changes: 17 additions & 8 deletions comm/sender/sender.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ char tempChars[numChars];
boolean newData = false;
int id, count;

typedef struct struct_message{
char message[numChars];
} struct_message;

struct_message commands;

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


Expand All @@ -24,14 +30,14 @@ esp_now_peer_info_t peerInfo;
void setup() {
Serial.begin(115200);

ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_init();
esp_event_loop_create_default();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
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(14, WIFI_SECOND_CHAN_NONE));
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_wifi_set_max_tx_power(84);

// ESP_ERROR_CHECK
Expand Down Expand Up @@ -63,6 +69,7 @@ void loop() {
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 Down Expand Up @@ -107,5 +114,7 @@ void recvWithStartEndMarkers(){

void sendData(){
// esse delay é necessário para que os dados sejam enviados corretamente
esp_err_t receivedChars = esp_now_send(broadcast_adr, (uint8_t *) &receivedChars, sizeof(receivedChars));
esp_err_t message = esp_now_send(broadcast_adr, (uint8_t *) &commands, sizeof(commands));
delay(3);

}
6 changes: 3 additions & 3 deletions comm/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from random import randint

# No Linux, a porta não será "COMX" e sim algo parecido com a linha abaixo
esp32 = serial.Serial('/dev/ttyUSB0', 115200)
esp32 = serial.Serial('/dev/ttyUSB1', 115200)
# esp32 = serial.Serial("COM6", 115200)

i = 0
Expand All @@ -13,11 +13,11 @@
v_l1 = str(80)
v_a1 = str(80)

id2 = str(0)
id2 = str(9)
v_l2 = str(100)
v_a2 = str(100)

id3 = str(9)
id3 = str(0)
v_l3 = str(120)
v_a3 = str(120)

Expand Down

0 comments on commit 0735a9b

Please sign in to comment.