Files
picar/car/src/car/control/motor_servicer.py

49 lines
1.8 KiB
Python

from threading import Timer, Thread
from concurrent import futures
import time
import car.control.motorService_pb2 as motorService_pb2
import car.control.motorService_pb2_grpc as motorService_pb2_grpc
from car.control.gpio.recording_vehicle_decorator import VehicleRecordingDecorator
class MotorServicer(motorService_pb2_grpc.CarControlServicer):
def __init__(self, vehicle):
self.vehicle = VehicleRecordingDecorator(vehicle)
self._timer = None
def SetThrottle(self, request, context):
# gRPC streams currently don't work between python and android.
# If we don't get a response every 3 seconds, stop the car.
print('Setting throttle to: ' + str(request.throttle))
self.set_timeout(3)
self.vehicle.throttle = request.throttle
return motorService_pb2.ThrottleResponse(throttleSet=True)
def SetSteering(self, request, context):
print('Setting steering to: ' + str(request.steering))
self.vehicle.steering = request.steering
return motorService_pb2.SteeringResponse(steeringSet=True)
def set_timeout(self, min_timeout):
"""Stops the old timer and restarts it to the specified time.
min_timeout -- The minimum time that can be used for the timer.
"""
if self._timer is not None:
self._timer.cancel()
self._timer = Timer(min_timeout, self.timeout_elapsed)
self._timer.start()
def timeout_elapsed(self):
"""Election or heartbeat timeout has elapsed."""
print("Node timeout elapsed")
self.vehicle.stop()
def Record(self, request, context):
"""Indicate whether the vehicle data should be recorded."""
self.vehicle.record = request.record
def SaveRecordedData(self, request, context):
self.vehicle.save_data(request.file)