How to drive your Arduino-propulsed robot arm with Python

Theo Nathanael Combelles
Towards Data Science
3 min readSep 19, 2019

--

Arduino micro-controler is a beautiful piece of electronic. Out of the box it can drive most of your devices and prototypes. The number of GPIO, the user-friendly IDE and the low price brought major changes in the modern prototyping approach. However, this kind monster can’t address strong computational process. Since a robot deeply involves complex algorithms you should consider a Python-Arduino cooperation to make it alive.

This is the second article in the series of “The design of a robot arm from scratch”.

In the first article we obtained a fairly usable Keras model and we tested it graphically on the simulation.

Now I’m very excited to use the model on the real prototype! We want to assess how good it is compared to the simulation results and draw the first conclusion about our approach value. The final work will be to design some method to minimize the error.

The serial link between Python and Arduino.

To link them I want to use the pySerial library which able us to communicate through the serial port of the Arduino.

Basically when the Arduino is listening through its serial port, in the same time the python script will send some information to him. This rudimentary communication process able us to send our predicted angle triplet to the robot in order to drive it to the wished position.

Serial Communication Process

In the port argument you need to put the communication port used by the Arduino card. The baud should be the same as the one on your Arduino script.

Code on the python-side

In the line Serial.begin(9600) you are setting the baud.

Code on the Arduino-side

The use of the model to generate inferences (to predict the need angles)

Thanks to the Keras library the job is done. You just need to use the function

model.predict

Also you can decide to make two distinct scripts : one will train the model and the other will exploit it. To do so you will need to save the model out of the first script and to load it in the second.

Obviously Keras already did the job for you. Here are the key functions :

model.save(“model.h5”)

And also to load it :

model = load_model(“model.h5”)

Now you are really close to what you expected. The duty is now to setup your servos, generate the prediction for your (X, Y) coordinates and send the predicted angular to the robot — enjoy the results.

Thank you so much for reading, please feel free to reach out, should you have any question or should you have valid criticism or simply should you want to exchange about your projects.

The next article will be about to compare the simulation and the application robustness. To do so I use a CNN to detect the position of robot using visual recognition.

Stay Tuned !!

Here is the complete Python script.

Here is the complete Arduino script.

You should read the previous article for a better comprehension :

“An efficient and fast way to control your robots. Truly.” https://link.medium.com/LIGf4r3k5Z

References :

Tensor Flow: https://en.wikipedia.org/wiki/TensorFlow

NumPy: https://en.wikipedia.org/wiki/NumPy

Matpotlib: https://en.wikipedia.org/wiki/Matplotlib

Keras: https://en.wikipedia.org/wiki/Keras

--

--