Ask Your Question
0

How to know /GPSFix message availability?

asked 2013-11-08 22:09:19 -0600

Bindu gravatar image

updated 2014-01-28 17:18:29 -0600

ngrennan gravatar image

Below program will perfectly subscribe /GPSFIX value from rosbag and print Latitude Longitude values. I want this program to print the value 0 always even in the absence of /GPSFIX publisher. Because always i want to print 0 at first then followed by Lat/Long if publisher is available otherwise 0 alone always .

EDIT: Using this code how to check whether /GPS message is available through c++ program?

First i want to check availability of /GPSFix messagge after that i want to print LatLong like this format. That is value zero followed by lat/long message.

Program:

    #include "ros/ros.h"
    #include <gps_common/GPSFix.h>

    using namespace std;
    void GPSCallback(const gps_common::GPSFix::ConstPtr& msg)
    {
    printf("%f\n", msg->longitude); 
    printf("%f\n", msg->latitude);
    fflush(stdout);
    sleep(2);
    }

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

    ros::init(argc, argv, "listener");
    ros::NodeHandle n;
    ros::Subscriber sub = n.subscribe("/GPSFix", 10, GPSCallback);

    ros::spin();

    return 0;

    }

Current Output:

    Longitude: 80.0910
    Latitude: 13.1289

    Longitude: 80.0911
    Latitude: 13.1289

    Longitude: 80.0912
    Latitude: 13.1290

    Longitude: 80.0913
    Latitude: 13.1289

    Longitude: 80.0912
    Latitude: 13.1288

My expected output format if /GPSFix is available:

    0
    Longitude: 80.0910
    Latitude: 13.1289

    0        
    Longitude: 80.0911
    Latitude: 13.1289

    0
    Longitude: 80.0912
    Latitude: 13.1290

    0
    Longitude: 80.0913
    Latitude: 13.1289

    0
    Longitude: 80.0912
    Latitude: 13.1288

Incase /GPSFix is not available output should be like

    0
    0
    0
    0
    0
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2013-11-12 16:19:08 -0600

Thomas D gravatar image

updated 2013-11-13 03:43:21 -0600

You could do this several ways. Quick and dirty is to combine the GPS message callback and a Timer. Below you see that a timer is created that gets called at 10 Hz and uses the latest GPS data. It then resets the stored GPS data back to zeros until a new GPS message arrives. This will always print out zeros unless you have fresh GPS data.

Note that global variables are used here, but the link to the Timers page has examples of how to use a callback function for a Timer that is part of a class. Then, you could store the latest GPS data as class member variables.

As an aside, you rarely ever want to call sleep() inside a callback function. I'm pretty sure that this will cause that thread to stop running and you will get unexpected results, most likely dropping a lot of messages as your queue fills up.

EDIT For the updated question you could keep track of whether you recently received a GPS message. In the timer callback you always print zero, and based on whether you have new GPS data you would print that as well.

#include <ros/ros.h>
#include <ros/time.h>
#include <gps_common/GPSFix.h>

double g_latest_lat = 0.0;
double g_latest_lon = 0.0;
bool g_have_fresh_gps_data = false;

void timerCallback(const ros::TimerEvent& event)
{
    printf("0\n");
    if (g_have_fresh_gps_data)
    {
        printf("Longitude: %lf\nLatitude: %lf\n\n", g_latest_lon, g_latest_lat);
    }
    g_latest_lat = 0.0;
    g_latest_lon = 0.0;
    g_have_fresh_gps_data = false;
}

void GPSCallback(const gps_common::GPSFix::ConstPtr& msg)
{
    g_latest_lon = msg->longitude;
    g_latest_lat = msg->latitude;
    g_have_fresh_gps_data = true;
}

int main(int argc, char **argv)
{
    ros::init(argc, argv, "listener");
    ros::NodeHandle n;
    ros::Subscriber sub = n.subscribe("/GPSFix", 10, GPSCallback);
    ros::Timer timer = n.createTimer(ros::Duration(0.1), timerCallback);

    ros::spin();

    return 0;
}
edit flag offensive delete link more

Comments

Using this code how to check whether /GPS message is available?

Bindu gravatar imageBindu ( 2013-11-12 20:15:39 -0600 )edit

Thank you so much for your answer. . .

Bindu gravatar imageBindu ( 2013-11-15 17:37:44 -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: 2013-11-08 22:09:19 -0600

Seen: 477 times

Last updated: Nov 13 '13