Fix Motor Server and python controller

This commit is contained in:
Piv
2019-11-14 20:48:46 +10:30
parent 5024796785
commit a968a4ce97
2 changed files with 18 additions and 22 deletions

View File

@@ -3,6 +3,7 @@
from threading import Timer, Thread
from gpiozero import Servo
from concurrent import futures
import time
import grpc
@@ -11,14 +12,9 @@ import car.MotorControl.motorService_pb2_grpc as motorService_pb2_grpc
from MotorControl.gpiozero.motor_session import Motor
servo_pin = 18
timeout_length = 3
motor = Motor()
servo = Servo(servo_pin)
class MotorServicer(motorService_pb2_grpc.CarControlServicer):
def __init__(self):
global motor, servo
def __init__(self, motor, servo):
self.motor = motor
self.servo = servo
self._timer = None
@@ -29,15 +25,16 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
# be sending values...
throttleFailed = False
for throttleRequest in request_iterator:
print('Setting throttle to: ' + throttleRequest.throttle)
self.set_timeout(timeout_length)
print('Setting throttle to: ' + str(throttleRequest.throttle))
self.set_timeout(3)
throttleFailed = self.motor.set_throttle(throttleRequest.throttle)
if throttleFailed:
if not throttleFailed:
break
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
return motorService_pb2.SteeringResponse(steeringSet = True)
@@ -59,15 +56,15 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
def start_server(self):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=8))
motorService_pb2_grpc.add_CarControlServicer_to_server(MotorServicer(), server)
motorService_pb2_grpc.add_CarControlServicer_to_server(self, server)
server.add_insecure_port('[::]:50051')
server.start()
while True:
time.sleep(60*60)
servicer = MotorServicer()
servicer.start_server()
motor = Motor()
servo = Servo(servo_pin)
servicer = MotorServicer(motor, servo)
run = True
while run:
inp = input('Please press q to stop the server')
if inp == 'q' or inp == 'Q':
run = False
service_thread = Thread(target=servicer.start_server)
service_thread.start()