Hello sir.
I saw a post where a user asked about multithreading concept.
I have been struggling since many weeks to solve the Services section of ROS Basics in 5 days course, not able to move ahead in the course since then. Can you please help me out sir? I tried copying the code written by the user and editing it. I replaced rclpy
with rospy
. I thought rclpy
is used for ROS2.
# import the Empty module from std_servs service interface
#from std_srvs.srv import Empty
from topics_section_1.srv import FindWall
# import the Twist module from geometry_msgs messages interface
from geometry_msgs.msg import Twist
# import the ROS2 python client libraries
import rospy
'''import rclpy
from rclpy.node import Node
from rclpy.qos import ReliabilityPolicy, QoSProfile'''
from sensor_msgs.msg import LaserScan
from rospy.callback_groups import MutuallyExclusiveCallbackGroup
class FindwallService(Node):
def __init__(self):
# Multithreading
self.group1 = MutuallyExclusiveCallbackGroup()
self.group2 = MutuallyExclusiveCallbackGroup()
# defines the type, name and callback function
self.srv = self.create_service(FindWall, '/find_wall', self.Empty_callback, callback_group=self.group1)
class FindwallService(Node):
def __init__(self):
# Here we have the class constructor
# call the class constructor to initialize the node as service_moving
super().__init__('find_wall')
# create the service server object
# defines the type, name and callback function
self.srv = self.create_service(FindWall, 'find_wall', self.Empty_callback)
# create the publisher object
# in this case the publisher will publish on /cmd_vel topic with a queue size of 10 messages.
# use the Twist module
self.publisher = self.create_publisher(Twist, 'cmd_vel', 10)
self.subscriber = self.create_subscription(LaserScan,'/scan', self.listener_callback, QoSProfile(depth=10, reliability=ReliabilityPolicy.BEST_EFFORT))
self.subscriber
self.laser_front = 0
self.laser_right = 0
def Empty_callback(self, request, response):
# The callback function recives the self class parameter,
# received along with two parameters called request and response
# - receive the data by request
# - return a result as response
near_wall = False
vel = Twist()
while near_wall == False:
self.get_logger().info("Front distance %s" %str(self.laser_front))
self.get_logger().info("Right distance %s" %str(self.laser_right))
if self.laser_front > 0.5:
self.get_logger().info("Vel = 0.2")
vel.linear.x = 0.2
publisher.publish(vel)
elif self.laser_front < 0.5 and self.laser_front > 0.1:
self.get_logger().info("Vel = 0.1")
vel.linear.x = 0.1
publisher.publish(vel)
else:
vel.linear.x = 0
publisher.publish(vel)
near_wall = True
self.get_logger().info("Near the wall")
response.wallfound = True
break
self.get_logger().info("Publishig speed")
# return the response parameter
return response
def listener_callback (self, laser):
self.get_logger().info("Reading")
self.laser_front = laser.ranges[0]
self.laser_right = laser.ranges[120]
#self.get_logger().info("Front distance %s" %str(self.laser_front))
#self.get_logger().info("Right distance %s" %str(self.laser_right))
def main(args=None):
# initialize the ROS communication
rospy.init(args=args)
try:
# declare the node constructor
findwall_service = FindwallService()
executor = MultiThreadedExecutor(num_threads=2)
executor.add_node(findwall_service)
try:
# pause the program execution, waits for a request to kill the node (ctrl+c)
executor.spin()
finally:
executor.shutdown()
findwall_service.destroy_node()
finally:
# shutdown the ROS communication
rospy.shutdown()
if __name__ == '__main__':
main()
But still it gives the following errors :
from: can't read /var/mail/geometry_msgs.msg
/home/user/catkin_ws/src/topics_section_1/scripts/find_wall_server.py: line 7: import: command not found
/home/user/catkin_ws/src/topics_section_1/scripts/find_wall_server.py: line 10: $'import rclpy\nfrom rclpy.node import Node\nfrom rclpy.qos import ReliabilityPolicy, QoSProfile': command not found
from: can't read /var/mail/sensor_msgs.msg
from: can't read /var/mail/rospy.callback_groups
/home/user/catkin_ws/src/topics_section_1/scripts/find_wall_server.py: line 14: syntax error near unexpected token `('
/home/user/catkin_ws/src/topics_section_1/scripts/find_wall_server.py: line 14: `class FindwallService(Node):'
I find sometimes a huge gap between what we are taught in the course and what we are expected to do in the project. Maybe my perception is wrong. I donāt have any other source where i get to understand ROS. Many days are just wasted when I am stuck at any point.