-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_control.cpp
182 lines (152 loc) · 4.33 KB
/
remote_control.cpp
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
/*
* remote_control.cpp
* Purple Mountain Majesties
* - Eddy Zhang, Alexander Lea, Connor Nightingale, Jacob Carulli
* 5/6/2024
*
*/
#include <Arduino.h>
#include <ArduinoMqttClient.h>
#include <WiFiNINA.h>
#include "remote_control.h"
#include "arduino_secrets.h"
#include "pins.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID2; // your network SSID
char pass[] = SECRET_PASS2; // your network password
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
const char broker[] = "10.5.15.62";
int port = 1883;
const char topic[] = "Remote";
/* Function: runRemote
* Description: Call this function to drive the bot remotely via MQTT
* Parameters:
* - None
* Returns: None
*/
void runRemote() {
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(broker);
if (!mqttClient.connect(broker, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
while (1);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
// set the message receive callback
mqttClient.onMessage(onMqttMessage);
Serial.print("Subscribing to topic: ");
Serial.println(topic);
Serial.println();
// subscribe to a topic
mqttClient.subscribe(topic);
// topics can be unsubscribed using:
// mqttClient.unsubscribe(topic);
Serial.print("Topic: ");
Serial.println(topic);
Serial.println();
while (true) {
// call poll() regularly to allow the library to receive MQTT messages and
// send MQTT keep alive which avoids being disconnected by the broker
mqttClient.poll();
}
}
/* Function: onMqttMessage
* Description: Function called when the mqtt client reads a new message
* Parameters:
* - messageSize: Size of incoming message, required by mqttClient
* Returns: None
*/
void onMqttMessage(int messageSize) {
// Read the message into a buffer
char message[messageSize + 1];
for (int i = 0; i < messageSize; i++) {
message[i] = (char)mqttClient.read();
}
message[messageSize] = '\0'; // Null terminate the string
// Parse the message as two int numbers
int x, y;
if (sscanf(message, "%d,%d", &x, &y) != 2) {
Serial.println("Failed to parse message");
return;
}
y = -y;
int speed = sqrt(x * x + y * y);
if (abs(y) > abs(x)) {
if (y > 0) {
forward(speed);
Serial.println("forward " + String(speed));
} else {
back(speed);
Serial.println("back" + String(speed));
}
} else {
if (x > 0) {
right(speed);
Serial.println("right" + String(speed));
} else {
left(speed);
Serial.println("left" + String(speed));
}
}
}
/* Function: forward
* Description: Drive forward at the given speed
* Parameters:
* - speed: Integer between 0 and 100 which controls speed of bot
* Returns: None
*/
void forward(int speed) {
analogWrite(Pins::motor1, 0);
analogWrite(Pins::motor2, speed);
analogWrite(Pins::motor3, int(speed * 1.42));
analogWrite(Pins::motor4, 0);
}
/* Function: left
* Description: Turn left at the given speed
* Parameters:
* - speed: Integer between 0 and 100 which controls speed of bot
* Returns: None
*/
void left(int speed) {
analogWrite(Pins::motor1, speed);
analogWrite(Pins::motor2, 0);
analogWrite(Pins::motor3, speed);
analogWrite(Pins::motor4, 0);
}
/* Function: back
* Description: Drive backward at the given speed
* Parameters:
* - speed: Integer between 0 and 100 which controls speed of bot
* Returns: None
*/
void back(int speed) {
analogWrite(Pins::motor1, speed);
analogWrite(Pins::motor2, 0);
analogWrite(Pins::motor3, 0);
analogWrite(Pins::motor4, int(speed * 1.36));
}
/* Function: right
* Description: Turn right at the given speed
* Parameters:
* - speed: Integer between 0 and 100 which controls speed of bot
* Returns: None
*/
void right(int speed) {
analogWrite(Pins::motor1, 0);
analogWrite(Pins::motor2, speed);
analogWrite(Pins::motor3, 0);
analogWrite(Pins::motor4, speed);
}