1
|
/*
|
2
|
Progetto : 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
|
Autore : Cristina Begliomini
|
7
|
*/
|
8
|
#include <Servo.h>
|
9
|
|
10
|
Servo frontServo;
|
11
|
Servo rearServo;
|
12
|
/* Servo motors - global variables */
|
13
|
int centerPos = 90;
|
14
|
int frontRightUp = 72;
|
15
|
int frontLeftUp = 108;
|
16
|
int backRightForward = 75;
|
17
|
int backLeftForward = 105;
|
18
|
int walkSpeed = 150; // How long to wait between steps in milliseconds
|
19
|
int centerTurnPos = 96;
|
20
|
int frontTurnRightUp = 81;
|
21
|
int frontTurnLeftUp = 99;
|
22
|
int backTurnRightForward = 81;
|
23
|
int backTurnLeftForward = 111;
|
24
|
|
25
|
/* Misurazione della distanza - global variables */
|
26
|
int pingPin = 4;
|
27
|
long int duration;
|
28
|
long distanceFront=0; //cm
|
29
|
int startAvoidanceDistance=20; //distanza massima in cm per considerare un ostacolo
|
30
|
|
31
|
long microsecondsToCentimeters(long microseconds)
|
32
|
{
|
33
|
return microseconds / 29 / 2;
|
34
|
}
|
35
|
|
36
|
long distanceCm(){
|
37
|
pinMode(pingPin, OUTPUT);
|
38
|
digitalWrite(pingPin, LOW);
|
39
|
delayMicroseconds(2);
|
40
|
digitalWrite(pingPin, HIGH);
|
41
|
delayMicroseconds(5);
|
42
|
digitalWrite(pingPin, LOW);
|
43
|
|
44
|
pinMode(pingPin, INPUT);
|
45
|
duration = pulseIn(pingPin, HIGH);
|
46
|
|
47
|
return microsecondsToCentimeters(duration);
|
48
|
}
|
49
|
|
50
|
void moveForward()
|
51
|
{
|
52
|
frontServo.write(frontRightUp);
|
53
|
rearServo.write(centerPos);
|
54
|
delay(65);
|
55
|
rearServo.write(backLeftForward);
|
56
|
delay(125);
|
57
|
frontServo.write(centerPos);
|
58
|
delay(65);
|
59
|
frontServo.write(frontLeftUp);
|
60
|
rearServo.write(centerPos);
|
61
|
delay(65);
|
62
|
rearServo.write(backRightForward);
|
63
|
delay(125);
|
64
|
frontServo.write(centerPos);
|
65
|
delay(65);
|
66
|
}
|
67
|
|
68
|
void moveBackRight()
|
69
|
{
|
70
|
frontServo.write(frontRightUp);
|
71
|
rearServo.write(backRightForward-6);
|
72
|
delay(125);
|
73
|
frontServo.write(centerPos);
|
74
|
rearServo.write(centerPos-6);
|
75
|
delay(65);
|
76
|
frontServo.write(frontLeftUp+9);
|
77
|
rearServo.write(backLeftForward-6);
|
78
|
delay(125);
|
79
|
|
80
|
frontServo.write(centerPos);
|
81
|
rearServo.write(centerPos);
|
82
|
delay(65);
|
83
|
}
|
84
|
|
85
|
void moveTurnLeft()
|
86
|
{
|
87
|
frontServo.write(frontTurnRightUp);
|
88
|
rearServo.write(centerTurnPos);
|
89
|
delay(65);
|
90
|
rearServo.write(backTurnLeftForward);
|
91
|
delay(125);
|
92
|
frontServo.write(centerTurnPos);
|
93
|
delay(65);
|
94
|
frontServo.write(frontTurnLeftUp);
|
95
|
rearServo.write(centerTurnPos);
|
96
|
delay(65);
|
97
|
rearServo.write(backTurnRightForward);
|
98
|
delay(125);
|
99
|
frontServo.write(centerTurnPos);
|
100
|
delay(65);
|
101
|
}
|
102
|
|
103
|
void setup()
|
104
|
{
|
105
|
frontServo.attach(2);
|
106
|
rearServo.attach(3);
|
107
|
pinMode(pingPin, OUTPUT);
|
108
|
}
|
109
|
|
110
|
void loop()
|
111
|
{
|
112
|
distanceFront=distanceCm();
|
113
|
if (distanceFront > 1){ // Filters out any stray 0.00 error readings
|
114
|
if (distanceFront<startAvoidanceDistance) {
|
115
|
for(int i=0; i<=8; i++) {
|
116
|
moveBackRight();
|
117
|
delay(walkSpeed);
|
118
|
}
|
119
|
for(int i=0; i<=10; i++) {
|
120
|
distanceFront=distanceCm(); // inseramo un controllo della presenza di ostacoli anche in fase di cambio di direzione
|
121
|
if (distanceFront > 1){ // Filters out any stray 0.00 error readings
|
122
|
if (distanceFront<startAvoidanceDistance) {
|
123
|
moveBackRight();
|
124
|
delay(walkSpeed);
|
125
|
} else {
|
126
|
moveTurnLeft();
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
} else {
|
131
|
moveForward();
|
132
|
}
|
133
|
}
|
134
|
}
|
135
|
|