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;
10
+ int count = 1 ;
11
+ bool gps_ref_is_init;
9
12
10
13
std::vector<double > g_pressure_heights;
11
14
int g_its;
@@ -18,49 +21,63 @@ enum EMode
18
21
// average over, or wait for, n GPS fixes
19
22
EMode g_mode;
20
23
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
+ }
21
34
22
35
void gps_callback (const sensor_msgs::NavSatFixConstPtr & msg)
23
36
{
37
+ ros::NodeHandle nh;
38
+ nh.getParam (" /gps_ref_is_init" , gps_ref_is_init);
39
+
40
+ if (!gps_ref_is_init){
24
41
25
- static int count = 1 ;
26
42
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
+ }
31
47
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 ;
35
51
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 );
37
53
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
+ }
48
64
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 );
54
65
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 );
56
70
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
+ }
62
78
63
79
count++;
80
+ }
64
81
}
65
82
66
83
int main (int argc, char ** argv)
@@ -101,6 +118,7 @@ int main(int argc, char** argv)
101
118
(g_mode == MODE_AVERAGE) ? " averaging to get the reference" : " taking the last as reference" );
102
119
103
120
ros::Subscriber gps_sub = nh.subscribe (" gps" , 1 , &gps_callback);
121
+ ros::ServiceServer reset_srv = nh.advertiseService (" reset_gps_reference" , &reset_callback);
104
122
105
123
ros::spin ();
106
124
}
0 commit comments