Roslaunch error for service server

Hello
Could someone help me out about the reason of the roslaunch error :

MoveInSquare.srv:


---
bool complete

My python file for the service server :

#! /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
rate = rospy.Rate(2)

def my_callback(request):
    print("callback called")

    i=0
    while i<4:
        print("callback called")
        print("moving forward")

        cmd.linear.x = (0.15)/5
        cmd.angular.z = 0
        pub.publish(cmd)
        rospy.sleep(5)

        print("Rotate")
        cmd.linear.x = 0
        cmd.angular.z = 0.33
        pub.publish(cmd)
        rospy.sleep(5)

        if ls.ranges[360] < 0.2:
            print("stop")
            cmd.linear.x = 0
            cmd.angular.z = 0
            pub.publish(cmd)
            pub2.publish(ls)
            rate.sleep()
            print("There is an Obstacle in font...")
        i+=1

    print("stop")
    cmd.linear.x = 0
    cmd.angular.z = 0
    pub.publish(cmd)

    rospy.sleep(5)
    response = MoveInSquare()
    response.complete= True
    return response  # the service Response class, in this case EmptyResponse


rospy.init_node('move_in_square_server')
my_service = rospy.Service('/move_in_square', MoveInSquare, my_callback)
#response = MoveInSquareMessageResponse()
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
pub2 = rospy.Publisher('/scan', LaserScan, queue_size=1)

while pub.get_num_connections() < 1:
    pass

cmd = Twist()
ls = LaserScan()
rospy.spin()

launch file for the service server

<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>

This is part of the error

rospy.exceptions.ROSInitException: time is not initialized. Have you called init_node()?
[move_in_square_server-1] process has died [pid 26524, exit code 1, cmd /home/user/catkin_ws/src/turtlebot3_move/src/Service_server.py __name:=move_in_square_server __log:=/home/user/.ros/log/175ccd66-8cd8-11ec-980e-0242c0a8d007/move_in_square_server-1.log].
log file: /home/user/.ros/log/175ccd66-8cd8-11ec-980e-0242c0a8d007/move_in_square_server-1*.log

it turns out to be that before defining rate, node need to be initialized.

rospy.init_node('move_in_square_server')
rate = rospy.Rate(2)

It works this way, but I don’ t know why node needs to be initialized first.

it worked, thanks :slight_smile: