![Page 1: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/1.jpg)
Programming Robots with Python
Shamyl Bin Mansoor
Co-founder & CTO LearnOBots
![Page 2: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/2.jpg)
• Is an EdTech company developing educational technology tools
• Promotes Science Technology Engineering Arts & Mathematics (STEAM) in Pakistani Schools
• Developed a Technology based school curriculum and educational kits for learning to Code, Make Robots, Electronics, Astronomy etc etc
• Kids projects have been featured by Hackaday / MIT
![Page 3: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/3.jpg)
Me?
• Teach CS at NUST
• Make “things” at LearnOBots
• Love making / playing with Technology / Hardware / Gadgets• Was part of the recent CERN
Hackathon
• Work has been featured on Hackaday
• Hate publishing research papers, but still have a few
![Page 4: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/4.jpg)
Outline
• Introduction • Python for Hardware
• MicroPython
• Motivation
• Programming Robots with Python• Python on the Raspberry PI
• Accessing GPIOs on the PI
• Sensors and Actuators interfacing
• Programming Robot Movement
• Conclusion
![Page 5: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/5.jpg)
Disclaimer
• Hardware prototype never works when you show it to someone!
![Page 6: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/6.jpg)
Python for Hardware
• Programming hardware was hard!• 8051, C / Assembly• High Learning Curve
• Suitable for Hardware?• Easy to use• Packages available• Raspberry PI
• What about Real time requirements?• Trade off• Use a dedicated Microcontroller for Real time requirements
• Who uses python with hardware?
![Page 7: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/7.jpg)
MicroPython
• Lean and efficient implementation of Python3
• Small subset of Python Standard Library
• Optimized to run on microcontrollers
![Page 8: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/8.jpg)
Why Robots with Python?
• Python is easy!
• Robots are Fun!
• Lots of Libraries!
![Page 9: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/9.jpg)
Fun
+
=
![Page 10: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/10.jpg)
Robots: What do we need?
• A brain (Processor / Controller)• Raspberry PI or an Arduino
• Some Actuators• Motors (DC or Servo)
• Some Sensors• Sonar / Depth / Vision
![Page 11: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/11.jpg)
What we will Use?
• Raspberry PI since we like Python
• Motors (DC, two of them)
• Sonar Sensor (For sensing obstacles)
![Page 12: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/12.jpg)
Getting started with Raspberry PI
• Buy one! (instock.pk or ewall.com.pk)
• Download Raspbian Image (A Debian based OS for the Raspberry PI)• www.raspberrypi.org
• Copy Image to an SD Card
• Plug in Raspberry PI in a TV, connect keyboard and mouse and you’re good to go!
![Page 13: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/13.jpg)
![Page 14: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/14.jpg)
Accessing GPIO Pins
import RPi.GPIO as GPIOGPIO.setmode(GPIO.BCM)GPIO.setup(23, GPIO.IN)GPIO.setup(24, GPIO.OUT)
![Page 15: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/15.jpg)
Python on Raspberry PI
• IDLE and Python Come pre-installed on Raspbian
• We will use our favourite editor to write python scripts
![Page 16: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/16.jpg)
Configuring VNC
• Install VNC viewer from https://www.realvnc.com/ on your MAC or Windows
• Raspbian already comes pre-installed with VNC
• You can connect to your PI over the same network or over the internet• Need to create an account with VNC
• Follow https://www.realvnc.com/en/connect/docs/raspberry-pi.html#raspberry-pi-connect-cloud to connect to Raspberry PI
![Page 17: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/17.jpg)
Task 1: Hello World
• Blinking an LED• Connect an LED to IO Port of Raspberry PI
• Make it Blink
• We will use pin 11 (GPIO 17)
• And pin 9 (Gnd)
![Page 18: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/18.jpg)
GPIO Pins
![Page 19: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/19.jpg)
LED Circuit
![Page 20: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/20.jpg)
What’s in the Code?
![Page 21: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/21.jpg)
BLINK : HELLO WORLD!
![Page 22: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/22.jpg)
Main Function
![Page 23: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/23.jpg)
Output!
![Page 24: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/24.jpg)
LED Blinking: Hellooo World!
![Page 25: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/25.jpg)
Congratulations!
You are a Python Hardware Expert Now!
![Page 26: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/26.jpg)
Task 2: Interfacing Motors
• Using CodiBot we will be using an L298 Hbridge to control the 2 motors for robot movement
• Moving Codi Forwards
• Moving Codi Backwards
• Turning Codi using Differential Drive
![Page 27: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/27.jpg)
Have you ever tried this?
![Page 28: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/28.jpg)
H-Bridge
• Changing Directions
![Page 29: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/29.jpg)
Differential Drive
![Page 30: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/30.jpg)
![Page 31: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/31.jpg)
![Page 32: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/32.jpg)
![Page 33: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/33.jpg)
![Page 34: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/34.jpg)
![Page 35: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/35.jpg)
Motor Connections H-Bridge
![Page 36: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/36.jpg)
Motors Movement
![Page 37: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/37.jpg)
Task 3: Sensing the world
• Interfacing a Sonar Sensor with Raspberry PI
• Using sonar to detect obstacles
![Page 38: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/38.jpg)
![Page 39: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/39.jpg)
![Page 40: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/40.jpg)
Task 4: Obstacle Avoidance Robot
• Using our learning from the previous 3 tasks we can now program an obstacle avoidance robot.
• Robot moves randomly and turns as soon as it encounters an obstacle
• But let’s leave that task for you
![Page 41: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/41.jpg)
![Page 42: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/42.jpg)
Conclusion• Python is Easy
• Lots of library support
• Raspberry PI is a natural tool to develop applications that use Python / Hardware and a GUI
![Page 43: Programming Robots with Python - PyCon · Getting started with Raspberry PI •Buy one! (instock.pk or ewall.com.pk) •Download Raspbian Image (A Debian based OS for the Raspberry](https://reader034.vdocuments.mx/reader034/viewer/2022042923/5f703a67c55d9c401349a596/html5/thumbnails/43.jpg)
Thank You
Find Today’s Code on
https://github.com/shamyl/Pycon
• t/@shamylmansoor
• fb.com/learnobots