Services quiz troubles

Hello

I have tried completing the services quiz. While it seems like the robot turns correctly compared to the GIF in the notebook, the autograder fails me with the following message:

“The robot did NOT fully perform the expected turn”

I watched the movement closely for a while, and it seems like the robot continues to drift a very tiny bit to the right. This seemed weird, so I changed the script to specifically set all the values for both linear and angular velocities of the /cmd_vel topic to 0.0. Sadly this did not fix the drifting, and even when I echo the commands sent to the /cmd_vel topic, the last message published is:

linear:
x: 0.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0

Here is the used service callback function:

def turn_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()
        msg.linear.x = 0.0
        msg.linear.y = 0.0
        msg.linear.z = 0.0
        msg.angular.x = 0.0
        msg.angular.y = 0.0
        msg.angular.z = 0.0

        
        if request.direction == "right":
            msg.angular.z = -request.angular_velocity

            self.get_logger().info(f'Turning right with a velocity of {request.angular_velocity} rad/s for {request.time} seconds!!')
            self.publisher_.publish(msg)
            response.success = True

            # Sleep for movement duration
            time.sleep(request.time)

            # Stop turning
            self.get_logger().info('Done with movement!')
            msg.angular.z = 0.0
            self.publisher_.publish(msg)
        
        elif request.direction == "left":
            msg.angular.z = request.angular_velocity

            self.get_logger().info(f'Turning left with a velocity of {request.angular_velocity} rad/s for {request.time} seconds!!')
            self.publisher_.publish(msg)
            response.success = True

            # Sleep for movement duration
            time.sleep(request.time)

            # Stop turning
            self.get_logger().info('Done with movement!')
            msg.angular.z = 0.0
            self.publisher_.publish(msg)
        
        return response

Any help would be highly appreciated!

That is strange, I did something similar and worked. The difference is that I wrote the false option, I just change the angular.z to zero and not all the linear and angular options, and I create a while loop to publish the the twist msg. Here is my code:

    def spin_callback(self, request, response):
        twist = Twist()

        # Set angular velocity based on direction
        if request.direction == "right":
            twist.angular.z = -request.angular_velocity
        elif request.direction == "left":
            twist.angular.z = request.angular_velocity
        else:
            response.success = False
            return response

        # Publish the twist message for the specified duration
        end_time = self.get_clock().now() + rclpy.duration.Duration(seconds=request.time)
        while self.get_clock().now() < end_time:
            self.publisher.publish(twist)
            time.sleep(0.1)

        # Stop the robot
        twist.angular.z = 0.0
        self.publisher.publish(twist)

        response.success = True
        return response

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