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,然后尝试从中解析出两个整数,当它显然没有它们时。

很难确定,但看起来你需要writesprintf上面那个read之后,然后在之后移动它才能真正得到响应, before sscanf


那么这个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操作,但它似乎是一个字符串缓冲区 - 正如使用至少一个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.        
}

您如何“<em>补偿您命令的电压与您实际达到的速度之间的差异”??我对机器人一无所知……但你可以试试:?

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 回答