Skip to content

Commit

Permalink
v2.0: Use sonar sensor to detect distance to walls and obstacles;
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 21 deletions.
62 changes: 62 additions & 0 deletions WheelBot-01/Brain.ino
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;
}
2 changes: 2 additions & 0 deletions WheelBot-01/Motors.ino
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ void goBackward(unsigned int speed, unsigned int time) {
}
}
void leftBackward(unsigned int speed, unsigned int time) {
brakeOff();
digitalWrite(LEFT_DIRECTION, LOW);
leftSpeed(speed);
if (time > 0) {
delay(time);
}
}
void rightBackward(unsigned int speed, unsigned int time) {
brakeOff();
digitalWrite(RIGHT_DIRECTION, HIGH);
rightSpeed(speed);
if (time > 0) {
Expand Down
12 changes: 12 additions & 0 deletions WheelBot-01/Sonar.ino
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();
}
57 changes: 36 additions & 21 deletions WheelBot-01/WheelBot-01.ino
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
#include <NewPing.h>

#define DEBUG_MODE true
#define DEBUG_RATE 9600
#define DEBUG_RATE 115200

#define SONAR_PING_PIN 39
#define SONAR_GROUND_PIN 43
#define SONAR_POWER_PIN 41
#define SONAR_MAX_DISTANCE 500
#define SONAR_MIN_DISTANCE 20

#define LEFT_DECREASE 0
#define RIGHT_DECREASE 2
#define LEFT_DECREASE 2
#define RIGHT_DECREASE 0

#define LEFT_SPEED 3
#define LEFT_DIRECTION 12
#define LEFT_BRAKE 9
#define LEFT_SNS A0
#define LEFT_SPEED 11
#define LEFT_DIRECTION 13
#define LEFT_BRAKE 8
#define LEFT_SNS A1

#define RIGHT_SPEED 11
#define RIGHT_DIRECTION 13
#define RIGHT_BRAKE 8
#define RIGHT_SNS A1
#define RIGHT_SPEED 3
#define RIGHT_DIRECTION 12
#define RIGHT_BRAKE 9
#define RIGHT_SNS A0

#define SPEED_MAX 255
#define SPEED_MEDIUM 150
#define SPEED_LOW 70
#define SPEED_ZERO 0

#define CURRENT_CONSUMPTION_MAX 250

void setup() {
initLogger();
initSonar();

pinMode(LEFT_BRAKE, OUTPUT);
pinMode(LEFT_DIRECTION, OUTPUT);
Expand All @@ -30,14 +41,18 @@ void setup() {
}

void loop() {
goForward(SPEED_LOW, 3500);
brakeOn(500);

rotate();
brakeOn(1000);

goForward(SPEED_LOW, 3500);
brakeOn(500);

while(1);
int currentDistance = getDistanceInCm();

if (currentDistance > SONAR_MIN_DISTANCE || currentDistance == 0) {
goForward(SPEED_LOW, 150);
if (isStuck()) {
brakeOn(200);
goBackward(SPEED_MEDIUM, 300);
lookAroundAndTurn();
}
} else if (currentDistance > 0) {
Serial.println("look around");
brakeOn(200);
lookAroundAndTurn();
}
}

0 comments on commit b464864

Please sign in to comment.