-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotPong.c
266 lines (224 loc) · 5.8 KB
/
RobotPong.c
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
#include "SoftwareSerial.h"
#define CA1 A0
#define CA2 A1
#define CA3 A2
#define CA4 A3
#define BTTRY A4
#define A_PH 8
#define A_EN 9
#define B_PH 10
#define B_EN 11
#define FORWARD 0
#define BACKWARD 1
#define RIGHT 1
#define LEFT 0
#define STX 0x02
#define ETX 0x03
#define ledPin 13
#define SLOW 750 // Datafields refresh rate (ms)
#define FAST 250 // Datafields refresh rate (ms)
int vod = 0; // 1 - Top 2 - Bot 3 - Right 4 - Left 0 - Reset
void sensorTop();
void sensorBot();
void sensorRight();
void sensorLeft();
void controlMotor(int joyX, int joyY);
// BlueTooth module: pin#2=TX pin#3=RX
byte cmd[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // bytes received
byte buttonStatus = 0; // first Byte sent to Android device
long previousMillis = 0; // will store last time Buttons status was updated
long sendInterval = SLOW; // interval between Buttons status transmission (milliseconds)
String displayStatus = "xxxx"; // message to Android device
void setup() {
Serial.begin(115200);
Serial.begin(115200);
while(Serial.available()) Serial.read(); // empty RX buffer
pinMode(A_PH, OUTPUT);
pinMode(A_EN, OUTPUT);
pinMode(B_EN, OUTPUT);
pinMode(B_PH, OUTPUT);
}
void loop() {
if(Serial.available()) { // data received from smartphone
delay(2);
cmd[0] = Serial.read();
if(cmd[0] == STX) {
int i=1;
while(Serial.available()) {
delay(1);
cmd[i] = Serial.read();
if(cmd[i]>127 || i>7) break; // Communication error
if((cmd[i]==ETX) && (i==2 || i==7)) break; // Button or Joystick data
i++;
}
if (i==2) getButtonState(cmd[1]); // 3 Bytes ex: < STX "C" ETX >
else if(i==7) getJoystickState(cmd); // 6 Bytes ex: < STX "200" "180" ETX >
}
}
sendBlueToothData();
}
void sendBlueToothData() {
static long previousMillis = 0;
long currentMillis = millis();
if(currentMillis - previousMillis > sendInterval) {
previousMillis = currentMillis;
Serial.print((char)STX);
Serial.print(getButtonStatusString()); Serial.print((char)0x1);
Serial.print(GetdataInt1()); Serial.print((char)0x4);
Serial.print(GetdataFloat2()); Serial.print((char)0x5);
Serial.print(displayStatus);
Serial.print((char)ETX);
}
}
String getButtonStatusString() {
String bStatus = "";
for(int i=0; i<6; i++) {
if(buttonStatus & (B100000 >>i)) bStatus += "1";
else bStatus += "0";
}
return bStatus;
}
int GetdataInt1() { // Data dummy values sent to Android device for demo purpose
static int i= -30; // Replace with your own code
i ++;
if(i >0) i = -30;
return i;
}
float GetdataFloat2() { // Data dummy values sent to Android device for demo purpose
static float i=50; // Replace with your own code
i-=.5;
if(i <-50) i = 50;
return i;
}
void getJoystickState(byte data[8]) {
int joyX = (data[1]-48)*100 + (data[2]-48)*10 + (data[3]-48);
int joyY = (data[4]-48)*100 + (data[5]-48)*10 + (data[6]-48);
joyX = joyX - 100;
joyY = joyY - 100;
joyX = map(joyX, 0, 200, 0, 256);
joyY = map(joyY, 0, 200, 0, 256);
controlMotor(joyX, joyY);
}
void getButtonState(int bStatus) {
switch (bStatus) {
// ----------------- BUTTON #1 -----------------------
case 'A':
buttonStatus |= B000001;
// Auto
break;
case 'B':
buttonStatus &= B111110;
// Manu
break;
// ----------------- BUTTON #2 -----------------------
case 'C':
buttonStatus |= B000010;
break;
case 'D':
buttonStatus &= B111101;
break;
// ----------------- BUTTON #3 -----------------------
case 'E':
buttonStatus |= B000100;
break;
case 'F':
buttonStatus &= B111011;
break;
// ----------------- BUTTON #4 -----------------------
case 'G':
buttonStatus |= B001000;
break;
case 'H':
buttonStatus &= B110111;
sendInterval = SLOW;
break;
// ----------------- BUTTON #6 -----------------------
case 'K':
buttonStatus |= B100000;
break;
case 'L':
buttonStatus &= B011111;
break;
}
}
void controlMotor(int joyX, int joyY) {
boolean sens, dir;
int16_t speedL, speedR;
uint8_t x = joyX;
uint8_t y = joyY;
Serial.print(x);
Serial.print(" ");
Serial.println(y);
if (x > 128 - 10 && x < 128 + 10)
x = 128;
if (y > 128 - 10 && y < 128 + 10)
y = 128;
if (x > 128)
{
sens = BACKWARD;
x = x - 128;
}
else
{
x = 128 - x;
sens = FORWARD;
}
if (y > 128)
{
dir = RIGHT;
y = y - 128;
}
else
{
y = 128 - y;
dir = LEFT;
}
if (dir == LEFT)
{
speedL = 2*x - y*2;
speedR = 2*x + y*2;
}
else
{
speedL = 2*x + y*2;
speedR = 2*x - y*2;
}
if (sens == FORWARD)
{
digitalWrite (A_PH, HIGH);
digitalWrite (B_PH, HIGH);
}
else
{
digitalWrite (A_PH, LOW);
digitalWrite (B_PH, LOW);
}
if (speedL < 0)
{
digitalWrite (A_PH, LOW);
speedL = speedL*-1;
}
if (speedR < 0)
{
digitalWrite (B_PH, LOW);
speedR = speedR *-1;
}
if (speedL > 255)
speedL = 255;
if (speedR > 255)
speedR = 255;
analogWrite (A_EN, speedL);
analogWrite (B_EN, speedR);
}
void sensorTop() {
vod = 1;
}
void sensorBot() {
vod = 2;
}
void sensorRight() {
vod = 3;
}
void sensorLeft() {
vod = 4;
}