Start adding alternate message streamers. Make algorithms less bad.

This commit is contained in:
Piv
2020-02-26 21:58:42 +10:30
parent 6e00106d90
commit 136c96a926
3 changed files with 69 additions and 13 deletions

View File

@@ -0,0 +1,46 @@
import zmq
class MessageFactory():
def getZmqPubSubStreamer(self, topic):
'''
Not thread-safe. Always get this inside the thread/process where you intend
to use it.
'''
return ZmqPubSubStreamer(topic)
class Streamer():
def send_message(self, message_bytes):
raise NotImplementedError
def send_message_topic(self, topic, message_bytes):
raise NotImplementedError
class ZmqPubSubStreamer(Streamer):
'''
Not thread-safe. Always get this inside the thread/process where you intend
to use it.
'''
def __init__(self, port):
# Should create the socket here always, since zmq is not thread safe.
# Hopefully whoever uses this is not stupid enough to create it then
# pass it into a thread.
pass
def send_message(self, message_bytes):
pass
def send_message_topic(self, topic, message_bytes):
pass
class BluetoothStreamer(Streamer):
def __init__(self):
pass
def send_message(self, message_bytes):
pass

View File

@@ -1,7 +1,13 @@
import math import math
class Group: class Group:
_minX = None
_maxX = None
_minY = None
_maxY = None
def __init__(self, number, points=[]): def __init__(self, number, points=[]):
self._points = points self._points = points
@@ -27,23 +33,22 @@ class Group:
''' '''
converted_point = self.convert_lidar_to_cartesian(new_point) converted_point = self.convert_lidar_to_cartesian(new_point)
if not hasattr(self, '_minX') or self._minX > converted_point[0]: if self._minX is None or self._minX > converted_point[0]:
self._minX = converted_point[0] self._minX = converted_point[0]
if not hasattr(self, '_maxX') or self._maxX < converted_point[0]: if self._maxX is None or self._maxX < converted_point[0]:
self._maxX = converted_point[0] self._maxX = converted_point[0]
if not hasattr(self, '_miny') or self._miny > converted_point[1]: if self._minY is None or self._minY > converted_point[1]:
self._minY = converted_point[1] self._minY = converted_point[1]
if not hasattr(self, '_maxY') or self._maxY < converted_point[1]: if self._maxY is None or self._maxY < converted_point[1]:
self._maxY = converted_point[0] self._maxY = converted_point[1]
def convert_lidar_to_cartesian(self, new_point): def convert_lidar_to_cartesian(self, new_point):
x = new_point[2] * math.sin(new_point[1]) x = new_point[2] * math.sin(new_point[1])
y = new_point[2] * math.cos(new_point[1]) y = new_point[2] * math.cos(new_point[1])
return (x,y) return (x, y)
def get_minX(self): def get_minX(self):
return self._minY return self._minY
@@ -57,6 +62,7 @@ class Group:
def get_maxY(self): def get_maxY(self):
return self._maxY return self._maxY
def calc_groups(scan): def calc_groups(scan):
''' '''
Calculates groups of points from a lidar scan. The scan should Calculates groups of points from a lidar scan. The scan should
@@ -74,8 +80,9 @@ def calc_groups(scan):
prevPoint = point prevPoint = point
continue continue
# Distances are in mm. # Distances are in mm.
if (point[2] - prevPoint[2]) ** 2 < 10 ** 2: # within 1cm makes a group. Will need to play around with this. # within 1cm makes a group. Will need to play around with this.
if (point[2] - prevPoint[2]) ** 2 < 10 ** 2:
if currentGroup is None: if currentGroup is None:
currentGroup = Group(currentGroupNumber) currentGroup = Group(currentGroupNumber)
allGroups.append(currentGroup) allGroups.append(currentGroup)
@@ -89,9 +96,11 @@ def calc_groups(scan):
return allGroups return allGroups
def find_centre(group): def find_centre(group):
return ((group.get_maxX() + group.get_minX()) / 2, (group.get_maxY() + group.get_minY()) / 2) return ((group.get_maxX() + group.get_minX()) / 2, (group.get_maxY() + group.get_minY()) / 2)
def assign_groups(prev_groups, new_groups): def assign_groups(prev_groups, new_groups):
''' '''
Assigns group numbers to a new scan based on the groups of an old scan. Assigns group numbers to a new scan based on the groups of an old scan.
@@ -106,10 +115,11 @@ def assign_groups(prev_groups, new_groups):
return new_groups return new_groups
def updateCarVelocity(oldGroup, newGroup): def updateCarVelocity(oldGroup, newGroup):
''' '''
Return a tuple (throttleChange, steeringChange) that should be Return a tuple (throttleChange, steeringChange) that should be
applied given the change in the centre of the groups. applied given the change in the centre of the groups.
''' '''
pass pass

View File

@@ -2,6 +2,7 @@ import rplidar
from rplidar import RPLidar from rplidar import RPLidar
from threading import Thread from threading import Thread
from persontracking import algorithms from persontracking import algorithms
import persontracking.lidar_tracker_pb2 as tracker_pb
import zmq import zmq
@@ -52,8 +53,7 @@ class LidarCache():
def fireGroupsChanged(self): def fireGroupsChanged(self):
# Send the updated groups to 0MQ socket. # Send the updated groups to 0MQ socket.
# self._socket.send_multipart(["lidar_map", ...]) self._socket.send_multipart(["lidar_map", tracker_pb.PointScan(points=[]).SerializeToString()])
pass
def stop_scanning(self): def stop_scanning(self):
self.run = False self.run = False