Ask Your Question
1

working with Markers

asked 2016-01-20 12:06:47 -0600

Commander Data gravatar image

updated 2016-02-02 04:04:42 -0600

The solution of my problem is beneath.

Hi guys,

before I posted this question, I did this tutorial about markers: http://wiki.ros.org/rviz/Tutorials/Ma... (Thank you for this tutorial!)

It worked pretty well! I understood how to change the value of the coordinates x,y, and z and how to publish points. Publishing points is no problem anymore.

Now, I would like to program a "listener", which receives exactly these points and print them out.

Unfortunately, I "only" found this tutorial: http://wiki.ros.org/ROS/Tutorials/Wri...

Therefore, I began with writing this "listener":

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "std_msgs/Int8.h"

#include <algorithm>
#include <iostream> 
#include <string> 
#include <unistd.h>  
#include <termios.h>
#include <cmath>

#include <visualization_msgs/Marker.h>


void chatterCallback(const visualization_msgs::Marker::ConstPtr& msg)
{
   std::cout << "I received: " << msg->p.x << std::endl;
}

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

  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("visualization_marker", 1000, chatterCallback); 

  ros::spin();   


  return 0;
}

Unfortunately, it didn't work out at all.

Can someone please help me solving the problem?

I would like to know how I can receive the coordinates and work with them. For instance:

sum = x+y+z;
ROS_INFO("The sum of a point with the coordinates x: [%d], y: [%d] and z: [%d] is : [%d]", p.x, p.y, p.z, sum);

With best regards Commander Data

SOLUTION:

The chatterCallback function needs to look like this:

void chatterCallback(const visualization_msgs::Marker::ConstPtr& msg)
{
    for (int i = 0; i <= number_of_points; i=i+1) {
         std::cout << "I received: " << msg->points[i].x << std::endl;
    }
}

Moreover, I changed in points_and_lines.cpp the line 93 for (uint32_t i = 0; i < 100; ++i) to for (int i = 0; i < 100; ++i).

I hope that this will help some researches in receiving points!

For everyone, who participate in finding the solution: THANK YOU A LOT!!!

With best regards, Commander Data

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2016-01-26 08:33:12 -0600

Hello!! I was trying to implement my task that is similar to yours and I got the output of x coordinate..

int sum;
for (uint32_t i=0; i<2; ++i)
{
geometry_msgs::Point p;

p.x = i;
p.y = 0.5;
p.z = 0.5;

line.points.push_back(p);
point.points.push_back(p);
p.z += 1.0;
point.points.push_back(p);
//std::cout<<"The coordinates: "<<p<<std::endl;
sum = p.x + p.y + p.z;
std::cout<<"The coordinates: "<<sum<<std::endl;
}

The output is:

The coordinates: 2
The coordinates: 3
The coordinates: 2
The coordinates: 3
edit flag offensive delete link more
0

answered 2016-01-21 15:58:01 -0600

Commander Data gravatar image

Hi Jorge,

actually, I would like to receive what the programme "points_and_lines.cpp" (from the mentioned tutorial) publishes.

As you can see in the source code of this programme, the coordinates are changing. Here is the section, which explains the change of the coordinates:

// Create the vertices for the points and lines
for (uint32_t i = 0; i < 100; ++i)
{
  float y = 5 * sin(f + i / 100.0f * 2 * M_PI);
  float z = 5 * cos(f + i / 100.0f * 2 * M_PI);

  geometry_msgs::Point p;
  p.x = (int32_t)i - 50;
  p.y = y;
  p.z = z;

  points.points.push_back(p);
  line_strip.points.push_back(p);

  // The line list needs two points for each line
  line_list.points.push_back(p);
  p.z += 1.0;
  line_list.points.push_back(p);
}

For the moment, let us try to receive only the x-coordinate.

The printout should be like this:

I received: -50
I received: -49
I received: -48
..... and so on .....

When I try your code, I get, as you described, only the same message over and over again:

I received: 49
I received: 49
I received: 49
I received: 49

Therefore, I tried this line of code, instead of yours:

std::cout << "I received: " << msg->points[0] << std::endl;

But then I receive:

I received: x: -50
y: 4.11811
z: 2.83569

I received: x: -50
y: 4.11811
z: 2.83569

I received: x: -50
y: 4.22832
z: 2.66858

I received: x: -50
y: 4.22832
z: 2.66858

I cannot explain, why I am getting the same value of x and why y and z are getting printed on the terminal at all. This is absolutely not what I want. I only want to work with the different x-coordinates.

Has somebody an idea, how to receive the different x-coordinates?

With best regards Commander Data

edit flag offensive delete link more
0

answered 2016-01-21 11:27:06 -0600

jorge gravatar image

updated 2016-01-22 04:40:46 -0600

It works, apart that there's a compilation error on the callback. If you write:

std::cout << "I received: " << msg->points.back().x << std::endl;

instead, you get 49. Not sure if that's what you want.

UPDATE All is fine here; it just turns out that the x coordinate is constant for every point; the snake-like movement only requires to change y and z. Just print the hole first and last points and you will notice all is fine:

std::cout << "I received: " << msg->points.front() << " ..."  << msg->points.back() << std::endl;

Btw, you see the whole point because geometric_msgs/Point objects (and all ROS messages) are serialized as yaml, and so shown on std::cout.

edit flag offensive delete link more

Comments

the x coordinate is NOT constant!

Commander Data gravatar imageCommander Data ( 2016-02-02 06:43:04 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-01-20 12:06:47 -0600

Seen: 425 times

Last updated: Feb 02 '16