Skip to content

Commit e75078d

Browse files
committed
add service to dynamically reset the gps reference --tested with bag file
1 parent 62a86c3 commit e75078d

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
<depend>geometry_msgs</depend>
2323
<!--cmake_modules dependency required for (find Eigen) command-->
2424
<depend>cmake_modules</depend>
25+
<!--std_srvs provides the Empty service used to reset the gps reference ]-->
26+
<depend>std_srvs</depend>
2527
</package>

src/set_gps_reference_node.cpp

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
#include <sensor_msgs/NavSatFix.h>
33
#include <string>
44
#include <vector>
5+
#include <std_srvs/Empty.h>
56

67
double g_lat_ref;
78
double g_lon_ref;
89
double g_alt_ref;
10+
int count = 1;
11+
bool gps_ref_is_init;
912

1013
std::vector<double> g_pressure_heights;
1114
int g_its;
@@ -18,49 +21,63 @@ enum EMode
1821
// average over, or wait for, n GPS fixes
1922
EMode g_mode;
2023

24+
bool reset_callback(std_srvs::Empty::Request&, std_srvs::Empty::Response&)
25+
{
26+
count = 1;
27+
g_lat_ref = 0.0;
28+
g_lon_ref = 0.0;
29+
g_alt_ref = 0.0;
30+
ros::NodeHandle nh;
31+
nh.setParam("/gps_ref_is_init", false);
32+
return true;
33+
}
2134

2235
void gps_callback(const sensor_msgs::NavSatFixConstPtr & msg)
2336
{
37+
ros::NodeHandle nh;
38+
nh.getParam("/gps_ref_is_init", gps_ref_is_init);
39+
40+
if (!gps_ref_is_init){
2441

25-
static int count = 1;
2642

27-
if (msg->status.status < sensor_msgs::NavSatStatus::STATUS_FIX) {
28-
ROS_WARN_STREAM_THROTTLE(1, "No GPS fix");
29-
return;
30-
}
43+
if (msg->status.status < sensor_msgs::NavSatStatus::STATUS_FIX) {
44+
ROS_WARN_STREAM_THROTTLE(1, "No GPS fix");
45+
return;
46+
}
3147

32-
g_lat_ref += msg->latitude;
33-
g_lon_ref += msg->longitude;
34-
g_alt_ref += msg->altitude;
48+
g_lat_ref += msg->latitude;
49+
g_lon_ref += msg->longitude;
50+
g_alt_ref += msg->altitude;
3551

36-
ROS_INFO("Current measurement: %f, %f, %f", msg->latitude, msg->longitude, msg->altitude);
52+
ROS_INFO("Current measurement: %f, %f, %f", msg->latitude, msg->longitude, msg->altitude);
3753

38-
if (count == g_its) {
39-
if (g_mode == MODE_AVERAGE) {
40-
g_lat_ref /= g_its;
41-
g_lon_ref /= g_its;
42-
g_alt_ref /= g_its;
43-
} else {
44-
g_lat_ref = msg->latitude;
45-
g_lon_ref = msg->longitude;
46-
g_alt_ref = msg->altitude;
47-
}
54+
if (count == g_its) {
55+
if (g_mode == MODE_AVERAGE) {
56+
g_lat_ref /= g_its;
57+
g_lon_ref /= g_its;
58+
g_alt_ref /= g_its;
59+
} else {
60+
g_lat_ref = msg->latitude;
61+
g_lon_ref = msg->longitude;
62+
g_alt_ref = msg->altitude;
63+
}
4864

49-
ros::NodeHandle nh;
50-
nh.setParam("/gps_ref_latitude", g_lat_ref);
51-
nh.setParam("/gps_ref_longitude", g_lon_ref);
52-
nh.setParam("/gps_ref_altitude", g_alt_ref);
53-
nh.setParam("/gps_ref_is_init", true);
5465

55-
ROS_INFO("Final reference position: %f, %f, %f", g_lat_ref, g_lon_ref, g_alt_ref);
66+
nh.setParam("/gps_ref_latitude", g_lat_ref);
67+
nh.setParam("/gps_ref_longitude", g_lon_ref);
68+
nh.setParam("/gps_ref_altitude", g_alt_ref);
69+
nh.setParam("/gps_ref_is_init", true);
5670

57-
ros::shutdown();
58-
return;
59-
} else {
60-
ROS_INFO(" Still waiting for %d measurements", g_its - count);
61-
}
71+
ROS_INFO("Final reference position: %f, %f, %f", g_lat_ref, g_lon_ref, g_alt_ref);
72+
73+
//ros::shutdown();
74+
return;
75+
} else {
76+
ROS_INFO(" Still waiting for %d measurements", g_its - count);
77+
}
6278

6379
count++;
80+
}
6481
}
6582

6683
int main(int argc, char** argv)
@@ -101,6 +118,7 @@ int main(int argc, char** argv)
101118
(g_mode == MODE_AVERAGE) ? "averaging to get the reference" : "taking the last as reference");
102119

103120
ros::Subscriber gps_sub = nh.subscribe("gps", 1, &gps_callback);
121+
ros::ServiceServer reset_srv = nh.advertiseService("reset_gps_reference", &reset_callback);
104122

105123
ros::spin();
106124
}

0 commit comments

Comments
 (0)