-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBalancingRobotRemote.ino
161 lines (149 loc) · 4.9 KB
/
BalancingRobotRemote.ino
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
/*
* The code is released under the GNU General Public License.
* Developed by Kristian Lauszus
* This is the remote control code for my balancing robot: https://github.com/TKJElectronics/BalancingRobot
* It uses my PS3 Controller Bluetooth library for Arduino: https://github.com/TKJElectronics/USB_Host_Shield_2.0
* For details, see http://blog.tkjelectronics.dk/2012/02/the-balancing-robot/
*/
#include <PS3BT.h>
#include "steer.h"
USB Usb;
PS3BT BT(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57);
double targetAngle = 90;
int lastDirection;
void setup()
{
Serial.begin(115200);
if (Usb.Init() == -1)
while(1); // halt
}
void loop()
{
Usb.Task();
if(BT.PS3BTConnected) {
if(BT.getButton(PS)) {
steer(stop);
BT.disconnect();
} else if(BT.getButton(SELECT))
steer(shutdown);
else if(BT.getButton(START))
steer(resume);
if(BT.getButton(UP)) { // It is only necessary to adjust the target angle if the encoders are not implemented
targetAngle -= 0.05;
Serial.print("T,");
Serial.print(targetAngle);
Serial.print(";");
while(BT.getButton(UP)) // Wait for release
Usb.Task();
} else if(BT.getButton(DOWN)) {
targetAngle += 0.05;
Serial.print("T,");
Serial.print(targetAngle);
Serial.print(";");
while(BT.getButton(DOWN)) // Wait for release
Usb.Task();
}
if(BT.getAnalogHat(LeftHatY) > 137) {
if(BT.getAnalogHat(RightHatY) > 137)
steer(backward);
else if(BT.getAnalogHat(RightHatY) < 117)
steer(leftRotate);
else
steer(left);
} else if(BT.getAnalogHat(LeftHatY) < 117) {
if(BT.getAnalogHat(RightHatY) < 117)
steer(forward);
else if(BT.getAnalogHat(RightHatY) > 137)
steer(rightRotate);
else
steer(right);
} else if(BT.getAnalogHat(RightHatY) > 137) {
steer(right);
} else if(BT.getAnalogHat(RightHatY) < 117) {
steer(left);
} else
steer(stop);
} else if(BT.PS3NavigationBTConnected) {
if(BT.getButton(PS)) {
steer(stop);
BT.disconnect();
}
if(BT.getButton(UP)) { // It is only necessary to adjust the target angle if the encoders are not implemented
targetAngle -= 0.05;
Serial.print("T,");
Serial.print(targetAngle);
Serial.print(";");
while(BT.getButton(UP)) // Wait for release
Usb.Task();
} else if(BT.getButton(DOWN)) {
targetAngle += 0.05;
Serial.print("T,");
Serial.print(targetAngle);
Serial.print(";");
while(BT.getButton(DOWN)) // Wait for release
Usb.Task();
}
if(BT.getAnalogHat(LeftHatX) > 250)
steer(rightRotate);
else if(BT.getAnalogHat(LeftHatX) > 200)
steer(right);
else if(BT.getAnalogHat(LeftHatX) < 5)
steer(leftRotate);
else if(BT.getAnalogHat(LeftHatX) < 55)
steer(left);
else if(BT.getAnalogHat(LeftHatY) > 137)
steer(backward);
else if(BT.getAnalogHat(LeftHatY) < 117)
steer(forward);
else
steer(stop);
} else
steer(stop);
}
void steer(steerDirection direction) {
if(BT.PS3BTConnected) {
if(direction == forward) { // It should keep sending the speed
double speed = (double)(map(BT.getAnalogHat(LeftHatY),116,0,0,7) + map(BT.getAnalogHat(RightHatY),116,0,0,7))/2; // calculate the average
Serial.print("F,");
Serial.print(speed);
Serial.print(";");
delay(10);
} else if(direction == backward) { // It should keep sending the speed
double speed = (double)(map(BT.getAnalogHat(LeftHatY),138,255,0,7) + map(BT.getAnalogHat(RightHatY),138,255,0,7))/2; // calculate the average
Serial.print("B,");
Serial.print(speed);
Serial.print(";");
delay(10);
}
}
else if(BT.PS3NavigationBTConnected) {
if(direction == forward) { // It should keep sending the speed
double speed = map(BT.getAnalogHat(LeftHatY),116,0,0,7);
Serial.print("F,");
Serial.print(speed);
Serial.print(";");
delay(10);
} else if(direction == backward) { // It should keep sending the speed
double speed = map(BT.getAnalogHat(LeftHatY),138,255,0,7);
Serial.print("B,");
Serial.print(speed);
Serial.print(";");
delay(10);
}
}
if(direction == left && lastDirection != left)
Serial.print("L;");
else if(direction == leftRotate && lastDirection != leftRotate)
Serial.print("LR;");
else if(direction == right && lastDirection != right)
Serial.print("R;");
else if(direction == rightRotate && lastDirection != rightRotate)
Serial.print("RR;");
else if(direction == stop && lastDirection != stop)
Serial.print("S;");
else if(direction == shutdown && lastDirection != shutdown)
Serial.print("A;"); // Abort
else if(direction == resume && lastDirection != resume)
Serial.print("C;"); // Continue
lastDirection = direction;
}