1

以下は、ロボットをシミュレーター内で動かすためのCコードの一部です。

while (1)
{
    sprintf(buf, "M LR 100 100\n");    //motor left and right moves with speed 100 each.
    write(sock, buf, strlen(buf));     //sends the buffer to the socket (simulator)
        int lme, rme;                  //lme and rme are right and left motor encoder values, the other value I need to send to buffer.
        sprintf(buf, "S MELR\n");      //sensor command to find ME values
        sscanf(buf, "S MELR %i %i\n", &lme, &rme);       //sending the actual ME values, that need to be sent to a(nother?) buffer.
        printf(buf, "lme , rme");      //the values of MEncoders.
    memset(buf, 0, 80);                //clear the buffer, set buffer value to 0
    read(sock, buf, 80);               //read from socket to get results.        
}

ロボットは速度100で移動しますが、端末にはS MELRのみが表示され、モーターエンコーダーの値は表示されないため、これは機能しませんが、M LRコマンドを削除すると値が表示されるため、MELR値と関係があると思います。バッファに送信されていません。これをどのように改善できますか、またはMELR値に新しいバッファーを設定するにはどうすればよいですか?

4

3 に答える 3

2

これらの行は明らかに間違っています:

sprintf(buf, "S MELR\n");      //sensor command to find ME values
sscanf(buf, "S MELR %i %i\n", &lme, &rme);  

上記では、最初にその文字列を に設定しbuf、それから 2 つの整数を解析しようとしましたが、明らかにそれらがありません。

確かに言うのは難しいですが、実際に応答を得るには、上記のwritesprintfに移動する必要があるようです。readsscanf


次に、この printf もナンセンスです。printf(buf, "lme , rme");

おそらく次のようなことを意味しますか?printf("Got lme %i, rme %i\n", lme, rme);

またはその目的は何printfですか?コメントからはよくわかりません...

于 2013-02-13T21:05:17.640 に答える
2

足りない/間違っていることがいくつかあるようです。ここであなたのコードに注釈を付けています:

while (1)
{
    sprintf(buf, "M LR 100 100\n");    //motor left and right moves with speed 100 each.

buf文字列「M LR 100 100\n」が含まれるようになりました

    write(sock, buf, strlen(buf));     //sends the buffer to the socket (simulator)

コマンドが書き込まれますsock

        int lme, rme;                  //lme and rme are right and left motor encoder values, the other value I need to send to buffer.

        sprintf(buf, "S MELR\n");      //sensor command to find ME values

新しいコマンドが書き込まれますbuf

        sscanf(buf, "S MELR %i %i\n", &lme, &rme);       //sending the actual ME values, that need to be sent to a(nother?) buffer.

から読み取ろうとしてbufいますが、含まれています"S MELR\n"...

--> おそらくwrite(sock, buf, strlen(buf)); read(sock,buf,80);ここに別のものが必要です

    printf(buf, "lme , rme");      //the values of MEncoders.
    memset(buf, 0, 80);                //clear the buffer, set buffer value to 0

に文字列を書き込んbufで、すぐにもう一度クリアしますか?

    read(sock, buf, 80);               //read from socket to get results.        
}

操作方法を誤解しているかもしれませんbufが、少なくとも 1 つのwrite(sock...操作を使用していることからもわかるように、文字列バッファーのようです。

以上の点に気をつければ、うまくいくかもしれません。

于 2013-02-13T21:11:26.010 に答える
0

もう一度 buf を読む必要はありませんか? lme と rme で何をしたいですか?

while (1)
{
    sprintf(buf, "M LR 100 100\n");    //motor left and right moves with speed 100 each.
    write(sock, buf, strlen(buf));     //sends the buffer to the socket (simulator)
        int lme, rme;                  //lme and rme are right and left motor encoder values, the other value I need to send to buffer.
        sprintf(buf, "S MELR\n");      //sensor command to find ME values
    write(sock, buf, strlen(buf));     //sends the buffer to the socket 
    read(sock, buf, 80);               //read from socket to get results.    
        sscanf(buf, "S MELR %i %i\n", &lme, &rme);       //sending the actual ME values, that need to be sent to a(nother?) buffer.
        // ???? printf(buf, "lme , rme");      //the values of MEncoders.
    memset(buf, 0, 80);                //clear the buffer, set buffer value to 0
    read(sock, buf, 80);  //???        //read from socket to get results.        
}

「指令した電圧と実際に達成した速度との差を補正する」方法は??. 私はロボットについて何も知りません… でも試してみてください : ?

int lme=100, rme=100;     //lme and rme are right and left motor encoder values, the other value I need to send to buffer.             
while (1)
{
    sprintf(buf, "M LR %i %i\n", lme, rme);    //motor left and right moves with speed 100 each.
    write(sock, buf, strlen(buf));     //sends the buffer to the socket (simulator)
        sprintf(buf, "S MELR\n");      //sensor command to find ME values
    write(sock, buf, strlen(buf));     //sends the buffer to the socket 
    read(sock, buf, 80);               //read from socket to get results.    
        sscanf(buf, "S MELR %i %i\n", &lme, &rme);       
    lme=100+(100-lme) ; rem=100+(100-rme);  // compensate  ????
}
于 2013-02-13T21:04:51.820 に答える