[Unit 5(service) - Quiz] The robot continues to rotate without stopping

ros2 service call /rotate services_quiz_srv/srv/Spin “{direction: ‘left’, angular_velocity: 0.5, time: 3}”

response:
I received the message services_quiz_srv.srv.Spin_Response(success=False).

What went wrong?

  void rotate_callback(const std::shared_ptr<SpinMessage::Request> request,
                       const std::shared_ptr<SpinMessage::Response> response) {

    auto message = geometry_msgs::msg::Twist();

    if (request->direction == "left" || request->direction == "right") {

      double angular_velocity = (request->direction == "left")
                                    ? request->angular_velocity
                                    : -request->angular_velocity;

      message.angular.z = angular_velocity;
      publisher_->publish(message);

      auto timer = this->create_wall_timer(seconds(time), [this, response]() {
        auto stop_message = geometry_msgs::msg::Twist();
        stop_message.angular.z = 0.0;
        publisher_->publish(stop_message);

        response->success = true;
      });

    } else {
      RCLCPP_ERROR(get_logger(), "Invalid direction: %s",
                   request->direction.c_str());
      response->success = false;
    }
  }

You need to find out what went wrong by putting logs in your code to see where things are not happening as expected. You should be able to follow your code line by line, top to bottom, jumping to functions and callbacks where necessary.

This is a major part of development work.

In any case, no one can know what went wrong without seeing the complete code for your package, and it’s a bad idea to share the whole thing.

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