Add complib client and TCP communication

This commit is contained in:
Konstantin Lampalzer 2022-05-22 23:00:57 +02:00
parent 1a033c8b03
commit a484bc2137
35 changed files with 815 additions and 232 deletions

View file

@ -0,0 +1,45 @@
import socket
import compLib.CompLib_pb2 as CompLib_pb2
UNIX_SOCKET_PATH = "/tmp/compLib"
TCP_SOCKET_HOST = "192.168.0.151"
TCP_SOCKET_PORT = 9090
class CompLibClient(object):
@staticmethod
def send(data: bytes, size: int) -> bytes:
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# sock.connect((TCP_SOCKET_HOST, TCP_SOCKET_PORT))
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
sock.connect(UNIX_SOCKET_PATH)
sock.sendall(size.to_bytes(1, byteorder='big'))
sock.sendall(data)
response_size_bytes = sock.recv(1)
response_size = int.from_bytes(response_size_bytes, byteorder="big")
# print(response_size)
response_bytes = sock.recv(response_size)
# print(response_bytes.hex())
# print(len(response_bytes))
CompLibClient.check_response(response_bytes)
return response_bytes
@staticmethod
def check_response(response_bytes: bytes) -> bool:
print(f"{response_bytes}")
res = CompLib_pb2.GenericResponse()
res.ParseFromString(response_bytes)
if res.status.successful:
return True
# TODO: Log error message if unsuccessful
return False

View file

