java 通过rosbridge从cartographer中订阅

2018-12-26  本文已影响0人  zengyuhang

服务信息:

rosservice info /submap_query
Node: /cartographer_node
URI: rosrpc://zyh-VirtualBox:53829
Type: cartographer_ros_msgs/SubmapQuery
Args: trajectory_id submap_index

ROS数据

rosservice call /submap_query 0 0
image.png
image.png

数据格式:

status
code: 0
message: "Success."
submap_version: 180
textures: 
  cells: [31, 139, 8, 0, 0, 0, 0, 0, 4,****]
  width: 674
  height: 677
  resolution: 0.0500000007451
  slice_pose: 
    position: 
      x: 14.5000002161
      y: 24.0500003584
      z: 0.0
    orientation: 
      x: 0.0
      y: 0.0
      z: 0.0
      w: 1.0

代码展示

public static void main(String args[]) throws Exception {
    Ros ros = new Ros("192.168.1.8", 9090, JRosbridge.WebSocketType.ws);
    ros.connect();
// Topic t1 = new Topic(ros, "submap_list", "cartographer_ros_msgs/SubmapList");
   Service service = new Service(ros, "submap_query","cartographer_ros_msgs/SubmapQuery"); 

    new Thread(() -> {
      try {
            ServiceRequest req = new ServiceRequest("{\"trajectory_id\":0,\"submap_index\":0}");
            ServiceResponse resp = service.callServiceAndWait(req);
            JsonArray textures = resp.toJsonObject().getJsonArray("textures");  
            int[] result = null;
            for (int index = 0, len = textures.size(); index < len; index++) {
                JsonObject obj = textures.getJsonObject(index);
                String cells=obj.getString("cells");
                result=unBase64(cells);
                System.out.println(Arrays.toString(result));
              }
          } catch (Exception e) {
            e.printStackTrace();
        }
     }).start();
}
public static byte[] decryptBASE64(String data) throws Exception {
    Base64.Decoder decoder = Base64.getDecoder();
    byte[] buffer = decoder.decode(data);
    return buffer;
}
private int[] unBase64(String cells) throws Exception {
    byte[] src = decryptBASE64(cells);
    int[] result = new int[src.length];
    for (int i = 0, length = src.length; i < length; i++) {
        byte value = src[i];
        result[i] = value >= 0 ? value : value + 256;
    }
    return result;
}

对比数据

[31, 139, 8, 0, 0, 0, 0, 0, 4, 255, 236, 189, 11, 119, 27, 219, ******中间省略,180, 192, 255, 15, 16, 115, 59, 174, 212, 236, 13, 0]
上一篇 下一篇

猜你喜欢

热点阅读