{"id":2915,"date":"2020-04-05T16:44:55","date_gmt":"2020-04-05T15:44:55","guid":{"rendered":"https:\/\/www.macerobotics.com\/?p=2915"},"modified":"2020-04-06T07:54:16","modified_gmt":"2020-04-06T06:54:16","slug":"control-a-mobile-robot-by-using-keyboard-python","status":"publish","type":"post","link":"https:\/\/www.macerobotics.com\/?p=2915","title":{"rendered":"Control a mobile robot by using keyboard &#8211; Python"},"content":{"rendered":"<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">An example python program<\/span><\/span> for control a mobile robot with keyboard:<\/p>\n<ul>\n<li>keyboard control<\/li>\n<li>serial communication<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"><br\/>#!\/usr\/bin\/python<br\/># -*- coding: iso-8859-15 -*-<br\/># Author : Mace Robotics (www.macerobotics.com)<br\/># Date : 05\/04\/2020<br\/># Version : 0.1<br\/>#<br\/>import serial<br\/>import keyboard<br\/>import time<br\/><br\/>print(&quot;Control borvo with keyboard&quot;)<br\/><br\/>port = serial.Serial(&#039;COM6&#039;)<br\/>print(port.name)<br\/><br\/>print(&quot;wait..&quot;)<br\/>try:<br\/>  # read keyboard<br\/>  while 1:<br\/>    if keyboard.is_pressed(&#039;8&#039;):  # if key &#039;8&#039; is pressed <br\/>      print(&#039;forward&#039;)<br\/>      port.write(b&#039;FOR!&#039;)<br\/>      time.sleep(0.5)<br\/>    if keyboard.is_pressed(&#039;2&#039;):  # if key &#039;2&#039; is pressed <br\/>      print(&#039;move back&#039;)<br\/>      port.write(b&#039;BAC!&#039;)<br\/>      time.sleep(0.5)<br\/>    if keyboard.is_pressed(&#039;4&#039;): <br\/>      print(&#039;turn left&#039;)<br\/>      port.write(b&#039;TUL!&#039;)\t <br\/>      time.sleep(0.5)<br\/>    if keyboard.is_pressed(&#039;6&#039;):  <br\/>      print(&#039;turn right&#039;)<br\/>      port.write(b&#039;TUR!&#039;)\t <br\/>      time.sleep(0.5)<br\/>    if keyboard.is_pressed(&#039;5&#039;):  <br\/>      print(&#039;stop&#039;)<br\/>      port.write(b&#039;STP!&#039;)\t <br\/>      time.sleep(0.5)<br\/><br\/>except KeyboardInterrupt as exception:<br\/>  print(&quot;\\nEnd\\n&quot;)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n","protected":false},"excerpt":{"rendered":"<p>An example python program for control a mobile robot with keyboard: keyboard control serial communication<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-2915","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=\/wp\/v2\/posts\/2915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2915"}],"version-history":[{"count":17,"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=\/wp\/v2\/posts\/2915\/revisions"}],"predecessor-version":[{"id":2933,"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=\/wp\/v2\/posts\/2915\/revisions\/2933"}],"wp:attachment":[{"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.macerobotics.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}