-
Notifications
You must be signed in to change notification settings - Fork 1
Motor Control Protocol
Link to text file
The Motor Control Protocol outlines how the Pi Zero and the base station communicate over TCP. Its objective is to communicate what motor needs to be changed, and what the motor's new power level and direction should be.
Currently this is achieved by a handshake followed by the client (the base station) sending python dictionary objects containing the desired states of the motors encoded in json, which are interpreted and put into effect by the server (the Pi Zero).
After the connection between the server (Pi Zero) and client (base station) is established, the interaction begins with the server sending HELLO CLIENT. The client should then respond with HELLO SERVER to complete the handshake. Note that each message send should end with a newline '\n', and no newline should be inside any message, as this will make the code interpret one message as two. After the handshake, the server sends Motor names, and then a json list of every connected motor. The client responds with OK. Following this every time the client wishes to alter the state of the motors, the client sends a python dictionary object encoded in json. The dictionary should follow this format:
{
'motor name 1' : ['direction', 'power'],
'motor name 2' : ['direction', 'power'],
'motor name 3' : ['direction', 'power'],
etc...
}
Not every motor that the server controls need to be listed every time, only those that should be changed.
The dictionary object can then be encoded in json in python with these lines:
import json
json_data = json.dumps(dictionary_object)
Then the server can load the data again.
import json
dictionary_object = json.loads(json_data)
As a side note, package loss can lead to not all the data arriving, and thus json.loads() may throw a JSONDecodeError when some data is lost. Therefore it is recommended to surround json.loads() with a try-except so that one fragmented piece of data doesn't lead to a complete shutdown of the server.
Motor Control With TCP contains the code for the server side of the communication, found on the Pi Zero.