Skip to content

Commit 3cacb83

Browse files
committed
Merge branch 'smart-car-feature-pack'
* smart-car-feature-pack: chore: adjust pins and speeds feat: add "curcuit breaker" to avoid getting stuck chore: align "one turn only" rule priority chore: correct "we got stuck" comment feat: increase movement speed feat: decrease sensor delay to 10ms feat: make sensor delay configurable
2 parents f874f76 + acadc7e commit 3cacb83

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

smart-car/smart-car.ino

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
//#define DEBUG
22

3+
#define SENSOR_DELAY 10
4+
5+
#define MAX_QUIRKS 3
6+
37
#define ENA_PIN 10
4-
#define ENB_PIN 9
8+
#define ENB_PIN 11
59
#define IN1_PIN 2
610
#define IN2_PIN 3
711
#define IN3_PIN 5
@@ -20,8 +24,8 @@
2024
#define RPCT 90
2125
#define LPCT 100
2226

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
2529

2630
const int turnDist = 40; // distance in cm on which normal turn will be triggered to avoid collision
2731
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;
3943
int pld;
4044
char pdir;
4145

46+
// quirk counter - triggers "circuir breaker" on reaching MAX_QUIRKS
47+
int qc = 0;
48+
4249
void setup() {
4350
#ifdef DEBUG
4451
Serial.begin(9600);
@@ -96,17 +103,22 @@ int lDist() {
96103
}
97104

98105
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 ...
105107
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;
107119
}
108120

109-
// we got stuck STUCK_LOOPS times in a row: spin left or right ...
121+
// we got stuck: spin left or right ...
110122
if ((cdist == pcd) and (rdist == prd) and (ldist == pld)) {
111123
// ... or just pull back if we already were spinning
112124
if ((pdir == _SPR) or (pdir == _SPL)) {
@@ -138,6 +150,11 @@ char chooseDirection(int cdist, int rdist, int ldist) {
138150
return _SPR;
139151
}
140152

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+
141158
// when obstacle detected: decide to turn left or right
142159
if (cdist < turnDist) {
143160
if (rdist < ldist) {
@@ -255,11 +272,11 @@ void stopMovement(int spd) {
255272

256273
void loop() {
257274
int cd = cDist();
258-
delay(33);
275+
delay(SENSOR_DELAY);
259276
int rd = rDist();
260-
delay(33);
277+
delay(SENSOR_DELAY);
261278
int ld = lDist();
262-
delay(33);
279+
delay(SENSOR_DELAY);
263280

264281
char dir = chooseDirection(cd, rd, ld);
265282
byte spd = getSpeed(dir);

0 commit comments

Comments
 (0)