Exercise 4.1 alternative (contains part of the solution)

Hi,

Doing the unit 4, services in ROS exercise I reach a solution which is pretty similar to the proposed one. I just wonder why my solution still works when passing the “get_food.txt” file path to the execute_trajectory ServiceProxy, instead of passing an ExecTrajRequest object containing the file path in the “file” attribute as proposed in your solution:

My solution:

# This rospack.get_path() works in the same way as $(find name_of_package) in the launch files.
trajectory_file_path = rospack.get_path('iri_wam_reproduce_trajectory') + "/config/get_food.txt"

# Create the connection to the service
execute_trajectory_service_client = rospy.ServiceProxy('/execute_trajectory', ExecTraj)

# Send through the connection the name of the trajectory to be executed by the robot
result = execute_trajectory_service_client(trajectory_file_path)

Proposed solution:

# This rospack.get_path() works in the same way as $(find name_of_package) in the launch files.
trajectory_file_path = rospack.get_path('iri_wam_reproduce_trajectory') + "/config/get_food.txt"

# Create the connection to the service
execute_trajectory_service_client = rospy.ServiceProxy('/execute_trajectory', ExecTraj)
execute_trajectory_request_object = ExecTrajRequest() # Create an object of type ExecTrajRequest
execute_trajectory_request_object.file = trajectory_file_path # Fill the variable file of this object with the desired file path

result = execute_trajectory_service_client(execute_trajectory_request_object)

Running both of this perform the trajectory as expected.

If you check the official documentation at section 2.1
http://wiki.ros.org/rospy/Overview/Services
You can see that your solution is the standard way and simpler.

@bayodesegun Perhaps this should be changed, as it adds unnecessary steps.

1 Like

@simon.steinmann91 @cpumargarcia
Both are accepted methods. If you read through that link more carefully, you will find that it is so.

The one we are using and we recommend for ROS beginners is the explicit approach because what is going on is clearer.

The implicit method does some internal matching and typecasting which might not be obvious to someone just learning to write code, and it also doesn’t work for complex cases.

So, it’s a tradeoff between clarity and conciseness, and the former is usually advised.

1 Like