Exercise: Square Test.

You are going to make a program for describing a square trajectory with the robot.

Instead of starting to code from scratch, you are going to reuse the code that you developed for the distance and turning exercises.

1. Starting position

For a better visual understanding of the task, it is recommended that the robot starts at the center of the room.

You can easily relocate the robot there by simply restarting the simulation.

2. Initialization

After restarting the simulation, the robot needs to be initialized.


In [ ]:
import packages.initialization
import pioneer3dx as p3dx
p3dx.init()

3. Program

The code is structured in three parts:

  1. The first part is a function for moving forward: you must copy and paste the code inside the body of the function template, in the following cell.
  2. The second part is a similar function for turning.
  3. Finally, the third part is the main code, consisting of a loop that calls the previous functions four times. The code also displays the pose of the robot (position and orientation) before and after the motion.

In [ ]:
def forward():
    # copy and paste your code here
    ...

In [ ]:
def turn():
    # copy and paste your code here
    ...

In [ ]:
print('Pose of the robot at the start')
p3dx.pose()
for _ in range(4):
    forward()
    turn()
print('Pose of the robot at the end')
p3dx.pose()

The trajectory can also be displayed:


In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
x, y = p3dx.trajectory()
plt.plot(x,y)

Try-a-Bot: an open source guide for robot programming

Developed by:

Sponsored by:

[![IEEE Robotics and Automation Society](../img/logo/ras.png "IEEE Robotics and Automation Society")](http://www.ieee-ras.org) [![Cyberbotics](../img/logo/cyberbotics.png "Cyberbotics")](http://www.cyberbotics.com) [![The Construct](../img/logo/theconstruct.png "The Construct")](http://www.theconstructsim.com)

Follow us:

[![Facebook](../img/logo/facebook.png "Facebook")](https://www.facebook.com/RobotProgrammingNetwork) [![YouTube](../img/logo/youtube.png "YouTube")](https://www.youtube.com/user/robotprogrammingnet)