Hello
I am doing my first project of ROS basics in 5 days,
My problem is that I can not figure that what part of the code for section 2 should change, anyway my code does not call service properly.
following is my python and lunch file
#! /usr/bin/env python
import rospy
from turtlebot3_move.srv import MoveInSquare, MoveInSquareResponse
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
import time
class RobotClass():
def __init__(self):
rospy.init_node('move_in_square_server', anonymous=True)
cmd_vel_topic = '/cmd_vel'
self.vel_publisher = rospy.Publisher(
cmd_vel_topic, Twist, queue_size=1)
self.cmd = Twist()
self.laser_subscriber = rospy.Subscriber(
'/scan', LaserScan, self.laser_callback)
self.ctrl_c = False
self.rate = rospy.Rate(1)
rospy.on_shutdown(self.shutdownhook)
def publish_once_in_cmd_vel(self):
while not self.ctrl_c:
connections = self.vel_publisher.get_num_connections()
if connections > 0:
self.vel_publisher.publish(self.cmd)
#rospy.loginfo("Cmd Published")
break
else:
self.rate.sleep()
def shutdownhook(self):
# works better than the rospy.is_shutdown()
self.ctrl_c = True
def laser_callback(self, msg):
self.laser_msg = msg
def get_front_laser(self):
time.sleep(1)
d = int((len(self.laser_msg.ranges))/2)
return self.laser_msg.ranges[d]
def stop_robot(self):
self.cmd.linear.x = 0.0
self.cmd.angular.z = 0.0
self.publish_once_in_cmd_vel()
def move_straight_time(self, motion, speed, time):
self.cmd.linear.y = 0
self.cmd.linear.z = 0
self.cmd.angular.x = 0
self.cmd.angular.y = 0
self.cmd.angular.z = 0
if motion == "forward":
self.cmd.linear.x = speed
elif motion == "backward":
self.cmd.linear.x = - speed
i = 0
while (i <= time):
self.vel_publisher.publish(self.cmd)
i += 1
self.rate.sleep()
self.stop_robot()
s = "Moved robot " + motion + " for " + str(time) + " seconds"
return s
def turn(self, clockwise, speed, time):
self.cmd.linear.x = 0
self.cmd.linear.y = 0
self.cmd.linear.z = 0
self.cmd.angular.x = 0
self.cmd.angular.y = 0
if clockwise == "clockwise":
self.cmd.angular.z = -speed
else:
self.cmd.angular.z = speed
i = 0
while (i <= time):
self.vel_publisher.publish(self.cmd)
i += 1
self.rate.sleep()
self.stop_robot()
s = "Turned robot " + clockwise + " for " + str(time) + " seconds"
return s
def my_callback(self, request):
print("callback called")
i = 0
while i < 4:
print("callback called")
self.move_straight_time("forward", 0.15/5, 5)
self.turn("clockwise", 0.33, 5)
if (self.get_front_laser()) < 0.2:
self.stop_robot()
print("obstcle")
raise Exception
i += 1
self.stop_robot()
response = MoveInSquareResponse()
response.complete = True
return response # the service Response class, in this case EmptyResponse
if __name__ == '__main__':
robotcontrol_object = RobotClass()
try:
robotcontrol_object.move_straight()
except rospy.ROSInterruptException:
pass
obj = RobotClass()
my_service = rospy.Service('/move_in_square', MoveInSquare, obj.my_callback)
rospy.spin()
Launch file
<launch>
<!-- Start Service Server for move_in_square service -->
<node pkg="turtlebot3_move" type="Service_server.py" name="move_in_square_server" output="screen">
</node>
</launch>