1
|
/*
|
2
|
Progetto : BoBeBo (Insetto Robot) - HackLab Terni
|
3
|
Il robot controlla la presenza di ostacoli, se ci sono cambia direzione, altrimenti procede in avanti.
|
4
|
Luglio 2012
|
5
|
Modificato: Ottobre 2012
|
6
|
Modificato: Marzo 2013
|
7
|
Inserito un arresto al momento del riconoscimento di un ostacolo.
|
8
|
Rallentato il movimento nell'andatura in avanti.
|
9
|
Modificato: Gennaio 2014
|
10
|
Autore : Cristina Begliomini
|
11
|
*/
|
12
|
#include <Servo.h>
|
13
|
|
14
|
Servo frontServo;
|
15
|
Servo rearServo;
|
16
|
/* Servo motors - global variables */
|
17
|
int centerPos = 90;
|
18
|
int frontRightUp = 72;
|
19
|
int frontLeftUp = 108;
|
20
|
int backRightForward = 75;
|
21
|
int backLeftForward = 105;
|
22
|
int walkSpeed = 150; // How long to wait between steps in milliseconds
|
23
|
int centerTurnPos = 96;
|
24
|
int frontTurnRightUp = 81;
|
25
|
int frontTurnLeftUp = 99;
|
26
|
int backTurnRightForward = 81;
|
27
|
int backTurnLeftForward = 111;
|
28
|
|
29
|
/* Misurazione della distanza - global variables */
|
30
|
int pingPin = 4;
|
31
|
long int duration;
|
32
|
long distanceFront=0; //cm
|
33
|
int startAvoidanceDistance=20; //distanza massima in cm per considerare un ostacolo
|
34
|
|
35
|
int danger = 0; // no obstacle
|
36
|
int frontRight = 0; // front right leg in low position
|
37
|
int slowMotion = 50; // delay amount for moving forwards
|
38
|
|
39
|
long microsecondsToCentimeters(long microseconds)
|
40
|
{
|
41
|
return microseconds / 29 / 2;
|
42
|
}
|
43
|
|
44
|
long distanceCm(){
|
45
|
pinMode(pingPin, OUTPUT);
|
46
|
digitalWrite(pingPin, LOW);
|
47
|
delayMicroseconds(2);
|
48
|
digitalWrite(pingPin, HIGH);
|
49
|
delayMicroseconds(5);
|
50
|
digitalWrite(pingPin, LOW);
|
51
|
|
52
|
pinMode(pingPin, INPUT);
|
53
|
duration = pulseIn(pingPin, HIGH);
|
54
|
|
55
|
return microsecondsToCentimeters(duration);
|
56
|
}
|
57
|
|
58
|
void moveForward()
|
59
|
{
|
60
|
int frontPos = frontServo.read(); //checks frontServo position
|
61
|
int rearPos = rearServo.read(); //checks rearServo position
|
62
|
if (frontRight==0) { // moves front right leg up
|
63
|
for (; frontPos >= frontRightUp; frontPos--) {
|
64
|
frontServo.write(frontPos);
|
65
|
delay(slowMotion);
|
66
|
}
|
67
|
distanceFront=distanceCm();
|
68
|
if (distanceFront > 1){ // Filters out any stray 0.00 error readings
|
69
|
if (distanceFront > startAvoidanceDistance) {
|
70
|
for (; rearPos <= backLeftForward; rearPos++) {
|
71
|
rearServo.write(rearPos);
|
72
|
delay(slowMotion);
|
73
|
}
|
74
|
frontRight = 1;
|
75
|
}
|
76
|
}
|
77
|
} else { // moves front left leg up
|
78
|
for (; frontPos <= frontLeftUp; frontPos++) {
|
79
|
frontServo.write(frontPos);
|
80
|
delay(slowMotion);
|
81
|
}
|
82
|
distanceFront=distanceCm();
|
83
|
if (distanceFront > 1){ // Filters out any stray 0.00 error readings
|
84
|
if (distanceFront > startAvoidanceDistance) {
|
85
|
for (; rearPos >= backRightForward; rearPos--) {
|
86
|
rearServo.write(rearPos);
|
87
|
delay(slowMotion);
|
88
|
}
|
89
|
frontRight = 0;
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
|
95
|
void moveBackRight()
|
96
|
{
|
97
|
frontServo.write(frontRightUp);
|
98
|
rearServo.write(backRightForward-6);
|
99
|
delay(125);
|
100
|
frontServo.write(centerPos);
|
101
|
rearServo.write(centerPos-6);
|
102
|
delay(65);
|
103
|
frontServo.write(frontLeftUp+9);
|
104
|
rearServo.write(backLeftForward-6);
|
105
|
delay(125);
|
106
|
|
107
|
frontServo.write(centerPos);
|
108
|
rearServo.write(centerPos);
|
109
|
delay(65);
|
110
|
}
|
111
|
|
112
|
void moveTurnLeft()
|
113
|
{
|
114
|
frontServo.write(frontTurnRightUp);
|
115
|
rearServo.write(centerTurnPos);
|
116
|
delay(65);
|
117
|
rearServo.write(backTurnLeftForward);
|
118
|
delay(125);
|
119
|
frontServo.write(centerTurnPos);
|
120
|
delay(65);
|
121
|
frontServo.write(frontTurnLeftUp);
|
122
|
rearServo.write(centerTurnPos);
|
123
|
delay(65);
|
124
|
rearServo.write(backTurnRightForward);
|
125
|
delay(125);
|
126
|
frontServo.write(centerTurnPos);
|
127
|
delay(65);
|
128
|
}
|
129
|
|
130
|
void setup()
|
131
|
{
|
132
|
frontServo.attach(2);
|
133
|
rearServo.attach(3);
|
134
|
pinMode(pingPin, OUTPUT);
|
135
|
}
|
136
|
|
137
|
void loop()
|
138
|
{
|
139
|
distanceFront=distanceCm();
|
140
|
if (distanceFront > 1){ // Filters out any stray 0.00 error readings
|
141
|
if (distanceFront<startAvoidanceDistance) {
|
142
|
if (danger==0) { // stops at first obstacle detection
|
143
|
delay(1000);
|
144
|
danger = 1;
|
145
|
}
|
146
|
for(int i=0; i<=8; i++) {
|
147
|
moveBackRight();
|
148
|
delay(walkSpeed);
|
149
|
}
|
150
|
for(int i=0; i<=10; i++) {
|
151
|
distanceFront=distanceCm(); // inseramo un controllo della presenza di ostacoli anche in fase di cambio di direzione
|
152
|
if (distanceFront > 1){ // Filters out any stray 0.00 error readings
|
153
|
if (distanceFront<startAvoidanceDistance) {
|
154
|
moveBackRight();
|
155
|
delay(walkSpeed);
|
156
|
} else {
|
157
|
danger = 0;
|
158
|
moveTurnLeft();
|
159
|
}
|
160
|
}
|
161
|
}
|
162
|
} else {
|
163
|
moveForward();
|
164
|
}
|
165
|
}
|
166
|
}
|
167
|
|