Difference between revisions of "Sokoban"
Line 8: | Line 8: | ||
'' Náš projekt mal 2 fázy. Zatiaľ máme dokončenú prvú fázu kde sme sa zoznámili s robotom TATRABOT vyskúšali sme jeho funkčnosť a naučili sme ho jazdiť po vygenerovanej trase. Druhú časť ktorá generuje trasu sa nám ešte nepodarilo spraviť.'' | '' Náš projekt mal 2 fázy. Zatiaľ máme dokončenú prvú fázu kde sme sa zoznámili s robotom TATRABOT vyskúšali sme jeho funkčnosť a naučili sme ho jazdiť po vygenerovanej trase. Druhú časť ktorá generuje trasu sa nám ešte nepodarilo spraviť.'' | ||
− | + | task main() | |
+ | { | ||
+ | #include "tatrabot.h" | ||
+ | #include "demo.h" | ||
+ | |||
+ | //char smer[256] = "DPDPDVPPPDDVDDPDPDPDVDLDLDDLPLDLPLDLDVPDVLDDVPPPDDVDLDLDVDPLDPDLDVLLLDDDVPPPD"; | ||
+ | char smer[255] = "DPDPDVPPP"; | ||
+ | |||
+ | void rt(void) { | ||
+ | //rightTurn | ||
+ | setMotor(2, -4000); | ||
+ | setMotor(1, 4000); | ||
+ | } | ||
+ | |||
+ | void lt(void) { | ||
+ | //leftTurn | ||
+ | setMotor(1, -4000); | ||
+ | setMotor(2, 4000); | ||
+ | } | ||
+ | void bd(void) { | ||
+ | //backward | ||
+ | setMotor(3, -4000); | ||
+ | } | ||
+ | |||
+ | void fd(int distance) { | ||
+ | unsigned int endA = countA + distance; | ||
+ | unsigned int endB = countB + distance; | ||
+ | while (countA <= endA && countB <= endB) { | ||
+ | uint8_t ln = senseLine(); | ||
+ | uint8_t led1 = (ln >> 3) & 1; | ||
+ | uint8_t led2 = (ln >> 2) & 1; | ||
+ | uint8_t ledx = ln & 1; | ||
+ | |||
+ | if (led2 == 1 || ledx == 1) { | ||
+ | led2 = 1; | ||
+ | } | ||
+ | else { | ||
+ | led2 = 0; | ||
+ | } | ||
+ | uint8_t led3 = (ln >> 1) & 1; | ||
+ | if (led1 == 1 && led2 == 1 && led3 == 0) { | ||
+ | lt(); | ||
+ | } | ||
+ | if (led1 == 0 && led2 == 1 && led3 == 1) { | ||
+ | rt(); | ||
+ | } | ||
+ | setMotor(3, 4000); | ||
+ | } | ||
+ | motorsOff(); | ||
+ | } | ||
+ | void back(int distance) { | ||
+ | unsigned int endA = countA + distance; | ||
+ | unsigned int endB = countB + distance; | ||
+ | while (countA <= endA && countB <= endB) { | ||
+ | //chprintf(BT, "cA = %5d, eA = %5d, CB = %5d, eB = %5d\n\r", countA, endA, countB, endB); | ||
+ | uint8_t ln = senseLine(); | ||
+ | uint8_t led1 = (ln >> 3) & 1; | ||
+ | uint8_t led2 = (ln >> 2) & 1; | ||
+ | uint8_t ledx = ln & 1; | ||
+ | |||
+ | if (led2 == 1 || ledx == 1) { | ||
+ | led2 = 1; | ||
+ | } | ||
+ | else { | ||
+ | led2 = 0; | ||
+ | } | ||
+ | uint8_t led3 = (ln >> 1) & 1; | ||
+ | if (led1 == 1 && led2 == 1 && led3 == 0) { | ||
+ | //lt | ||
+ | rt(); | ||
+ | } | ||
+ | if (led1 == 0 && led2 == 1 && led3 == 1) { | ||
+ | //rt | ||
+ | lt(); | ||
+ | } | ||
+ | //chprintf(BT, "dopredu...\r\n"); | ||
+ | bd(); | ||
+ | } | ||
+ | motorsOff(); | ||
+ | } | ||
+ | void turn(const char s) { | ||
+ | int d = 11, dis = 3; | ||
+ | if (s == 'v') { | ||
+ | dis = dis + 5; | ||
+ | } | ||
+ | if (s == 'v') { | ||
+ | back(d); | ||
+ | } | ||
+ | else { | ||
+ | fd(d); | ||
+ | } | ||
+ | unsigned int endA = countA + dis; | ||
+ | unsigned int endB = countB + dis; | ||
+ | while (countA <= endA && countB <= endB) { | ||
+ | if (s == 'l' || s == 'v') { | ||
+ | lt(); | ||
+ | } | ||
+ | if (s == 'r') { | ||
+ | rt(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | while (true) { | ||
+ | uint8_t ln = senseLine(); | ||
+ | uint8_t led1 = (ln >> 3) & 1; | ||
+ | uint8_t led2 = (ln >> 2) & 1; | ||
+ | uint8_t ledx = ln & 1; | ||
+ | if (led2 == 1 || ledx == 1) { | ||
+ | led2 = 1; | ||
+ | } | ||
+ | else { | ||
+ | led2 = 0; | ||
+ | } | ||
+ | uint8_t led3 = (ln >> 1) & 1; | ||
+ | chprintf(BT, "L1 = %5d, L2 = %5d, L3 = %5d\n\r", led1, led2, led3); | ||
+ | if (led1 == 0 && led2 == 1 && led3 == 0) { | ||
+ | break; | ||
+ | } | ||
+ | if (s == 'l' || s == 'v') { | ||
+ | lt(); | ||
+ | } | ||
+ | if (s == 'r') { | ||
+ | rt(); | ||
+ | } | ||
+ | } | ||
+ | if (s == 'v') { | ||
+ | back(d); | ||
+ | } | ||
+ | else { | ||
+ | fd(d); | ||
+ | } | ||
+ | } | ||
+ | void chod(void) { | ||
+ | int i = 0; | ||
+ | while (smer[i] != '\0') { | ||
+ | if (smer[i] == 'D') { | ||
+ | fd(22); | ||
+ | } | ||
+ | if (smer[i] == 'P') { | ||
+ | turn('r'); | ||
+ | } | ||
+ | if (smer[i] == 'L') { | ||
+ | turn('l'); | ||
+ | } | ||
+ | chprintf(BT, "==============i++============="); | ||
+ | i++; | ||
+ | } | ||
+ | } | ||
+ | void chodd(void) { | ||
+ | while (true) { | ||
+ | chprintf(BT, "zadajte pismeno: \r\n"); | ||
+ | uint8_t choice = chSequentialStreamGet(BT); | ||
+ | switch (choice) { | ||
+ | case 'd': | ||
+ | fd(22); | ||
+ | break; | ||
+ | case 'p': | ||
+ | turn('r'); | ||
+ | break; | ||
+ | case 'l': | ||
+ | turn('l'); | ||
+ | break; | ||
+ | case 'v': | ||
+ | turn('v'); | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | int main(void) { | ||
+ | tatrabotInit(); | ||
+ | //demo(); | ||
+ | chod(); | ||
+ | /* | ||
+ | while(true){ | ||
+ | chprintf(BT, "cA = %5d, eB = %5d\n\r", countA, countB); | ||
+ | } | ||
+ | */ | ||
+ | chprintf(BT, "end"); | ||
+ | motorsOff(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | ==Video== | ||
+ | <youtube>fOhg62UGtd0</youtube> |
Revision as of 16:09, 15 February 2016
Zadanie nášho projektu
Cieľom nášho projektu Sokoban bolo naprogramovať robota Tatrabot tak, aby zvládol nájsť správne riešenie zadanej hracej plochy viď obr. 1 hry Sokoban k dispozícii sme mali 4 hracie plochy ktoré sú na obr. 1 a následne týmto nájdeným riešením odohrať hru do úspešného konca, pričom hracia plocha pozostávala z navzájom kolmých čiar vytvárajúcich mriežku rôznych tvarov obsahujúcu priesečníky reprezentujúce kocky potrebné k odohratiu hry, teda ich dopraveniu na vopred určené miesta zadanej hracej plochy mriežky.
Náš projekt mal 2 fázy. Zatiaľ máme dokončenú prvú fázu kde sme sa zoznámili s robotom TATRABOT vyskúšali sme jeho funkčnosť a naučili sme ho jazdiť po vygenerovanej trase. Druhú časť ktorá generuje trasu sa nám ešte nepodarilo spraviť.
task main() {
- include "tatrabot.h"
- include "demo.h"
//char smer[256] = "DPDPDVPPPDDVDDPDPDPDVDLDLDDLPLDLPLDLDVPDVLDDVPPPDDVDLDLDVDPLDPDLDVLLLDDDVPPPD"; char smer[255] = "DPDPDVPPP";
void rt(void) {
//rightTurn setMotor(2, -4000); setMotor(1, 4000);
}
void lt(void) {
//leftTurn setMotor(1, -4000); setMotor(2, 4000);
} void bd(void) {
//backward setMotor(3, -4000);
}
void fd(int distance) {
unsigned int endA = countA + distance; unsigned int endB = countB + distance; while (countA <= endA && countB <= endB) { uint8_t ln = senseLine(); uint8_t led1 = (ln >> 3) & 1; uint8_t led2 = (ln >> 2) & 1; uint8_t ledx = ln & 1; if (led2 == 1 || ledx == 1) { led2 = 1; } else { led2 = 0; } uint8_t led3 = (ln >> 1) & 1; if (led1 == 1 && led2 == 1 && led3 == 0) { lt(); } if (led1 == 0 && led2 == 1 && led3 == 1) { rt(); } setMotor(3, 4000); } motorsOff();
} void back(int distance) {
unsigned int endA = countA + distance; unsigned int endB = countB + distance; while (countA <= endA && countB <= endB) { //chprintf(BT, "cA = %5d, eA = %5d, CB = %5d, eB = %5d\n\r", countA, endA, countB, endB); uint8_t ln = senseLine(); uint8_t led1 = (ln >> 3) & 1; uint8_t led2 = (ln >> 2) & 1; uint8_t ledx = ln & 1; if (led2 == 1 || ledx == 1) { led2 = 1; } else { led2 = 0; } uint8_t led3 = (ln >> 1) & 1; if (led1 == 1 && led2 == 1 && led3 == 0) { //lt rt(); } if (led1 == 0 && led2 == 1 && led3 == 1) { //rt lt(); } //chprintf(BT, "dopredu...\r\n"); bd(); } motorsOff();
} void turn(const char s) {
int d = 11, dis = 3; if (s == 'v') { dis = dis + 5; } if (s == 'v') { back(d); } else { fd(d); } unsigned int endA = countA + dis; unsigned int endB = countB + dis; while (countA <= endA && countB <= endB) { if (s == 'l' || s == 'v') { lt(); } if (s == 'r') { rt(); } } while (true) { uint8_t ln = senseLine(); uint8_t led1 = (ln >> 3) & 1; uint8_t led2 = (ln >> 2) & 1; uint8_t ledx = ln & 1; if (led2 == 1 || ledx == 1) { led2 = 1; } else { led2 = 0; } uint8_t led3 = (ln >> 1) & 1; chprintf(BT, "L1 = %5d, L2 = %5d, L3 = %5d\n\r", led1, led2, led3); if (led1 == 0 && led2 == 1 && led3 == 0) { break; } if (s == 'l' || s == 'v') { lt(); } if (s == 'r') { rt(); } } if (s == 'v') { back(d); } else { fd(d); }
} void chod(void) {
int i = 0; while (smer[i] != '\0') { if (smer[i] == 'D') { fd(22); } if (smer[i] == 'P') { turn('r'); } if (smer[i] == 'L') { turn('l'); } chprintf(BT, "==============i++============="); i++; }
} void chodd(void) {
while (true) { chprintf(BT, "zadajte pismeno: \r\n"); uint8_t choice = chSequentialStreamGet(BT); switch (choice) { case 'd': fd(22); break; case 'p': turn('r'); break; case 'l': turn('l'); break; case 'v': turn('v'); break; } }
} int main(void) {
tatrabotInit(); //demo(); chod(); /* while(true){ chprintf(BT, "cA = %5d, eB = %5d\n\r", countA, countB); } */ chprintf(BT, "end"); motorsOff();
}
}
Video