-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadripod.ino
231 lines (191 loc) · 4.41 KB
/
quadripod.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
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
#include <SoftwareSerial.h>
#include <NewPing.h>
#include <Servo.h>
Servo waist,front,back,radar;
#define TRIGGER_PIN 7
#define ECHO_PIN 8
#define ledPin 2
#define ARDU_TX 4
#define ARDU_RX 5
#define looptime 8
NewPing sonar(TRIGGER_PIN, ECHO_PIN, 100);
//Serial connexion for bluetooth module
SoftwareSerial BT(ARDU_RX, ARDU_TX);
// adjust values here to center your servos
#define wCenter 87
#define fCenter 90
#define bCenter 105
#define rCenter 90
#define rSwing 40
#define wSwing 17
#define fSwing 27
#define bSwing 27
#define FORWARD 0
#define BACKWARD 1
#define LEFT 2
#define RIGHT 3
#define STOP 4
#define SPEED_CMD_CHAR 's'
#define LEFT_CMD_CHAR 'l'
#define RIGHT_CMD_CHAR 'r'
#define FORWARD_CMD_CHAR 'f'
int wPos,fPos,bPos,cycle,dir,rPos, superCycle,robotspeed;
boolean evitement;
String inText;
void setup()
{
pinMode(ledPin, OUTPUT);
waist.attach(13);
front.attach(10);
back.attach(11);
radar.attach(9);
BT.begin(9600);
BT.println("Robot connected !");
BT.flush();
wPos=wCenter;
fPos=fCenter;
bPos=bCenter;
rPos=rCenter;
waist.write(wPos);
front.write(fPos);
back.write(bPos);
radar.write(rPos);
delay(5000);
dir=FORWARD;
cycle=0;
superCycle=0;
evitement = 0;
robotspeed=1; //lower is faster
}
long readDist(){
long distance, newdist;
radar.write(rCenter-rSwing);
delay(170);
distance = sonar.ping_cm();
if (distance== 0) { distance=200; }
delay(20);
radar.write(rCenter);
delay(170);
newdist = sonar.ping_cm();
if (newdist == 0) { newdist=200; }
delay(20);
if (newdist<distance){distance = newdist;}
radar.write(rCenter+rSwing);
delay(170);
newdist = sonar.ping_cm();
if (newdist == 0) { newdist=200; }
delay(20);
if (newdist<distance){distance = newdist;}
if (!distance) { distance=200; }
radar.write(rCenter);
return distance;
}
void doBackward(int cycle) {
if (cycle<12) wPos=wCenter+wSwing;
else if (cycle<37) wPos=wCenter-wSwing;
else if (cycle<62) wPos=wCenter+wSwing;
else if (cycle<87) wPos=wCenter-wSwing;
else wPos=wCenter+wSwing;
if (cycle<25) fPos=fCenter+fSwing;
else if (cycle<50) fPos=fCenter-fSwing;
else if (cycle<75) fPos=fCenter+fSwing;
else fPos=fCenter-fSwing;
if (cycle<25) bPos=bCenter-bSwing;
else if (cycle<50) bPos=bCenter+bSwing;
else if (cycle<75) bPos=bCenter-bSwing;
else bPos=bCenter+bSwing;
}
void doForward(int cycle) {
doBackward(100-cycle);
}
void doLeft(int cycle) {
doBackward(100-cycle);
if (wPos>wCenter) wPos=wCenter;
}
void doRight(int cycle) {
doBackward(100-cycle);
if (wPos<wCenter) wPos=wCenter;
}
void doStop(){
wPos=wCenter;
fPos=fCenter;
bPos=bCenter;
}
void posUpdate() {
cycle=(cycle+1)%100;
switch(dir) {
case FORWARD: doForward(cycle); break;
case BACKWARD: doBackward(cycle); break;
case LEFT: doLeft(cycle); break;
case RIGHT: doRight(cycle); break;
case STOP: doStop(); break;
}
}
void loop()
{
long distance;
char a;
int ard_command = 0;
posUpdate();
waist.write(wPos);
front.write(fPos);
back.write(bPos);
// lets manage with bluetooth
// BT.flush();
char get_char = ' '; //read serial
// wait for incoming data
if (BT.available() > 0) {
get_char = BT.read();
if (get_char == SPEED_CMD_CHAR) {
BT.println("speed command ");
ard_command = BT.parseInt();
if (ard_command != 0)
{
robotspeed=(int)ard_command;
BT.println("Speed set to : ");
BT.println(robotspeed);
}
} else if(get_char == LEFT_CMD_CHAR) {
BT.println("left command");
dir=LEFT;
} else if(get_char == RIGHT_CMD_CHAR) {
BT.println("right command");
dir=RIGHT;
} else if(get_char == FORWARD_CMD_CHAR) {
BT.println("FORWARD command");
dir=FORWARD;
}
}
if (cycle==5){
// No delay
if (dir==FORWARD) {
distance = readDist();
}
} else {
if(dir==LEFT or dir==RIGHT){
delay(looptime*robotspeed);
}
else {
delay(2*looptime*robotspeed);
}
}
if (evitement){
digitalWrite(ledPin, HIGH);
if (superCycle < 200) {
dir = BACKWARD;
} else {
dir = LEFT;
}
superCycle++;
if (superCycle >800){
evitement = 0;
superCycle=0;
dir= FORWARD;
digitalWrite(ledPin, LOW);
}
}
if (distance < 40){
digitalWrite(ledPin, HIGH);
evitement = 1;
}
}