-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
269 lines (219 loc) · 10.1 KB
/
Copy pathmain.cpp
File metadata and controls
269 lines (219 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// ============================================================================================================================================
//
// This is the preliminary code created for the RC car built by Sam, Evan, and Kenan during the Spring 2025 Hardware Hackathon.
// There are three sections - all compiled into this one file. Comment out the two sections you do not intend to upload (or create three individual files).
//
// Section 1 - Begin Remote/Sender Code:
// This is what the ESP32 remote should run. It uses ESP_NOW to communicate one-way to the ESP32 on the car itself. The broadcastAddress is the
// MAC address of the receiving ESP32 (see Section 3), the defined values are the pin numbers for the jumper wires (may change depending on how you wire).
// The struct message sends the Left and Right joystick values. We use analogRead() to read the incoming joystick values, remap them between 0 to 100,
// and send the value to the receiving ESP32 on the Car.
// Section 2 - Receiver Code:
// This is what the ESP32 on the car should run. It uses the receiving end of ESP_NOW for one-way communication. The defined values are similarly the
// pin numbers for jumper wires (battery, motor controller). We then map the incoming speed values to PWM values (0 - 255) and use the driveMotor
// function to write the values to the motor controller (which then goes straight to the wheels).
// Section 3 - Read ESP32 MAC Address:
// This is a brief section that will collect and print an ESP32's MAC address. This is necessary for gathering the correct MAC address of
// the receiver (ESP32 on the car).
//
// We predict most troubles will likely be due to wiring/incorrect pins. Double check the ESP32 pinout diagrams (for IO, Ground, Incoming power pins)
// and the X versus Y values for the analog joystick read (This implementation only uses 'either-or' for tank controls). This code works with the pins provided,
// so double check pins and wiring.
//
// Code written: 3/28/2025
// Description written: 4/15/2025
//
// ============================================================================================================================================
// ============================================================================================================================================
// BEGIN REMOTE/SENDER CODE
// ============================================================================================================================================
#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
uint8_t broadcastAddress[] = {0xc8, 0xf0, 0x9e, 0x33, 0xc8, 0x0c}; // Be sure this is correct (MAC address of receiving ESP32)
#define JOYSTICK1X 33
#define JOYSTICK1Y 35
#define JOYSTICK2X 32
#define JOYSTICK2Y 34
typedef struct struct_message { // MUST MATCH RECEIVER STRUCT
uint16_t a;
uint16_t b;
} struct_message;
struct_message myData;
esp_now_peer_info_t peerInfo;
// When data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); // Set device as WiFi Station
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register for Send CB to get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
// Set values to send
uint16_t J1x = analogRead(JOYSTICK1X);
uint16_t J1y = analogRead(JOYSTICK1Y);
uint16_t J2x = analogRead(JOYSTICK2X);
uint16_t J2y = analogRead(JOYSTICK2Y);
J1x = map(J1x, 0, 4095, 0, 100);
J1y = map(J1y, 0, 4095, 0, 100);
J2x = map(J2x, 0, 4095, 0, 100);
J2y = map(J2y, 0, 4095, 0, 100);
// Note that these may need to switch between J1y/J1x and J2y/J2x, depending on the orientation of the joystick.
myData.a = J1y; // J1x or J1y
myData.b = J2y; // J2x or J2y
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
// if (result == ESP_OK) {
// Serial.println("Sent with success");
// }
// else {
// Serial.println("Error sending the data");
// }
delay(100);
}
// ============================================================================================================================================
// END REMOTE/SENDER CODE
// ============================================================================================================================================
// ============================================================================================================================================
// BEGIN ROBOT/RECEIVER CODE
// ============================================================================================================================================
#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
// Motor 1 (Left)
#define MOTOR1_PIN1 32
#define MOTOR1_PIN2 33
#define ENABLE1_PIN 25
#define PWM_CHANNEL_1 0
// Motor 2 (Right)
#define MOTOR2_PIN1 26
#define MOTOR2_PIN2 27
#define ENABLE2_PIN 14
#define PWM_CHANNEL_2 1
const int freq = 30000;
const int resolution = 8;
typedef struct struct_message { // MUST MATCH SENDER STRUCT
uint16_t a;
uint16_t b;
} struct_message;
// remap w/ deadzone
int mapSpeed(int value) {
if (value >= 48) {
return map(value, 48, 100, 0, 255); // Forward (third balue (0) was 50)
} else if (value <= 45) {
return map(value, 45, 0, 0, -255); // Reverse (second 0 was -50)
}
return 0; // Dead zone
}
void driveMotor(int speed, int pin1, int pin2, int enablePin, int pwmChannel) {
if (speed > 0) { // Forward
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
ledcWrite(pwmChannel, speed);
} else if (speed < 0) { // Reverse
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
ledcWrite(pwmChannel, abs(speed)); // Ensure PWM is always positive
} else { // Stop
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
ledcWrite(pwmChannel, 0);
}
}
struct_message myData;
// When data received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("a: ");
Serial.print(myData.a);
Serial.print("\tb: ");
Serial.println(myData.b);
int speed1 = mapSpeed(myData.a); // Multiply by -1 if polarity/direction needes to change for motor1
int speed2 = mapSpeed(myData.b); // Multiply by -1 if polarity/direction needes to change for motor2
Serial.print("Motor 1 Speed: ");
Serial.print(speed1);
Serial.print("\tMotor 2 Speed: ");
Serial.println(speed2);
Serial.println();
Serial.println();
driveMotor(speed1, MOTOR1_PIN1, MOTOR1_PIN2, ENABLE1_PIN, PWM_CHANNEL_1);
driveMotor(speed2, MOTOR2_PIN1, MOTOR2_PIN2, ENABLE2_PIN, PWM_CHANNEL_2);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); // Set device as WiFi Station
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Rregister for recv CB to get recv packet info
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
// motor pins
pinMode(MOTOR1_PIN1, OUTPUT);
pinMode(MOTOR1_PIN2, OUTPUT);
pinMode(ENABLE1_PIN, OUTPUT);
pinMode(MOTOR2_PIN1, OUTPUT);
pinMode(MOTOR2_PIN2, OUTPUT);
pinMode(ENABLE2_PIN, OUTPUT);
// pwm channels
ledcSetup(PWM_CHANNEL_1, freq, resolution);
ledcSetup(PWM_CHANNEL_2, freq, resolution);
ledcAttachPin(ENABLE1_PIN, PWM_CHANNEL_1);
ledcAttachPin(ENABLE2_PIN, PWM_CHANNEL_2);
}
void loop() {}
// ============================================================================================================================================
// BEGIN ROBOT/RECEIVER CODE
// ============================================================================================================================================
// ============================================================================================================================================
// BEGIN MAC ADDRESS CODE
// ============================================================================================================================================
#include <Arduino.h>
#include <WiFi.h>
#include <esp_wifi.h>
void readMacAddress(){
uint8_t baseMac[6];
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
if (ret == ESP_OK) {
Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
baseMac[0], baseMac[1], baseMac[2],
baseMac[3], baseMac[4], baseMac[5]);
} else {
Serial.println("Failed to read MAC address");
}
}
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin();
Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
readMacAddress();
}
void loop(){
delay(1000);
Serial.print("[DEFAULT] ESP32 Board MAC Address: ");
readMacAddress();
Serial.println();
}
// ============================================================================================================================================
// END MAC ADDRESS CODE
// ============================================================================================================================================