Ask Your Question
0

How to publish messages on a topic of type rosgraph_msgs/Clock?

asked 2017-04-24 09:33:30 -0500

manupe94 gravatar image

updated 2017-04-24 11:35:59 -0500

Hello!

I want to give values to a topic with this type of mesage (rosgraph_msgs/Clock)

I create a variable called "reloj" for example:

rosgraph_msgs::Clock reloj;

How do I give a value to that variable?, because it has two parts (secs and nsecs)

For example in float64 I give it the value this way

std_msgs::Float64 variable;

variable.data = 0;

I would like to do the same but in rosgraph_msgs/Clock, one value for "secs" and one for "nsecs"

I tried: reloj.clock.secs = 1; reloj.clock.nsecs = 100;

reloj.clock_secs = 1; reloj.clock_nsecs = 100;

But this does not work

I found that putting the following does work

reloj.clock = {5,88};

but when creating the node executable, I get this warning:

warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 reloj.clock = {5,88};

I use ROS kinetic and roscpp (c++)

this is the code in c++

#include "ros/ros.h"
#include "rosgraph_msgs/Clock.h"
rosgraph_msgs::Clock reloj;
int main(int argc, char **argv)
{
ros::init(argc, argv, "pubsubclock");
ros::NodeHandle n;
ros::Publisher publicador;
publicador  = n.advertise<rosgraph_msgs::Clock>("topico_pub", 1000);
reloj.clock = {5,88};
ros::Rate loop_rate(10);
while (ros::ok()) {
publicador.publish(reloj);
ros::spinOnce();
loop_rate.sleep();
}  
return 0;
}

thank you very much

edit retag flag offensive close merge delete

Comments

1

But this does not work

that doesn't tell us very much. Please explain what you found that "does not work". Include any error messages that you get.

Please use the edit button/link to update your original question.

gvdhoorn gravatar image gvdhoorn  ( 2017-04-24 10:03:25 -0500 )edit

I updated the message

manupe94 gravatar image manupe94  ( 2017-04-24 11:33:54 -0500 )edit

The syntax reloj.clock = {5,88}; is a form of constructor calling that is new in C++11. You cannot use it without telling the compiler to compile in C++11 mode.

Geoff gravatar image Geoff  ( 2017-04-24 20:07:47 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-04-24 20:13:17 -0500

Geoff gravatar image

The rosgraph_msgs::Clock message definition field clock has the msg data type time. In C++ this maps to the ros::Time class. You need to use that class to work with it. For example:

reloj.clock = ros::Time(1, 100);
edit flag offensive delete link more

Comments

I'm attempting to do something similar from Python, and managed to determine that rosgraph_msgs/Clock was using the field clock of type time. But trying to jam a Time into a type rosgraph_msgs/Clock does not appear to work.

Craigstar gravatar image Craigstar  ( 2020-03-27 01:04:38 -0500 )edit

code sample follows...

import rospy
from std_msgs.msg import Time
from rosgraph_msgs.msg import Clock

def simtime_talker():
    pub = rospy.Publisher('timey',Time, queue_size=10)
    pub1 = rospy.Publisher('clockey',Clock, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    sim_speed_multiplier = 10

    zero_time = rospy.get_time()

    while not rospy.is_shutdown():

        timepassed_dat = rospy.get_time() - zero_time
        timepassedaccelerated_dat = sim_speed_multiplier*timepassed_dat
        time_dat = rospy.Time.from_sec(timepassedaccelerated_dat)

        pub.publish(rospy.get_time())
        pub1.publish(time_dat)
        rate.sleep(
Craigstar gravatar image Craigstar  ( 2020-03-27 01:06:03 -0500 )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: 2017-04-24 09:33:30 -0500

Seen: 1,472 times

Last updated: Apr 24 '17