java–Rosbridge–ros通信(基于webscoket)

  • Post author:
  • Post category:java




ros官网

www.ros.org

rosjava文档

http://wiki.ros.org/rosjava

https://github.com/RobotWebTools

搭建ROS环境

https://blog.csdn.net/qq_44339029/article/details/120579608

Ros、Rosbridge常用命令:查看rostopic发布的轨迹、启动rosbridge

ROS相关:

rostopic list                    //查看当前所有正在推送话题列表

rostopic info [话题名称]          //查看话题推送消息type  订阅者、推送者

rostopic echo [话题名称]          //查看实时话题广播消息

例:rostopic echo /current_pose


Rosbridge相关:

启动rosbridge(二选一):
cd /opt/ros            //ll查看文件
ll                     

//如果是/opt/ros/melodic,用下面语法启动:
roslaunch /opt/ros/melodic/share/rosbridge_server/launch/rosbridge_websocket.launch &


//如果是/opt/ros/neotic,用下面语法启动:
roslaunch /opt/ros/neotic/share/rosbridge_server/launch/rosbridge_websocket.launch &

确保有话题开始广播之后且rosbridge启动之后:

rosjava连接rosbridge开始订阅消息:

首先需要引入相关pom依赖:

<dependency>
    <groupId>edu.wpi.rail</groupId>
    <artifactId>jrosbridge</artifactId>
    <version>0.2.0</version>
</dependency>

然后上代码连接rosbridge且开始订阅ros话题消息:

import ****************.GnssCallBack; //这个包是自定义包(看你的GnssCallBack位置)
import edu.wpi.rail.jrosbridge.Ros;
import edu.wpi.rail.jrosbridge.Topic;
import edu.wpi.rail.jrosbridge.handler.RosHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.websocket.Session;


@RestController
public class RosController implements RosHandler {
    private static final Logger logger = LoggerFactory.getLogger(RosController.class);


    //properties配置文件配置

    //ros服务器ip
    @Value("${ros.ip}")
    private String ip;

    //指定订阅的话题名称/类型
    @Value("${ros.topic.gnss.name}")
    private String gnssName;
    @Value("${ros.topic.gnss.type}")
    private String gnssType;


    
    //实现了话题消息接收(可对消息进行逻辑处理)
    @Autowired
    private GnssCallBack gnssCallBack;


    Ros ros = null;
    boolean connect = false;

    /**
     * ros服务器故障断开每隔五秒重连
     */
    @Scheduled(cron = "*/5 * * * * ?")
    void reconnect() {
        if (connect == false) {
            logger.error("未连接服务!正在重连ros服务器!");
            init();
        }
    }

    @PostConstruct
    void init() {
        try {
            ros = new Ros(ip);
            connect = ros.connect();
            if (connect) {
                Topic gnss_pose = new Topic(ros, gnssName, gnssType);

                gnss_pose.subscribe(gnssCallBack);    //开始订阅话题
               
                ros.addRosHandler(this);    //监听器监听ros连接状态
            }

        } catch (Exception e) {
            logger.error("ros服务器连接错误:" + e.toString());
            connect = false;
        }

    }

    @Override
    public void handleConnection(Session session) {
        logger.info("ros已连接:" + session.getId());
    }

    @Override
    public void handleDisconnection(Session session) {
        logger.error("断开ros连接:" + session.getId());
        connect = false;
    }

    @Override
    public void handleError(Session session, Throwable t) {
        logger.error("ros连接失败:" + session.getId() + "-----失败原因:" + t.toString());
        connect = false;
    }
}

上面代码的/gnss_pose只是ros话题中的实时路径消息,其中的GnssCallBack;

import edu.wpi.rail.jrosbridge.callback.TopicCallback;
import edu.wpi.rail.jrosbridge.messages.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
public class GnssCallBack implements TopicCallback {
    private static final Logger logger = LoggerFactory.getLogger(GnssCallBack.class);

    @Override
    public void handleMessage(Message message) {
        try {
            Map maps = (Map) JSON.parse(message.toString());   //message即该话题的广播消息
            
            //todo:这里对map做逻辑处理    
        
        } catch (Exception e) {
            logger.error(e.toString());
        }
    }


}

其他的消息如需订阅,可以以此类比:

Topic **= new Topic(ros, **Name, **Type);

**.subscribe(**CallBack);    //开始订阅话题

完事。



版权声明:本文为ZHOU_0522原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。