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

Hey guys what is wrong with this piece of code? I am a beginner with rose and I am trying to create a frame that listens to another one. However, it keeps sending me a warning saying that the frame doesn't exist.

asked 2020-03-30 10:11:04 -0600

lucabrodieno gravatar image
    #include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <tf2/LinearMath/Quaternion.h>
#include <tf2_ros/transform_listener.h>
#include <geometry_msgs/TransformStamped.h>
#include <visualization_msgs/Marker.h>
#include <geometry_msgs/Twist.h>



int main(int argc, char** argv){

  ros::init(argc, argv, "camera_rev");
  ros::NodeHandle node;;
  ros::Publisher marker_pub = node.advertise<visualization_msgs::Marker>("project2_rev", 1);
  uint32_t shape = visualization_msgs::Marker::SPHERE;
  visualization_msgs::Marker marker;

  marker.header.frame_id = "robot";
  marker.header.stamp = ros::Time::now();

  marker.scale.x = 0.3;
  marker.scale.y = 0.3;
  marker.scale.z = 0.3;

  marker.color.r = 0.0f;
  marker.color.g = 1.0f;
  marker.color.b = 1.0f;
  marker.color.a = 1.0;

  marker.lifetime = ros::Duration();

  marker.ns = "camera_rev";
  marker.id = 1;

  marker.type = shape;

  marker.action = visualization_msgs::Marker::ADD;

  marker.pose.position.x = 0;
  marker.pose.position.y = 0;
  marker.pose.position.z = 1;
  marker.pose.orientation.x = 0.0;
  marker.pose.orientation.y = 0.0;
  marker.pose.orientation.z = 0.0;
  marker.pose.orientation.w = 1.0;

  tf2_ros::Buffer tfBuffer;
  tf2_ros::TransformListener tfListener(tfBuffer);

  ros::Rate rate(10.0);
  while (node.ok()){
    geometry_msgs::TransformStamped transformStamped;
    ;
    try{
      transformStamped = tfBuffer.lookupTransform("project2_rev", "robot",
                               ros::Time(0));
    }
    catch (tf2::TransformException &ex) {
      ROS_WARN("%s",ex.what());
      ros::Duration(1.0).sleep();
      continue;
    }

    marker.pose.position.x = transformStamped.transform.translation.x ;
    marker.pose.position.y = transformStamped.transform.translation.y ;
    marker.pose.position.z = transformStamped.transform.translation.z ;

  //  tfb.sendTransform(transformStamped);

    marker_pub.publish(marker);
    //tfb.sendTransform(transformStamped);
    rate.sleep();
    printf("sending\n");
  }

};
edit retag flag offensive close merge delete

Comments

Are you sure the frame exists?

billy gravatar image billy  ( 2020-03-30 15:59:55 -0600 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2020-03-31 05:06:45 -0600

updated 2020-03-31 05:09:09 -0600

Hi,

I am not sure if you only have this node. My assumption is that you probably have some misconceptions about frame in ROS.

Publishing a visualization_msgs/Marker message does not create a frame. In fact, you don't create individual frames in ROS. You define relationship between coordinate frames by broadcasting a transform to tf or tf2.

it keeps sending me a warning saying that the frame doesn't exist.

So it literally means that frame does not exist in the tf. When you use lookupTransform() , target_frame and source_frame must both already exist in the tf tree.

I suggest you look through Writing a tf2 broadcaster and Writing a tf2 listener to see how to write a proper tf broadcaster/listener.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2020-03-30 10:11:04 -0600

Seen: 86 times

Last updated: Mar 31 '20