30
Arduino Programming in Python Sergio Paniego Blanco

Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

  • Upload
    others

  • View
    10

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Arduino Programming in Python

Sergio Paniego Blanco

Page 2: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Who am I? Artificial Intelligence MSc - UPM

2

sergiopaniego sergiopaniego sergiopaniego

Informatic Engineering and Software Enginnering - URJC

Student Developer GSoC - JdeRobot

Page 3: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

SummaryGoogle Summer of Code

3

Problematic

State of the art

PyOnArduino

Demo

Page 4: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Google Summer of

Code

Global program Timeline§ February - Organizations announced

§ March - Student applications

§ April – Student Projects announced

§ April & May – Community Boarding

§ May to August – Coding

§ June, July & August - Evaluations

4

JdeRobot – 6 students

Bring students into open source development

Remote work + meetings

Page 5: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Problematic Arduino programming language complexity

5

Need of easy to use tools for newbies

Code Arduino boards using Python

Page 6: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

State of the art

Current solution for the problematicContinuous communication between the robot and the PC.

Possible approaches§ Pyxie

§ Cython

§ LLVM

6

Page 7: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

7

Page 8: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

PyOnArduinoJdeRobot Robot Programming tool

8

Translate Python-like code to Arduino code

People:José María Cañas

Gorka GuardiolaLuis Roberto Morales

Sergio Paniego BlancoTranslate, compile and upload the code directly

Page 9: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

9

Page 10: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Supported robots

10

MBOT COMPLUBOT

Page 11: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

PyOnArduino’sstructure

11

Page 12: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

12

Page 13: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Translator Abstract Syntax Tree

13

Each node – Statement in the code

Information for analyzing the code

Inessential puntuation and delimiters

Python library

NodeTransformer and NodeVisitor

Page 14: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

14

while b ≠ 0

if a > b

a := a − b

else

b := b − a

return a

Abstract Syntax TreeGreatest Common Divisor

Page 15: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Translator Abstract Syntax Tree

15

Each node – Statement in the code

Information for analyzing the code

Python library

NodeTransformer and NodeVisitor

Page 16: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

16

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)

NodeVisitor & NodeTransformer

Page 17: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

HALduinoHadware Abstraction Layer

17

halduino.py

Specific halduino for each robot

Get rid of Arduino’s complexity

Page 18: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

HALduinoexample

18

setSpeedEngines(leftSpeed: int, rightSpeed: int)

MeDCMotor leftMotor(9);MeDCMotor rightMotor(10);void setSpeedEngines(int speedLeft, int speedRight) {

leftMotor.run(speedLeft);rightMotor.run(speedRight);

}

Python

Arduino

Page 19: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Sensors and

actuators supported

19

Sensor/Actuator Supported functions

DC Engines setSpeedEngines (left,right)

Ultrasonic sensors getUS()

Infrared sensors getIR[1,2,3,4,5]()

Beep emitter playBeep(type)

Sound emitter playMelody(melody)

Screen write setScreenText(text), cleanScreen()

Complubot

Page 20: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Sensors and

actuators supported

20

Sensor/Actuator Supported functions

DC Engines setSpeedEngines (speed)

Ultrasonic sensors getUS()

LEDs setLeds(ledNumber, red, green, blue)

Infrared sensors getMessage(), sendMessage(message)

Light sensor getLightSensor()

Button isButtonPressed(), isButtonReleased()

Buzzer playBuzzer(tone, length)

External screen drawString(name), showClock(hour, min)

mBot

Page 21: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Python features

supported

21

Feature Limitations/Comments

Variable declaration SUPPORTED

Function declaration With/without return statement

Operators + - / * ^ %

Comparators < <= >= == !=

Logic operators And or is not

pass SUPPORTED

loops While, for(limited)

Sleep() SUPPORTED

if If/elif/else

Boolean operations and or

print SUPPORTED

Page 22: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Stop and go code example

Mbot

22

import HALduino.halduino as halduino

def set_engine(direction: int):if direction == 0:

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)

Page 23: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Stop and go code

exampleComplubot

23

import HALduino.halduino as halduino

def set_engine(direction: int):if direction == 0:

halduino.setSpeedEngines(0, 0)print('STOP!')

elif direction == 1:halduino.setSpeedEngines(100, 100)print('Forward')

def loop():if halduino.getUS() < 30:

set_engine(0)else:

set_engine(1)

Page 24: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Managing robot’s

architecture

24

Versatility

# Example of an mBot setup

leftMotor = 9rightMotor = 10ultrasonicSensor = 3rgbled = 7lightSensor = 6ledMtx = 3lineFollower = 2

Configure robot’s ports

Page 25: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Executing PyOnArduino Python 3.x

25

python3 translator/Translator.py [input-file] [robot]

python3 translator/Translator.py [input-file] [robot] [architecture-file]

Arduino IDE

Arduino Makefile

Page 26: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Make & Arduino

MakefileBuild automation

26

# mBot Makefile

ARDUINO_LIBS= Makeblock-Libraries-master Wire SPIMONITOR_PORT= /dev/cu.wchusbserial1420BOARD_TAG = unoARDUINO_DIR = /Applications/Arduino.app/Contents/Javainclude /usr/local/opt/arduino-mk/Arduino.mk

Directives used by make

make upload

Page 27: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Problems during

developmentDynamic typing in Python vs Arduino

27

Architectural Stop

Variables’ type in function declaration

Lost parentheses

Page 28: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

DEMO TIME!Lets see how PyOnArduino tool works!

28

Page 29: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

29

Page 30: Arduino Programming in Python - GitHub Pages...Buzzer playBuzzer(tone, length) External screen drawString(name), showClock(hour, min) mBot Python features supported 21 Feature Limitations/Comments

Thanks!Any questions?You can find me at @sergiopaniego

30