Fix Motor Server and python controller
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
from threading import Timer, Thread
|
from threading import Timer, Thread
|
||||||
from gpiozero import Servo
|
from gpiozero import Servo
|
||||||
from concurrent import futures
|
from concurrent import futures
|
||||||
|
import time
|
||||||
|
|
||||||
import grpc
|
import grpc
|
||||||
|
|
||||||
@@ -11,14 +12,9 @@ import car.MotorControl.motorService_pb2_grpc as motorService_pb2_grpc
|
|||||||
from MotorControl.gpiozero.motor_session import Motor
|
from MotorControl.gpiozero.motor_session import Motor
|
||||||
|
|
||||||
servo_pin = 18
|
servo_pin = 18
|
||||||
timeout_length = 3
|
|
||||||
|
|
||||||
motor = Motor()
|
|
||||||
servo = Servo(servo_pin)
|
|
||||||
|
|
||||||
class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
||||||
def __init__(self):
|
def __init__(self, motor, servo):
|
||||||
global motor, servo
|
|
||||||
self.motor = motor
|
self.motor = motor
|
||||||
self.servo = servo
|
self.servo = servo
|
||||||
self._timer = None
|
self._timer = None
|
||||||
@@ -29,15 +25,16 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
|||||||
# be sending values...
|
# be sending values...
|
||||||
throttleFailed = False
|
throttleFailed = False
|
||||||
for throttleRequest in request_iterator:
|
for throttleRequest in request_iterator:
|
||||||
print('Setting throttle to: ' + throttleRequest.throttle)
|
print('Setting throttle to: ' + str(throttleRequest.throttle))
|
||||||
self.set_timeout(timeout_length)
|
self.set_timeout(3)
|
||||||
throttleFailed = self.motor.set_throttle(throttleRequest.throttle)
|
throttleFailed = self.motor.set_throttle(throttleRequest.throttle)
|
||||||
if throttleFailed:
|
if not throttleFailed:
|
||||||
break
|
break
|
||||||
return motorService_pb2.ThrottleResponse(throttleSet = throttleFailed)
|
return motorService_pb2.ThrottleResponse(throttleSet = throttleFailed)
|
||||||
|
|
||||||
def SetSteering(self, request, context):
|
def SetSteering(self, request, context):
|
||||||
# TODO: Fix this to use the motor object as well to check for bounds.
|
# 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.servo.value = request.steering
|
||||||
return motorService_pb2.SteeringResponse(steeringSet = True)
|
return motorService_pb2.SteeringResponse(steeringSet = True)
|
||||||
|
|
||||||
@@ -59,15 +56,15 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
|||||||
|
|
||||||
def start_server(self):
|
def start_server(self):
|
||||||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=8))
|
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.add_insecure_port('[::]:50051')
|
||||||
server.start()
|
server.start()
|
||||||
|
while True:
|
||||||
|
time.sleep(60*60)
|
||||||
|
|
||||||
servicer = MotorServicer()
|
motor = Motor()
|
||||||
servicer.start_server()
|
servo = Servo(servo_pin)
|
||||||
|
servicer = MotorServicer(motor, servo)
|
||||||
|
|
||||||
run = True
|
service_thread = Thread(target=servicer.start_server)
|
||||||
while run:
|
service_thread.start()
|
||||||
inp = input('Please press q to stop the server')
|
|
||||||
if inp == 'q' or inp == 'Q':
|
|
||||||
run = False
|
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ print("Connecting to pi")
|
|||||||
import grpc
|
import grpc
|
||||||
from concurrent import futures
|
from concurrent import futures
|
||||||
import motorService_pb2_grpc
|
import motorService_pb2_grpc
|
||||||
from motorService_pb2 import ThrottleRequest
|
from motorService_pb2 import SteeringRequest, ThrottleRequest
|
||||||
import time
|
import time
|
||||||
|
|
||||||
throttle = 0
|
throttle = 0.1
|
||||||
timer = None
|
timer = None
|
||||||
|
|
||||||
class ThrottleIterator:
|
class ThrottleIterator:
|
||||||
@@ -27,8 +27,7 @@ class ThrottleIterator:
|
|||||||
channel = grpc.insecure_channel('10.0.0.53:50051')
|
channel = grpc.insecure_channel('10.0.0.53:50051')
|
||||||
stub = motorService_pb2_grpc.CarControlStub(channel)
|
stub = motorService_pb2_grpc.CarControlStub(channel)
|
||||||
|
|
||||||
stub.SetThrottle(ThrottleIterator())
|
response = stub.SetThrottle(ThrottleIterator())
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
inp = int(input('Please enter a value for the throttle between -100 and 100'))
|
throttle = int(input('Please enter a value for the throttle between -100 and 100'))
|
||||||
throttle = inp / 100
|
|
||||||
|
|||||||
Reference in New Issue
Block a user