Android使用ROSBridge与ROS通信 简单使用

ROS官方只支持了C++和Python,想要在Android上与ROS通讯,我的选择是ROSBridge

环境
ROS kinetic

ROS 服务端

安装
    sudo apt-get install ros-<rosdistro>-rosbridge-suite
启动
    roslaunch rosbridge_server rosbridge_websocket.launch

在这之前不需要开启 roscore, 因为 rosbridge 会默认执行 roscore

Android客户端

要让 android 接收或者发送 ROS 消息的话,首先要在 Android上完成 websocket,然后按照协议解析,也很麻烦,不过又要站在巨人的肩膀上了,找到一个开源项目:ROSBridgeClient,这位同学使用 java-websocket 的包在Android上实现了 websocket 的应用,很棒。

直接把 src/com/jilk/ros 目录复制到 我的 Android 项目里,
当然会报错啦,这些代码依赖了第三方库,加在Android工程的libs 里面 引用

  • eventbus.jar 用于发送从ROS接收到的消息
  • java_websocket.jar 用于websocket 的实现
  • json-simple-1.1.jar 用于json解析

复制到项目包里的 代码包含了一个 example .
完全可以使用

public class Example {
    
    public Example() {}
    
    public static void main(String[] args) {        
        ROSBridgeClient client = new ROSBridgeClient("ws://162.243.238.80:9090");
        client.connect();
        //testTopic(client);
        try {
        testService(client);
        }
        catch (RuntimeException ex) {
            ex.printStackTrace();
        }
        finally {
            client.disconnect();
        }
    }            
    
    public static void testService(ROSBridgeClient client) {
        try {
            Service<Empty, GetTime> timeService =
                    new Service<Empty, GetTime>("/rosapi/get_time", Empty.class, GetTime.class, client); 
            timeService.verify();
            //System.out.println("Time (secs): " + timeService.callBlocking(new Empty()).time.sec);
            
            Service<com.jilk.ros.rosapi.message.Service, Type> serviceTypeService =
                    new Service<com.jilk.ros.rosapi.message.Service, Type>("/rosapi/service_type",
                        com.jilk.ros.rosapi.message.Service.class, Type.class, client);
            serviceTypeService.verify();
            String type = serviceTypeService.callBlocking(new com.jilk.ros.rosapi.message.Service("/rosapi/service_response_details")).type;
            
            Service<Type, MessageDetails> serviceDetails =
                    new Service<Type, MessageDetails>("/rosapi/service_response_details",
                        Type.class, MessageDetails.class, client);
            serviceDetails.verify();
            //serviceDetails.callBlocking(new Type(type)).print();
            
            Topic<Log> logTopic =
                    new Topic<Log>("/rosout", Log.class, client);
            logTopic.verify();

            /*
            System.out.println("Nodes");
            for (String s : client.getNodes())
                System.out.println("    " + s);
            System.out.println("Topics");
            for (String s : client.getTopics()) {
                System.out.println(s + ":");
                client.getTopicMessageDetails(s).print();
            }
            System.out.println("Services");
            for (String s : client.getServices()) {
                System.out.println(s + ":");
                client.getServiceRequestDetails(s).print();
                System.out.println("-----------------");
                client.getServiceResponseDetails(s).print();
            }
            */
        }
        catch (InterruptedException ex) {
            System.out.println("Process was interrupted.");
        }
        /*
        Service<Empty, Topics> topicService =
                new Service<Empty, Topics>("/rosapi/topics", Empty.class, Topics.class, client);
        Service<Topic, Type> typeService =
                new Service<Topic, Type>("/rosapi/topic_type", Topic.class, Type.class, client);
        Service<Type, MessageDetails> messageService =
                new Service<Type, MessageDetails>("/rosapi/message_details", Type.class, MessageDetails.class, client);
        try {
            Topics topics = topicService.callBlocking(new Empty());
            for (String topicString : topics.topics) {
                Topic topic = new Topic();
                topic.topic = topicString;
                Type type = typeService.callBlocking(topic);
                MessageDetails details = messageService.callBlocking(type);
                System.out.println("Topic: " + topic.topic + " Type: " + type.type);
                details.print();
                System.out.println();
            }
            Type type = new Type();
            type.type = "time";
            System.out.print("Single type check on \'time\': ");
            messageService.callBlocking(type).print();
        }
        catch (InterruptedException ex) {
            System.out.println("testService: process was interrupted.");
        }
        */
    }

    public static void testTopic(ROSBridgeClient client) {
        Topic<Clock> clockTopic = new Topic<Clock>("/clock", Clock.class, client);
        clockTopic.subscribe();
        try {Thread.sleep(20000);} catch(InterruptedException ex) {}
        Clock cl = null;
        try {
            cl = clockTopic.take(); // just gets one
        }
        catch (InterruptedException ex) {}
        cl.print();
        cl.clock.nsecs++;
        clockTopic.unsubscribe();
        clockTopic.advertise();
        clockTopic.publish(cl);
        clockTopic.unadvertise();
    }
}

example很好理解
就看了下 topic 相关的东西 testTopic,我觉得如果有很多topic就要用很多的testXXXTopic了,有点麻烦,所以我二次封装了一个 RosBridgeClientManager 来用

连接 ROS master
/**
     * 连接 ROS master
     * @param url ROS master IP
     * @param port ROS master 端口
     * @param listener 连接状态监听器
     */
    public void connect(final String url, int port, final ROSClient.ConnectionStatusListener listener) {
        if (url != null && url.equals(mCurUrl)) {
            // already connected
        } else {
            mRosBridgeClient = new ROSBridgeClient("ws://" + url + ":" + port);
            mRosBridgeClient.connect(new ROSClient.ConnectionStatusListener() {
                @Override
                public void onConnect() {
                    // connected successful
                    mCurUrl = url;
                    if (listener != null) {
                        listener.onConnect();
                    }
                }

                @Override
                public void onDisconnect(boolean normal, String reason, int code) {
                    // client disconnected
                    if (listener != null) {
                        listener.onDisconnect(normal, reason, code);
                    }

                }

                @Override
                public void onError(Exception ex) {
                    // connect error
                    if (listener != null) {
                        listener.onError(ex);
                    }
                }
            });
        }
    }

加了一个连接监听器,可以在业务层进行状态判断了。

注册topic 到 ROS
    /**
     * 注册topic
     * @param topicName topic 名称
     * @param data_type 消息类型
     * @param <T>
     */
    public <T> void advertiseTopic(String topicName, T data_type) {
        AdvertiseTopicObject<T> topic = new AdvertiseTopicObject<>(topicName, data_type, mRosBridgeClient);
        topic.setMessage_type(data_type);
        topic.advertise();

        // 利用 反射获取泛型,主要是得到 T.class,我也没试
//        Class <T> entityClass = (Class <T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
//        Topic topic = new Topic(topicName, entityClass, client);
//        topic.advertise();
    }

原来的 advertise 已经很简单了,为什么我还要弄这个东西? 我也不知道啊
AdvertiseTopicObject.java

public class AdvertiseTopicObject<T> {

    private T message_type;

    private String topicName;

    private ROSBridgeClient client;

    public AdvertiseTopicObject(String topicName, T type, ROSBridgeClient rosBridgeClient) {
        this.client = rosBridgeClient;
        this.topicName = topicName;
        this.message_type = type;
    }


    public void advertise() {
        Topic topic = new Topic(topicName, message_type.getClass(), client);
        topic.advertise();
    }
}
发布topic 消息
/**
     * 发布 topic 消息
     * @param topicName topic名称
     * @param msg 消息
     * @param <T> 消息类型
     */
    public <T> void publishTopic(String topicName, T msg) {
        PublishTopicObject<T> publishTopicObject = new PublishTopicObject<>();
        publishTopicObject.setTopic(topicName);
        publishTopicObject.setMsg(msg);

        String msg_str = mGson.toJson(publishTopicObject);
        mRosBridgeClient.send(msg_str);
    }

跟上面的 AdvertiseTopicObject 保持一致,所以有了
PublishTopicObject.java

public class PublishTopicObject<T> {

    private String op = "publish";

    private String topic;

    private T msg;
}
订阅 topic
/**
     * 订阅topic
     * @param topicName topic 名称
     * @param listener 消息监听器
     */
    public void subscribeTopic(String topicName, OnRosMessageListener listener
    {
        SubscribeTopicObject subscribeTopicObject = new SubscribeTopicObject();
        subscribeTopicObject.setTopic(topicName);

        String msg_str = mGson.toJson(subscribeTopicObject);
        mRosBridgeClient.send(msg_str);
        addROSMessageListener(listener);

    }

同理:跟上面的 PublishTopicObject 保持一致,所以有了
SubscribeTopicObject.java

public class SubscribeTopicObject {

    private String op = "subscribe";

    private String topic;

    public String getOp() {
        return op;
    }
}
取消订阅 topic
/**
     * 取消订阅topic
     * @param topicName
     * @param listener
     */
    public void unSubscribeTopic(String topicName, OnRosMessageListener listener) {
        UnSubscribeTopicObject unSubscribeTopicObject = new UnSubscribeTopicObject();
        unSubscribeTopicObject.setTopic(topicName);

        String msg_str = mGson.toJson(unSubscribeTopicObject);
        
        mRosBridgeClient.send(msg_str);
        removeROSMessageListener(listener);
    
    }

还有 UnSubscribeTopicObject.java

public class UnSubscribeTopicObject {

    private String op = "unsubscribe";

    private String topic;
}
接收ROS 消息
//Receive data from ROS server, send from ROSBridgeWebSocketClient onMessage()
    // using eventbus ?!
    public void onEvent(final PublishEvent event) {
        Log.d("TAG", event.msg);
        for (int index = 0 ; index < mROSListenerList.size(); index++) {
            mROSListenerList.get(curIndex).onStringMessageReceive(event.name, stringData);
            //mROSListenerList.get(curIndex).onImageMessageReceive(event.name, imageData);
        }
    }

在 ROSBridgeWebSocketClient.java 里面找到了接收消息后发出来的代码

    EventBus.getDefault().post(new PublishEvent(operation,publish.topic,content));

Emmm... 用的是 EventBus,先用起来再说。

还有就是关于服务的封装了,没写。

完成代码在 Github gist

参考:
使用rosbridge协议实现安卓跟ROS的解耦
在泛型中得到T.class

已发布至:我说的这句话是谎话

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,566评论 25 707
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,472评论 2 59
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong阅读 21,767评论 1 92
  • 母亲时间。 ——它的制订标准不同于传统的时区划分,完全取决于母亲的心情,她说现在几点,就是几点 壁虎和班主任。 —...
    麦小兜小孩阅读 218评论 0 0
  • 提到山城,大家并不陌生,那就是美丽的重庆。多年前的电视剧《山城棒棒军》客观的展示了重庆的地理特点:不是所有的目...
    green00阅读 261评论 0 3