diff --git a/control/motor_servicer.py b/control/motor_servicer.py new file mode 100644 index 0000000..4e24b68 --- /dev/null +++ b/control/motor_servicer.py @@ -0,0 +1,40 @@ +from threading import Timer, Thread +from concurrent import futures +import time + +import control.motorService_pb2 as motorService_pb2 +import control.motorService_pb2_grpc as motorService_pb2_grpc + +class MotorServicer(motorService_pb2_grpc.CarControlServicer): + def __init__(self, vehicle): + self.vehicle = vehicle + self._timer = None + + def SetThrottle(self, request, context): + # gRPC streams currently don't work between python and android. + # If we don't get a response every 3 seconds, stop the car. + print('Setting throttle to: ' + str(request.throttle)) + self.set_timeout(3) + self.vehicle.throttle = request.throttle + return motorService_pb2.ThrottleResponse(throttleSet=True) + + def SetSteering(self, request, context): + print('Setting steering to: ' + str(request.steering)) + self.vehicle.steering = request.steering + return motorService_pb2.SteeringResponse(steeringSet=True) + + def set_timeout(self, min_timeout): + """Stops the old timer and restarts it to the specified time. + + min_timeout -- The minimum time that can be used for the timer. + """ + if self._timer is not None: + self._timer.cancel() + self._timer = Timer(min_timeout, self.timeout_elapsed) + self._timer.start() + + def timeout_elapsed(self): + """Election or heartbeat timeout has elapsed.""" + print("Node timeout elapsed") + self.vehicle.stop() + diff --git a/controller.py b/controller.py index e6c8df6..d56bcbb 100755 --- a/controller.py +++ b/controller.py @@ -6,51 +6,23 @@ import time import grpc -import control.motorService_pb2 as motorService_pb2 import control.motorService_pb2_grpc as motorService_pb2_grpc from control.gpio.vehicle import Vehicle +from control.motor_servicer import MotorServicer from slam.slam_servicer import SlamServicer import slam.SlamController_pb2_grpc as SlamController_pb2_grpc import tracking.lidar_tracker_pb2_grpc as lidar_tracker_pb2_grpc from tracking.lidar_servicer import LidarServicer -class MotorServicer(motorService_pb2_grpc.CarControlServicer): +class CarServer(): + def __init__(self, vehicle): self.vehicle = vehicle - self._timer = None - - def SetThrottle(self, request, context): - # gRPC streams currently don't work between python and android. - # If we don't get a response every 3 seconds, stop the car. - print('Setting throttle to: ' + str(request.throttle)) - self.set_timeout(3) - self.vehicle.throttle = request.throttle - return motorService_pb2.ThrottleResponse(throttleSet=True) - - def SetSteering(self, request, context): - print('Setting steering to: ' + str(request.steering)) - self.vehicle.steering = request.steering - return motorService_pb2.SteeringResponse(steeringSet=True) - - def set_timeout(self, min_timeout): - """Stops the old timer and restarts it to the specified time. - - min_timeout -- The minimum time that can be used for the timer. - """ - if self._timer is not None: - self._timer.cancel() - self._timer = Timer(min_timeout, self.timeout_elapsed) - self._timer.start() - - def timeout_elapsed(self): - """Election or heartbeat timeout has elapsed.""" - print("Node timeout elapsed") - self.vehicle.stop() def start_server(self): server = grpc.server(futures.ThreadPoolExecutor(max_workers=8)) - motorService_pb2_grpc.add_CarControlServicer_to_server(self, server) + motorService_pb2_grpc.add_CarControlServicer_to_server(self.create_motor_servicer(), server) SlamController_pb2_grpc.add_SlamControlServicer_to_server( self.create_slam_servicer(), server) lidar_tracker_pb2_grpc.add_PersonTrackingServicer_to_server( @@ -62,6 +34,9 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer): while True: time.sleep(60*60) + def create_motor_servicer(self): + return MotorServicer(self.vehicle) + def create_slam_servicer(self): return SlamServicer('/dev/ttyUSB0') @@ -81,8 +56,8 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer): if __name__ == '__main__': vehicle = Vehicle() - servicer = MotorServicer(vehicle) + server = CarServer(vehicle) # Can't remember why I do this, is it even needed? - service_thread = Thread(target=servicer.start_server) + service_thread = Thread(target=server.start_server) service_thread.start()