Fix server, add dummy python controller.
This commit is contained in:
@@ -14,7 +14,7 @@ servo_pin = 18
|
|||||||
timeout_length = 3
|
timeout_length = 3
|
||||||
|
|
||||||
motor = Motor()
|
motor = Motor()
|
||||||
servo = Servo()
|
servo = Servo(servo_pin)
|
||||||
|
|
||||||
class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -24,7 +24,7 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
|||||||
self._timer = None
|
self._timer = None
|
||||||
|
|
||||||
def SetThrottle(self, request_iterator, context):
|
def SetThrottle(self, request_iterator, context):
|
||||||
# If we don't get a response every 3 seconds, stop the car.
|
# If we don't get a response every 3 seconds, stop the car.
|
||||||
# This isn't a stream right now, however may change it to be so since we'll constantly
|
# This isn't a stream right now, however may change it to be so since we'll constantly
|
||||||
# be sending values...
|
# be sending values...
|
||||||
throttleFailed = False
|
throttleFailed = False
|
||||||
@@ -58,7 +58,7 @@ class MotorServicer(motorService_pb2_grpc.CarControlServicer):
|
|||||||
self.motor.stop()
|
self.motor.stop()
|
||||||
|
|
||||||
def start_server(self):
|
def start_server(self):
|
||||||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
|
server = grpc.server(futures.ThreadPoolExecutor(max_workers=8))
|
||||||
motorService_pb2_grpc.add_CarControlServicer_to_server(MotorServicer(), server)
|
motorService_pb2_grpc.add_CarControlServicer_to_server(MotorServicer(), server)
|
||||||
server.add_insecure_port('[::]:50051')
|
server.add_insecure_port('[::]:50051')
|
||||||
server.start()
|
server.start()
|
||||||
|
|||||||
@@ -2,10 +2,9 @@ print("Connecting to pi")
|
|||||||
|
|
||||||
import grpc
|
import grpc
|
||||||
from concurrent import futures
|
from concurrent import futures
|
||||||
from .MotorServer import MotorServicer
|
import motorService_pb2_grpc
|
||||||
from . import motorService_pb2_grpc
|
from motorService_pb2 import ThrottleRequest
|
||||||
import random
|
import time
|
||||||
from threading import Timer
|
|
||||||
|
|
||||||
throttle = 0
|
throttle = 0
|
||||||
timer = None
|
timer = None
|
||||||
@@ -21,30 +20,15 @@ class ThrottleIterator:
|
|||||||
def __next__(self):
|
def __next__(self):
|
||||||
if throttle > 1 or throttle < -1:
|
if throttle > 1 or throttle < -1:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
return throttle
|
time.sleep(1)
|
||||||
|
return ThrottleRequest(throttle=throttle)
|
||||||
|
|
||||||
|
|
||||||
def serve():
|
channel = grpc.insecure_channel('10.0.0.53:50051')
|
||||||
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 = motorService_pb2_grpc.CarControlStub(channel)
|
||||||
|
|
||||||
stub.SetThrottle(ThrottleIterator())
|
stub.SetThrottle(ThrottleIterator())
|
||||||
|
|
||||||
|
while True:
|
||||||
|
inp = int(input('Please enter a value for the throttle between -100 and 100'))
|
||||||
|
throttle = inp / 100
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
|
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
|
||||||
import grpc
|
import grpc
|
||||||
|
|
||||||
import car.MotorControl.motorService_pb2 as motorService__pb2
|
import MotorControl.motorService_pb2 as motorService__pb2
|
||||||
|
|
||||||
|
|
||||||
class CarControlStub(object):
|
class CarControlStub(object):
|
||||||
|
|||||||
Reference in New Issue
Block a user