ROS中canopen的使用(2)
上一篇中主要谈及的是发送can消息,使用topic_to_socketcan节点。虽然小车的底盘can消息可以通过socketcan_to_topic节点自动地转化成frame.msg。但是我还是自己使用can测试工具自己验证了一下,并初步找到了过滤方式。
1.创建虚拟can设备
使用can-utils,我是很久之前下的,忘记怎么弄的了。csdn上搜就好了,然后创建虚拟can口。创建虚拟can。注意创建的can名字,后面要绑定在sockecan_to_topic的节点上的,所以很重要。
2.接收topic.cpp
#include<iostream>
#include <stdlib.h>
#include <stdio.h>
#include"ros/ros.h"
#include"learning_vehicle/Frame.h"
using namespace std;
void callback(const learning_vehicle::Frame &velocity)
{
std::string v=to_string(velocity.data[0])+" "+to_string(velocity.data[1])+" "+
to_string(velocity.data[2])+" "+to_string(velocity.data[3])+" "+to_string(velocity.data[4])+" "+
to_string(velocity.data[5])+" "+to_string(velocity.data[6])+" "+to_string(velocity.data[7]);
ROS_INFO("%u,%s",velocity.id,v.c_str());
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"lenovo1");
ros::NodeHandle nh;
ros::Subscriber sub=nh.subscribe("received_messages",10,callback);
ros::spin();
return 0;
}
代码很简单,主要是回调函数如何打印这个,本人代码能力太差只想到这个方法。因为接收的frame.msg里的data是无符号整型数组,得转换成字符串来显示。
cansend can0 0A4#1111111122334455
3. 设置过滤标识符
看了好久的源代码,还是不会使用filters,希望有兄弟看懂了私信教我一下,目前我的方法如下。
将图中的代码116行弄成这个样子,用if包起来,只有id=164的也就是0A4的can报文才会被发布成topic。
效果如下图
可以看到一共发送了6条消息,candump全部接收到了,但是Lenovo1节点只接受到了id为0A4的三条。
结语
欢迎兄弟们使用之后交流心得。