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