{"id":1992,"date":"2016-02-15T18:34:35","date_gmt":"2016-02-15T18:34:35","guid":{"rendered":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1992"},"modified":"2016-02-15T18:34:35","modified_gmt":"2016-02-15T18:34:35","slug":"programming-lego-mindstorms-ev3-with-python","status":"publish","type":"post","link":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/programming-lego-mindstorms-ev3-with-python\/","title":{"rendered":"Programming Lego Mindstorms EV3 with Python"},"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=https%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fprogramming-lego-mindstorms-ev3-with-python%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\/2016\/02\/lego_ev3.jpg\"><img loading=\"lazy\" class=\"alignnone size-large wp-image-1993\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2016\/02\/lego_ev3-1024x576.jpg\" alt=\"Lego EV3\" width=\"608\" height=\"342\" srcset=\"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2016\/02\/lego_ev3-1024x576.jpg 1024w, https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2016\/02\/lego_ev3-300x168.jpg 300w, https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2016\/02\/lego_ev3-900x506.jpg 900w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">In our educational makerspace, our team has promoted the python programming language with young makers. \u00a0\u00a0The <a href=\"https:\/\/www.codecademy.com\/learn\/python\" target=\"_blank\">python<\/a> programming language has engaged students young and old for many reasons. \u00a0\u00a0The language tends to be approachable and concise. \u00a0\u00a0The language connects to a broad range of situations including <a href=\"https:\/\/www.raspberrypi.org\/learning\/getting-started-with-minecraft-pi\/worksheet\/\" target=\"_blank\">Minecraft programming<\/a>, Raspberry Pi, <a href=\"http:\/\/pygame.org\/hifi.html\" target=\"_blank\">video game programming<\/a>, and <a href=\"https:\/\/cloud.google.com\/appengine\/docs\/python\/\" target=\"_blank\">web development<\/a>. \u00a0\u00a0Software professionals love this language too! \u00a0Python is one of the most popular programming environments at Google! \u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Lego Mindstorms have become a common tool for introducing students to robotics, sensors, and computational thinking. \u00a0\u00a0I had heard that the Lego Mindstorm EV3 platform was based on Linux. \u00a0\u00a0I started looking into ways that you could program Mindstorm robots using python and linux. \u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">There\u2019s an amazing community of Linux\/Lego hackers who have created a platform to fill this need. \u00a0\u00a0Check out <\/span><a href=\"http:\/\/www.ev3dev.org\/\"><span style=\"font-weight: 400;\">http:\/\/www.ev3dev.org\/<\/span><\/a><span style=\"font-weight: 400;\"> . \u00a0\u00a0All that you need to add to your Lego Mindstorm ev3 is a small wifi dongle, a microsd card and a way to write to it. \u00a0\u00a0\u00a0Here\u2019s some tutorials to help you get started with installing the \u201cev3dev\u201d environment and setting up Wifi. \u00a0\u00a0Please keep in mind that using the \u201cev3dev\u201d environment is completely reversible. \u00a0\u00a0\u00a0Just take out the microsd card from your \u201cev3.\u201d<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"http:\/\/www.ev3dev.org\/docs\/getting-started\/\"><span style=\"font-weight: 400;\">http:\/\/www.ev3dev.org\/docs\/getting-started\/<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/github.com\/rhempel\/ev3dev-lang-python\"><span style=\"font-weight: 400;\">https:\/\/github.com\/rhempel\/ev3dev-lang-python<\/span><\/a><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Setup Lego Mindstorm Wifi<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"http:\/\/www.monobrick.dk\/guides\/how-to-establish-a-wifi-connection-with-the-ev3-brick\/\"><span style=\"font-weight: 400;\">http:\/\/www.monobrick.dk\/guides\/how-to-establish-a-wifi-connection-with-the-ev3-brick\/<\/span><\/a><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Lego Mindstorms EV3 \/ iPython<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\"><a href=\"https:\/\/www.youtube.com\/watch?v=vJHr6vIZk0k\">https:\/\/www.youtube.com\/watch?v=vJHr6vIZk0k<\/a><\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong><span style=\"font-weight: 400;\">Here\u2019s a sample program to give you a taste of programming \u201cev3\u201d using python. \u00a0\u00a0Using this small program, the user can control an ev3 robot using the keyboard.<\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><code><br \/>\nimport time<br \/>\nimport termios<br \/>\nimport tty<br \/>\nimport ev3dev.ev3 as ev3<br \/>\nimport sys<\/p>\n<p>motor_left = ev3.LargeMotor('outB')<br \/>\nmotor_right = ev3.LargeMotor('outC')<br \/>\nmotor_a = ev3.MediumMotor('outA')<\/p>\n<p>#==============================================<\/p>\n<p>def getch():<br \/>\n   fd = sys.stdin.fileno()<br \/>\n   old_settings = termios.tcgetattr(fd)<br \/>\n   try:<br \/>\n      tty.setraw(fd)<br \/>\n      ch = sys.stdin.read(1)<br \/>\n   finally:<br \/>\n      termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)<\/p>\n<p>   return ch<\/p>\n<p>#==============================================<\/p>\n<p>def fire():<br \/>\n   motor_a.run_timed(time_sp=3000, duty_cycle_sp=100)<\/p>\n<p>#==============================================<\/p>\n<p>def forward():<br \/>\n   motor_left.run_direct(duty_cycle_sp=75)<br \/>\n   motor_right.run_direct(duty_cycle_sp=75)<\/p>\n<p>#==============================================<\/p>\n<p>def back():<br \/>\n   motor_left.run_direct(duty_cycle_sp=-75)<br \/>\n   motor_right.run_direct(duty_cycle_sp=-75)<\/p>\n<p>#==============================================<\/p>\n<p>def left():<br \/>\n   motor_left.run_direct( duty_cycle_sp=-75)<br \/>\n   motor_right.run_direct( duty_cycle_sp=75)<\/p>\n<p>#==============================================<\/p>\n<p>def right():<br \/>\n   motor_left.run_direct( duty_cycle_sp=75)<br \/>\n   motor_right.run_direct( duty_cycle_sp=-75)<\/p>\n<p>#==============================================<\/p>\n<p>def stop():<br \/>\n   motor_left.run_direct( duty_cycle_sp=0)<br \/>\n   motor_right.run_direct( duty_cycle_sp=-0)<\/p>\n<p>#==============================================<\/p>\n<p>while True:<br \/>\n   k = getch()<br \/>\n   print(k)<br \/>\n   if k == 'w':<br \/>\n      forward()<br \/>\n   if k == 's':<br \/>\n      back()<br \/>\n   if k == 'a':<br \/>\n      left()<br \/>\n   if k == 'd':<br \/>\n      right()<br \/>\n   if k == 'f':<br \/>\n      fire()<br \/>\n   if k == ' ':<br \/>\n      stop()<br \/>\n   if k == 'q':<br \/>\n      exit()<br \/>\n<\/code><\/p>\n<p>In my case, I added a \u201cwifi\u201d module to my \u201cev3\u201d while using ev3dev.   This enabled me to use a secure shell terminal to access the \u201cev3\u201d Linux environment.    This program was saved in a python script called \u201crobot_control.py\u201d    To execute the program, I simply type the following command into the Linux prompt.<\/p>\n<p>python robot_control.py<\/p>\n<p>Let\u2019s break down this program into parts.  In the following section, we import some code and setup variables to access the various motors of the \u201cev3\u201d brick.<\/p>\n<p>import time<br \/>\nimport termios<br \/>\nimport tty<br \/>\nimport ev3dev.ev3 as ev3<br \/>\nimport sys<\/p>\n<p>motor_left = ev3.LargeMotor(&#8216;outB&#8217;)<br \/>\nmotor_right = ev3.LargeMotor(&#8216;outC&#8217;)<br \/>\nmotor_a = ev3.MediumMotor(&#8216;outA&#8217;)<\/p>\n<p>In the following function, we grab one character from the keyboard. <\/p>\n<p><code><br \/>\ndef getch():<br \/>\n   fd = sys.stdin.fileno()<br \/>\n   old_settings = termios.tcgetattr(fd)<br \/>\n   try:<br \/>\n      tty.setraw(fd)<br \/>\n      ch = sys.stdin.read(1)<br \/>\n   finally:<br \/>\n      termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)<\/p>\n<p>   return ch<br \/>\n<\/code><\/p>\n<p>To move the robot forward and backward, I use the following functions.   There are similar functions for turning the robot, firing a marble gun and stopping.  <\/p>\n<p><code><br \/>\ndef forward():<br \/>\n   motor_left.run_direct(duty_cycle_sp=75)<br \/>\n   motor_right.run_direct(duty_cycle_sp=75)<\/p>\n<p>def back():<br \/>\n   motor_left.run_direct(duty_cycle_sp=-75)<br \/>\n   motor_right.run_direct(duty_cycle_sp=-75)<br \/>\n<\/code><\/p>\n<p>In the main loop of the program, we ask for one character from the user.   Based on this input, the system executes different functions in the program.   The robot follows the following protocol for movement:<\/p>\n<p>w : move robot forward<br \/>\ns : move robot back<br \/>\na : turn robot left<br \/>\nd : turn robot right<br \/>\nspace bar : stop the robot<br \/>\nf : fire the marble gun.<br \/>\nq: quit the program.<\/p>\n<p><code><br \/>\nwhile True:<br \/>\n   k = getch()<br \/>\n   print(k)<br \/>\n   if k == 'w':<br \/>\n      forward()<br \/>\n   if k == 's':<br \/>\n      back()<br \/>\n   if k == 'a':<br \/>\n      left()<br \/>\n   if k == 'd':<br \/>\n      right()<br \/>\n   if k == 'f':<br \/>\n      fire()<br \/>\n   if k == ' ':<br \/>\n      stop()<br \/>\n   if k == 'q':<br \/>\n      exit()<br \/>\n<\/code><\/p>\n\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fprogramming-lego-mindstorms-ev3-with-python%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>In our educational makerspace, our team has promoted the python programming language with young makers. \u00a0\u00a0The python programming language has engaged students young and old for many reasons. \u00a0\u00a0The language tends to be approachable and concise. \u00a0\u00a0The language connects to a broad range of situations including Minecraft programming, Raspberry Pi, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1992"}],"collection":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/comments?post=1992"}],"version-history":[{"count":1,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1992\/revisions"}],"predecessor-version":[{"id":1995,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1992\/revisions\/1995"}],"wp:attachment":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/media?parent=1992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/categories?post=1992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/tags?post=1992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}