@ -0,0 +1,190 @@
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: CompLib.proto
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\rCompLib.proto\x12\x07\x43ompLib\"\x1e\n\x06Header\x12\x14\n\x0cmessage_type\x18\x01 \x01(\t\"3\n\x06Status\x12\x12\n\nsuccessful\x18\x01 \x01(\x08\x12\x15\n\rerror_message\x18\x02 \x01(\t\"1\n\x0eGenericRequest\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\"S\n\x0fGenericResponse\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\x12\x1f\n\x06status\x18\x02 \x01(\x0b\x32\x0f.CompLib.Status\">\n\x1b\x45ncoderReadPositionsRequest\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\"w\n\x1c\x45ncoderReadPositionsResponse\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\x12\x1f\n\x06status\x18\x02 \x01(\x0b\x32\x0f.CompLib.Status\x12\x15\n\tpositions\x18\x03 \x03(\x05\x42\x02\x10\x01\"?\n\x1c\x45ncoderReadVelocitiesRequest\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\"y\n\x1d\x45ncoderReadVelocitiesResponse\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\x12\x1f\n\x06status\x18\x02 \x01(\x0b\x32\x0f.CompLib.Status\x12\x16\n\nvelocities\x18\x03 \x03(\x01\x42\x02\x10\x01\"9\n\x16IRSensorsEnableRequest\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\":\n\x17IRSensorsDisableRequest\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\":\n\x17IRSensorsReadAllRequest\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\"n\n\x18IRSensorsReadAllResponse\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\x12\x1f\n\x06status\x18\x02 \x01(\x0b\x32\x0f.CompLib.Status\x12\x10\n\x04\x64\x61ta\x18\x03 \x03(\rB\x02\x10\x01\"U\n\x15MotorsSetPowerRequest\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\x12\x0c\n\x04port\x18\x02 \x01(\r\x12\r\n\x05power\x18\x03 \x01(\x01\"U\n\x15MotorsSetSpeedRequest\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\x12\x0c\n\x04port\x18\x02 \x01(\r\x12\r\n\x05speed\x18\x03 \x01(\x01\"6\n\x13OdometryReadRequest\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\"\x95\x01\n\x14OdometryReadResponse\x12\x1f\n\x06header\x18\x01 \x01(\x0b\x32\x0f.CompLib.Header\x12\x1f\n\x06status\x18\x02 \x01(\x0b\x32\x0f.CompLib.Status\x12\x12\n\nx_position\x18\x03 \x01(\x01\x12\x12\n\ny_position\x18\x04 \x01(\x01\x12\x13\n\x0borientation\x18\x05 \x01(\x01\x62\x06proto3')
_HEADER = DESCRIPTOR.message_types_by_name['Header']
_STATUS = DESCRIPTOR.message_types_by_name['Status']
_GENERICREQUEST = DESCRIPTOR.message_types_by_name['GenericRequest']
_GENERICRESPONSE = DESCRIPTOR.message_types_by_name['GenericResponse']
_ENCODERREADPOSITIONSREQUEST = DESCRIPTOR.message_types_by_name['EncoderReadPositionsRequest']
_ENCODERREADPOSITIONSRESPONSE = DESCRIPTOR.message_types_by_name['EncoderReadPositionsResponse']
_ENCODERREADVELOCITIESREQUEST = DESCRIPTOR.message_types_by_name['EncoderReadVelocitiesRequest']
_ENCODERREADVELOCITIESRESPONSE = DESCRIPTOR.message_types_by_name['EncoderReadVelocitiesResponse']
_IRSENSORSENABLEREQUEST = DESCRIPTOR.message_types_by_name['IRSensorsEnableRequest']
_IRSENSORSDISABLEREQUEST = DESCRIPTOR.message_types_by_name['IRSensorsDisableRequest']
_IRSENSORSREADALLREQUEST = DESCRIPTOR.message_types_by_name['IRSensorsReadAllRequest']
_IRSENSORSREADALLRESPONSE = DESCRIPTOR.message_types_by_name['IRSensorsReadAllResponse']
_MOTORSSETPOWERREQUEST = DESCRIPTOR.message_types_by_name['MotorsSetPowerRequest']
_MOTORSSETSPEEDREQUEST = DESCRIPTOR.message_types_by_name['MotorsSetSpeedRequest']
_ODOMETRYREADREQUEST = DESCRIPTOR.message_types_by_name['OdometryReadRequest']
_ODOMETRYREADRESPONSE = DESCRIPTOR.message_types_by_name['OdometryReadResponse']
Header = _reflection.GeneratedProtocolMessageType('Header', (_message.Message,), {
'DESCRIPTOR' : _HEADER,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.Header)
})
_sym_db.RegisterMessage(Header)
Status = _reflection.GeneratedProtocolMessageType('Status', (_message.Message,), {
'DESCRIPTOR' : _STATUS,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.Status)
})
_sym_db.RegisterMessage(Status)
GenericRequest = _reflection.GeneratedProtocolMessageType('GenericRequest', (_message.Message,), {
'DESCRIPTOR' : _GENERICREQUEST,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.GenericRequest)
})
_sym_db.RegisterMessage(GenericRequest)
GenericResponse = _reflection.GeneratedProtocolMessageType('GenericResponse', (_message.Message,), {
'DESCRIPTOR' : _GENERICRESPONSE,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.GenericResponse)
})
_sym_db.RegisterMessage(GenericResponse)
EncoderReadPositionsRequest = _reflection.GeneratedProtocolMessageType('EncoderReadPositionsRequest', (_message.Message,), {
'DESCRIPTOR' : _ENCODERREADPOSITIONSREQUEST,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.EncoderReadPositionsRequest)
})
_sym_db.RegisterMessage(EncoderReadPositionsRequest)
EncoderReadPositionsResponse = _reflection.GeneratedProtocolMessageType('EncoderReadPositionsResponse', (_message.Message,), {
'DESCRIPTOR' : _ENCODERREADPOSITIONSRESPONSE,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.EncoderReadPositionsResponse)
})
_sym_db.RegisterMessage(EncoderReadPositionsResponse)
EncoderReadVelocitiesRequest = _reflection.GeneratedProtocolMessageType('EncoderReadVelocitiesRequest', (_message.Message,), {
'DESCRIPTOR' : _ENCODERREADVELOCITIESREQUEST,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.EncoderReadVelocitiesRequest)
})
_sym_db.RegisterMessage(EncoderReadVelocitiesRequest)
EncoderReadVelocitiesResponse = _reflection.GeneratedProtocolMessageType('EncoderReadVelocitiesResponse', (_message.Message,), {
'DESCRIPTOR' : _ENCODERREADVELOCITIESRESPONSE,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.EncoderReadVelocitiesResponse)
})
_sym_db.RegisterMessage(EncoderReadVelocitiesResponse)
IRSensorsEnableRequest = _reflection.GeneratedProtocolMessageType('IRSensorsEnableRequest', (_message.Message,), {
'DESCRIPTOR' : _IRSENSORSENABLEREQUEST,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.IRSensorsEnableRequest)
})
_sym_db.RegisterMessage(IRSensorsEnableRequest)
IRSensorsDisableRequest = _reflection.GeneratedProtocolMessageType('IRSensorsDisableRequest', (_message.Message,), {
'DESCRIPTOR' : _IRSENSORSDISABLEREQUEST,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.IRSensorsDisableRequest)
})
_sym_db.RegisterMessage(IRSensorsDisableRequest)
IRSensorsReadAllRequest = _reflection.GeneratedProtocolMessageType('IRSensorsReadAllRequest', (_message.Message,), {
'DESCRIPTOR' : _IRSENSORSREADALLREQUEST,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.IRSensorsReadAllRequest)
})
_sym_db.RegisterMessage(IRSensorsReadAllRequest)
IRSensorsReadAllResponse = _reflection.GeneratedProtocolMessageType('IRSensorsReadAllResponse', (_message.Message,), {
'DESCRIPTOR' : _IRSENSORSREADALLRESPONSE,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.IRSensorsReadAllResponse)
})
_sym_db.RegisterMessage(IRSensorsReadAllResponse)
MotorsSetPowerRequest = _reflection.GeneratedProtocolMessageType('MotorsSetPowerRequest', (_message.Message,), {
'DESCRIPTOR' : _MOTORSSETPOWERREQUEST,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.MotorsSetPowerRequest)
})
_sym_db.RegisterMessage(MotorsSetPowerRequest)
MotorsSetSpeedRequest = _reflection.GeneratedProtocolMessageType('MotorsSetSpeedRequest', (_message.Message,), {
'DESCRIPTOR' : _MOTORSSETSPEEDREQUEST,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.MotorsSetSpeedRequest)
})
_sym_db.RegisterMessage(MotorsSetSpeedRequest)
OdometryReadRequest = _reflection.GeneratedProtocolMessageType('OdometryReadRequest', (_message.Message,), {
'DESCRIPTOR' : _ODOMETRYREADREQUEST,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.OdometryReadRequest)
})
_sym_db.RegisterMessage(OdometryReadRequest)
OdometryReadResponse = _reflection.GeneratedProtocolMessageType('OdometryReadResponse', (_message.Message,), {
'DESCRIPTOR' : _ODOMETRYREADRESPONSE,
'__module__' : 'CompLib_pb2'
# @@protoc_insertion_point(class_scope:CompLib.OdometryReadResponse)
})
_sym_db.RegisterMessage(OdometryReadResponse)
if _descriptor._USE_C_DESCRIPTORS == False:
DESCRIPTOR._options = None
_ENCODERREADPOSITIONSRESPONSE.fields_by_name['positions']._options = None
_ENCODERREADPOSITIONSRESPONSE.fields_by_name['positions']._serialized_options = b'\020\001'
_ENCODERREADVELOCITIESRESPONSE.fields_by_name['velocities']._options = None
_ENCODERREADVELOCITIESRESPONSE.fields_by_name['velocities']._serialized_options = b'\020\001'
_IRSENSORSREADALLRESPONSE.fields_by_name['data']._options = None
_IRSENSORSREADALLRESPONSE.fields_by_name['data']._serialized_options = b'\020\001'
_HEADER._serialized_start=26
_HEADER._serialized_end=56
_STATUS._serialized_start=58
_STATUS._serialized_end=109
_GENERICREQUEST._serialized_start=111
_GENERICREQUEST._serialized_end=160
_GENERICRESPONSE._serialized_start=162
_GENERICRESPONSE._serialized_end=245
_ENCODERREADPOSITIONSREQUEST._serialized_start=247
_ENCODERREADPOSITIONSREQUEST._serialized_end=309
_ENCODERREADPOSITIONSRESPONSE._serialized_start=311
_ENCODERREADPOSITIONSRESPONSE._serialized_end=430
_ENCODERREADVELOCITIESREQUEST._serialized_start=432
_ENCODERREADVELOCITIESREQUEST._serialized_end=495
_ENCODERREADVELOCITIESRESPONSE._serialized_start=497
_ENCODERREADVELOCITIESRESPONSE._serialized_end=618
_IRSENSORSENABLEREQUEST._serialized_start=620
_IRSENSORSENABLEREQUEST._serialized_end=677
_IRSENSORSDISABLEREQUEST._serialized_start=679
_IRSENSORSDISABLEREQUEST._serialized_end=737
_IRSENSORSREADALLREQUEST._serialized_start=739
_IRSENSORSREADALLREQUEST._serialized_end=797
_IRSENSORSREADALLRESPONSE._serialized_start=799
_IRSENSORSREADALLRESPONSE._serialized_end=909
_MOTORSSETPOWERREQUEST._serialized_start=911
_MOTORSSETPOWERREQUEST._serialized_end=996
_MOTORSSETSPEEDREQUEST._serialized_start=998
_MOTORSSETSPEEDREQUEST._serialized_end=1083
_ODOMETRYREADREQUEST._serialized_start=1085
_ODOMETRYREADREQUEST._serialized_end=1139
_ODOMETRYREADRESPONSE._serialized_start=1142
_ODOMETRYREADRESPONSE._serialized_end=1291
# @@protoc_insertion_point(module_scope)

