2013-11-03 252 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個參數的原型。