Services Quiz Unit 7

Hello everyone,

Here is my code for the quiz. I get 7/10 I would like to know what is the issue? Is my robot too slow and how slow? I did not want to use only my grate tests.

Here is the ERROR:
:heavy_multiplication_x: [09:10:49] [assess] The robot did NOT fully perform the expected movements. Is it too hard?!

  • Did the robot move the right distance for each side of the two squares as stated in the notebook?
  • Did the robot complete the movements within the stipulated time? (mark: 7)
#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
from services_quiz.srv import BB8CustomServiceMessage, BB8CustomServiceMessageResponse

class MoveInSquare:

    def __init__(self):

        self.my_service = rospy.Service('/move_bb8_in_square_custom', BB8CustomServiceMessage, self.my_callback) # create the Service called my_service with the defined callback = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
        self.move = Twist()
        rospy.loginfo("Service is ready")
        rospy.spin() # maintain the service open.

    def my_callback(self,request):

        print("Request Data==> duration="+str(request.repetitions))

        for i in range(request.repetitions):

            for j in range(4):

            # Move straight
                self.move.linear.x = 0.2
                self.move.angular.z = 0
                rospy.sleep(self.side_size(request.side))  # Move straight for 2 seconds

                # Stop the robot
                self.move.linear.x = 0
                rospy.sleep(1)  # Stop for 1 second

                # Turn the robot
                self.move.angular.z = 1.57 / 2  # Turn 90 degrees
                rospy.sleep(2)  # Turn for 2 seconds

                # Stop the turn
                self.move.angular.z = 0
                rospy.sleep(1)  # Stop for 1 second
                rospy.loginfo("Completed side %d of square %d", j + 1, i + 1)
        self.move.angular.z = 0
        self.move.linear.x = 0
        my_response = BB8CustomServiceMessageResponse()
        my_response.success = True

        return  my_response # the service Response class, in this case MyCustomServiceMessageResponse

    def side_size(self,side_length):
        # Define the expected range of side lengths
        min_side_length = 0.1  # minimum side length in meters
        max_side_length = 10.0  # maximum side length in meters

        # Normalize the side_length to the range [0, 1]
        normalized_length = (side_length - min_side_length) / (max_side_length - min_side_length)

        # Scale the normalized value to the range [0, 20] seconds
        min_duration = 0.0
        max_duration = 20.0
        duration = normalized_length * (max_duration - min_duration) + min_duration

        return duration

if __name__ == '__main__':

Hi @omarsahri123,

I was checking the log messages here, and it looks like your robot didn’t move enough.

It looks like it didn’t even move at all, because the total distance it moved was only 0.07373, instead of 14-16 meters.

Please make sure your robot is moving accordingly.

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