{"id":87,"date":"2024-03-25T23:14:56","date_gmt":"2024-03-25T23:14:56","guid":{"rendered":"https:\/\/smarthomerobot.ir\/?post_type=epkb_post_type_1&#038;p=87"},"modified":"2024-03-26T01:44:48","modified_gmt":"2024-03-26T01:44:48","slug":"python-coding-guide-for-robot-control","status":"publish","type":"epkb_post_type_1","link":"https:\/\/smarthomerobot.ir\/?epkb_post_type_1=python-coding-guide-for-robot-control","title":{"rendered":"Python Coding Guide for Robot Control"},"content":{"rendered":"\n\t\t<div id=\"eckb-article-page-container-v2\" class=\"eckb-article-page-content-counter eckb_ap_active_theme_twentytwentyfour \" data-mobile_breakpoint=\"768\">    \t\t\t\t<a href=\"#eckb-article-content\" class=\"eckb-skip-link eckb-screen-reader-text\">Skip to main content<\/a>   <div id=\"eckb-article-header\" ><\/div>\n\t\t\t<div id=\"eckb-article-body\">  <div id=\"eckb-article-left-sidebar\" ><\/div>\n\t\t        <article id=\"eckb-article-content\" tabindex=\"-1\" data-article-id=\"87\" >                        <div id=\"eckb-article-content-header-v2\" ><div id=\"eckb-article-content-header-row-1\"><div class=\"eckb-article-content-header-row-left-group\"><div id=\"eckb-article-back-navigation-container\"><div class=\"eckb-navigation-back  \"  style=\"margin-top: 4px; margin-right: 15px; margin-bottom: 4px; margin-left: 4px;\" ><div tabindex=\"0\" class=\"eckb-navigation-button\"  style=\"padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; color: #000000; background-color: #ffffff; font-size:14px;border-radius: 3px; border-style: solid; border-width: 1px; border-color: #b5b5b5;\"  onclick=\"history.go(-1);\" >&lt; All Topics<\/div><\/div><\/div><div id=\"eckb-article-content-breadcrumb-container\">\n\t<div class=\"eckb-breadcrumb\"  style=\"padding-top: 0px; padding-right: 4px; padding-bottom: 0px; padding-left: 4px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size:14px;\" >    \t\t<nav class=\"eckb-breadcrumb-outline\" aria-label=\"Breadcrumb\">\n\t\t\t<ul class=\"eckb-breadcrumb-nav\">       <li  style=\"font-size:14px;\" >\t<span class=\"eckb-breadcrumb-link\"><a tabindex=\"0\" href=\"https:\/\/smarthomerobot.ir\/?page_id=7\"><span  style=\"color: #000000;\"  >Main<\/span><\/a><span class=\"eckb-breadcrumb-link-icon ep_font_icon_arrow_carrot_right\" aria-hidden=\"true\"><\/span>\t<\/span><\/li><li  style=\"font-size:14px;\" >\t<span class=\"eckb-breadcrumb-link\"><a tabindex=\"0\" href=\"https:\/\/smarthomerobot.ir\/?epkb_post_type_1_category=python-samples\"><span  style=\"color: #000000;\"  >Python Samples<\/span><\/a><span class=\"eckb-breadcrumb-link-icon ep_font_icon_arrow_carrot_right\" aria-hidden=\"true\"><\/span>\t<\/span><\/li><li  style=\"font-size:14px;\" >\t<span class=\"eckb-breadcrumb-link\"><span aria-current=\"page\" style=\"color: #000000;\"  >Python Coding Guide for Robot Control<\/span>\t<\/span><\/li>\t\t\t<\/ul>\n\t\t<\/nav>\n\n\t<\/div>          <\/div><\/div><\/div><div id=\"eckb-article-content-header-row-2\"><div class=\"eckb-article-content-header-row-left-group\"><div id=\"eckb-article-content-title-container\"><h1 class=\"eckb-article-title\">Python Coding Guide for Robot Control<\/h1><\/div><\/div><\/div><\/div><div id=\"eckb-article-content-body\" >\n<p>Welcome to the Python Coding Guide for controlling your robot in the Smart Home Robotics Competition simulation environment. In this guide, we&#8217;ll walk you through the process of setting up your Python script to control your robot&#8217;s movements effectively.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding Required Libraries<\/h3>\n\n\n\n<p>Before we dive into coding, let&#8217;s start by importing the necessary libraries:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from controller import Robot, DistanceSensor, Motor, PositionSensor<\/code><\/code><\/pre>\n\n\n\n<p>These libraries provide the essential functions and components needed to interact with the robot&#8217;s sensors and actuators.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Initializing the Robot<\/h3>\n\n\n\n<p>Next, let&#8217;s initialize the robot and set up some parameters:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>robot = Robot() <span style=\"background-color: initial; font-family: inherit; font-size: inherit; color: var(--wp--preset--color--contrast-2);\"># Create robot object <\/span><\/code><code><span style=\"background-color: initial; font-family: inherit; font-size: inherit; color: var(--wp--preset--color--contrast-2);\">timeStep = int(robot.getBasicTimeStep()) <\/span><\/code><code><span style=\"background-color: initial; font-family: inherit; font-size: inherit; color: var(--wp--preset--color--contrast-2);\">maxSpeed = 6.28<\/span><\/code><\/code><\/pre>\n\n\n\n<p>Here, we create a <code>Robot<\/code> object and retrieve the basic time step. We also define the maximum speed for our robot.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding Motors<\/h3>\n\n\n\n<p>Now, let&#8217;s add the motors to control the robot&#8217;s wheels:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>wheel_left = robot.getDevice(\"wheel1 motor\") wheel_left.setPosition(float('inf')) <\/code><code>wheel_right = robot.getDevice(\"wheel2 motor\") wheel_right.setPosition(float('inf'))<\/code><\/code><\/pre>\n\n\n\n<p>We retrieve the motor devices and set their position to infinity, allowing them to rotate freely.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Movement Function<\/h3>\n\n\n\n<p>To control the robot&#8217;s movement, we&#8217;ll define a simple function:<\/p>\n\n\n\n<p>pythonCopy code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def move(left, right): wheel_right.setVelocity(right * maxSpeed \/ 10) wheel_left.setVelocity(left * maxSpeed \/ 10)<\/code><\/code><\/pre>\n\n\n\n<p>This function takes two parameters: <code>left<\/code> and <code>right<\/code>, representing the desired velocity of the left and right wheels, respectively. We calculate the velocity based on the maximum speed and set the wheel velocities accordingly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Controlling the Robot<\/h3>\n\n\n\n<p>Now that we have our <code>move<\/code> function, we can easily control the robot&#8217;s movement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while robot.step(timeStep) != -1:\n<code> move(10, 10) # Forward with maximum speed<\/code><code> move(-5, -5) # Backward with half speed<\/code><code> move(5, -5) # Turn<\/code><\/code><\/pre>\n\n\n\n<p>By calling the <code>move<\/code> function with appropriate parameters, we can make the robot move forward, backward, or turn as desired.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>With these simple steps, you can now start coding in Python to control your robot in the simulation environment. Experiment with different movements and strategies to optimize your robot&#8217;s performance and excel in the Smart Home Robotics Competition.<\/p>\n\n\n\n<p>Happy coding!<\/p>\n<\/div><div id=\"eckb-article-content-footer\" ><\/div>\n\t\t        <\/article><!-- \/#eckb-article-content -->     <div id=\"eckb-article-right-sidebar\" >\n\t\t\t<div class=\"eckb-article-toc  eckb-article-toc--bmode-between eckb-article-toc-reset \"\t\t\t\t\n\t\t\t\tdata-offset=\"130\"\n\t\t\t\tdata-min=\"2\"\n\t\t\t\tdata-max=\"6\"\n\t\t\t\tdata-speed=\"300\"\n\t\t\t\tdata-exclude_class=\"\"\n\t\t\t\t><div class=\"eckb-article-toc__title\">Table of Contents<\/div><\/div>\n\t\t\t<\/div>\n\t\t\t<\/div><!-- \/#eckb-article-body -->              <div id=\"eckb-article-footer\" ><\/div>\n\t\t<\/div><!-- \/#eckb-article-page-container-v2 -->\n\n\t\t<style id=\"eckb-article-styles\" type=\"text\/css\">@media only screen and (min-width:768px){#eckb-article-page-container-v2 #eckb-article-body #eckb-article-content #eckb-article-content-header-v2 #eckb-article-content-header-row-1,#eckb-article-page-container-v2 #eckb-article-body #eckb-article-content #eckb-article-content-header-v2 #eckb-article-content-header-row-2,#eckb-article-page-container-v2 #eckb-article-body #eckb-article-content #eckb-article-content-header-v2 #eckb-article-content-header-row-3,#eckb-article-page-container-v2 #eckb-article-body #eckb-article-content #eckb-article-content-header-v2 #eckb-article-content-header-row-4,#eckb-article-page-container-v2 #eckb-article-body #eckb-article-content #eckb-article-content-header-v2 #eckb-article-content-header-row-5{flex-direction:row;}}#eckb-article-content-header-row-1{margin-bottom:8px;}#eckb-article-content-header-row-1 .eckb-article-content-header-row-left-group,#eckb-article-content-header-row-1 .eckb-article-content-header-row-right-group{align-items:center;}#eckb-article-content-header-row-2{margin-bottom:0px;}#eckb-article-content-header-row-2 .eckb-article-content-header-row-left-group,#eckb-article-content-header-row-2 .eckb-article-content-header-row-right-group{align-items:flex-end;}#eckb-article-body .eckb-article-toc ul a.active{background-color:#1e73be;color:#ffffff;}#eckb-article-body .eckb-article-toc ul a:hover{background-color:#e1ecf7;color:#000000;}#eckb-article-body .eckb-article-toc__inner{border-color:#2b98e5;font-size:14px !important;background-color:#fcfcfc;}#eckb-article-body .eckb-article-toc__inner a{color:#2b98e5;font-size:14px !important;}#eckb-article-body .eckb-article-toc__title{color:#2b98e5;font-size:15px !important;}<\/style>   ","protected":false},"excerpt":{"rendered":"<p>Skip to main content &lt; All Topics Main Python Samples Python Coding Guide for Robot Control Python Coding Guide for Robot ControlWelcome to the Python Coding Guide for controlling your robot in the Smart Home Robotics Competition simulation environment. In this guide, we&#8217;ll walk you through the process of setting up your Python script to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"epkb_post_type_1_category":[14],"epkb_post_type_1_tag":[],"class_list":["post-87","epkb_post_type_1","type-epkb_post_type_1","status-publish","hentry","epkb_post_type_1_category-python-samples"],"_links":{"self":[{"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=\/wp\/v2\/epkb_post_type_1\/87"}],"collection":[{"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=\/wp\/v2\/epkb_post_type_1"}],"about":[{"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=\/wp\/v2\/types\/epkb_post_type_1"}],"author":[{"embeddable":true,"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=87"}],"version-history":[{"count":2,"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=\/wp\/v2\/epkb_post_type_1\/87\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=\/wp\/v2\/epkb_post_type_1\/87\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"epkb_post_type_1_category","embeddable":true,"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fepkb_post_type_1_category&post=87"},{"taxonomy":"epkb_post_type_1_tag","embeddable":true,"href":"https:\/\/smarthomerobot.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fepkb_post_type_1_tag&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}