This repository was archived by the owner on Jan 28, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMSPRobot.ino
More file actions
200 lines (161 loc) · 4.62 KB
/
Copy pathMSPRobot.ino
File metadata and controls
200 lines (161 loc) · 4.62 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
#include "Romi_Motor_Power.h"
#include "RSLK_Pins.h"
#include "SimpleRSLK.h"
#include <driverlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SPI.h>
#include <WiFi.h>
#include <PubSubClient.h>
int count = 0;
int toggle = 0;
// WIFI METHODS AND VARIABLES
char ssid[] = "eec172";
char server[] = "io.adafruit.com";
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
uint8_t direction, speed;
void callback(char* topic, byte* payload, unsigned int length) {
char* str = (char*) payload;
if (strcmp(topic, "ryanachen/feeds/msp-robot-direction") == 0) {
direction = str[0] - '0';
} else if (strcmp(topic, "ryanachen/feeds/speed") == 0) {
speed = str[0] - '0';
}
}
void pollBroker() {
while (!client.connected()) {
Serial.print("Connecting... ");
if (!client.connect("energiaClient", "ryanachen", "aio_uWnL05y5P3xSEhlR4PUiSAn5AkUz")) {
Serial.println("Connection failed.");
} else {
Serial.print("Connection successful. ");
if (client.subscribe("ryanachen/feeds/msp-robot-direction")) {
Serial.print("Direction successful. ");
if (client.subscribe("ryanachen/feeds/speed")) {
Serial.println("Speed successful.");
break;
} else {
Serial.println("Speed failed.");
}
} else {
Serial.println("Direction failed.");
}
}
delay(1000);
}
client.poll();
}
void wifiInit() {
// try to connect to network
Serial.print("Attempting to connect to network named: ");
Serial.print(ssid);
WiFi.begin(ssid);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(300);
}
// wait for IP address
Serial.println("\nNow connected to a network.");
Serial.print("Waiting for an IP address");
while (WiFi.localIP() == INADDR_NONE) {
Serial.print(".");
delay(300);
}
// print wifi status
Serial.println("\nIP Address obtained.\n");
}
// ROBOT MOTOR METHODS AND VARIABLES
Romi_Motor_Power left_motor;
Romi_Motor_Power right_motor;
int8_t lSpeed, rSpeed;
// allows negative speed values by changing motor direction
void moveBot() {
if (lSpeed >= 0) left_motor.directionForward();
else left_motor.directionBackward();
if (rSpeed >= 0) right_motor.directionForward();
else right_motor.directionBackward();
left_motor.setSpeed(speed*abs(lSpeed));
right_motor.setSpeed(speed*abs(rSpeed));
}
void moveLeft() { lSpeed = -10; rSpeed = 10; moveBot(); }
void moveRight() { lSpeed = 10; rSpeed = -10; moveBot(); }
void moveForward() { lSpeed = 20; rSpeed = 20; moveBot(); }
void moveBackward() { lSpeed = -20; rSpeed = -20; moveBot(); }
void stopBot() { lSpeed = 0; rSpeed = 0; moveBot(); }
void setup() {
WDT_A_hold(WDT_A_BASE);
Serial.begin(115200);
left_motor.begin(MOTOR_L_SLP_PIN, MOTOR_L_DIR_PIN, MOTOR_L_PWM_PIN);
right_motor.begin(MOTOR_R_SLP_PIN, MOTOR_R_DIR_PIN, MOTOR_R_PWM_PIN);
pinMode(BP_SW_PIN_0, INPUT_PULLUP);
pinMode(BP_SW_PIN_1, INPUT_PULLUP);
pinMode(BP_SW_PIN_2, INPUT_PULLUP);
pinMode(BP_SW_PIN_3, INPUT_PULLUP);
pinMode(BP_SW_PIN_4, INPUT_PULLUP);
pinMode(BP_SW_PIN_5, INPUT_PULLUP);
stopBot();
speed = 1;
left_motor.enableMotor();
right_motor.enableMotor();
wifiInit();
}
void loop() {
pollBroker();
switch (direction) {
case 0:
stopBot();
break;
case 1:
moveForward();
break;
case 2:
moveBackward();
break;
case 3:
moveLeft();
break;
case 4:
moveRight();
break;
}
if (digitalRead(BP_SW_PIN_0) == 0 ||
digitalRead(BP_SW_PIN_1) == 0 ||
digitalRead(BP_SW_PIN_2) == 0 ||
digitalRead(BP_SW_PIN_3) == 0 ||
digitalRead(BP_SW_PIN_4) == 0 ||
digitalRead(BP_SW_PIN_5) == 0) {
Serial.println("Collision detected");
stopBot();
}
Serial.print(direction);
Serial.print(" ");
Serial.println(speed);
int tempSensor = analogRead(A19);
float tempC = ((3.3*tempSensor/1023.0))/0.01;
float tempF = tempC*(9.0/5.0)+32.0;
Serial.println(tempF); // degrees Fahrenheit
int aqiSensor = analogRead(A18);
Serial.println(aqiSensor); // ppm CO2
if(count >= 6){
if(toggle == 0){
char str[50];
sprintf(str, "%0.2f", tempF);
if(client.publish("ryanachen/feeds/msp-robot.temperature", str)){
Serial.println("Temp Publish Succeeded");
}
toggle = 1;
}else if(toggle == 1){
char str[50];
sprintf(str, "%d", aqiSensor);
if(client.publish("ryanachen/feeds/msp-robot.air-quality", str)){
Serial.println("AQ Publish Succeeded");
}
toggle = 0;
}
count = 0;
}
count++;
delay(500);
}