33#include < WiFi.h>
44
55
6- // pin definitions
6+ // definição dos pins
77#define PWMA 19
88#define PWMB 27
99#define A1 5
1212#define B2 26
1313#define stby 33
1414
15- // This is de code for the board that is in robots
16- float v_l, v_a ;
15+ int robot_id = 9 ;
16+ int id ;
1717int first_mark, second_mark;
1818
19+ float v_l, v_a;
1920
2021const byte numChars = 64 ;
2122char commands[numChars];
2223char tempChars[numChars];
2324
24- int robot_id = 0 ;
25- int id;
26- float robot_vl;
27- float robot_va;
2825
2926typedef struct struct_message {
3027 char message[64 ];
@@ -40,6 +37,7 @@ void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len)
4037 strcpy (commands, rcv_commands.message );
4138}
4239
40+
4341void motor_R (int speedR) { // se o valor for positivo gira para um lado e se for negativo troca o sentido
4442 if (speedR > 0 ) {
4543 digitalWrite (A1, 1 );
@@ -50,6 +48,8 @@ void motor_R(int speedR) { // se o valor for positivo gira para um lado e se for
5048 }
5149 ledcWrite (1 , abs ( speedR));
5250}
51+
52+
5353void motor_L (int speedL) {
5454 if (speedL > 0 ) {
5555 digitalWrite (B1, 1 );
@@ -61,6 +61,7 @@ void motor_L(int speedL) {
6161 ledcWrite (2 , abs ( speedL));
6262}
6363
64+
6465void motors_control (float linear, float angular) {
6566 angular = pid (angular, - get_theta_speed ());
6667
@@ -116,49 +117,38 @@ void setup() {
116117 ESP_ERROR_CHECK ( esp_wifi_set_storage (WIFI_STORAGE_RAM) );
117118 ESP_ERROR_CHECK ( esp_wifi_set_mode (WIFI_MODE_STA) );
118119 ESP_ERROR_CHECK ( esp_wifi_start ());
119- ESP_ERROR_CHECK ( esp_wifi_set_channel (14 , WIFI_SECOND_CHAN_NONE));
120- // esp_wifi_set_max_tx_power(84);
121-
122- // ESP_ERROR_CHECK
123- // WiFi.mode(WIFI_STA);
124- // esp_wifi_set_channel(14, WIFI_SECOND_CHAN_NONE);
120+ ESP_ERROR_CHECK ( esp_wifi_set_channel (1 , WIFI_SECOND_CHAN_NONE));
125121
126122 if (esp_now_init () != ESP_OK)
127123 {
128124 Serial.println (" Error initializing ESP-NOW" );
129125 return ;
130126 }
131127
132- // esp_wifi_set_channel(CHANNEL, WIFI_SECOND_CHAN_NONE);
133-
134128 esp_now_register_recv_cb (OnDataRecv);
135129
136130 // configuração mpu
137-
138131 mpu_init ();
139132}
140133
134+
141135void loop () {
142136 second_mark = millis ();
143137
144- strcpy (tempChars, commands);
138+ strcpy (tempChars, commands); // necessário para proteger a informação original
145139 parseData ();
146140
147- v_l = robot_vl;
148- v_a = robot_va;
149-
150-
151141 if (second_mark - first_mark > 500 ) {
152142 v_l = 0.00 ;
153143 v_a = 0.00 ;
154144 }
155145
156-
157146 motors_control (v_l, v_a); // aplica os valores para os motores
158147}
159148
149+
160150void parseData (){
161- char * strtokIndx; // this is used by strtok() as an index
151+ char * strtokIndx;
162152
163153 strtokIndx = strtok (tempChars, " ," );
164154
@@ -167,9 +157,9 @@ void parseData(){
167157
168158 if (id == robot_id){
169159 strtokIndx = strtok (NULL , " ," );
170- robot_vl = atof (strtokIndx);
160+ v_l = atof (strtokIndx);
171161 strtokIndx = strtok (NULL , " ," );
172- robot_va = atof (strtokIndx);
162+ v_a = atof (strtokIndx);
173163 strtokIndx = strtok (NULL , " ," );
174164 }
175165
0 commit comments