{"id":2090,"date":"2016-08-06T11:35:18","date_gmt":"2016-08-06T11:35:18","guid":{"rendered":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=2090"},"modified":"2016-08-06T11:54:33","modified_gmt":"2016-08-06T11:54:33","slug":"drive-a-raspberry-pi-robot-using-python-web-api","status":"publish","type":"post","link":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/drive-a-raspberry-pi-robot-using-python-web-api\/","title":{"rendered":"Drive Your Raspberry Pi Robot using Python Web API"},"content":{"rendered":"\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fdrive-a-raspberry-pi-robot-using-python-web-api%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/05\/PIRobot.jpg\" rel=\"attachment wp-att-1809\"><img loading=\"lazy\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/05\/PIRobot.jpg\" alt=\"PIRobot\" width=\"1044\" height=\"587\" class=\"alignnone size-full wp-image-1809\" srcset=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/05\/PIRobot.jpg 1044w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/05\/PIRobot-300x168.jpg 300w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/05\/PIRobot-1024x575.jpg 1024w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/05\/PIRobot-900x506.jpg 900w\" sizes=\"(max-width: 1044px) 100vw, 1044px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">I\u2019ve enjoyed building my own DIY Raspberry Pi robot and wanted to share the ideas behind building a remote control interface using the Flask python web framework. \u00a0This small HTTP based programming interface enabled me to build multiple apps to drive my robot. \u00a0You might enjoy using these ideas in your hobby robot design.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here\u2019s some background on the robot:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The design uses a Raspberry PI 2<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The robot has a laser cut frame based on designs from <\/span><a href=\"http:\/\/www.coderbot.org\/en\/index.html\"><span style=\"font-weight: 400;\">CoderBot.org<\/span><\/a><span style=\"font-weight: 400;\"> . \u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The robot moves using two continuous rotation servos. \u00a0You can purchase similar servos through Amazon or Parallax.com.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The Raspberry Pi is powered by a small cell phone battery charger.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The servos are powered by 4 AA batteries.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">The following diagram shows how the robot is wired using an Arduino. \u00a0Wiring to a Raspberry Pi is very similar. \u00a0\u00a0Keep in mind that the signal wires for the left and right servo get connected to GPIO PINS 4 and 17 respectively on the Raspberry Pi.<\/span><\/p>\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/servoRobot.png\" rel=\"attachment wp-att-1829\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1829\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/servoRobot.png\" alt=\"Servo Robot\" width=\"600\" height=\"397\" srcset=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/servoRobot.png 600w, http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/servoRobot-300x198.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><b>Building a HTTP programming interface using Flask<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s break down the major parts of the python script driving the robot. \u00a0This python script uses the following libraries:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>RPi.GPIO:<\/b><span style=\"font-weight: 400;\"> This library enables the programmer to send digital signals to the input and output pins on the Raspberry PI.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Flask:<\/b><span style=\"font-weight: 400;\"> To enable the robot to accept HTTP requests from a web browser, we used a library known as Flask.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">A continuous rotation servo has a simple protocol for controlling rotational<\/span><\/p>\n<p><span style=\"font-weight: 400;\">motion and speed using a frequency of voltage pulses. \u00a0\u00a0This set of pulses is known as pulse width modulation. (PWM) \u00a0In the <\/span><span style=\"font-weight: 400;\">RPi.GPIO<\/span><span style=\"font-weight: 400;\"> framework, the following code sets up pin 4 for PWM communication and stops the rotation of the servo.<\/span><\/p>\n<p><code><br \/>\n#servo setup<br \/>\nPIN_LEFT = 4<br \/>\nGPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme<br \/>\nGPIO.setup(PIN_LEFT,  GPIO.OUT) # PWM pin set as output<br \/>\nleftPWM = GPIO.PWM(PIN_LEFT,21.7)<br \/>\nleftPWM.start(0) <\/p>\n<p>#stop the servo<br \/>\nleftPWM.ChangeDutyCycle(0)<br \/>\n<\/code><\/p>\n<p>To move the servo in one direction, you might do the following:<\/p>\n<p>leftPWM.ChangeDutyCycle(5)<\/p>\n<p>The code below initializes our pins 4 and 17 to send voltage pulses.  The frequency of the voltage pulses on these pins will control the motion of the servos on the robot. <\/p>\n<p><code><br \/>\nimport RPi.GPIO as GPIO<br \/>\nfrom flask import Flask, render_template<br \/>\nimport os<\/p>\n<p>PIN_LEFT = 4<br \/>\nPIN_RIGHT = 17<\/p>\n<p># Pin Setup:<br \/>\nGPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme<br \/>\nGPIO.setup(PIN_LEFT,  GPIO.OUT) # PWM pin set as output<br \/>\nGPIO.setup(PIN_RIGHT, GPIO.OUT)<\/p>\n<p>leftPWM = GPIO.PWM(PIN_LEFT,21.7)<br \/>\nrightPWM = GPIO.PWM(PIN_RIGHT,21.7 )<br \/>\nleftPWM.start(0)<br \/>\nrightPWM.start(0)<br \/>\n<\/code><\/p>\n<p>\nUsing the Flask python web framework, we create an instance of a flask application.   We configure the root of the website(\u201c\/\u201d) to return the template \u201cdriveRobot.html\u201d     This HTML file contains user interface elements and JavaScript that will make HTTP requests to the Flask server.\n<\/p>\n<p><code><br \/>\napp = Flask(__name__)<\/p>\n<p>@app.route(\"\/\")<br \/>\ndef drive1():<br \/>\n    return render_template(\"driveRobot.html\")<br \/>\n<\/code><\/p>\n<p>\nThe functions below show the process of sending out voltage frequencies to servos so that the robot can move forward, backward and stop.  You can invoke these functions using HTTP requests.\n<\/p>\n<p><code><br \/>\n@app.route(\"\/forward\")<br \/>\ndef forward():<br \/>\n    leftPWM.ChangeDutyCycle(5)<br \/>\n    rightPWM.ChangeDutyCycle(1)<br \/>\n    return \"forward\"<\/p>\n<p>@app.route(\"\/back\")<br \/>\ndef back():<br \/>\n    leftPWM.ChangeDutyCycle(1)<br \/>\n    rightPWM.ChangeDutyCycle(5)<br \/>\n    return \"back\"<\/p>\n<p>@app.route(\"\/stop\")<br \/>\ndef stop():<br \/>\n    leftPWM.ChangeDutyCycle(0)<br \/>\n    rightPWM.ChangeDutyCycle(0)<br \/>\n<\/code><br \/>\n&nbsp;<\/p>\n<p>The final lines run the web application.<\/p>\n<p><code><br \/>\nif __name__ == \"__main__\":<br \/>\n    app.run(host='0.0.0.0')<br \/>\n<\/code><\/p>\n<p>To run this application, execute the following:<\/p>\n<p>sudo python robotMove.py<\/p>\n<p>Using a web browser, navigate to http:\/\/yourRaspberryPi:<port>\/<\/p>\n<h1>Putting it all together<\/h1>\n<p><code><br \/>\nimport RPi.GPIO as GPIO<br \/>\nfrom flask import Flask, render_template<br \/>\nimport os<\/p>\n<p>PIN_LEFT = 4<br \/>\nPIN_RIGHT = 17<\/p>\n<p># Pin Setup:<br \/>\nGPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme<br \/>\nGPIO.setup(PIN_LEFT,  GPIO.OUT) # PWM pin set as output<br \/>\nGPIO.setup(PIN_RIGHT, GPIO.OUT)<\/p>\n<p>leftPWM = GPIO.PWM(PIN_LEFT,21.7)<br \/>\nrightPWM = GPIO.PWM(PIN_RIGHT,21.7 )<\/p>\n<p>leftPWM.start(0)<br \/>\nrightPWM.start(0)<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(\"\/\")<br \/>\ndef drive1():<br \/>\n    return render_template(\"driveRobot.html\")<\/p>\n<p>@app.route(\"\/forward\")<br \/>\ndef forward():<br \/>\n    leftPWM.ChangeDutyCycle(5)<br \/>\n    rightPWM.ChangeDutyCycle(1)<br \/>\n    return \"forward\"<\/p>\n<p>@app.route(\"\/back\")<br \/>\ndef back():<br \/>\n    leftPWM.ChangeDutyCycle(1)<br \/>\n    rightPWM.ChangeDutyCycle(5)<br \/>\n    return \"back\"<\/p>\n<p>@app.route(\"\/stop\")<br \/>\ndef stop():<br \/>\n    leftPWM.ChangeDutyCycle(0)<br \/>\n    rightPWM.ChangeDutyCycle(0)<br \/>\n    return \"stop\"<\/p>\n<p>@app.route(\"\/left\")<br \/>\ndef left():<br \/>\n    leftPWM.ChangeDutyCycle(1)<br \/>\n    rightPWM.ChangeDutyCycle(1)<br \/>\n    return \"left\"<\/p>\n<p>@app.route(\"\/right\")<br \/>\ndef right():<br \/>\n    leftPWM.ChangeDutyCycle(5)<br \/>\n    rightPWM.ChangeDutyCycle(5)<br \/>\n    return \"right\"\t<\/p>\n<p>if __name__ == \"__main__\":<br \/>\n    app.run(host='0.0.0.0')<br \/>\n<\/code><\/p>\n<p><strong>Top Stories<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1285\">10 Free Resources for Learning JavaScript and HTML5<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1238\">17 Fun Tools To Teach Kids To Code by @ChrisBetcher<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=623\">Benefits of Teaching Kids To Code That No One Is Talking About<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1349\">Easy Recipes for Building Android Apps using MIT App Inventor<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1079\" target=\"_blank\">Simple Minecraft Programming Using ScriptCraftJS<\/a><\/li>\n<\/ul>\n\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fdrive-a-raspberry-pi-robot-using-python-web-api%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve enjoyed building my own DIY Raspberry Pi robot and wanted to share the ideas behind building a remote control interface using the Flask python web framework. \u00a0This small HTTP based programming interface enabled me to build multiple apps to drive my robot. \u00a0You might enjoy using these ideas in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,23,11,8],"tags":[],"_links":{"self":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2090"}],"collection":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/comments?post=2090"}],"version-history":[{"count":3,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2090\/revisions"}],"predecessor-version":[{"id":2093,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2090\/revisions\/2093"}],"wp:attachment":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/media?parent=2090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/categories?post=2090"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/tags?post=2090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}