View file

@ -0,0 +1,35 @@
import compLib.CompLib_pb2 as CompLib_pb2
from compLib.CompLibClient import CompLibClient
class Encoder(object):
"""Class used to read the encoders
"""
@staticmethod
def read_all_positions():
"""Read all encoder positions.
:return: Tuple of all current encoder positions
"""
request = CompLib_pb2.EncoderReadPositionsRequest()
request.header.message_type = request.DESCRIPTOR.full_name
response = CompLib_pb2.EncoderReadPositionsResponse()
response.ParseFromString(CompLibClient.send(request.SerializeToString(), request.ByteSize()))
return tuple(i for i in response.positions)
@staticmethod
def read_all_velocities():
"""Read the velocity of all motors connected.
:return: Tuple of all current motor velocities
"""
request = CompLib_pb2.EncoderReadVelocitiesRequest()
request.header.message_type = request.DESCRIPTOR.full_name
response = CompLib_pb2.EncoderReadVelocitiesResponse()
response.ParseFromString(CompLibClient.send(request.SerializeToString(), request.ByteSize()))
return tuple(i for i in response.velocities)

View file

@ -0,0 +1,39 @@
import compLib.CompLib_pb2 as CompLib_pb2
from compLib.CompLibClient import CompLibClient
class IRSensor(object):
"""Access the different IR Sensors of the robot
"""
@staticmethod
def read_all():
"""Read all IR sensors at once.
:return: Tuple of all current ir sensors
"""
request = CompLib_pb2.IRSensorsReadAllRequest()
request.header.message_type = request.DESCRIPTOR.full_name
response = CompLib_pb2.IRSensorsReadAllResponse()
response.ParseFromString(CompLibClient.send(request.SerializeToString(), request.ByteSize()))
return tuple(i for i in response.data)
@staticmethod
def enable():
"""Turn on all IR emitters
"""
request = CompLib_pb2.IRSensorsEnableRequest()
request.header.message_type = request.DESCRIPTOR.full_name
CompLibClient.send(request.SerializeToString(), request.ByteSize())
@staticmethod
def disable():
"""Turn off all IR emitters
"""
request = CompLib_pb2.IRSensorsDisableRequest()
request.header.message_type = request.DESCRIPTOR.full_name
CompLibClient.send(request.SerializeToString(), request.ByteSize())

