Skip to content
Yuuta-watanabe edited this page Oct 16, 2014 · 1 revision

Function of use time

Topic publisher example (tms_ss_vicon)

#include <boost/date_time/posix_time/posix_time.hpp>
#include <tms_msg_db/TmsdbStamped.h>
#include <tms_msg_db/Tmsdb.h>

(省略)

ros::Publisher db_pub;
db_pub    = nh.advertise<tms_msg_db::TmsdbStamped> ("tms_db_data", 10);

(省略)

ros::Time now = ros::Time::now() + ros::Duration(9*60*60); // GMT +9

tms_msg_db::TmsdbStamped db_msg;
tms_msg_db::Tmsdb tmpData;

db_msg.header.frame_id  = frame_id;
db_msg.header.stamp     = now;

tmpData.time    = boost::posix_time::to_iso_extended_string(now.toBoost());
tmpData.id      = id;
tmpData.x       = pose_msg.translation.x;
tmpData.y       = pose_msg.translation.y;
tmpData.z       = pose_msg.translation.z;
tmpData.rr      = pose_msg.eulerXYZ[0];
tmpData.rp      = pose_msg.eulerXYZ[1];
tmpData.ry      = pose_msg.eulerXYZ[2];
tmpData.place   = idPlace;
tmpData.sensor  = idSensor;
tmpData.state   = 1;

db_msg.tmsdb.push_back(tmpData);

db_pub.publish(db_msg);

(省略)

Subscriber example

#include <boost/date_time/posix_time/posix_time.hpp>

void rosCheckTime(boost::posix_time::ptime time)
{
    // or using struct tm
    struct tm T = boost::posix_time::to_tm(time);     // ptime -> struct tm

    cout << T.tm_year + 1900 << "/" << T.tm_mon + 1 << "/" << T.tm_mday << " "
         << T.tm_hour        << ":" << T.tm_min     << ":" << T.tm_sec  << endl;
}

void fooCallback(const tms_msg_db::TmsdbStamped::ConstPtr& msg)
{
    ros::Time headerTime = msg->header.stamp;

    uint64_t nsec = headerTime.toNSec();
    printf("nsec = %llu nsec\n", nsec);

    rosCheckTime(headerTime.toBoost());  // view or save header time

    // using Fractional Seconds (micro second / 0.000001 sec / ex: 2014-05-07T17:41:45.308261)
    std::cout << boost::posix_time::to_iso_extended_string(headerTime.toBoost()) << std::endl;
}
Clone this wiki locally