Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: RouteCalculation.cpp
- Revision:
- 4:aff0722b4e50
- Parent:
- 2:cb6bae534500
- Child:
- 5:695c5531f65e
--- a/RouteCalculation.cpp Fri Apr 27 13:19:09 2018 +0000 +++ b/RouteCalculation.cpp Sat Apr 28 12:09:27 2018 +0000 @@ -17,15 +17,15 @@ Return: Neue momentanrichtung */ int directionControl(int turnDirection,int currentDirection){ // Links = 1, Rechts= 2 -if (turnDirection = 1){ //Drehung nach Links +if (turnDirection == 1){ //Drehung nach Links currentDirection = currentDirection -1; - if(currentDirection = 0){ + if(currentDirection == 0){ currentDirection = 4; } } -if (turnDirection = 2){ //Drehung nach Rechts +if (turnDirection == 2){ //Drehung nach Rechts currentDirection = currentDirection +1; - if(currentDirection = 5){ + if(currentDirection == 5){ currentDirection = 1; } } @@ -39,21 +39,24 @@ */ int RouteCalculation(){ - map[20,10]; // Wird mit Werten des Mapping() gefüllt + int map[20][10]; // Wird mit Werten des Mapping() gefüllt int X = 19; int Y = 9; int direction; int actionIndex; // Number des Befehls - char *route = (*char)malloc(2*sizeof(char)); // Speicher muss alloziert werden!! + + //char **route; + char *route = (char *)malloc(sizeof(char)); // Speicher muss alloziert werden!! + // Pos in route[X,0] - const int ZIEL = 0 - const int FULLDRIVE = 1 - const int TURNRIGHT = 2 - const int TURNLEFT = 3 - const int PLACETURN90 = 4 - const int LEER = 256 + const char ZIEL = 0; + const char FULLDRIVE = 1; + const char TURNRIGHT = 2; + const char TURNLEFT = 3; + const char PLACETURN90 = 4; + const char LEER = 256; // Pos in route[0,Y] @@ -62,25 +65,27 @@ const int LENGHT = 1; // Codierung Richtungenänderungen - const int DREHUNG_LINKS = 1 - const int DREHUNG_RECHTS = 2 + const int DREHUNG_LINKS = 1; + const int DREHUNG_RECHTS = 2; - int actionIndex = 1; + actionIndex = 1; int i = 0; int counterS = 0; int counterZ = 0; - + + char *str; + //SD-Karte lesen printf("Reading from SD card..."); - fp = fopen("/sd/map.csv", "r"); + FILE *fp = fopen("/sd/map.csv", "r"); if (fp != NULL) { - char *str = fgets(fp); // liesst gesammten Inhalt von map.csv ein + fgets(str,500,fp); // liesst gesammten Inhalt von map.csv ein char *ptr = strtok(str, ",;"); for (counterZ; counterZ < 9; counterZ++) { for (counterS; counterS < 19; counterS++){ - map[counterS][counterZ]= atoi(ptr); + map[counterS][counterZ] = atoi(ptr); ptr = strtok(NULL, ",;"); // zum nächsten Element wechseln } } @@ -93,31 +98,31 @@ //----------------------------------------------------------------------- - if (map[X-1, Y-1] == 0){ - route[actionIndex,TYPE] = PLACETURN90; + if (map[X-1][ Y-1] == 0){ + route[actionIndex][TYPE] = PLACETURN90; direction = 1; }else{ - route[actionIndex,TYPE] = FULLDRIVE; + route[actionIndex][TYPE] = FULLDRIVE; direction = 4; } - if(route[actionIndex,TYPE] == FULLDRIVE){ - while(map[X-1, Y] == 0){ + if(route[actionIndex][TYPE] == FULLDRIVE){ + while(map[X-1][Y] == 0){ if(X >= 1 | X <= 19){ X = X -2; i = i + 1; } } - route[actionIndex,LENGHT] = i; + route[actionIndex][LENGHT] = i; actionIndex = actionIndex +1; } - while (map[X,Y] != 100){ // Ziel = Abbruchbedingung + while (map[X][Y] != 100){ // Ziel = Abbruchbedingung i = 0; // ------------------------------------------------------------------------------------------- Grade Strecken fahren switch (direction){ - case 1: while(map[X-1,Y-1 == 0]){ // Gegen oben + case 1: while(map[X-1][Y-1] == 0){ // Gegen oben if (Y >= 1 | Y <= 7){ Y = Y - 2; i = i + 1; @@ -125,7 +130,7 @@ } break; - case 2: while(map[X,Y-1 == 0]){ // Gegen rechts + case 2: while(map[X][Y-1] == 0){ // Gegen rechts if (Y >= 1 | Y <= 19){ X = X + 2; i = i + 1; @@ -133,7 +138,7 @@ } break; - case 3: while(map[X,Y == 0]){ // Gegen unten + case 3: while(map[X][Y] == 0{ // Gegen unten if (Y >= 1 | Y <= 7){ Y = Y + 2; i = i + 1; @@ -141,7 +146,7 @@ } break; - case 4: while(map[X-1,Y == 0]){ // Gegen rechts + case 4: while(map[X-1][Y] == 0){ // Gegen rechts if (Y >= 1 | Y <= 19){ X = X - 2; i = i + 1; @@ -150,79 +155,79 @@ break; } actionIndex = actionIndex + 1; // Zur nächstesten Aktion - char *route = (*char)realloc(2*sizeof(char)); // Speicher für neue Aktion initialisieren - route[actionIndex,TYPE] = FULLDRIVE; // Fahrmodus auf geradeausfahren - route[actionIndex,LENGHT] = i; // Länge der Strecke eintragen + char *route = (char *)realloc(2*sizeof(char)); // Speicher für neue Aktion initialisieren + route[actionIndex][TYPE] = FULLDRIVE; // Fahrmodus auf geradeausfahren + route[actionIndex][LENGHT] = i; // Länge der Strecke eintragen // ------------------------------------------------------------------------------------------- Drehungen fahren switch(direction){ case 1: // Roboter gegen oben ausgerichtet - if(map[X,Y-1] == 0){ // gegen Rechts abbiegen + if(map[X][Y-1] == 0){ // gegen Rechts abbiegen actionIndex = actionIndex + 1; // Zur nächstesten Aktion - char *route = (*char)realloc(2*sizeof(char)); // Speicher für neue Aktion initialisieren - route[actionIndex, TYPE] = TURNRIGHT; - route[actionIndex, LENGHT] = LEER; + char *route = (char*)realloc(2*sizeof(char)); // Speicher für neue Aktion initialisieren + route[actionIndex][ TYPE] = TURNRIGHT; + route[actionIndex][ LENGHT] = LEER; direction = directionControl(DREHUNG_RECHTS, direction); } - if(map[X-1,Y] == 0){ // gegen Links abbiegen + if(map[X-1][Y] == 0){ // gegen Links abbiegen actionIndex = actionIndex + 1; - char *route = (*char)realloc(2*sizeof(char)); - route[actionIndex, TYPE] = TURNLEFT; - route[actionIndex, LENGHT] = LEER; + char *route = (char *)realloc(2*sizeof(char)); + route[actionIndex][ TYPE] = TURNLEFT; + route[actionIndex][ LENGHT] = LEER; direction = directionControl(DREHUNG_LINKS, direction); } case 2: // Roboter gegen rechts ausgerichtet - if(map[X-1,Y-1] == 0){ // gegen Oben abbiegen + if(map[X-1][Y-1] == 0){ // gegen Oben abbiegen actionIndex = actionIndex + 1; - char *route = (*char)realloc(2*sizeof(char)); - route[actionIndex, TYPE] = TURNLEFT; - route[actionIndex, LENGHT] = LEER; + char *route = (char *)realloc(2*sizeof(char)); + route[actionIndex][ TYPE] = TURNLEFT; + route[actionIndex][ LENGHT] = LEER; direction = directionControl(DREHUNG_LINKS, direction); } - if(map[X,Y] == 0){ // gegen Unten abbiegen + if(map[X][Y] == 0){ // gegen Unten abbiegen actionIndex = actionIndex + 1; - char *route = (*char)realloc(2*sizeof(char)); - route[actionIndex, TYPE] = TURNRIGHT; - route[actionIndex, LENGHT] = LEER; + char *route = (char *)realloc(2*sizeof(char)); + route[actionIndex][ TYPE] = TURNRIGHT; + route[actionIndex][ LENGHT] = LEER; direction = directionControl(DREHUNG_RECHTS, direction); } case 3: // Roboter gegen Unten ausgerichtet (Seitenverkehrt) - if(map[X,Y-1] == 0){ // gegen Rechts abbiegen + if(map[X][Y-1] == 0){ // gegen Rechts abbiegen actionIndex = actionIndex + 1; - char *route = (*char)realloc(2*sizeof(char)); - route[actionIndex, TYPE] = TURNLEFT; - route[actionIndex, LENGHT] = LEER; + char *route = (char *)realloc(2*sizeof(char)); + route[actionIndex][ TYPE] = TURNLEFT; + route[actionIndex][ LENGHT] = LEER; direction = directionControl(DREHUNG_LINKS, direction); } - if(map[X-1,Y] == 0){ // gegen Links abbiegen + if(map[X-1][Y] == 0){ // gegen Links abbiegen actionIndex = actionIndex + 1; - char *route = (*char)realloc(2*sizeof(char)); - route[actionIndex, TYPE] = TURNRIGHT; - route[actionIndex, LENGHT] = LEER; + char *route = (char *)realloc(2*sizeof(char)); + route[actionIndex][ TYPE] = TURNRIGHT; + route[actionIndex][ LENGHT] = LEER; direction = directionControl(DREHUNG_RECHTS, direction); } case 4: // Roboter gegen links ausgerichtet - if(map[X-1,Y-1] == 0){ // gegen oben abbiegen + if(map[X-1][Y-1] == 0){ // gegen oben abbiegen actionIndex = actionIndex + 1; - char *route = (*char)realloc(2*sizeof(char)); - route[actionIndex, TYPE] = TURNRIGHT; - route[actionIndex, LENGHT] = LEER; + char *route = (char *)realloc(2*sizeof(char)); + route[actionIndex][ TYPE] = TURNRIGHT; + route[actionIndex][ LENGHT] = LEER; direction = directionControl(DREHUNG_RECHTS, direction); } - if(map[X,Y] == 0){ // gegen unten abbiegen + if(map[X][Y] == 0){ // gegen unten abbiegen actionIndex = actionIndex + 1; - char *route = (*char)realloc(2*sizeof(char)); - route[actionIndex, TYPE] = TURNLEFT; + char *route = (char *)realloc(2*sizeof(char)); + route[actionIndex][ TYPE] = TURNLEFT; direction = directionControl(DREHUNG_LINKS, direction); } } - if (map[X,Y] == 100){ - char *route = (*char)realloc(2*sizeof(char)); + if (map[X][Y] == 100){ + char *route = (char *)realloc(2*sizeof(char)); actionIndex = actionIndex + 1; - route[actionIndex, TYPE] = ZIEL; - route[actionIndex, LENGHT] = ZIEL; + route[actionIndex][ TYPE] = ZIEL; + route[actionIndex][ LENGHT] = ZIEL; }