Arduino
Programming
in Python
Sergio Paniego Blanco
Artificial Intelligence MSc - UPM
Who am I?
Informatic Engineering and Software
Enginnering - URJC
Student Developer GSoC - JdeRobot
sergiopaniego sergiopaniego sergiopaniego
2
Summary
Google Summer of Code
Problematic
State of the art
PyOnArduino
Demo
3
Google Global program
Timeline
Summer of § February - Organizations announced
Bring students into open source § March - Student applications
Code development § April – Student Projects announced
Remote work + meetings § April & May – Community Boarding
§ May to August – Coding
JdeRobot – 6 students § June, July & August - Evaluations
4
Problematic Arduino programming language complexity
Need of easy to use tools for newbies
Code Arduino boards using Python
5
State of the
art
Current solution for the Possible approaches
problematic § Pyxie
Continuous communication between § Cython
the robot and the PC. § LLVM
6
7
PyOnArduino
JdeRobot Robot Programming tool
People:
José María Cañas
Gorka Guardiola Translate Python-like code to Arduino code
Luis Roberto Morales
Sergio Paniego Blanco
Translate, compile and upload the code directly
8
9
Supported MBOT COMPLUBOT
robots
10
PyOnArduino’s
structure
11
12
Translator Abstract Syntax Tree
Each node – Statement in the code
Information for analyzing the code
Inessential puntuation and delimiters
Python library
NodeTransformer and NodeVisitor
13
Abstract Syntax Tree
Greatest Common Divisor
while b ≠ 0
if a > b
a := a − b
else
b := b − a
return a
14
Translator Abstract Syntax Tree
Each node – Statement in the code
Information for analyzing the code
Python library
NodeTransformer and NodeVisitor
15
NodeVisitor & NodeTransformer
import ast
class MyVisitor(ast.NodeVisitor):
def visit_Str(self, node):
print('String Node: "' + node.s + '"')
class MyTransformer(ast.NodeTransformer):
def visit_Str(self, node):
return ast.Str('str: ' + node.s)
parsed = ast.parse("print('Hello World’)”)
MyTransformer().visit(parsed)
MyVisitor().visit(parsed)
16
HALduino
Hadware Abstraction Layer
halduino.py
Specific halduino for each robot
Get rid of Arduino’s complexity
17
Python
setSpeedEngines(leftSpeed: int, rightSpeed: int)
HALduino
example
Arduino
MeDCMotor leftMotor(9);
MeDCMotor rightMotor(10);
void setSpeedEngines(int speedLeft, int speedRight) {
leftMotor.run(speedLeft);
rightMotor.run(speedRight);
}
18
Complubot
Sensors Sensor/Actuator Supported functions
and DC Engines setSpeedEngines (left,right)
actuators Ultrasonic sensors getUS()
supported Infrared sensors getIR[1,2,3,4,5]()
Beep emitter playBeep(type)
Sound emitter playMelody(melody)
Screen write setScreenText(text), cleanScreen()
19
mBot
Sensor/Actuator Supported functions
Sensors
DC Engines setSpeedEngines (speed)
and
Ultrasonic sensors getUS()
actuators
LEDs setLeds(ledNumber, red, green, blue)
supported
Infrared sensors getMessage(), sendMessage(message)
Light sensor getLightSensor()
Button isButtonPressed(), isButtonReleased()
Buzzer playBuzzer(tone, length)
External screen drawString(name), showClock(hour, min)
20
Feature Limitations/Comments
Variable declaration SUPPORTED
Function declaration With/without return statement
Python
Operators +-/*^%
features
Comparators < <= >= == !=
supported
Logic operators And or is not
pass SUPPORTED
loops While, for(limited)
Sleep() SUPPORTED
if If/elif/else
Boolean operations and or
print SUPPORTED
21
import HALduino.halduino as halduino
Stop and go
code example def set_engine(direction: int):
if direction == 0:
Mbot
halduino.setSpeedEngines(0, 0)
print('STOP!')
elif direction == 1:
halduino.setSpeedEngines(100, 100)
print('Forward')
def loop():
if halduino.getUS() < 10:
set_engine(0)
else:
set_engine(1)
22
Stop and go
import HALduino.halduino as halduino
code def set_engine(direction: int):
if direction == 0:
example halduino.setSpeedEngines(0, 0)
print('STOP!')
Complubot elif direction == 1:
halduino.setSpeedEngines(100, 100)
print('Forward')
def loop():
if halduino.getUS() < 30:
set_engine(0)
else:
set_engine(1)
23
Managing
Versatility
robot’s
architecture Configure robot’s ports
# Example of an mBot setup
leftMotor = 9
rightMotor = 10
ultrasonicSensor = 3
rgbled = 7
lightSensor = 6
ledMtx = 3
lineFollower = 2
24
Executing
PyOnArduino Python 3.x
Arduino IDE
Arduino Makefile
python3 translator/Translator.py [input-file] [robot]
python3 translator/Translator.py [input-file] [robot] [architecture-file]
25
Make &
Arduino Build automation
Makefile Directives used by make
make upload
# mBot Makefile
ARDUINO_LIBS= Makeblock-Libraries-master Wire SPI
MONITOR_PORT= /dev/cu.wchusbserial1420
BOARD_TAG = uno
ARDUINO_DIR = /Applications/Arduino.app/Contents/Java
include /usr/local/opt/arduino-mk/Arduino.mk
26
Problems
during Dynamic typing in Python vs Arduino
development Architectural Stop
Variables’ type in function declaration
Lost parentheses
27
DEMO TIME!
Lets see how PyOnArduino tool works!
28
29
Thanks!
Any questions?
You can find me at
@sergiopaniego
30