Add support for mock vehicle

This commit is contained in:
Piv
2020-03-06 19:47:03 +10:30
parent 1ec1e5acc7
commit f807e73219
3 changed files with 135 additions and 12 deletions

View File

@@ -9,15 +9,15 @@ import grpc
import control.motorService_pb2 as motorService_pb2
import control.motorService_pb2_grpc as motorService_pb2_grpc
from control.gpiozero.motor_session import Motor
from control.gpiozero.vehicle import Vehicle
from control.gpiozero.mockvehicle import MockVehicle
from slam.slam_servicer import SlamServicer
import slam.SlamController_pb2_grpc as SlamController_pb2_grpc
class MotorServicer(motorService_pb2_grpc.CarControlServicer):
def __init__(self, motor, servo):
self.motor = motor
self.servo = servo
def __init__(self, vehicle):
self.vehicle = vehicle
self._timer = None
def SetThrottle(self, request, context):
@@ -26,13 +26,12 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
throttleFailed = False
print('Setting throttle to: ' + str(request.throttle))
self.set_timeout(3)
throttleFailed = self.motor.set_throttle(request.throttle)
throttleFailed = self.vehicle.set_throttle(request.throttle)
return motorService_pb2.ThrottleResponse(throttleSet=throttleFailed)
def SetSteering(self, request, context):
# TODO: Fix this to use the motor object as well to check for bounds.
print('Setting steering to: ' + str(request.steering))
self.servo.value = request.steering
self.vehicle.steering = request.steering
return motorService_pb2.SteeringResponse(steeringSet=True)
def set_timeout(self, min_timeout):
@@ -48,7 +47,7 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
def timeout_elapsed(self):
"""Election or heartbeat timeout has elapsed."""
print("Node timeout elapsed")
self.motor.stop()
self.vehicle.stop()
def start_server(self):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=8))
@@ -66,6 +65,7 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
return SlamServicer('/dev/ttyUSB0')
def create_credentials(self):
# Relativise this stuff.
pvtKeyPath = '/home/pi/tls/device.key'
pvtCertPath = '/home/pi/tls/device.crt'
@@ -74,10 +74,8 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
return grpc.ssl_server_credentials([[pvtKeyBytes, pvtCertBytes]])
motor = Motor()
servo = Servo(18)
servicer = MotorServicer(motor, servo)
vehicle = Vehicle() if __name__ == '__main__' else MockVehicle()
servicer = MotorServicer(vehicle)
service_thread = Thread(target=servicer.start_server)
service_thread.start()