This is the library for the wireless communication.
The communication protocol consists of multiple steps.
-
Ping mode Robot 1 continuously broadcasts a certain character ('>'). Robot 2 will respond with '<' if it reads the ping command. This establishes a "sender-receiver"-relation between the two robots. All outgoing communication (so the messages) from the receiver and all incoming communication to the sender are blocked until further notice.
-
Sending and receiving Robot 1 will send the first message from the send-queue multiple times. This queue consists of
Message
structs, which are defined below. At sending, thesent
flag is set totrue
. When robot 2 calls the read function in the main loop, it will detect the message that has been sent. All message bodies are written in the following syntax:#<number>-<message>!
. When the message is found and seperated, the robot will send back an affirmation of the message by sending a message of the following syntax:$<number>!
. The message is put into a read-queue in robot 2 for later analysis. Robot one will see this acknowledgement in a call of the read function. Thereceived
flag is set to true and the next message will be sent. This loop continous until all messages have been sent and received. Robot one will send the message#THE!
to indicate the end of the transmission. Robot two will send one more affirmation (#END!
) The communication channels on both robots will be opened again. All messages from the read-queue (robot 2) can now be analysed and actions can be taken according to them.
struct Message {
bool sent;
bool received;
String body;
};
Explanation for the complexity This system might seem overly complex, but there are reasons for the design of every single part of this protocol. Firstly, we need to assure that bi-directional wireless communication is possible. That means that robot 1 and robot 2 have to be able to both receive and send messages. However, if both robots are just sending messages, there is no evidence of the validity and the timing of them. That means that the data might be useless. The maximum size of the Serial buffer (64 bytes of char data) also has to be taken into account. That is why establishing a temporary direction of data flow is useful (the ping mode).
Because we can not assume all communication will be flawless, the acknowledgements of the receiver to the sender are necessary. This helps the sender to check whether it still needs to send the message of if it can move on to the next one.
Finally, both Arduinos are single-threaded; they can only do one thing at the time. This means that messages that come from other functions have to be put in a queue, because sending them in the background is no option.
Please note: This is a first design of the protocol, so I can not guarantee that this will work in the actual implementation.
The syntax of the wireless commands are relatively simple.
Every command begins with a #
and ends with a !
.
The ping command is the only command that does not have this syntax, as it is just a mere character that is being sent.
Examples:
#angle30!
#hello!