ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Thanks Ɓukasz,

I used your hint to answer my Q1. The code follows. I am happy with the progress there. I am still trying to digest the C11 material you pointed me to for Q2.

 void detections_callback(const vision_msgs::msg::Detection2DArray::SharedPtr detections2dArray) const
  {
    int isDone = false;
    int detectionsCount = 0;

    RCLCPP_INFO(this->get_logger(), "---- ");
    RCLCPP_INFO(this->get_logger(), "Object detection message");

    try
    {
      while (not isDone)
      {
        vision_msgs::msg::Detection2D aDetection = detections2dArray->detections[detectionsCount++];
        RCLCPP_INFO(this->get_logger(), " ");

        // Id and confidence
        vision_msgs::msg::ObjectHypothesisWithPose result = aDetection.results[0];
        int64_t id = stoi(result.id);
        float score = result.score;
        RCLCPP_INFO(this->get_logger(), "   id    %i", id);
        RCLCPP_INFO(this->get_logger(), "   score %f", score);

        // Bounding boxes
        vision_msgs::msg::BoundingBox2D boundingBox2d = aDetection.bbox;
        geometry_msgs::msg::Pose2D center = boundingBox2d.center;
        RCLCPP_INFO(this->get_logger(), "   position (%f, %f)", center.x, center.y);
        RCLCPP_INFO(this->get_logger(), "   size %f x %f", boundingBox2d.size_x, boundingBox2d.size_y);
       }
    }

    // Exception handling for array out of bounds behavior is undefined. The behavior in
    // this example may not be the same in other C++ environments. Behavior was susceptible to
    //   https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why
    // Behavior is consistent enough in my environment to explore message parsing.
    catch (exception& e)
    {
      RCLCPP_INFO(this->get_logger(), "Exception %s", e.what());
      isDone = true;
    }
  }

I am surprised that the "id" was encoded as an ascii string in the detection message. I had to use an stoi to convert it to an integer. Id is declared as an int64 in the ObjectHypothesisWithPose data structure. Score, pose and bounding boxes where encoded as floats. No conversion was necessary.

I am also surprised that ObjectHypothesisWithPose is an array of results. I just used the first one and suspect there are some interesting reasons for the others.

Q2: I know that using exceptions to determine the end of an object detection is not acceptable. I wonder if I should re-implement this program in Python. I am under the impression it deals with this situation better. It would also give me an excuse to improve my Python. On the other hand maybe I should dig into C11. I clearly need to fix this.