EV3 Mailboxes in Python

Recently I wanted to enter the Alexa / LEGO MINDSTORMS challenge:

https://www.hackster.io/contests/alexa-lego-voice-challenge

My idea required being able to send EV3 Mailbox messages via Bluetooth between ev3dev and a stock EV3 running the EV3g language – from Python. I’m new to Python, I’m a Perl programmer at heart, so this was somewhat of a learning curve moment. I had to get to grips with Bluetooth (not that difficult as I’m used the IP networking) and Python at the same time.

I figured that one of the major selling points of Python was its extensive library of support functions, so set to looking for something providing EV3 Mailbox handling. My research wasn’t as fruitful as I’d hoped for. I could find various chunks of code but either they were flawed in their behaviour, or much more heavyweight than I wanted. So I decided to jump in feet first and write my own library.

I’d written a library for App Inventor 2 [0] [1] [2] [3] [4] that would encode & decode EV3 Mailbox payloads before, so this wasn’t too daunting a task. One aspect of Mailbox messages is that there isn’t an identifier within the payload that identifies the content: String, Float (IEE754 32 bit), Boolean. Normally this is handled in EV3g by expecting a specific type relating to the message name – i.e. a message called “status” would be defined to always be a Boolean, but “command” would always be a String – the code forces the type to remain constant. However, the type of the payload can be deduced to some extent, so I decide that I’d implement that within the Python class:

  • Payload length = 1 byte => Boolean
  • Payload length = 4 bytes
    • Last byte != NULL or NULL in the other bytes => Float
    • Otherwise => String
  • All other payloads => String

The only issue with the logic above is that really, really, small numbers may get decoded as strings, e.g. “@@@\x00” would get seen as a string, not 5.90052e-39 which is also a valid decode of it. As such I also implemented a .force_float() method which will re-decode the payload.

The git repo for this library can be found at:

https://gitlab.com/Jander/ev3-mailbox-python

To use it do something like:

from ev3mailbox import EV3Mailbox as Mailbox

message = EV3Mailbox.encode("Name", "Message value", Mailbox.Type.TEXT)
print(message)

# Data from Bluetooth
 mailbox = EV3Mailbox.decode(payload)
 print(mailbox)

Hopefully this will prove useful to others. The code has been released under the GPLv3: https://www.gnu.org/licenses/gpl-3.0.txt

My First BT Message !

Screen Shot 2016-08-16 at 22.07.58

The code above may not look much, but I’m very pleased with it 🙂 I’ve been working with MIT’s App Inventor 2 in an effort to build an app that can send text (in some form) to the EV3 for further processing – that code has managed it!

I’d extended the EV3  tilt-to-drive tutorial from the AI2 site to add in an extra button, that when pressed runs that code. It’s fixed at the moment but it encapsulates (little endian) 0x0018, 0x0001, 0x81, 0x9e, 0x5, beep\0, 0x0006, Beep!\0. All this info has been derived from the LEGO® Communications Developer Kit docs.

To test this I had a simple program, as below, running on the brick:

Screen Shot 2016-08-16 at 22.16.40

Press the button on the app, and the EV3 beeps – superb!

It also shows an interesting behaviour which I may well exploit in an upcoming model – I had one program running on the EV3, solely to beep, but was still able to drive the bot from my phone. Being able to externally control an EV3 whilst it is running its own program has a lot of merit.