简体   繁体   中英

Little help needed with C Code

Hello I am working on a line follower robot with a single IR sensor .

I have been provided the simulation client + hardware . I am trying simulation part now in which I have to move robot along valid path until I found a token in the maze. Then stop and give out the coordinates x, y like this: "token found on x , y". The coordinates given by the server application based on a global coordinate system (eg east, west...) your robot has an local coordinate system (eg to the left, to the right, forward...). Choose an orientation of the robot at start and show on screen, the actual coordinates too. Before every movement send to the server application give out on the screen the necessary correction of the robots orientation ("to the left", "forward","to the right", "turn around") and then the coordinates send to the server. Example: Welcome to Sim Robot is on intersection: 3 , 5 with orientation to: south robot move: to the left robot go to: 4 , 5 ... token found on: 2 , 1

There are two ways to simulate the program either locally or on remote server , on local machine I can see the maze but on remote server is always gives me the unknown maze.

I have two C file

roboproxy.c

    #include <stdarg.h>
#include "../h/RobotProxy.h"

/// initialized with ROBOT_FAIL
int currentIntersection = ROBOT_FAIL;

#if !defined(vasprintf)
static int vasprintf(char **s, const char *format, va_list ap) {
    /* Guess we need no more than 100 bytes. */
    int n, size = 100;
    va_list save_ap;

    if ((*s = (char*) malloc(size)) == NULL)
        return -1;
    while (1) {
        /* work on a copy of the va_list because of a bug
         in the vsnprintf implementation in x86_64 libc
         */
#ifdef __va_copy
        __va_copy(save_ap, ap);
#else
        save_ap = ap;
#endif
        /* Try to print in the allocated space. */
#ifdef _vsnprintf
        n = _vsnprintf(*s, size, format, save_ap);
#else
        n = vsnprintf(*s, size, format, save_ap);
#endif
        va_end(save_ap);
        /* If that worked, return the string. */
        if (n > -1 && n < size) {
            return n;
        }
        /* Else try again with more space. */
        if (n > -1) { /* glibc 2.1 */
            size = n + 1; /* precisely what is needed */
        } else { /* glibc 2.0 */
            size *= 2; /* twice the old size */
        }
        if ((*s = (char*) realloc(*s, size)) == NULL) {
            return -1;
        }
    }
}
#endif

#if !defined(asprintf)
static int asprintf(char **s, const char *format, ...) {
    va_list vals;
    int result;

    va_start(vals, format);
    result = vasprintf(s, format, vals);
    va_end(vals);
    return result;
}
#endif

/// Set the robot to the specified position
/// @ returns ROBOT_SUCCESS, ROBOT_FAIL or ROBOT_TOKENFOUND
int Robot_Move(int x, int y) {
    char* buffer;
    asprintf(&buffer, "{\"x\":%d,\"y\":%d}", x, y);
    char* query = url_encode(buffer);
    free(buffer);
    char* response = sendAndRecieve(concat(URL, query));

    if (response == NULL) {
        puts("Connection to server failed!");
        return ROBOT_FAIL;
    }

    if (contains(response, "\"code\":1")) {
        puts("Connection declined!");
        return ROBOT_FAIL;
    }

    if (contains(response, "\"code\":2")) {
        puts("Connection blocked!");
        return ROBOT_FAIL;
    }

    if (contains(response, "\"code\":3")) {
        printf("Invalid position! (x=%d, y=%d)\n", x, y);
        return ROBOT_FAIL;
    }

    int foundIntersection = 0;
    bool token = false;

    if (contains(response, "\"north\":true"))
        foundIntersection |= D_N;
    if (contains(response, "\"east\":true"))
        foundIntersection |= D_E;
    if (contains(response, "\"south\":true"))
        foundIntersection |= D_S;
    if (contains(response, "\"west\":true"))
        foundIntersection |= D_W;

    if (contains(response, "\"token\":true"))
        token = true;

    free(query);

    currentIntersection = foundIntersection;
    if (token)
        return ROBOT_TOKENFOUND;

    return ROBOT_SUCCESS;
}

/// Get the intersections of the current node that the robot is at
/// @ returns always the intersection at position x=0,y=0 if Robot_Move was not called first
int Robot_GetIntersections() {
    if (currentIntersection == ROBOT_FAIL)
        Robot_Move(0, 0);
    return currentIntersection;
}

Roboclientsim.C

    #include "../h/Configuration.h"

int main(void) {

    printf("Token: %d\n", Robot_Move(1, 0));
    printf("Intersection: %d\n", Robot_GetIntersections());

    return EXIT_SUCCESS;
}

as I run the program in console It gives me that Token is 1 and then after finding any token it will become 2.

so in roboclientsim.c I added "

 If (Robot_Move()==2) //as Robot_move is returning the %d value for Token
printf("another token found ");

"

But my compiler is giving problem that few arguemnts for the robot_move,

Can anyone help me with this how to do it?

Robot_Move takes 2 arguments:

int Robot_Move(int x, int y);

And you call it without any argument.

If (Robot_Move()==2)

That's the error. I suspect that what you wanted to do was:

int main(void) {
    int token = Robot_Move(1, 0);
    printf("Token: %d\n", token);
    printf("Intersection: %d\n", Robot_GetIntersections());

    if (token == 2) 
       printf("another token found ");

    return EXIT_SUCCESS;
}

or maybe:

int main(void) {
    int token = Robot_Move(1, 0);

    if (token == 2) {
       printf("Token: %d\n", token);
       printf("Intersection: %d\n", Robot_GetIntersections());
    } else {
       printf("another token found ");
    }

    return EXIT_SUCCESS;
}

检查方法int Robot_Move(int x, int y)的原型。调用它时需要提供2个参数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM