RosSLAM、OpenCV、Linux、ROS等在大学踩过的坑

小白踩坑/关于ROS和手机端的通信的一些经验(一)

2019-12-24  本文已影响0人  LLLL先生

首先贴上我参考的文章

https://blog.csdn.net/dadachenchen/article/details/83865255

示例源码

编译环境

个人建议是直接在unbuntu系统中安装Android Studio
linux版本安装教程

demo运行

更改build.gridlew文件

基本教程是有了,但是要实现ROS和手机通信的基本功能是不能把上文里给出的源码直接编译的。遇到的第一个问题就是因为源码过于“古老”而产生的版本不通问题。这一问题基本可以自行百度解决。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28//更改版本
    defaultConfig {
        applicationId "com.example.dadac.testrosbridge"
        minSdkVersion 15
        targetSdkVersion 28//更改版本
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation files('libs/json-simple-1.1.jar')
}
安装rosbridge

参考文章开头转载的博客里的教程即可。
安装时注意系统版本,输入的命令不同。

运行示例源码

有时会提示缺少扩展包,直接默认下载即可。
为了测试通信效果,需要发布一个测试topic。关于rostopic怎么发布与编译同样有很多教程可以参考了。
rostopic教程

代码重要功能函数

app/src/main/java/com/dadac/testrosbridge/RosBridgeActivity.java

public class RosBridgeActivity extends Activity implements View.OnClickListener {

    ROSBridgeClient client;
    String ip = "192.168.2.119";   //此处即为ROS端即电脑的IP

上面为输入电脑IP的位置

下面是Android端的连接函数,为之后的的应用做准备。

    public void onConnect(String ip, String port) {

        client = new ROSBridgeClient("ws://" + ip + ":" + port);
        boolean conneSucc = client.connect(new ROSClient.ConnectionStatusListener() {
            @Override
            public void onConnect() {
                client.setDebug(true);
                ((RCApplication) getApplication()).setRosClient(client);
                showTip("Connect ROS success");
                Log.d("dachen", "Connect ROS success");
            }


            @Override
            public void onDisconnect(boolean normal, String reason, int code) {
                showTip("ROS disconnect");
                Log.d("dachen", "ROS disconnect");
            }

            @Override
            public void onError(Exception ex) {
                ex.printStackTrace();
                showTip("ROS communication error");
                Log.d("dachen", "ROS communication error");
            }
        });
    }

    //接收来自Ros端的数据
    private void ReceiveDataToRos() {
        if (isSubscrible == true) {
            String msg1 = "{\"op\":\"subscribe\",\"topic\":\"/chatter\"}";
            client.send(msg1);
        } else if (isSubscrible == false) {
            String msg2 = "{\"op\":\"unsubscribe\",\"topic\":\"/chatter\"}";
            client.send(msg2);
        }
    }

    //发送数据到ROS端
    private void SendDataToRos(String data) {
        String msg1 = "{ \"op\": \"publish\", \"topic\": \"/chatter\", \"msg\": { \"data\": \"" + data + " \" }}";
        //        String msg2 = "{\"op\":\"publish\",\"topic\":\"/cmd_vel\",\"msg\":{\"linear\":{\"x\":" + 0 + ",\"y\":" +
        //                0 + ",\"z\":0},\"angular\":{\"x\":0,\"y\":0,\"z\":" + 0.5 + "}}}";
        client.send(msg1);
    }


    private void showTip(final String tip) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(RosBridgeActivity.this, tip, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void onEvent(final PublishEvent event) {
        if ("/chatter".equals(event.name)) {
            parseChatterTopic(event);
            return;
        }
        Log.d("dachen", event.msg);
    }

    private void parseChatterTopic(PublishEvent event) {
        try {
            JSONParser parser = new JSONParser();
            org.json.simple.JSONObject jsonObject = (org.json.simple.JSONObject) parser.parse(event.msg);
            String jsondata = (String) jsonObject.get("data");
            DC_TextView_ShowData.setText(jsondata);
            Log.i("dachen", jsondata);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

其中
String msg1 = "{\"op\":\"subscribe\",\"topic\":\"/chatter\"}";是订阅话题的信息,chatter为话题名。
String msg1 = "{ \"op\": \"publish\", \"topic\": \"/chatter\", \"msg\": { \"data\": \"" + data + " \" }}";是发布topic话题的信息,chatter为话题名。
需要特别说明的是,我个人在发布话题时发现只能发布较为简单格式的话题,JSON格式嵌套过多的话发布会报错,暂不知原因。例如
String msg2 ={\"op\":\"publish\",\"topic\":\"/cmd_vel\",\"msg\":{\"linear\":{\"x\":" + 0 + ",\"y\":" +0 + ",\"z\":0},\"angular\":{\"x\":0,\"y\":0,\"z\":" + 0.5 + "}}}";
就可以发布出geometry_msgs/Twist.msg 格式的topic。

运行结果

demo运行结果如下


运行结果
监听脚本

代码的移植与应用之后更新

上一篇 下一篇

猜你喜欢

热点阅读