filmov
tv
How to create a custom code to send the TurtleSim to the desired position.

Показать описание
Step1: Create a package turtle_control in catkin_ws/src
catkin_create_pkg turtle_control std_msgs rospy roscpp
build and source the catkin_ws workspace.
Step2: Create a python script in catkin_ws/src/turtle_control
code:
#!/usr/bin/env python3
import rospy
def move_turtle(x, y):
teleport_absolute = rospy.ServiceProxy('/turtle1/teleport_absolute', TeleportAbsolute)
try:
teleport_absolute(x, y, 0.0)
except rospy.ServiceException as e:
if __name__ == '__main__':
try:
roll_number = 10
x_desired = roll_number / 10.0
y_desired = roll_number / 5.0
move_turtle(x_desired, y_desired)
except rospy.ROSInterruptException:
pass
Explanation of the above code:-
rospy: The ROS Python library.
move_turtle(x, y) initializes a ROS node with the name 'turtle_mover'. It waits for the teleport_absolute service to become available and creates a proxy for the teleport_absolute service using rospy.ServiceProxy.
if __name__ == '__main__' in this we initialize the desired turtle's position by using a formula based on a roll number (My roll number is 74). The turtle's x and y positions are calculated as roll_number / 10.0 and roll_number / 5.0, respectively.
move_turtle function is then called with the calculated x and y positions.
Step 3: Build and source the catkin_ws workspace.
Step 4: Run the above Python script.
catkin_create_pkg turtle_control std_msgs rospy roscpp
build and source the catkin_ws workspace.
Step2: Create a python script in catkin_ws/src/turtle_control
code:
#!/usr/bin/env python3
import rospy
def move_turtle(x, y):
teleport_absolute = rospy.ServiceProxy('/turtle1/teleport_absolute', TeleportAbsolute)
try:
teleport_absolute(x, y, 0.0)
except rospy.ServiceException as e:
if __name__ == '__main__':
try:
roll_number = 10
x_desired = roll_number / 10.0
y_desired = roll_number / 5.0
move_turtle(x_desired, y_desired)
except rospy.ROSInterruptException:
pass
Explanation of the above code:-
rospy: The ROS Python library.
move_turtle(x, y) initializes a ROS node with the name 'turtle_mover'. It waits for the teleport_absolute service to become available and creates a proxy for the teleport_absolute service using rospy.ServiceProxy.
if __name__ == '__main__' in this we initialize the desired turtle's position by using a formula based on a roll number (My roll number is 74). The turtle's x and y positions are calculated as roll_number / 10.0 and roll_number / 5.0, respectively.
move_turtle function is then called with the calculated x and y positions.
Step 3: Build and source the catkin_ws workspace.
Step 4: Run the above Python script.