从IRobot Create 2打印串行数据

我在irobot create 2的接口上工作,无法读取单个传入的数据包。在哪里可以找到有关通过termios,read(),write()和printf()使用此操作的详细信息?除了大学期间的一些机器人项目外,我对这种编程还很陌生,并且可能缺少一些关键点。请饶恕我的无知。

到目前为止,我已经成功确认发送了用于初始化机器人,以各种模式启动它,启动/停止IO以及关闭机器人的命令。为了读取作为单个字节输入的数据,我已发送命令以通过写功能定位指定的传感器数据包,这让我感到困惑。我已经为单个字节分配了一个向量,并尝试读取()返回的值,然后使用printf()。我不确定在打印值或什至得到想要的数据时要使用哪种数据类型。

*yungBot.cpp*
#include "yungBot.h"
#include <iostream>


/*initialize robot*/
int yungBot::init(const char *yungin){

    fd = open(yungin,O_RDWR | O_NOCTTY | O_NDELAY);
    if(fd == -1){ 
        perror("Error,failed to connect");
    }
    else{
        fcntl(fd,F_setfL,0);
        tcflush (fd,TCIFLUSH);
    }
    struct termios parameters;

    int get = tcgetattr(fd,&parameters);
    if(get == -1){ 
        perror("Error getting attributes");
    }
    else{ 
        printf("%s\n","Get attributes: success");
    }
    cfmakeraw (&parameters); 
    //sets input and output baud rate
    cfsetispeed(&parameters,B115200);
    cfsetospeed(&parameters,B115200);

    // or forces values to 1; and forces all values to 0 (off); 
    parameters.c_iflag &= ~(IXON | IXOFF); //flow control off; 
    parameters.c_cflag |=(CLOCAL | CREAD);
    parameters.c_cflag &= ~(PARENB | CSTOPB);//no parity 
    parameters.c_cflag &= ~CSIZE; //mask the character bits
    parameters.c_cflag |= (CS8); //8 bit character size 
    parameters.c_oflag = 0;
    parameters.c_lflag = 0;//ICANON=canonical mode
    parameters.c_cc[VMIN] = 0; // 1 input byte is enough to return from read()
    parameters.c_cc[VTIME] = 1;// Timer off 

    //set attribute immediately
    int set = tcsetattr(fd,TCSANOW,&parameters); 
    if(set == -1){
        perror("Error setting attributes \n");
    }
    if (fd == -1){
        perror(yungin);
        return -1;
    }   
    usleep(200000);         
    return fd;
}


/*stream desired data packets of 1 unsigned byte*/
int yungBot::stream(int packet){
    unsigned char command[]={142,packet};
    if(write(fd,command,sizeof(command))==-1){
        perror("failed to retrieve data packet");
        return -1;
    }
    unsigned char response[1];
    if(read(fd,response,sizeof(response))!=1){
        perror("failed to write data packet");
        return -1;
    }
    //shift through the byte for individual bits
    /*unsigned char bit = response[1]; 
    for(int i = 0; i < CHAR_BIT; i++){
    printf("%d",(bit>>i)&1);
    }
    */
    printf("%i",response[0]);
return response[0];
}

老实说,我不确定会发生什么。如果我读到诸如充电状态之类的信息,似乎在不充电时返回0值,而在当前bc充满电时返回3值。当返回颠簸和车轮跌落传感器之类的东西(作为单个字节发送,范围为0-15)时,我不确定该怎么做。按此按钮的不同部分时,我会在该范围内得到不同的值。保险杠等..但是字节的4位对应于4个不同的值,而其他4个则被“保留”。我该如何读取类似的信息?我注意到车轮跌落/碰撞传感器得到了某些值取决于我按下/抬起机器人的位置,例如:

right bump=1
left bump=2
middle bump=3

right drop=4
left drop =8
both dropped = 12

这就是我所需要的吗?

pengjigui 回答:从IRobot Create 2打印串行数据

哦,这很简单。您可以通过类似...的方式读取这些位。

#define LEFT_BUMPER 1
#define RIGHT_BUMPER 2
#define REAR_BUMPER 4
#define FRONT_BUMPER 8

if(bumper_state & LEFT_BUMPER){ printf("left bumper actuated\n") }
if(bumper_state & RIGHT_BUMPER){ printf("right bumper actuated\n") }

最适合我们使用的数据类型通常是与您正在读取的内容大小相同的无符号类型,在这种情况下,最好使用uint8_t。

此外,您不需要数组,可以根据需要读入单个字节。以后可能会帮助避免混乱。

本文链接:https://www.f2er.com/3121472.html

大家都在问