AGX与底盘通讯方式有两种:

1.socketcan,自己创建socket,向底盘发送can帧

2.ros_canopen,启动topic_to_socketcan节点,只需向/sent_message话题发送can_msgs::Frame类型的消息,节点自动将消息转换成socketcan模式,无需自己创建socket

 

socketcan_to_topic :将socketcan帧转换为话题消息

topic_to_socketcan:将话题消息转换为socketcan帧

 

#include <ros/ros.h>
#include <ros/spinner.h>
#include <std_msgs/String.h>
#include <tf/transform_broadcaster.h>
#include <nav_msgs/Odometry.h>
#include <geometry_msgs/Twist.h>
#include <std_msgs/String.h>
#include <socketcan_bridge/topic_to_socketcan.h>
#include <socketcan_bridge/socketcan_to_topic.h>
#include <can_msgs/Frame.h>
#include <socketcan_interface/socketcan.h>

void send_can_message()

{
    ros::NodeHandle roscan("~");

    ros::Publisher roscan_send_message;

    roscan_send_message = roscan.advertise<can_msgs::Frame>("/sent_messages", 100);

    while (ros::ok())

    {

        ROS_INFO("start send can_message");

        setlocale(LC_ALL, "");

        uint8_t transition[8]; 

        can_msgs::Frame can_frame;

        memset(&transition, 0, sizeof(transition)); 

        transition[0] = 0x2b;

        transition[1] = 0x00;

        transition[2] = 0x20;

        transition[3] = 0x00;

        transition[4] = 0xe8;

        transition[5] = 0x03;

        transition[6] = 0x00;

        transition[7] = 0x00;

        can_frame.id = 0x601; //需要修改

        can_frame.is_extended = true;

        can_frame.is_rtr = false;

        can_frame.is_error = false;

        can_frame.dlc = 8; //有效数据长度

        for (uint8_t i = 0; i < can_frame.dlc; i++)

        {

            can_frame.data[i] = transition[i];
        }

        roscan_send_message.publish(can_frame);

        ROS_INFO("send messages succeed!");

        ros::Duration(0.3).sleep();

        ros::spinOnce();
    }
}

int main(int argc, char **argv)
{

    ros::init(argc, argv, "driver");

    ros::NodeHandle n("~");

    send_can_message();
}

使用candump命令过滤收到的消息:candump can0,581:DFFFFFFF

EXample:
  candump -c -c -ta can0,123:7FF,400:700,#000000FF can2,400~7F0 can3 can8   candump -l any,0~0,#FFFFFFFF   (log only error frames but no(!) data frames)   candump -l any,0:0,#FFFFFFFF    (log error frames and also all data frames)   candump vcan2,92345678:DFFFFFFF    (match only for extended CAN ID 12345678)   candump vcan2,123:7FF    (matches CAN ID 123 - including EFF and RTR frames)   candump vcan2,123:C00007FF    (matches CAN ID 123 - only SFF and non-RTR frames)

 

参考:https://blog.csdn.net/zyf_to_utopia/article/details/116209605

 

posted on 2022-07-21 16:14  Manuel  阅读(1129)  评论(0编辑  收藏  举报