Hrvatsko društvo za robotiku - Uvod u robotiku - vježbe

Mapiranje zida

Cilj

Spremiti status zida u memoriju.

Iteracija

void RobotMaze::map() {
	for (uint8_t i = 0; i < 4; i++) { // Iterate the 4 directions.
		Direction dir = (Direction)i; // An integer 
			(i) can be casted to enum (Directory).
		...
	}
	...
}
"for" broji od 0 do 3.

"dir" dobijemo iz "i" i pokazuje redom u sva 4 smjera.

Je li zid već spremljen?

void RobotMaze::map() {
	for (uint8_t i = 0; i < 4; i++) { // Iterate the 4 directions.
		Direction dir = (Direction)i; // An integer 
			(i) can be casted to enum (Directory).
		if (tileCurrent->wallGet(dir) == WallStatus::WALL_UNKNOWN){ // Map 
			only the walls not mapped before (that have status WALL_UNKNOWN).
			...
		}
	}
	...
}
Ako još ne znamo koji je status zida u izabranom smjeru, a sve gledamo za jednu pločicu, onda ćemo ga mapirati.

Znači, spremiti u neku varijablu.

Spremanje zida

void RobotMaze::map() {
	for (uint8_t i = 0; i < 4; i++) { // Iterate the 4 directions.
		Direction dir = (Direction)i; // An integer 
			(i) can be casted to enum (Directory).
		if (tileCurrent->wallGet(dir) == WallStatus::WALL_UNKNOWN){ // Map 
			only the walls not mapped before (that have status WALL_UNKNOWN).
			/*If both sensors in this direction detect walls, 
			conclude that a wall is ahead. Note that this is not a 
			straightforward decision. The alternative strategy would 
			be to declare a wall if ANY of the sensors 
			detects a wall and it would be more appropriate for the 
			situation when the robot has not gone far enough and its 
			rear sensor is missing the wall, which is only 1 cm ahead. 
			The third strategy would be to consider a more elaboraty 
			solution. The last one is the best.
			*/

			if (distance(dir, false) < NO_WALL_DISTANCE && distance(dir, true) 
					< NO_WALL_DISTANCE)
				tileCurrent->wallSet(dir, WallStatus::WALL_WITHOUT_VICTIM); 
			else
				tileCurrent->wallSet(dir, WallStatus::NO_WALL);
		}
	}
	...
}
Ako obadva senzora udaljenosti u danom smjeru mjere malu udaljenost, znači da je tu zid.

U tom slučaju spremamo postojanje zida funkcijom "wallSet()".

U suprotnom spremam da ne postoji zid.

Kraj

void RobotMaze::map() {
	...
	actionSet(actionDecide); // After mapping the next action will be a 
		decision what to do.
	mazePrint(); // A debug tool: print all the maze after each map event.
}
Na kraju aktiviramo sljedeću akciju i štampamo labirint.

Zadatak: crtanje

Iscrtajte na 8x8 displeju rubove na način da rub svijetli, ako je zid u tom smjeru, inače ne.

Primjedbe



Projekt "Uvod u robotiku" sufinanciran je iz Europskog socijalnog fonda, poziv "Jačanje kapaciteta organizacija civilnoga društva za popularizaciju STEM-a". Relevantne stranice: Sadržaj vježbe za virtualne radionice isključiva je odgovornost Hrvatskog društva za robotiku.