Skip to content
Snippets Groups Projects

Arduino - Python communication wrapper

Description

There are many times you want to connect between Arduino(s) and some Python script. This can be for exmple data collection reasons or coordinating between multiple devices (Robot arms (e.g.: UR5 arms), digital servos (e.g.: Dynamixels), webcams, etc ).

However, for many, using a middleware communication system such as ROS can be a huge learning curve and an unnecessary overhead.

The point for this wrapper is to provide a simple solution to estabilish a reliable two-way communication channel between mulitple arduinos and a central python script.

The aim is to be able to simple messages back and forth without going through the often troublesome process of synchronising messages, opening serial communication channels, etc. The general functionality is limited to keep everything straightforward and simple.

The project is constantly developing so please report bugs or any problems.


Core functionality

  • Estabilish a stable communication channel between multiple Arduinos and a single Python script
  • Send multiple strings form the Python side to the Arduino side (e.g.: can be used to tell the arduino "stop the motor" while sending the motor demand position)
  • Send mulitple messages with specified messages names from the Arduino side to the Python side (e.g.: can be used to transfer multiple sensor values, status messages, timestamps, etc)
  • Simplified keyboard keypress reading functionality to test your mechatronic setup

Points to bare in mind

  • On both sides, the messages reflect the most recent message. For now, there is no buffer or queue of messages (in order to simplify and increase the robustness the communication channel from edge cases).

Known issues

  • Aduino Nano / Teensy microcontrollers on a Windows system performs poorly (see table below)
  • Teensy microcontrollers on a Linux system performs poorly (see table below)

How to use this tool

Download the release

Download the latest release from here. You can see rough changes from the previous versions in the CHANGELOG.

In the release you will find three files: comms_wrapper.py, pyCommsLib.cpp, and pyCommsLib.h. These three files will be used as "Libraries" to provide the communication wrapper functionality.

Initial setup (Python side)

Copy and paste the comms_wrapper.py into the same directory as your Python script. In your Python script, import the contents of comms_wrapper.py by writing from comms_wrapper import *.

Initial setup (Arduino side)

Copy and paste the pyCommsLib.cpp and pyCommsLib.h into the same directory as your Arduino .ino file. If you do this correctly, you should see the two files as two tabs on the Arduino IDE.

How to use the wrapper

Currently, the best way to understand how to use the wrapper is to look at the example code found in the Exmples/ folder. The implementation for the Python and Arduino side is shown the corresponding Exmples/Python/ and Exmples/Arduino/ folders.

API Description

To be written. Currently, all the documentation is in the form of comments in the example files.


Python libraries you need to install

pyserial

pynput


Testing and compatability

This has been tested with Arduino UNO and Arduino Nano Every with an Ubuntu 20.04 machine.