Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUTTON/BUTTON_CLICK not working on InteractiveMarker #1313

Open
RaviRoKes opened this issue Dec 19, 2024 · 0 comments
Open

BUTTON/BUTTON_CLICK not working on InteractiveMarker #1313

RaviRoKes opened this issue Dec 19, 2024 · 0 comments

Comments

@RaviRoKes
Copy link

I am using ros2 rolling and RViz version 14.3.3 (ROS 2). When i click on CUBE marker callback is not working.

This is my code:

void BasicControlsNode::createBoxMarker(const tf2::Vector3 &position)
{
visualization_msgs::msg::InteractiveMarker int_marker;
int_marker.header.frame_id = "base_link";
int_marker.header.stamp = this->get_clock()->now();
int_marker.name = "box_marker_" + std::to_string(position.x()) + "_" + std::to_string(position.y());
int_marker.description = "Interactive Box Marker";
int_marker.scale = 1.0;
int_marker.pose.position.x = position.x();
int_marker.pose.position.y = position.y();
int_marker.pose.position.z = position.z();

// Add a simple box visual for the button
visualization_msgs::msg::Marker box_marker;
box_marker.type = visualization_msgs::msg::Marker::CUBE;
box_marker.scale.x = 0.5;
box_marker.scale.y = 0.5;
box_marker.scale.z = 0.5;
box_marker.color.r = 0.0f;
box_marker.color.g = 1.0f;
box_marker.color.b = 0.0f;
box_marker.color.a = 1.0f;

// Add a control for interaction 
visualization_msgs::msg::InteractiveMarkerControl button_control;
button_control.name = "button_control";                                                         // Ensure the control name is unique and relevant
button_control.interaction_mode = visualization_msgs::msg::InteractiveMarkerControl::BUTTON;
button_control.always_visible = true;

// Create a marker for the box
button_control.markers.push_back(box_marker);  // Add the box marker to the control
int_marker.controls.push_back(button_control); // Add the control to the interactive marker

server_->insert(int_marker, std::bind(&BasicControlsNode::processBoxClick, this, std::placeholders::_1)); // Callback function processBoxClick is assigned using server_->setCallback
RCLCPP_DEBUG(get_logger(), "Callback bound to marker: %s", int_marker.name.c_str());
server_->applyChanges();
RCLCPP_DEBUG(get_logger(), "Marker '%s' inserted and changes applied to InteractiveMarkerServer.", int_marker.name.c_str());

}

/// dfk//
void BasicControlsNode::processBoxClick(const visualization_msgs::msg::InteractiveMarkerFeedback::ConstSharedPtr &feedback)
{
RCLCPP_DEBUG(get_logger(), "Received feedback: %s with event type: %d", feedback->marker_name.c_str(), feedback->event_type);

if (feedback->event_type == visualization_msgs::msg::InteractiveMarkerFeedback::BUTTON_CLICK)
{
  RCLCPP_INFO(rclcpp::get_logger("basic_controls"), "Marker clicked: %s", feedback->marker_name.c_str());

  // Retrieve the clicked marker
  visualization_msgs::msg::InteractiveMarker int_marker;
  if (!server_->get(feedback->marker_name, int_marker))
  {
    RCLCPP_WARN(get_logger(), "Marker '%s' not found!", feedback->marker_name.c_str());
    return;
  }
  RCLCPP_INFO(get_logger(), "Erasing marker and creating 6-DOF marker at: [%f, %f, %f]",
              feedback->pose.position.x, feedback->pose.position.y, feedback->pose.position.z);

  server_->erase(feedback->marker_name); // This will erase the previous marker

  // Extract position from feedback->pose and convert to tf2::Vector3
  tf2::Vector3 position(feedback->pose.position.x, feedback->pose.position.y, feedback->pose.position.z);
  make6DofMarker(true, visualization_msgs::msg::InteractiveMarkerControl::MOVE_3D, position, true);

  RCLCPP_INFO(get_logger(), "6-DOF marker created at position: [%f, %f, %f]",
              position.x(), position.y(), position.z());
}
else
{
  RCLCPP_WARN(rclcpp::get_logger("basic_controls"), "Unexpected event type: %d", feedback->event_type);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant