2
2
#include < sensor_msgs/NavSatFix.h>
3
3
#include < string>
4
4
#include < vector>
5
+ #include < std_srvs/Empty.h>
5
6
6
7
double g_lat_ref;
7
8
double g_lon_ref;
8
9
double g_alt_ref;
9
-
10
- std::vector< double > g_pressure_heights ;
10
+ int g_count = 1 ;
11
+ bool gps_ref_is_init ;
11
12
int g_its;
12
13
13
14
enum EMode
@@ -18,49 +19,62 @@ enum EMode
18
19
// average over, or wait for, n GPS fixes
19
20
EMode g_mode;
20
21
22
+ bool reset_callback (std_srvs::Empty::Request&, std_srvs::Empty::Response&)
23
+ {
24
+ g_count = 1 ;
25
+ g_lat_ref = 0.0 ;
26
+ g_lon_ref = 0.0 ;
27
+ g_alt_ref = 0.0 ;
28
+ ros::NodeHandle nh;
29
+ nh.setParam (" /gps_ref_is_init" , false );
30
+ return true ;
31
+ }
21
32
22
33
void gps_callback (const sensor_msgs::NavSatFixConstPtr & msg)
23
34
{
35
+ ros::NodeHandle nh;
36
+ nh.getParam (" /gps_ref_is_init" , gps_ref_is_init);
24
37
25
- static int count = 1 ;
38
+ if (!gps_ref_is_init){
26
39
27
- if (msg->status .status < sensor_msgs::NavSatStatus::STATUS_FIX) {
28
- ROS_WARN_STREAM_THROTTLE (1 , " No GPS fix" );
29
- return ;
30
- }
31
40
32
- g_lat_ref += msg->latitude ;
33
- g_lon_ref += msg->longitude ;
34
- g_alt_ref += msg->altitude ;
41
+ if (msg->status .status < sensor_msgs::NavSatStatus::STATUS_FIX) {
42
+ ROS_WARN_STREAM_THROTTLE (1 , " No GPS fix" );
43
+ return ;
44
+ }
35
45
36
- ROS_INFO (" Current measurement: %f, %f, %f" , msg->latitude , msg->longitude , msg->altitude );
46
+ g_lat_ref += msg->latitude ;
47
+ g_lon_ref += msg->longitude ;
48
+ g_alt_ref += msg->altitude ;
37
49
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
- }
50
+ ROS_INFO (" Current measurement: %f, %f, %f" , msg->latitude , msg->longitude , msg->altitude );
48
51
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 );
52
+ if (g_count == g_its) {
53
+ if (g_mode == MODE_AVERAGE) {
54
+ g_lat_ref /= g_its;
55
+ g_lon_ref /= g_its;
56
+ g_alt_ref /= g_its;
57
+ } else {
58
+ g_lat_ref = msg->latitude ;
59
+ g_lon_ref = msg->longitude ;
60
+ g_alt_ref = msg->altitude ;
61
+ }
54
62
55
- ROS_INFO (" Final reference position: %f, %f, %f" , g_lat_ref, g_lon_ref, g_alt_ref);
56
63
57
- ros::shutdown ();
58
- return ;
59
- } else {
60
- ROS_INFO (" Still waiting for %d measurements" , g_its - count);
61
- }
64
+ nh.setParam (" /gps_ref_latitude" , g_lat_ref);
65
+ nh.setParam (" /gps_ref_longitude" , g_lon_ref);
66
+ nh.setParam (" /gps_ref_altitude" , g_alt_ref);
67
+ nh.setParam (" /gps_ref_is_init" , true );
68
+
69
+ ROS_INFO (" Final reference position: %f, %f, %f" , g_lat_ref, g_lon_ref, g_alt_ref);
62
70
63
- count++;
71
+ return ;
72
+ } else {
73
+ ROS_INFO (" Still waiting for %d measurements" , g_its - g_count);
74
+ }
75
+
76
+ g_count++;
77
+ }
64
78
}
65
79
66
80
int main (int argc, char ** argv)
@@ -101,6 +115,7 @@ int main(int argc, char** argv)
101
115
(g_mode == MODE_AVERAGE) ? " averaging to get the reference" : " taking the last as reference" );
102
116
103
117
ros::Subscriber gps_sub = nh.subscribe (" gps" , 1 , &gps_callback);
118
+ ros::ServiceServer reset_srv = nh.advertiseService (" reset_gps_reference" , &reset_callback);
104
119
105
120
ros::spin ();
106
121
}
0 commit comments