From 28e691f50ccc3b39fab1f78f30e1a2cccd7491f8 Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Tue, 18 Feb 2020 21:37:49 +1030 Subject: [PATCH] Fix the subscription to work with pyzmq --- .../com/example/carcontroller/SLAM/ZmqSlamUpdater.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/example/carcontroller/SLAM/ZmqSlamUpdater.java b/app/src/main/java/com/example/carcontroller/SLAM/ZmqSlamUpdater.java index 4bfddc4..0b96001 100644 --- a/app/src/main/java/com/example/carcontroller/SLAM/ZmqSlamUpdater.java +++ b/app/src/main/java/com/example/carcontroller/SLAM/ZmqSlamUpdater.java @@ -7,6 +7,8 @@ import org.zeromq.SocketType; import org.zeromq.ZContext; import org.zeromq.ZMQ; +import java.util.Arrays; + /** * Connects to Pi and retrieves updates of the SLAM map, * notifying listeners of changes to the map when they arrive. @@ -18,6 +20,7 @@ public class ZmqSlamUpdater extends SlamUpdater { private String host; private String port; private boolean running = false; + private static final byte[] SLAM_SUBSCRIPTION = "slam_map".getBytes(); public ZmqSlamUpdater(String host, String port) { super(); @@ -39,10 +42,13 @@ public class ZmqSlamUpdater extends SlamUpdater { // Receive map from zmq and update appropriately. try (ZMQ.Socket socket = context.createSocket(SocketType.SUB)) { socket.connect("tcp://" + host + ":" + port); - socket.subscribe("slam_map"); + socket.subscribe(SLAM_SUBSCRIPTION); while (running) { byte[] map = socket.recv(); - fireMapChanged(SlamScan.parseFrom(map)); + // Don't want to do the event when we just receive the header. + if (!Arrays.equals(map, SLAM_SUBSCRIPTION)) { + fireMapChanged(SlamScan.parseFrom(map)); + } } } catch (InvalidProtocolBufferException e) { System.out.println("Invalid map found");