<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Pratik Ingle</title>
<link>https://pratik-ingle.github.io/posts/tutorials/index.html</link>
<atom:link href="https://pratik-ingle.github.io/posts/tutorials/index.xml" rel="self" type="application/rss+xml"/>
<description>Hands-on robotics and programming tutorials.</description>
<generator>quarto-1.0.37</generator>
<lastBuildDate>Tue, 30 Nov 2021 23:00:00 GMT</lastBuildDate>
<item>
  <title>Getting Started With Webots</title>
  <link>https://pratik-ingle.github.io/posts/tutorials/getting-started-with-webots/index.html</link>
  <description><![CDATA[ 



<p>Webots is a simulation software package for rapidly prototyping mobile robots and autonomous systems. It provides a 3D physics-based environment with support for sensors, controllers, data visualization, and robot models.</p>
<section id="what-can-webots-be-used-for" class="level2">
<h2 class="anchored" data-anchor-id="what-can-webots-be-used-for">What can Webots be used for?</h2>
<ul>
<li>Mobile robot prototyping for academic research and industry.</li>
<li>Robot locomotion research, including legged robots.</li>
<li>Multi-agent and swarm robotics.</li>
<li>Adaptive behavior experiments such as genetic algorithms.</li>
<li>Teaching robotics and running robot contests.</li>
</ul>
</section>
<section id="what-you-should-know-first" class="level2">
<h2 class="anchored" data-anchor-id="what-you-should-know-first">What you should know first</h2>
<p>You can use Webots with C, C++, Java, Python, or MATLAB. If you want to create custom robot models or environments, it also helps to know some 3D graphics concepts and the VRML97 descriptive language.</p>
<p>A Webots simulation typically includes:</p>
<ol type="1">
<li>A world file (<code>.wbt</code>) that defines robots and the environment.</li>
<li>Controller programs that run the robot logic.</li>
<li>Optional physics plugins for custom physics behavior.</li>
</ol>
</section>
<section id="create-the-first-project" class="level2">
<h2 class="anchored" data-anchor-id="create-the-first-project">Create the first project</h2>
<p>Open Webots and go to <strong>Wizards &gt; New Project Directory</strong>. Name the project <code>bug</code>, name the world file <code>bug.wbt</code>, and select the options to add a rectangular arena.</p>
<p>Before adding a robot, change the arena size from <code>1 x 1 m</code> to <code>2 x 2 m</code> by editing <code>RectangleArena &gt; floorSize</code>.</p>
</section>
<section id="add-an-e-puck-robot" class="level2">
<h2 class="anchored" data-anchor-id="add-an-e-puck-robot">Add an e-puck robot</h2>
<p>Click <strong>Add</strong> in the scene tree, then choose <strong>PROTO nodes &gt; robots &gt; gctronic &gt; e-puck &gt; E-puck</strong>. The robot should appear in the arena. Save the world before running the simulation.</p>
<p>The e-puck has a default obstacle avoidance controller, so you can run the simulation immediately to see the robot move.</p>
</section>
<section id="create-a-simple-controller" class="level2">
<h2 class="anchored" data-anchor-id="create-a-simple-controller">Create a simple controller</h2>
<p>Go to <strong>Wizards &gt; New Robot Controller</strong>, choose Python, and name the controller <code>move_forward</code>.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;">"""move_forward controller."""</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;">from</span> controller <span class="im" style="color: #00769E;">import</span> Robot</span>
<span id="cb1-3"></span>
<span id="cb1-4">robot <span class="op" style="color: #5E5E5E;">=</span> Robot()</span>
<span id="cb1-5">time_step <span class="op" style="color: #5E5E5E;">=</span> <span class="dv" style="color: #AD0000;">32</span></span>
<span id="cb1-6">max_speed <span class="op" style="color: #5E5E5E;">=</span> <span class="fl" style="color: #AD0000;">6.24</span></span>
<span id="cb1-7"></span>
<span id="cb1-8">left_motor <span class="op" style="color: #5E5E5E;">=</span> robot.getMotor(<span class="st" style="color: #20794D;">"left wheel motor"</span>)</span>
<span id="cb1-9">right_motor <span class="op" style="color: #5E5E5E;">=</span> robot.getMotor(<span class="st" style="color: #20794D;">"right wheel motor"</span>)</span>
<span id="cb1-10"></span>
<span id="cb1-11">left_motor.setPosition(<span class="bu" style="color: null;">float</span>(<span class="st" style="color: #20794D;">"inf"</span>))</span>
<span id="cb1-12">right_motor.setPosition(<span class="bu" style="color: null;">float</span>(<span class="st" style="color: #20794D;">"inf"</span>))</span>
<span id="cb1-13">left_motor.setVelocity(<span class="fl" style="color: #AD0000;">0.0</span>)</span>
<span id="cb1-14">right_motor.setVelocity(<span class="fl" style="color: #AD0000;">0.0</span>)</span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="cf" style="color: #003B4F;">while</span> robot.step(time_step) <span class="op" style="color: #5E5E5E;">!=</span> <span class="op" style="color: #5E5E5E;">-</span><span class="dv" style="color: #AD0000;">1</span>:</span>
<span id="cb1-17">    left_speed <span class="op" style="color: #5E5E5E;">=</span> max_speed <span class="op" style="color: #5E5E5E;">*</span> <span class="fl" style="color: #AD0000;">0.5</span></span>
<span id="cb1-18">    right_speed <span class="op" style="color: #5E5E5E;">=</span> max_speed <span class="op" style="color: #5E5E5E;">*</span> <span class="fl" style="color: #AD0000;">0.25</span></span>
<span id="cb1-19"></span>
<span id="cb1-20">    left_motor.setVelocity(left_speed)</span>
<span id="cb1-21">    right_motor.setVelocity(right_speed)</span></code></pre></div>
<p>Set the e-puck controller to <code>move_forward</code> and run the simulation. Try changing the wheel speeds to make the robot move in a circular path.</p>
</section>
<section id="line-following-with-ir-sensors" class="level2">
<h2 class="anchored" data-anchor-id="line-following-with-ir-sensors">Line following with IR sensors</h2>
<p>Add infrared ground sensors to the e-puck by using the <code>groundSensorsSlot</code>. Name two sensors <code>ir0</code> and <code>ir1</code>, then enable them in the controller.</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;">from</span> controller <span class="im" style="color: #00769E;">import</span> Robot</span>
<span id="cb2-2"></span>
<span id="cb2-3">robot <span class="op" style="color: #5E5E5E;">=</span> Robot()</span>
<span id="cb2-4">time_step <span class="op" style="color: #5E5E5E;">=</span> <span class="dv" style="color: #AD0000;">32</span></span>
<span id="cb2-5">max_speed <span class="op" style="color: #5E5E5E;">=</span> <span class="fl" style="color: #AD0000;">6.24</span></span>
<span id="cb2-6"></span>
<span id="cb2-7">left_motor <span class="op" style="color: #5E5E5E;">=</span> robot.getMotor(<span class="st" style="color: #20794D;">"left wheel motor"</span>)</span>
<span id="cb2-8">right_motor <span class="op" style="color: #5E5E5E;">=</span> robot.getMotor(<span class="st" style="color: #20794D;">"right wheel motor"</span>)</span>
<span id="cb2-9">left_motor.setPosition(<span class="bu" style="color: null;">float</span>(<span class="st" style="color: #20794D;">"inf"</span>))</span>
<span id="cb2-10">right_motor.setPosition(<span class="bu" style="color: null;">float</span>(<span class="st" style="color: #20794D;">"inf"</span>))</span>
<span id="cb2-11">left_motor.setVelocity(<span class="fl" style="color: #AD0000;">0.0</span>)</span>
<span id="cb2-12">right_motor.setVelocity(<span class="fl" style="color: #AD0000;">0.0</span>)</span>
<span id="cb2-13"></span>
<span id="cb2-14">left_ir <span class="op" style="color: #5E5E5E;">=</span> robot.getDistanceSensor(<span class="st" style="color: #20794D;">"ir1"</span>)</span>
<span id="cb2-15">right_ir <span class="op" style="color: #5E5E5E;">=</span> robot.getDistanceSensor(<span class="st" style="color: #20794D;">"ir0"</span>)</span>
<span id="cb2-16">left_ir.enable(time_step)</span>
<span id="cb2-17">right_ir.enable(time_step)</span>
<span id="cb2-18"></span>
<span id="cb2-19"><span class="cf" style="color: #003B4F;">while</span> robot.step(time_step) <span class="op" style="color: #5E5E5E;">!=</span> <span class="op" style="color: #5E5E5E;">-</span><span class="dv" style="color: #AD0000;">1</span>:</span>
<span id="cb2-20">    left_ir_value <span class="op" style="color: #5E5E5E;">=</span> left_ir.getValue()</span>
<span id="cb2-21">    right_ir_value <span class="op" style="color: #5E5E5E;">=</span> right_ir.getValue()</span>
<span id="cb2-22"></span>
<span id="cb2-23">    left_speed <span class="op" style="color: #5E5E5E;">=</span> max_speed <span class="op" style="color: #5E5E5E;">*</span> <span class="fl" style="color: #AD0000;">0.25</span></span>
<span id="cb2-24">    right_speed <span class="op" style="color: #5E5E5E;">=</span> max_speed <span class="op" style="color: #5E5E5E;">*</span> <span class="fl" style="color: #AD0000;">0.25</span></span>
<span id="cb2-25"></span>
<span id="cb2-26">    <span class="cf" style="color: #003B4F;">if</span> left_ir_value <span class="op" style="color: #5E5E5E;">&gt;</span> right_ir_value <span class="kw" style="color: #003B4F;">and</span> <span class="dv" style="color: #AD0000;">6</span> <span class="op" style="color: #5E5E5E;">&lt;</span> left_ir_value <span class="op" style="color: #5E5E5E;">&lt;</span> <span class="dv" style="color: #AD0000;">15</span>:</span>
<span id="cb2-27">        left_speed <span class="op" style="color: #5E5E5E;">=</span> <span class="op" style="color: #5E5E5E;">-</span>max_speed <span class="op" style="color: #5E5E5E;">*</span> <span class="fl" style="color: #AD0000;">0.25</span></span>
<span id="cb2-28">    <span class="cf" style="color: #003B4F;">elif</span> right_ir_value <span class="op" style="color: #5E5E5E;">&gt;</span> left_ir_value <span class="kw" style="color: #003B4F;">and</span> <span class="dv" style="color: #AD0000;">6</span> <span class="op" style="color: #5E5E5E;">&lt;</span> right_ir_value <span class="op" style="color: #5E5E5E;">&lt;</span> <span class="dv" style="color: #AD0000;">15</span>:</span>
<span id="cb2-29">        right_speed <span class="op" style="color: #5E5E5E;">=</span> <span class="op" style="color: #5E5E5E;">-</span>max_speed <span class="op" style="color: #5E5E5E;">*</span> <span class="fl" style="color: #AD0000;">0.25</span></span>
<span id="cb2-30"></span>
<span id="cb2-31">    left_motor.setVelocity(left_speed)</span>
<span id="cb2-32">    right_motor.setVelocity(right_speed)</span></code></pre></div>
<p>Original standalone tutorial: <a href="https://pratik-ingle.github.io/webots_tutorial/" class="uri">https://pratik-ingle.github.io/webots_tutorial/</a></p>


</section>

 ]]></description>
  <category>tutorials</category>
  <category>robotics</category>
  <category>Webots</category>
  <guid>https://pratik-ingle.github.io/posts/tutorials/getting-started-with-webots/index.html</guid>
  <pubDate>Tue, 30 Nov 2021 23:00:00 GMT</pubDate>
  <media:content url="https://pratik-ingle.github.io/images/webot.gif" medium="image" type="image/gif"/>
</item>
</channel>
</rss>
