1
1
// #define DEBUG
2
2
3
+ #define SENSOR_DELAY 10
4
+
5
+ #define MAX_QUIRKS 3
6
+
3
7
#define ENA_PIN 10
4
- #define ENB_PIN 9
8
+ #define ENB_PIN 11
5
9
#define IN1_PIN 2
6
10
#define IN2_PIN 3
7
11
#define IN3_PIN 5
20
24
#define RPCT 90
21
25
#define LPCT 100
22
26
23
- const byte initSpd = 128 ; // speed to start on the first loop to gain acceleration
24
- const byte moveSpd = 64 ; // speed to continue after first loop passed and we gained initial acceleration
27
+ const byte initSpd = 192 ; // speed to start on the first loop to gain acceleration
28
+ const byte moveSpd = 128 ; // speed to continue after first loop passed and we gained initial acceleration
25
29
26
30
const int turnDist = 40 ; // distance in cm on which normal turn will be triggered to avoid collision
27
31
const int spinDist = 15 ; // distance in cm on which obstacle is considered too close and radical spin or pulling back will be triggered
@@ -39,6 +43,9 @@ int prd;
39
43
int pld;
40
44
char pdir;
41
45
46
+ // quirk counter - triggers "circuir breaker" on reaching MAX_QUIRKS
47
+ int qc = 0 ;
48
+
42
49
void setup () {
43
50
#ifdef DEBUG
44
51
Serial.begin (9600 );
@@ -96,17 +103,22 @@ int lDist() {
96
103
}
97
104
98
105
char chooseDirection (int cdist, int rdist, int ldist) {
99
- // don't keep turning right or left more than one loop step
100
- if ((pdir == _RHT) or (pdir == _LFT)) {
101
- return _FWD;
102
- }
103
-
104
- // below "0" is a sensor quirk: continue with previously chosen direction in this case
106
+ // below or equal "0" is a sensor quirk ...
105
107
if ((cdist <= 0 ) or (rdist <= 0 ) or (ldist <= 0 )) {
106
- return pdir;
108
+ // ... continue with previously chosen direction before reaching MAX_QUIRKS
109
+ if (qc < MAX_QUIRKS) {
110
+ qc++;
111
+ return pdir;
112
+ }
113
+ // ... pull back after reaching MAX_QUIRKS
114
+ qc = 0 ;
115
+ return _BCK;
116
+ } else {
117
+ // reset quirk counter if sensor quirk is not the case
118
+ qc = 0 ;
107
119
}
108
120
109
- // we got stuck STUCK_LOOPS times in a row : spin left or right ...
121
+ // we got stuck: spin left or right ...
110
122
if ((cdist == pcd) and (rdist == prd) and (ldist == pld)) {
111
123
// ... or just pull back if we already were spinning
112
124
if ((pdir == _SPR) or (pdir == _SPL)) {
@@ -138,6 +150,11 @@ char chooseDirection(int cdist, int rdist, int ldist) {
138
150
return _SPR;
139
151
}
140
152
153
+ // don't keep turning right or left more than one loop step
154
+ if ((pdir == _RHT) or (pdir == _LFT)) {
155
+ return _FWD;
156
+ }
157
+
141
158
// when obstacle detected: decide to turn left or right
142
159
if (cdist < turnDist) {
143
160
if (rdist < ldist) {
@@ -255,11 +272,11 @@ void stopMovement(int spd) {
255
272
256
273
void loop () {
257
274
int cd = cDist ();
258
- delay (33 );
275
+ delay (SENSOR_DELAY );
259
276
int rd = rDist ();
260
- delay (33 );
277
+ delay (SENSOR_DELAY );
261
278
int ld = lDist ();
262
- delay (33 );
279
+ delay (SENSOR_DELAY );
263
280
264
281
char dir = chooseDirection (cd, rd, ld);
265
282
byte spd = getSpeed (dir);
0 commit comments