Make SLAM view easier to implement in multiple protocols.
This commit is contained in:
@@ -3,67 +3,69 @@ package com.example.carcontroller.SLAM;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import com.example.carcontroller.SlamScan;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.example.carcontroller.SlamLocation;
|
||||
import com.google.protobuf.ByteString;
|
||||
|
||||
import org.zeromq.SocketType;
|
||||
import org.zeromq.ZContext;
|
||||
import org.zeromq.ZMQ;
|
||||
public class SlamView extends SurfaceView implements SlamUpdater.MapChangedListener {
|
||||
|
||||
public class SlamView extends SurfaceView implements Runnable {
|
||||
|
||||
ZContext context;
|
||||
String host;
|
||||
String port;
|
||||
SlamUpdater slam;
|
||||
Thread mapThread;
|
||||
Context context;
|
||||
private int width;
|
||||
private int height;
|
||||
private SurfaceHolder surfaceHolder;
|
||||
|
||||
public SlamView(Context context) {
|
||||
super(context);
|
||||
this.context = context;
|
||||
init();
|
||||
}
|
||||
|
||||
public SlamView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
this.context = context;
|
||||
init();
|
||||
}
|
||||
|
||||
public SlamView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
this.context = context;
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
context = new ZContext();
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
host = prefs.getString("host", "10.0.0.53");
|
||||
port = prefs.getString("zmqPort", "5050");
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String host = prefs.getString("host", "10.0.0.53");
|
||||
String port = prefs.getString("zmqPort", "5050");
|
||||
slam = new ZmqSlamUpdater(host, port);
|
||||
slam.addMapChangedListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// Receive map from zmq and update appropriately.
|
||||
try (ZMQ.Socket socket = context.createSocket(SocketType.PAIR)) {
|
||||
socket.connect("tcp://" + host + ":" + port);
|
||||
socket.send("Hi");
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
byte[] map = socket.recv();
|
||||
SlamScan scan = SlamScan.parseFrom(map);
|
||||
for (Byte b : scan.getMap()) {
|
||||
|
||||
}
|
||||
}
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
System.out.println("Invalid map found");
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by MainActivity.onResume() to start a thread.
|
||||
*/
|
||||
public void resume() {
|
||||
mapThread = new Thread(slam);
|
||||
mapThread.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
// Use grpc to tell the loader to stop. Interrupt the loader thread as well.
|
||||
mapThread.interrupt();
|
||||
slam.stop();
|
||||
try {
|
||||
mapThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mapChanged(ByteString map, SlamLocation location) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user