2013-11-03 262 views
0

您好,我正在使用单个红外传感器的线性跟随器机器人。C代码需要帮助

我已经提供了模拟客户端+硬件。我现在正在尝试模拟部分,我必须沿着有效的路径移动机器人,直到我在迷宫中找到一个令牌。然后停止 并发出像这样的坐标x,y:“在x,y上发现的令牌”。由服务器应用程序基于全局坐标系统(例如,东,西...)给出的坐标 您的机器人具有本地坐标系统(例如向左,向右,向前...) 。 在开始时选择机器人的方向并在屏幕上显示,实际坐标 也是如此。在每次移动发送到服务器应用程序之前,在屏幕上发出 必要的机器人方向校正(“向左”,“向前”,“向右”, “转身”),然后坐标发送到服务器。 实施例: 欢迎SIM 机器人是在交叉点:3,5 与取向:南 机器人移动:向左 机器人去:4,5 ... 令牌上找到:2,1

有两种方法可以在本地或远程服务器上模拟程序,在本地机器上我可以看到迷宫,但在远程服务器上总是会给我带来未知的迷宫。

我有两个C文件

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; 
} 

我在控制台运行它,它给了我,令牌是1,然后找到任何令牌后它将变成2.

所以在roboclientsim.c我加 “

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

但我的编译器是给问题,对于robot_move几个arguemnts,

谁能帮我这该怎么办呢?

+1

'Robot_Move'是一个函数,有两个参数,你不能没有任何参数调用它。 – ouah

+0

欢迎来到堆栈溢出! “代码长城”问题不适合网站的问答形式。您需要将问题分离到比这更具体的问题。 – dasblinkenlight

回答

1

Robot_Move需要两个参数:

int Robot_Move(int x, int y); 

而且你怎么称呼它不带任何参数。

If (Robot_Move()==2) 

这就是错误。我怀疑那是你想要做的是:

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; 
} 

或可能:

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; 
} 
+0

问题是这两个文件在同一个项目中,并且toke已经在robotproxy.c文件中定义为bool。 因为robot_move返回一个单一%d值,我想那个值,我需要把这个条件对每个intersction,如果机器人移动(X,Y)== 2则printf的令牌发现 或者有没有更好的如何做到这一点? 我试着用外部变量的命令,但它不是与 – user2828488

+0

你绝对需要了解本地和全局变量的概念工作。网上有很多很好的解释。请阅读有关C语言基础知识的教程。了解'Robot_Move'中定义的'token'变量只能在该函数内部访问。即使他们有相同的名字,他们也不一样!不要使用全局变量来执行解决方法。 Robot_Move会返回它是否运行良好,如果有的话。当你写Robot_Move时,你要求你的机器人移动。 –

0

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