-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2.0: Use sonar sensor to detect distance to walls and obstacles;
v2.1: Check current consumption to know when is stuck and need to get out;
- Loading branch information
Demven
committed
Dec 19, 2016
1 parent
f01b2e5
commit b464864
Showing
4 changed files
with
112 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
void lookAroundAndTurn() { | ||
int leftDistance = getDistanceOnTheLeft(); | ||
int rightDistance = getDistanceOnTheRight(); | ||
|
||
// decide where to go | ||
if (leftDistance > rightDistance) { | ||
turnLeft(); | ||
} else { | ||
turnRight(); | ||
} | ||
} | ||
|
||
int getDistanceOnTheLeft() { | ||
int distanceOnTheLeft; | ||
|
||
// turn backwards to left | ||
leftBackward(SPEED_MEDIUM, 0); | ||
rightBackward(SPEED_LOW, 0); | ||
delay(300); | ||
brakeOn(200); | ||
|
||
distanceOnTheLeft = getDistanceInCm(); | ||
|
||
// get back to straight position | ||
leftForward(SPEED_MEDIUM, 0); | ||
rightForward(SPEED_LOW, 0); | ||
delay(300); | ||
brakeOn(200); | ||
|
||
return distanceOnTheLeft; | ||
} | ||
|
||
int getDistanceOnTheRight() { | ||
int distanceOnTheRight; | ||
|
||
// turn backwards to right | ||
leftBackward(SPEED_LOW, 0); | ||
rightBackward(SPEED_MEDIUM, 0); | ||
delay(300); | ||
brakeOn(200); | ||
|
||
distanceOnTheRight = getDistanceInCm(); | ||
|
||
// get back to straight position | ||
leftForward(SPEED_LOW, 0); | ||
rightForward(SPEED_MEDIUM, 0); | ||
delay(300); | ||
brakeOn(200); | ||
|
||
return distanceOnTheRight; | ||
} | ||
|
||
boolean isStuck() { | ||
boolean stuck = false; | ||
int leftCurrent = getLeftConsumption(); | ||
int rightCurrent = getRightConsumption(); | ||
if (leftCurrent > CURRENT_CONSUMPTION_MAX || rightCurrent > CURRENT_CONSUMPTION_MAX) { | ||
stuck = true; | ||
} | ||
|
||
return stuck; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
NewPing sonar(SONAR_PING_PIN, SONAR_PING_PIN, SONAR_MAX_DISTANCE); | ||
|
||
void initSonar() { | ||
pinMode(SONAR_GROUND_PIN,OUTPUT); | ||
pinMode(SONAR_POWER_PIN,OUTPUT); | ||
digitalWrite(SONAR_GROUND_PIN, LOW); | ||
digitalWrite(SONAR_POWER_PIN, HIGH); | ||
} | ||
|
||
int getDistanceInCm() { | ||
return sonar.ping_cm(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters