From bd74ca3c665331ca671618aff36e934fbf328283 Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Sun, 29 Sep 2019 21:15:11 +0930 Subject: [PATCH] Add python client for the car controller. --- MotorControl/PythonRemoteController.py | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 MotorControl/PythonRemoteController.py diff --git a/MotorControl/PythonRemoteController.py b/MotorControl/PythonRemoteController.py new file mode 100644 index 0000000..ab4e20d --- /dev/null +++ b/MotorControl/PythonRemoteController.py @@ -0,0 +1,50 @@ +print("Connecting to pi") + +import grpc +from concurrent import futures +from .MotorServer import MotorServicer +from . import motorService_pb2_grpc +import random +from threading import Timer + +throttle = 0 +timer = None + +class ThrottleIterator: + ''' + Class to get the current throttle for the car. + Will return a random throttle between + ''' + def __iter__(self): + return self + + def __next__(self): + if throttle > 1 or throttle < -1: + raise StopIteration + return throttle + + +def serve(): + server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) + motorService_pb2_grpc.add_CarControlServicer_to_server( + MotorServicer(), server) + server.add_insecure_port('[::]:50051') + server.start() + +def resetThrottle(): + global throttle + throttle = random.random() * 2 - 1 + global timer + if timer is not None: + timer.cancel() + timer = Timer(10, resetThrottle) + +serve() + +timer = Timer(10, resetThrottle) + +channel = grpc.insecure_channel('localhost:50051') +stub = motorService_pb2_grpc.CarControlStub(channel) + +stub.SetThrottle(ThrottleIterator()) +