Skip to content

Assignment 2. Automatic Path

Info

To complete this assignment, you should have completed:

  • Lab 5.1. ROS Topics in Python
  • Lab 5.2. ROS Services in Python
  • Lab 5.3. ROS Package, msg, srv

Motivation

Objective: Control a simulated robot to follow a predefined path

  • In M2, one of the programmer tasks each year is to program the robot such that it can follow a path on the game field automatically.
  • In this assignment, you will write a node to control the movement of a simple simulated robot (the turtle) after a ROS service is called to trigger the start of the path.

Background. What is the turtle?

This section is exactly the same as that in assignment 1. If you have read that before, you can skip this part.

turtle_key

The "turtle" is the name for the simulated robot. This simulator is maintained by the ROS organization, the documentation is here: http://wiki.ros.org/turtlesim?distro=noetic

The above interface is running inside a simulator node called turtlesim_node.

Running the turtle

To run the turtlesim_node, run rosrun turtlesim turtlesim_node, and the above window should pop up.

The turtlesim package also includes a keyboard-based controller. To run it, in a new terminal, run rosrun turtlesim turtle_teleop_key.

# on a separate terminal
$ rosrun turtlesim turtlesim_node
# on a separate terminal
$ rosrun turtlesim turtle_teleop_key

...(omitted)
Reading from keyboard
---------------------------
Use arrow keys to move the turtle.
  • Now you can use the arrow keys of the keyboard to drive the turtle around. If you cannot drive the turtle, select the terminal window of the turtle_teleop_key to make sure that the keys that you type are recorded.

Topics provided by turtlesim

The turtlesim_node receives velocity instructions via the topic /turtle1/cmd_vel. Try to use rostopic echo on a new terminal to see how the messages are interpreted when you are using the keyboard controller.

$ rostopic echo /turtle1/cmd_vel
linear: 
  x: 2.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---
linear: 
  x: 2.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0
---
  • The topic type can be obtained via rostopic type [topic] or rostopic info [topic].
$ rostopic type /turtle1/cmd_vel
geometry_msgs/Twist

Position and velocity feedback from the turtle

When the turtle is moving, the turtlesim_node actually will also publish a message containing the current position and velocity of the turtle under turtle1/pose.

  • In a real robot, we often call this the "position feedback" and "velocity feedback".
  • Note that the msg type of turtle1/pose is turtlesim/Pose.

To test this behavior, you can try to drive the turtle and check the topic being published:

  • Run the nodes (you can skip this if they are already running):
    $ rosrun turtlesim turtlesim_node
    $ rosrun turtlesim turtle_teleop_key
    
  • Drive the robot around using your keyboard. At the same time, you can use rostopic echo to display the position published:
$ rostopic echo /turtle1/pose
x: 0.746215641499
y: 0.0329808667302
theta: 2.94571447372
linear_velocity: 0.0
angular_velocity: 0.0
---
x: 0.746215641499
y: 0.0329808667302
theta: 2.94571447372
linear_velocity: 0.0
angular_velocity: 0.0
---
...(omitted)

The turtlesim_node also provides the /turtle1/teleport_absolute service that allows you to teleport the turtle directly to a position on the field.

Try to call the service in the terminal!

$ rosservice call /turtle1/teleport_absolute "x: 2.0
y: 2.0
theta: 0.0"
  • You should see that the turtle has moved to the lower-left corner of the field.

Required functionalities of your node

You should write a node, which via service servers, provides the following features:

  • Turn to a specified global orientation

    • Your node should provide a service named /set_orientation.
    • When this service is called, the turtle should start turning (in the direction which leads to a decrease in the absolute difference to the target orientation) until the turtle has reached the target orientation. You can check the current orientation from the /turtle1/Pose topic stated previously.
    • The srv type of this service should be SetOrientation.srv. You should create this file by yourself. The required request and responses are:
      • Request: There should be a float64 field named orientation.
        • This field specifies the target orientation (in radians) that the turtle should turn to.
    • Response: There should be a bool field named success.
      • When the turtle has finished turning, the service should return with success: True
        • (ignore if you don’t understand) To avoid floating point precision issues, the turtle will be considered "at the correct orientation" if the absolute difference between the final orientation and the target orientation is less than 0.05 .
  • Move forward a specified distance

    • Your node should provide a service named /walk_distance.
    • When this service is called, the turtle should start moving forward (positive velocity linear.x) until the turtle has moved forward the specified units.
    • The srv type of this service should be WalkDistance.srv. You should create this file by yourself. The required request and responses are:
      • Request: There should be a float64 field named distance.
        • If distance is positive, this field specifies the distance the turtle should move.
        • The boundaries of the turtle map is \([0, 11]\) for both \(x\) and \(y\). If moving distance will result in the turtle moving out of the field (i.e. hit the wall), the turtle should not move.
    • If distance is negative, the turtle should not move.
      • Response: There should be a bool field named success.
        • If distance is positive, then the turtle has finished moving, the service should return with success: True
        • If the turtle does not move due to distance being too large (and beyond the range of the map) or distance being negative, the service should return with success: False
    • Precision issues will be handled the same as the previous task.

Template package

Where do I get even started with this assignment… there are so many tasks…

To help you, we have provided you with a template package, and you can work on the controller node directly!

  • Inside m2cs_ros_tutorial, you should see a package named turtle_path.
  • The node that you should be working on is turtle_path/src/path_manager.py.
  • If you are lost, there are some "blanks" and hints in the file that you can fill in to complete the assignment.
  • Note that apart from path_manager.py, you should also modify CMakeLists.txt, package.xml and create appropriate srv files by yourself!

Assignment evaluation/testing procedures

This part describes how your assignment submission will be evaluated by the trainers, which also gives hints on how you can test your program while developing it.

  1. The trainer's machine will have turtlesim and your turtle_path installed.
  2. The trainers will run the command roslaunch turtle_path test.launch. This command runs turtlesim_node and runs your file path_manager.py.
  3. The trainers will test the features stated in "Required functionalities of your node", by driving the turtle to a random location then using rosservice call.
  4. The trainers will read your source code to check your implementation skills and coding style. You are encouraged (but not required) to discuss with other trainees, but we require you to write your own code (no plagiarism!).

Submission instructions

  1. Create a Github account if you don't have
  2. Fork this training repository: https://github.com/m2robocon/m2cs_ros_tutorial
  3. Git clone this repository to your local machine
  4. Commit your work in local and push to Github

Warning

We will start marking your assignments after the deadline, so make sure it is pushed!