View file

@ -0,0 +1,69 @@
import compLib.CompLib_pb2 as CompLib_pb2
from compLib.CompLibClient import CompLibClient
MOTOR_COUNT = 4
class Motor(object):
"""Class used to control the motors
"""
@staticmethod
def power(port: int, percent: float):
"""Set specified motor to percentage power
:param port: Port, which the motor is connected to. 0-3
:param percent: Percentage of max speed. between -100 and 100
:raises: IndexError
"""
if port < 0 or port >= MOTOR_COUNT:
raise IndexError("Invalid Motor port specified!")
if percent < -100 or percent > 100:
raise IndexError("Invalid Motor speed specified! Speed is between -100 and 100 percent!")
request = CompLib_pb2.MotorsSetPowerRequest()
request.header.message_type = request.DESCRIPTOR.full_name
request.port = port
request.power = percent
CompLibClient.send(request.SerializeToString(), request.ByteSize())
@staticmethod
def speed(port: int, speed: float):
"""Set specified motor to percentage power
:param port: Port, which the motor is connected to. 0-3
:param speed: Speed at which a motor should turn in RPM
:raises: IndexError
"""
if port < 0 or port >= MOTOR_COUNT:
raise IndexError("Invalid Motor port specified!")
request = CompLib_pb2.MotorsSetSpeedRequest()
request.header.message_type = request.DESCRIPTOR.full_name
request.port = port
request.speed = speed
CompLibClient.send(request.SerializeToString(), request.ByteSize())
# @staticmethod
# def all_off():
# """
# Turns of all motors
# """
# Logging.get_logger().debug(f"Motor.all_off")
#
# for i in range(1, MOTOR_COUNT + 1):
# Motor.active_break(i)
# @staticmethod
# def active_break(port: int):
# """
# Actively break with a specific motor
#
# :param port: Port, which the motor is connected to. 1-4
# """
# Motor.pwm(port, 0, MotorMode.BREAK)

View file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.