Quiz4 Grade Check Performance

Hello, I passed almost all the checkpoints. My testing environment shows that the robot was able to turn with specified direction, angular_velocity, and time.

Here’s my client side:

    def send_request(self):

        print(sys.argv)
        # send the request
        self.req.direction = sys.argv[1]
        self.req.angular_velocity = float(sys.argv[2])
        self.req.time = int(sys.argv[3])

        # uses sys.argv to access command line input arguments for the request.
        self.future = self.client.call_async(self.req)
        # to print in the console

My shell command is as follow:
ros2 run services_quiz services_quiz_client right 5.0 10

my server side is:

    def custom_service_callback(self, request, response):
        # The callback function receives the self-class parameter,
        # received along with two parameters called request and response
        # - receive the data by request
        # - return a result as a response

        # create a Twist message
        msg = Twist()

        if request.direction == "right":
            # define the linear x-axis velocity of /cmd_vel topic parameter to 0.1
            # msg.linear.x = 0.1
            # define the angular z-axis velocity of /cmd_vel topic parameter to -0.5 to turn right
            msg.angular.z = -(request.angular_velocity)
            # Publish the message to the topic
            self.publisher_.publish(msg)
            # print a pretty message
            self.get_logger().info('Turning to right direction!!')
            # response state

            start_time = time.time()
            while(time.time() - start_time < request.time):
                pass

            msg.angular.z = 0.0
            self.publisher_.publish(msg)

            response.success = True
        elif request.direction == "left":
            # define the linear x-axis velocity of /cmd_vel topic parameter to 0.1
            # msg.linear.x = 0.1
            # define the angular z-axis velocity of /cmd_vel topic parameter to 0.5 to turn left
            msg.angular.z = request.angular_velocity
            # Publish the message to the topic
            self.publisher_.publish(msg)
            # print a pretty message
            self.get_logger().info('Turning to left direction!!')
            # response state

            start_time = time.time()
            while(time.time() - start_time < request.time):
                # print(time.time() - start_time, request.time)
                pass
            msg.angular.z = 0.0
            self.publisher_.publish(msg)

            response.success = True
        # elif request.move == "Stop":
        #     # define the linear x-axis velocity of /cmd_vel topic parameter to 0
        #     msg.linear.x = 0.0
        #     # define the angular z-axis velocity of /cmd_vel topic parameter to 0
        #     msg.angular.z = 0.0
        #     # Publish the message to the topic
        #     self.publisher_.publish(msg)
        #     # print a pretty message
        #     self.get_logger().info('Stop there!!')
        #     # response state
        #     response.success = True
        else:
            # response state
            response.success = False

        # return the response parameter
        return response

I did import python time module to keep track of how many seconds, not sure if this is the desired way.

with the command ros2 run services_quiz services_quiz_client right 0.2 10
I get less than 90 degree-ish
image
image

======= update ======

Got a little closer, but still unable to pass the 7 marker
Using time with self.get_clock().now().seconds_nanoseconds()[0]

image

I noticed the robot will move slightly over time even though the Response state (client) has ended, carried by some momentum (the change is very small)

Hello @Genozen ,

Your client has to contain the values to be sent to the server inside the code. This means, you cannot capture the values from the command with sys.argv[1]. This is because the correction script is going to launch the client like this:

ros2 launch services_quiz services_quiz_client.launch.py

Without specifying any values in the command.

1 Like

Thank you! this was the missing info I needed!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.