Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.58 KB | None | 0 0
  1. #ifndef WEG_ALGO
  2. #define WEG_ALGO
  3.  
  4. #define WINDOWS 1
  5.  
  6. #ifdef WINDOWS
  7. #include "stdio.h"
  8. #include "inttypes.h"
  9. #endif // WINDOWS
  10.  
  11. #define HOEHE 14
  12. #define BREITE 14
  13. #define MAX_WEG_K 16 //Maximale Anzahl an Wegknoten
  14. #define K_COUNT 60
  15. #define INVALID -1
  16. #define INVALID_SHORTTEST 127
  17.  
  18. int8_t g_kCount = 1;
  19.  
  20. // k[0] = Start
  21. struct Knoten {
  22.     int8_t distance; // Code Optimiert für max 127 distance
  23.     int8_t vorgaenger;
  24.     int8_t besucht;
  25.     int8_t x;
  26.     int8_t y;
  27.     int8_t vip[4]; // 0 = Start // 1 = Tisch1 ...
  28.     int8_t KnotenNachbarDistanceSumme[K_COUNT];
  29.     int8_t KnotenNachbarDistanceX[K_COUNT];
  30.     int8_t KnotenNachbarDistanceY[K_COUNT];
  31. } K[K_COUNT];
  32.  
  33. struct weg {
  34.     int8_t knoten;
  35.     int8_t xPos;
  36.     int8_t yPos;
  37. };
  38.  
  39. enum punkte {
  40.     a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
  41. };
  42.  
  43. enum printMethode {
  44.     JUNK, ALPHABET
  45. };
  46.  
  47. char array[HOEHE][BREITE] = {
  48.         "##############",
  49.         "#     S      #",
  50.         "##   #####   #",
  51.         "#    11      #",
  52.         "#            #",
  53.         "#   ####   2 #",
  54.         "#      #   2 #",
  55.         "#      #     #",
  56.         "#   ##########",
  57.         "#   #        #",
  58.         "#   #   33   #",
  59.         "#   ###      #",
  60.         "#            #",
  61.         "##############"
  62. };
  63.  
  64. // Initalisiere Knoten und Startpunkt
  65. void initStruct() {
  66.     // Init array
  67.     for (int8_t i = 0; i < K_COUNT; i++) {
  68.         K[i].distance = -1;
  69.         K[i].vorgaenger = -1;
  70.         K[i].besucht = 0;
  71.         K[i].x = -1;
  72.         K[i].y = -1;
  73.         K[i].vip[0] = 0;
  74.         K[i].vip[1] = 0;
  75.         K[i].vip[2] = 0;
  76.         K[i].vip[3] = 0;
  77.  
  78.         for (int8_t j = 0; j < K_COUNT; j++) {
  79.             K[i].KnotenNachbarDistanceSumme[j] = 0;
  80.             K[i].KnotenNachbarDistanceX[j] = 0;
  81.             K[i].KnotenNachbarDistanceY[j] = 0;
  82.  
  83.         }
  84.     }
  85. }
  86.  
  87. void algJunkSpecialSet(char Punktbuchstabe, char Tischnummer) {
  88.  
  89.     for (int8_t i = 1; i < HOEHE - 1; i++) {
  90.         for (int8_t j = 1; j < BREITE - 1; j++) {
  91.             if (array[i][j] == Tischnummer) {
  92.                 //Check Oben
  93.                 if (array[i - 1][j - 1] == ' ') array[i - 1][j - 1] = Punktbuchstabe;
  94.                 if (array[i - 1][j] == ' ') array[i - 1][j] = Punktbuchstabe;
  95.                 if (array[i - 1][j + 1] == ' ') array[i - 1][j + 1] = Punktbuchstabe;
  96.  
  97.                 //Check links rechts
  98.                 if (array[i][j - 1] == ' ') array[i][j - 1] = Punktbuchstabe;
  99.                 if (array[i][j + 1] == ' ') array[i][j + 1] = Punktbuchstabe;
  100.  
  101.                 //Check Unten
  102.                 if (array[i + 1][j - 1] == ' ') array[i + 1][j - 1] = Punktbuchstabe;
  103.                 if (array[i + 1][j] == ' ') array[i + 1][j] = Punktbuchstabe;
  104.                 if (array[i + 1][j + 1] == ' ') array[i + 1][j + 1] = Punktbuchstabe;
  105.             }
  106.         }
  107.  
  108.     }
  109. }
  110.  
  111. // Setzte Eck und Tischpunkte
  112. void algoJunk() {
  113.  
  114.     int8_t links = 0, rechts = 0, oben = 0, unten = 0;
  115.  
  116.     for (int8_t i = 1; i < HOEHE - 1; i++) {
  117.         for (int8_t j = 1; j < BREITE - 1; j++) {
  118.  
  119.             //   # = J = Junk         1 = X = Tisch1        2 = Y = Tisch2        3 = Z = Tisch3
  120.             if (array[i][j] == '#' || array[i][j] == '1' || array[i][j] == '2' || array[i][j] == '3') {
  121.  
  122.                 if ((array[i - 1][j] == ' ') || (array[i - 1][j] == 'J'))
  123.                     oben = 1;
  124.  
  125.                 if ((array[i + 1][j] == ' ') || (array[i + 1][j] == 'J'))
  126.                     unten = 1;
  127.  
  128.                 if ((array[i][j - 1] == ' ') || (array[i][j - 1] == 'J'))
  129.                     links = 1;
  130.  
  131.                 if ((array[i][j + 1] == ' ') || (array[i][j + 1] == 'J'))
  132.                     rechts = 1;
  133.  
  134.                 if ((array[i - 1][j + 1] == ' ') && oben && rechts)
  135.                     array[i - 1][j + 1] = 'J';
  136.  
  137.                 if ((array[i - 1][j - 1] == ' ') && oben && links)
  138.                     array[i - 1][j - 1] = 'J';
  139.  
  140.                 if ((array[i + 1][j + 1] == ' ') && unten && rechts)
  141.                     array[i + 1][j + 1] = 'J';
  142.  
  143.                 if ((array[i + 1][j - 1] == ' ') && unten && links)
  144.                     array[i + 1][j - 1] = 'J';
  145.  
  146.                 links = 0; rechts = 0; oben = 0; unten = 0;
  147.             }
  148.         }
  149.     }
  150. }
  151.  
  152. int8_t algoDijkstraNextKnoten(int8_t kNum) {
  153.  
  154.     int8_t shortway = INVALID_SHORTTEST; // 127
  155.     int8_t shortnum = INVALID_SHORTTEST;
  156.  
  157.     for (int8_t i = 0; i < g_kCount; i++) {
  158.         if (K[kNum].KnotenNachbarDistanceSumme[i] > 0 && K[i].besucht == 0) { // Valider Punkt
  159.  
  160.             if (K[kNum].KnotenNachbarDistanceSumme[i] < shortway) { // Ist dieser Punkt kürzer als der vorherige
  161.                 shortway = K[kNum].KnotenNachbarDistanceSumme[i];
  162.                 shortnum = i;
  163.             }
  164.  
  165.         }
  166.     }
  167.  
  168.     if (shortnum == INVALID_SHORTTEST)
  169.         return -1;
  170.  
  171.     return shortnum;
  172. }
  173.  
  174. //Möglicherweise nicht notwendig -> im Notfall für sicheren Weg
  175. void algoJunk2() {
  176.  
  177.     for (int8_t i = 1; i < HOEHE - 1; i++) {
  178.         for (int8_t j = 1; j < BREITE - 1; j++) {
  179.  
  180.             //   # = J = Junk         1 = X = Tisch1        2 = Y = Tisch2        3 = Z = Tisch3
  181.             if (array[i][j] == 'J') {
  182.  
  183.                 // Links && Oben
  184.                 if ((array[i - 1][j] == ' ') && (array[i][j - 1] == ' ') && (array[i - 1][j - 1] == ' '))
  185.                     array[i - 1][j - 1] = 'J';
  186.  
  187.                 // Links && Unten
  188.                 if ((array[i][j - 1] == ' ') && (array[i + 1][j] == ' ') && (array[i + 1][j - 1] == ' '))
  189.                     array[i + 1][j - 1] = 'J';
  190.  
  191.                 // Rechts && Oben
  192.                 if ((array[i - 1][j] == ' ') && (array[i][j + 1] == ' ') && (array[i - 1][j + 1] == ' '))
  193.                     array[i - 1][j + 1] = 'J';
  194.  
  195.                 // Rechts && Unten
  196.                 if ((array[i + 1][j] == ' ') && (array[i][j + 1] == ' ') && (array[i + 1][j + 1] == ' '))
  197.                     array[i + 1][j + 1] = 'J';
  198.             }
  199.  
  200.         }
  201.     }
  202. }
  203.  
  204. //Suche Konoten mit selben x / y postition
  205. int8_t getKnoten(int8_t yPos, int8_t xPos) {
  206.  
  207.     for (int8_t i = 0; i < g_kCount; i++) {
  208.         if (K[i].x == xPos && K[i].y == yPos)
  209.             return i;
  210.     }
  211.     return 0; // nicht notwendig
  212. }
  213.  
  214.  
  215. void printFeld(enum printMethode m) {
  216. #ifdef WINDOWS
  217.     int c;
  218.     for (int8_t i = 0; i < HOEHE; i++) {
  219.         for (int8_t j = 0; j < BREITE; j++) {
  220.  
  221.             if ((m == ALPHABET) && (array[i][j] == 'J'))
  222.             {
  223.                 c = (char)getKnoten(i, j) + 'a'; //Offset für ASCII
  224.                 printf("%c", c);
  225.             }
  226.             else { // JUNK
  227.                 printf("%c", array[i][j]);
  228.             }
  229.         }
  230.         printf("\n");
  231.     }
  232. #endif
  233. }
  234.  
  235. int8_t isVIP(int8_t yPos, int8_t xPos, char tNum) {
  236.     //Check Oben
  237.     if (array[yPos - 1][xPos - 1] == tNum) return 1;
  238.     if (array[yPos - 1][xPos] == tNum) return 1;
  239.     if (array[yPos - 1][xPos + 1] == tNum) return 1;
  240.  
  241.     //Check links rechts
  242.     if (array[yPos][xPos - 1] == tNum) return 1;
  243.     if (array[yPos][xPos + 1] == tNum) return 1;
  244.  
  245.     //Check Unten
  246.     if (array[yPos + 1][xPos - 1] == tNum) return 1;
  247.     if (array[yPos + 1][xPos] == tNum) return 1;
  248.     if (array[yPos + 1][xPos + 1] == tNum) return 1;
  249.  
  250.     return 0;
  251. }
  252.  
  253.  
  254.  
  255. void initStructUpdate() {
  256.  
  257.     for (int8_t i = 1; i < HOEHE; i++) {
  258.         for (int8_t j = 1; j < BREITE; j++) {
  259.  
  260.             // Start init (sonder Bedingung)
  261.             if ('S' == array[i][j]) {
  262.                 K[0].distance = 0;
  263.                 K[0].vorgaenger = 0;
  264.                 K[0].vip[0] = 1;
  265.                 K[0].x = j;
  266.                 K[0].y = i;
  267.                 K[0].besucht = 1;
  268.             }
  269.  
  270.             // Update Table and Junk
  271.             if (array[i][j] == 'J' && 0 == getKnoten(i, j)) {
  272.                 K[g_kCount].x = j;
  273.                 K[g_kCount].y = i;
  274.                 K[g_kCount].vip[1] = isVIP(i, j, '1');
  275.                 K[g_kCount].vip[2] = isVIP(i, j, '2');
  276.                 K[g_kCount].vip[3] = isVIP(i, j, '3');
  277.  
  278.                 g_kCount++;
  279.             }
  280.         }
  281.     }
  282.  
  283. }
  284.  
  285. int8_t algJunkSpecial() {
  286.  
  287.     int8_t vip1 = 0;
  288.     int8_t vip2 = 0;
  289.     int8_t vip3 = 0;
  290.  
  291.     for (int8_t i = 0; i < g_kCount; i++) {
  292.         if (K[i].vip[1]) vip1++;
  293.         if (K[i].vip[2]) vip2++;
  294.         if (K[i].vip[3]) vip3++;
  295.     }
  296.  
  297.     if (vip1 == 0) algJunkSpecialSet('J', '1');
  298.     if (vip2 == 0) algJunkSpecialSet('J', '2');
  299.     if (vip3 == 0) algJunkSpecialSet('J', '3');
  300.  
  301.     if (vip1 == 0 || vip2 == 0 || vip3 == 0)
  302.         return 1; // Struct updaten
  303.  
  304.     return 0; // Struct nicht updaten
  305. }
  306.  
  307.  
  308. // hier optimieren für kreuz suche oder ähnlich
  309. void algoDistanceLRUD(int8_t kNum) {
  310.  
  311.     // Such Rechts
  312.     for (int8_t i = K[kNum].x + 1; i < BREITE; i++) {
  313.         if (array[K[kNum].y][i] == '#') break;
  314.         if (array[K[kNum].y][i] == 'J') {
  315.             int kFound = getKnoten(K[kNum].y, i);
  316.             K[kNum].KnotenNachbarDistanceSumme[kFound] = i - K[kNum].x;
  317.             K[kNum].KnotenNachbarDistanceX[kFound] = i - K[kNum].x;
  318.             K[kNum].KnotenNachbarDistanceY[kFound] = 0;
  319.             break;
  320.         }
  321.     }
  322.     // Suche Links
  323.     for (int8_t i = K[kNum].x - 1; i > 0; i--) {
  324.         if (array[K[kNum].y][i] == '#') break;
  325.         if (array[K[kNum].y][i] == 'J') {
  326.             int kFound = getKnoten(K[kNum].y, i);
  327.             K[kNum].KnotenNachbarDistanceSumme[kFound] = ((i - K[kNum].x) * (-1));
  328.             K[kNum].KnotenNachbarDistanceX[kFound] = i - K[kNum].x;
  329.             K[kNum].KnotenNachbarDistanceY[kFound] = 0;
  330.             break;
  331.         }
  332.     }
  333.     // Suche unten
  334.     for (int8_t i = K[kNum].y + 1; i < HOEHE; i++) {
  335.         if (array[i][K[kNum].x] == '#') break;
  336.         if (array[i][K[kNum].x] == 'J') {
  337.             int kFound = getKnoten(i, K[kNum].x);
  338.             K[kNum].KnotenNachbarDistanceSumme[kFound] = i - K[kNum].y;
  339.             K[kNum].KnotenNachbarDistanceX[kFound] = 0;
  340.             K[kNum].KnotenNachbarDistanceY[kFound] = i - K[kNum].y;
  341.             break;
  342.         }
  343.     }
  344.     // Suche oben
  345.     for (int8_t i = K[kNum].y - 1; i > 0; i--) {
  346.         if (array[i][K[kNum].x] == '#') break;
  347.         if (array[i][K[kNum].x] == 'J') {
  348.             int kFound = getKnoten(i, K[kNum].x);
  349.             K[kNum].KnotenNachbarDistanceSumme[kFound] = ((i - K[kNum].y) * (-1));
  350.             K[kNum].KnotenNachbarDistanceX[kFound] = 0;
  351.             K[kNum].KnotenNachbarDistanceY[kFound] = i - K[kNum].y;
  352.             break;
  353.         }
  354.     }
  355. }
  356.  
  357. //Prototyp Abfrage für Querfeld
  358. void algoDistanceQUER(int8_t kNum) {
  359.  
  360.     //// RechtsOben
  361.     //if (array[K[kNum].y - 1][K[kNum].x] == ' ' && array[K[kNum].y][K[kNum].x + 1] == ' ' && array[K[kNum].y - 1][K[kNum].x + 1] === 'J')
  362.     //  //setzen
  363.  
  364.  
  365.     //  // RechtsUnten
  366.     //  if (array[K[kNum].y - 1][K[kNum].x] == ' ' && array[K[kNum].y][K[kNum].x + 1] == ' ' && array[K[kNum].y + 1][K[kNum].x + 1] > 'a')
  367.     //      //setzen
  368.  
  369.  
  370.     //      // LinksOben
  371.     //      if (array[K[kNum].y - 1][K[kNum].x] == ' ' && array[K[kNum].y][K[kNum].x - 1] == ' ' && array[K[kNum].y - 1][K[kNum].x - 1] > 'a')
  372.     //          //setzen
  373.  
  374.  
  375.     //          // LinksUnten
  376.     //          if (array[K[kNum].y + 1][K[kNum].x] == ' ' && array[K[kNum].y][K[kNum].x - 1] == ' ' && array[K[kNum].y + 1][K[kNum].x - 1] > 'a')
  377.     //              //setzen
  378. }
  379.  
  380. void algoDistance() {
  381.  
  382.     for (int8_t i = 0; i < g_kCount; i++)
  383.         algoDistanceLRUD(i);
  384.  
  385.     for (int8_t i = 0; i < g_kCount; i++)
  386.         algoDistanceQUER(i);
  387.  
  388. }
  389.  
  390.  
  391. void algoDijkstra() {
  392.  
  393.     int start = 0;
  394.     int randKnoten = -1;
  395.     int nextKnoten = -1;
  396.  
  397.     int randDistance = 0;
  398.     int nextDistance = 0;
  399.     int startNextDistance = 0;
  400.  
  401.     // Vorgaenger von Start setzen
  402.     for (int8_t i = 0; i < g_kCount; i++) {
  403.         if (K[start].KnotenNachbarDistanceSumme[i] > 0)
  404.             K[i].vorgaenger = start;
  405.     }
  406.  
  407.     while (1) {
  408.         // Suche kürzesten Knoten von Start
  409.         randKnoten = algoDijkstraNextKnoten(start);
  410.  
  411.         // Abbruch kein Knoten gefunden
  412.         if (randKnoten == -1)
  413.             break;
  414.  
  415.         // Hole die Distanz zum Randknoten
  416.         randDistance = K[start].KnotenNachbarDistanceSumme[randKnoten];
  417.  
  418.         // Durchsuche alle NextKnoten
  419.         for (int8_t i = 0; i < g_kCount; i++) {
  420.             nextDistance = K[randKnoten].KnotenNachbarDistanceSumme[i];
  421.  
  422.             // Wenn keine Verbindung -> Abbruch
  423.             if (nextDistance == 0)
  424.                 continue;
  425.  
  426.             // Hole die Distanz von Start zum NextKnoten
  427.             startNextDistance = K[start].KnotenNachbarDistanceSumme[i];
  428.  
  429.             // Wenn kürzer oder nicht existiert dann setzte die Verbindung
  430.             if (startNextDistance == 0 || startNextDistance > randDistance + nextDistance) {
  431.                 K[start].KnotenNachbarDistanceSumme[i] = randDistance + nextDistance;
  432.                 K[i].vorgaenger = randKnoten;
  433.             }
  434.  
  435.         }
  436.         K[randKnoten].besucht = 1;
  437.     }
  438. }
  439.  
  440. void printStart() {
  441. #ifdef WINDOWS
  442.  
  443.     for (int8_t i = 0; i < g_kCount; i++) {
  444.         printf("%c = %d  ", (char)i + 'a', K[0].KnotenNachbarDistanceSumme[i]);
  445.     }
  446.     printf("\nVorgaenger: \n");
  447.     for (int8_t i = 0; i < g_kCount; i++) {
  448.         printf("%c = %c  ", (char)i + 'a', ((char)K[i].vorgaenger) + 'a');
  449.     }
  450.     printf("\n");
  451. #endif
  452. }
  453.  
  454.  
  455. int8_t checkAufVorgaengerFehlt() {
  456.  
  457.     for (int8_t i = 0; i < g_kCount; i++)
  458.         if (K[i].vorgaenger == -1)
  459.             return -1;
  460.  
  461.     return 0;
  462. }
  463.  
  464.  
  465. /////////////////Roboter fahren ANFANG 
  466.  
  467.  
  468. #ifdef WINDOWS
  469.  
  470. void rotateR() {
  471.     printf("rechts drehung\n");
  472. }
  473. void rotateL() {
  474.     printf("links drehung\n");
  475. }
  476. void drive1() {
  477.     printf("fahren x positiv\n");
  478. }
  479. void drive2() {
  480.     printf("fahren x negativ\n");
  481. }
  482. void drive3() {
  483.     printf("fahren y positiv\n");
  484. }
  485. void drive4() {
  486.     printf("fahren y negativ\n");
  487. }
  488.  
  489. #endif
  490.  
  491. ////////////////////////////////////////////////////////////einstellen
  492.  
  493. void drive() {
  494.  
  495.     Motor_Drive(Port_A, motorDir, 35);
  496.     Motor_Drive(Port_B, motorDir, 35);
  497.     Delay(1000);
  498. }
  499. void rotateR() {
  500.     Motor_Drive(Port_A, Motor_dir_forward, 50); //Drehen 90 grad anpassen !!!
  501.     Motor_Drive(Port_B, Motor_dir_forward, 10);
  502.     Delay(1000);
  503. }
  504. void rotateL() {
  505.     Motor_Drive(Port_A, Motor_dir_forward, 10); //Drehen 90 grad anpassen !!!
  506.     Motor_Drive(Port_B, Motor_dir_forward, 50);
  507.     Delay(1000);
  508.  
  509. }
  510.  
  511. //////////////////////////////////////////////////////////einstellen
  512.  
  513.  
  514. void suche_start() {
  515.    
  516.     for (int i = 0; i < 14; i++) {
  517.         for (int j = 0; j < 14; j++) {
  518.  
  519.             if (array[i][j] == 'S') {
  520.                 position[0] = i;
  521.                 position[1] = j;
  522.                
  523.             }
  524.  
  525.         }
  526.     }
  527. }
  528.  
  529. #define NORDEN 1
  530. #define OSTEN  2
  531. #define SUEDEN 3
  532. #define WESTEN 4
  533. int ausrichtung = NORDEN;       //Startrichtung
  534. int position[2];
  535.  
  536. void RobiFahr(int y, int x) {
  537.  
  538.     int x_now = position[0];
  539.     int y_now = position[1];
  540.  
  541.     printf("%d %d\n", y_now, x_now);
  542.  
  543.  
  544.     if (x > 0) {
  545.         switch (ausrichtung) {
  546.         case NORDEN:
  547.             rotateR();
  548.             ausrichtung = OSTEN;
  549.             printf("Osten\n");
  550.             break;
  551.         case SUEDEN:
  552.             rotateL();
  553.             ausrichtung = OSTEN;
  554.             printf("Osten\n");
  555.             break;
  556.         case WESTEN:
  557.             rotateL();
  558.             rotateL();
  559.             ausrichtung = OSTEN;
  560.             printf("Osten\n");
  561.             break;
  562.         }
  563.         for (int i = 0; i < x; i++)
  564.             drive();
  565.         position[1] ++;
  566.     }
  567.  
  568.     if (x < 0) {
  569.         switch (ausrichtung) {
  570.         case NORDEN:
  571.             rotateL();
  572.             ausrichtung = WESTEN;
  573.             printf("Westen\n");
  574.             break;
  575.         case OSTEN:
  576.             rotateL();
  577.             rotateL();
  578.             ausrichtung = WESTEN;
  579.             printf("Westen\n");
  580.             break;
  581.         case SUEDEN:
  582.             rotateR();
  583.             ausrichtung = WESTEN;
  584.             printf("Westen\n");
  585.             break;
  586.         }
  587.         x = x * (-1);
  588.         for (int i = 0; i < x; i++) {
  589.             drive();
  590.             position[1]--;
  591.         }
  592.     }
  593.  
  594.     if (y > 0) {
  595.         switch (ausrichtung) {
  596.         case NORDEN:
  597.             rotateR();
  598.             rotateR();
  599.             ausrichtung = SUEDEN;
  600.             printf("Sueden\n");
  601.             break;
  602.         case OSTEN:
  603.             rotateR();
  604.             ausrichtung = SUEDEN;
  605.             printf("Sueden\n");
  606.             break;
  607.         case WESTEN:
  608.             rotateL();
  609.             ausrichtung = SUEDEN;
  610.             printf("Sueden\n");
  611.             break;
  612.  
  613.            
  614.         }
  615.         for (int i = 0; i < y; i++) {
  616.             drive();
  617.             position[0]++;
  618.         }
  619.  
  620.     }
  621.  
  622.     if (y < 0) {
  623.         switch (ausrichtung) {
  624.         case WESTEN:
  625.             rotateR();
  626.             ausrichtung = NORDEN;
  627.             printf("Norden\n");
  628.             break;
  629.         case OSTEN:
  630.             rotateL();
  631.             ausrichtung = NORDEN;
  632.             printf("Norden\n");
  633.             break;
  634.         case SUEDEN:
  635.             rotateR();
  636.             rotateR();
  637.             ausrichtung = NORDEN;
  638.             printf("Norden\n");
  639.             break;
  640.  
  641.            
  642.         }
  643.         y = y * (-1);
  644.         for (int i = 0; i < y; i++) {
  645.             drive();
  646.             position[0]--;
  647.         }
  648.     }
  649.  
  650.  
  651.    
  652.    
  653.     ////////////////////////////////////////einstellen
  654.  
  655.     if (x == 0 && y == 0) {
  656.  
  657.         if (array[x_now + 1][y_now+1] == '1' || array[x_now + 1][y_now+1] == '2' || array[x_now + 1][y_now+1] == '3') {
  658.             printf("rechts unten\n");
  659.             Motor_Drive(Port_A, Motor_dir_forward, 10);             //Drehung einstellen
  660.             Motor_Drive(Port_B, Motor_dir_forward, 50);
  661.             Delay(1000);
  662.             Motor_Drive(Port_A, Motor_dir_forward, 10);            
  663.             Motor_Drive(Port_B, Motor_dir_forward, 50);
  664.             Delay(1000);
  665.         }
  666.  
  667.         if (array[x_now - 1][y_now-1] == '1' || array[x_now - 1][y_now-1] == '2' || array[x_now - 1][y_now-1] == '3') {
  668.             printf("links oben\n");
  669.             Motor_Drive(Port_A, Motor_dir_forward, 10);                    
  670.             Motor_Drive(Port_B, Motor_dir_forward, 50);
  671.             Delay(1000);
  672.             Motor_Drive(Port_A, Motor_dir_forward, 10);                  
  673.             Motor_Drive(Port_B, Motor_dir_forward, 50);
  674.             Delay(1000);
  675.         }
  676.  
  677.         if (array[x_now-1][y_now + 1] == '1' || array[x_now-1][y_now + 1] == '2' || array[x_now-1][y_now + 1] == '3') {
  678.             printf("links unten\n");
  679.             Motor_Drive(Port_A, Motor_dir_forward, 10);
  680.             Motor_Drive(Port_B, Motor_dir_forward, 50);
  681.             Delay(1000);
  682.             Motor_Drive(Port_A, Motor_dir_forward, 10);
  683.             Motor_Drive(Port_B, Motor_dir_forward, 50);
  684.             Delay(1000);
  685.         }
  686.  
  687.         if (array[x_now][y_now - 1] == '1' || array[x_now][y_now - 1] == '2' || array[x_now][y_now - 1] == '3') {
  688.             printf("rechts oben\n");
  689.             Motor_Drive(Port_A, Motor_dir_forward, 10);
  690.             Motor_Drive(Port_B, Motor_dir_forward, 50);
  691.             Delay(1000);
  692.             Motor_Drive(Port_A, Motor_dir_forward, 10);
  693.             Motor_Drive(Port_B, Motor_dir_forward, 50);
  694.             Delay(1000);
  695.         }
  696.  
  697.     }
  698.  
  699.     //////////////////////////////////////////////einstellen
  700. }
  701.  
  702.  
  703. ////////////////////////////Roboter fahren ENDE
  704.  
  705.  
  706. void calcWeg(struct weg* t1, int8_t * arrLength, int8_t table) {
  707.  
  708.     struct weg trash[MAX_WEG_K];
  709.     (*arrLength) = 0;
  710.  
  711.     //Initialisieren der struct arrays
  712.     for (int8_t i = 0; i < MAX_WEG_K; i++) {
  713.         t1[i].knoten = -1;
  714.         trash[i].knoten = -1;
  715.  
  716.         t1[i].xPos = 0;
  717.         t1[i].yPos = 0;
  718.         trash[i].yPos = 0;
  719.         trash[i].yPos = 0;
  720.     }
  721.  
  722.     // Finde kuerzersten VIP[]
  723.     int8_t zieldis1 = INVALID_SHORTTEST;
  724.     int8_t zielpos1 = 0;
  725.     for (int8_t i = 0; i < g_kCount; i++) {
  726.         if (K[i].vip[table] == 1) {
  727.             if (K[0].KnotenNachbarDistanceSumme[i] < zieldis1) {
  728.                 zieldis1 = K[0].KnotenNachbarDistanceSumme[i];
  729.                 zielpos1 = i;
  730.             }
  731.         }
  732.     }
  733.  
  734.     // Vorgaenger Knoten finden
  735.     int8_t laufKnoten = zielpos1;
  736.     trash[0].knoten = zielpos1;
  737.     for (int8_t j = 1; laufKnoten != 0; j++) { // 0 = der Start Knoten
  738.         trash[j].knoten = K[laufKnoten].vorgaenger;
  739.         laufKnoten = trash[j].knoten;
  740.     }
  741.  
  742.     // Array Laenge finden
  743.     while (1) {
  744.         if (trash[(*arrLength)].knoten == -1)
  745.             break;
  746.         (*arrLength)++;
  747.     }
  748.  
  749.     // Array umsortieren und ins Fahrarray schreiben
  750.     for (int8_t i = 0, j = (*arrLength) - 1; i < (*arrLength); i++, j--) {
  751.         t1[i].knoten = trash[j].knoten;
  752.     }
  753.  
  754.     // Array mit xPos und yPos fuellen
  755.     for (int8_t i = 0; i < (*arrLength) - 1; i++) {
  756.         t1[i].xPos = K[t1[i].knoten].KnotenNachbarDistanceX[t1[i + 1].knoten];
  757.         t1[i].yPos = K[t1[i].knoten].KnotenNachbarDistanceY[t1[i + 1].knoten];
  758.     }
  759.  
  760. }
  761.  
  762. void sendeWeg() {
  763.  
  764.     struct weg t1[MAX_WEG_K];
  765.     int8_t aLength = 0;
  766.  
  767.     for (int8_t j = 1; j <= 3; j++) {
  768.  
  769.         calcWeg(&t1[0], &aLength, j);
  770.  
  771.         //Hinweg übergeben an Roboter
  772.         for (int8_t i = 0; i < aLength; i++) {
  773.             RobiFahr(t1[i].yPos, t1[i].xPos);
  774.         }
  775.  
  776.         //Rückweg übergeben an Roboter
  777.         for (int8_t i = aLength - 1; i >= 0; i--) {
  778.             RobiFahr(t1[i].yPos * (-1), t1[i].xPos * (-1));
  779.         }
  780.  
  781.     }
  782. }
  783.  
  784.  
  785.  
  786. int main() {
  787.     suche_start();
  788.     initStruct();
  789.     printFeld(JUNK);
  790.  
  791.     algoJunk();
  792.     printFeld(JUNK);
  793.  
  794.     //algoJunk2();
  795.     //printFeld(JUNK);
  796.  
  797.     initStructUpdate();
  798.     printFeld(ALPHABET);
  799.  
  800.     if (algJunkSpecial());
  801.     initStructUpdate();
  802.     printFeld(ALPHABET);
  803.  
  804.     algoDistance();
  805.     algoDijkstra();
  806.     printStart();
  807.  
  808.     if (checkAufVorgaengerFehlt())
  809.         return 1; //Ausgabe am Bildschirm Error 404 not found
  810.  
  811.     sendeWeg();
  812.  
  813.    
  814.  
  815.     return 0;
  816. }
  817.  
  818.  
  819. #endif // !WEG_ALGO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement