-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into menu-management
- Loading branch information
Showing
5 changed files
with
200 additions
and
3 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,34 @@ | ||
#ifndef COLLISION_AVOIDANCE_H | ||
#define COLLISION_AVOIDANCE_H | ||
#include "proximity.h" | ||
#include "limit_switch.h" | ||
#define CLOSEST_ALLOWED_DISTANCE 5 | ||
|
||
int collisionDetectionInit(void); | ||
int collisionDetected(void); | ||
int tooCloseToWall(void); | ||
|
||
int collisionDetectionInit() { | ||
proximity_init(); | ||
limit_switch_init(); | ||
} | ||
|
||
int collisionDetected() { | ||
int state = 0x0; | ||
if (!hasNOT_collided(SWITCH_FRONT)) state |= 1; | ||
else if (!hasNOT_collided(SWITCH_LEFT)) state |= 2; | ||
else if (!hasNOT_collided(SWITCH_RIGHT)) state |= 4; | ||
return state; | ||
} | ||
|
||
int tooCloseToWall() { | ||
int state = 0x0; | ||
double leftSensor,rightSensor; | ||
leftSensor = proximity_read_average(8,20); | ||
rightSensor = proximity_read_average(9, 20); | ||
if (leftSensor < CLOSEST_ALLOWED_DISTANCE) state |= 1; | ||
else if (rightSensor < CLOSEST_ALLOWED_DISTANCE) state |= 2; | ||
return state; | ||
} | ||
|
||
#endif |
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,23 @@ | ||
#ifndef LIMIT_SWITCH_H | ||
#define LIMIT_SWITCH_H | ||
#include "MKL25Z4.h" | ||
#define SWITCH_FRONT 0 | ||
#define SWITCH_LEFT 1 | ||
#define SWITCH_RIGHT 2 | ||
|
||
void limit_switch_init(void); | ||
bool hasNOT_collided(int pinNumber); | ||
|
||
void limit_switch_init(void){ | ||
SIM->SCGC5 |= 0x0800; /* enable clock to Port C */ | ||
PORTC->PCR[0] = 0x103; /* PTC0, GPIO, enable pullup*/ | ||
PORTC->PCR[1] = 0x103; /* PTC1, GPIO, enable pullup*/ | ||
PORTC->PCR[2] = 0x103; /* PTC2, GPIO, enable pullup*/ | ||
PTC->PDDR = 0x07; /* make PTC2-0 as input pins */ | ||
} | ||
|
||
bool hasNOT_collided(int pinNumber) { | ||
return PTC->PDIR & (0x01 << pinNumber); | ||
} | ||
|
||
#endif |
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,126 @@ | ||
#ifndef PATHFOLLOWER_H | ||
#define PATHFOLLOWER_H | ||
#include "hbridge.h" | ||
#include "collisionAvoidance.h" | ||
#include "delays.h" | ||
#define NORMAL_OPERATION_SPEED 0.1f | ||
#define ROOMBA_LENGTH_TIME 1.2f | ||
#define NINETY_DEGREE_TURN_TIME 1.0f | ||
#define SENSOR_DISTANCE_AT_WALL 3 | ||
#define STOP_TIME 0.5f | ||
|
||
void turn90Left(void); | ||
void turn90Right(void); | ||
void emptyRoomClean(bool); | ||
void spillClean(int); | ||
void batteryTest(void); | ||
void roomOutline(int); | ||
|
||
void turn90Left() { | ||
rotate_left(NORMAL_OPERATION_SPEED); | ||
delayMs(NINETY_DEGREE_TURN_TIME*1000); | ||
stop(); | ||
} | ||
|
||
void turn90Right() { | ||
rotate_right(NORMAL_OPERATION_SPEED); | ||
delayMs(NINETY_DEGREE_TURN_TIME*1000); | ||
stop(); | ||
} | ||
|
||
/* | ||
The robot follows a simple, zig zag pattern | ||
(long forward, turn right, short forward, turn right, long forward, | ||
turn left, short forward, turn left, repeat) until it gets to the | ||
end of the room. | ||
*/ | ||
void emptyRoomClean(bool deep) { | ||
bool sensorFacingEnd = 1; //0 left, 1 right | ||
while (proximity_read_average(8+sensorFacingEnd, 20) >= SENSOR_DISTANCE_AT_WALL) { | ||
forward(NORMAL_OPERATION_SPEED); | ||
while (hasNOT_collided(SWITCH_FRONT)) { | ||
delayMs(ROOMBA_LENGTH_TIME*1000); | ||
if (deep) { | ||
stop(); | ||
delayMs(STOP_TIME*1000); | ||
forward(NORMAL_OPERATION_SPEED); | ||
} | ||
} | ||
stop(); | ||
if (sensorFacingEnd) { | ||
turn90Right(); | ||
forward(NORMAL_OPERATION_SPEED); | ||
delayMs(ROOMBA_LENGTH_TIME*1000); | ||
stop(); | ||
turn90Right(); | ||
} else { | ||
turn90Left(); | ||
forward(NORMAL_OPERATION_SPEED); | ||
delayMs(ROOMBA_LENGTH_TIME*1000); | ||
stop(); | ||
turn90Left(); | ||
} | ||
sensorFacingEnd = !sensorFacingEnd; | ||
} | ||
forward(NORMAL_OPERATION_SPEED); | ||
while (hasNOT_collided(SWITCH_FRONT)) {} | ||
stop(); | ||
} | ||
|
||
/* | ||
The robot does an outward spiral, implemented with | ||
an increasing counter, so as to clean a specific area. It stops | ||
every roomba length or so to do a more thorough cleaning. | ||
*/ | ||
void spillClean(int totalTime) { | ||
int msLeft = totalTime*1000; | ||
int forwardTimeMs = ROOMBA_LENGTH_TIME*1000; | ||
int temp; | ||
int iterations = 1; | ||
while (msLeft > 0) { | ||
for (int i=0; i<2; i++) { | ||
temp = forwardTimeMs; | ||
while (temp > 0) { | ||
forward(NORMAL_OPERATION_SPEED); | ||
delayMs(ROOMBA_LENGTH_TIME*1000); | ||
stop(); | ||
delayMs(STOP_TIME*1000); | ||
temp -= ROOMBA_LENGTH_TIME*1000; | ||
} | ||
turn90Left(); | ||
} | ||
msLeft -= forwardTimeMs + STOP_TIME*1000*iterations*2 + NINETY_DEGREE_TURN_TIME*1000*2; | ||
forwardTimeMs += ROOMBA_LENGTH_TIME*1000; | ||
iterations++; | ||
} | ||
} | ||
|
||
void batteryTest() { //The robot goes forward indefinitely. | ||
forward(1); | ||
} | ||
|
||
/* | ||
The robot goes around the room, trying to minimize | ||
the measurement of the left proximity sensor and maximize the measurement | ||
of the right one. This is done to cover the areas that are usually dirtier | ||
after sweeping the floor. | ||
*/ | ||
void roomOutline(int time) { | ||
int msTime = time * 1000; | ||
int proximityState; | ||
while (msTime > 0) { | ||
proximityState = tooCloseToWall(); | ||
if (proximityState == 1) { //left too close, right not too close | ||
forward(NORMAL_OPERATION_SPEED); | ||
} else if (proximityState == 0) { //both not too close | ||
turn90Left(); | ||
} else if (proximityState == 2) { | ||
turn90Right(); | ||
} | ||
msTime--; | ||
delay1ms(); | ||
} | ||
stop(); | ||
} | ||
|
||
#endif |
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