-
Notifications
You must be signed in to change notification settings - Fork 12
/
mars_wrapper_gps.cpp
235 lines (190 loc) · 8.58 KB
/
mars_wrapper_gps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (C) 2021 Christian Brommer, Control of Networked Systems, University of Klagenfurt, Austria.
//
// All rights reserved.
//
// This software is licensed under the terms of the BSD-2-Clause-License with
// no commercial use allowed, the full terms of which are made available
// in the LICENSE file. No license in patents is granted.
//
// You can contact the author at <christian.brommer@ieee.org>
#include "mars_wrapper_gps.h"
#include <mars/core_logic.h>
#include <mars/core_state.h>
#include <mars/sensors/gps/gps_measurement_type.h>
#include <mars/sensors/imu/imu_measurement_type.h>
#include <mars/sensors/imu/imu_sensor_class.h>
#include <mars/type_definitions/buffer_data_type.h>
#include <mars/type_definitions/buffer_entry_type.h>
#include <mars_msg_conv.h>
#include <ros/ros.h>
#include <sensor_msgs/Imu.h>
#include <sensor_msgs/NavSatFix.h>
#include <Eigen/Dense>
#include <iostream>
#include <string>
#include "mars_msg_conv.h"
using namespace mars;
MarsWrapperGps::MarsWrapperGps(ros::NodeHandle nh)
: reconfigure_cb_(boost::bind(&MarsWrapperGps::configCallback, this, _1, _2))
, p_wi_init_(0, 0, 0)
, q_wi_init_(Eigen::Quaterniond::Identity())
{
reconfigure_srv_.setCallback(reconfigure_cb_);
initialization_service_ = nh.advertiseService("init_service", &MarsWrapperGps::initServiceCallback, this);
// Read parameter
publish_on_propagation_ = nh.param<bool>("pub_on_prop", publish_on_propagation_);
use_ros_time_now_ = nh.param<bool>("use_ros_time_now", use_ros_time_now_);
verbose_output_ = nh.param<bool>("verbose", verbose_output_);
verbose_ooo_ = nh.param<bool>("verbose_out_of_order", verbose_ooo_);
discard_ooo_prop_meas_ = nh.param<bool>("discard_ooo_prop_meas", discard_ooo_prop_meas_);
pub_cb_buffer_size_ = uint32_t(nh.param<int>("pub_cb_buffer_size", int(pub_cb_buffer_size_)));
sub_imu_cb_buffer_size_ = uint32_t(nh.param<int>("sub_imu_cb_buffer_size", int(sub_imu_cb_buffer_size_)));
sub_sensor_cb_buffer_size_ = uint32_t(nh.param<int>("sub_sensor_cb_buffer_size", int(sub_sensor_cb_buffer_size_)));
std::cout << "Setup framework components" << std::endl;
// Framework components
imu_sensor_sptr_ = std::make_shared<mars::ImuSensorClass>("IMU");
core_states_sptr_ = std::make_shared<mars::CoreState>();
core_states_sptr_.get()->set_propagation_sensor(imu_sensor_sptr_);
core_logic_ = mars::CoreLogic(core_states_sptr_);
core_logic_.buffer_.set_max_buffer_size(800);
core_logic_.verbose_ = verbose_output_;
core_logic_.verbose_out_of_order_ = verbose_ooo_;
core_logic_.discard_ooo_prop_meas_ = discard_ooo_prop_meas_;
core_states_sptr_->set_noise_std(Eigen::Vector3d(0.013, 0.013, 0.013), Eigen::Vector3d(0.0013, 0.0013, 0.0013),
Eigen::Vector3d(0.083, 0.083, 0.083), Eigen::Vector3d(0.0083, 0.0083, 0.0083));
// Sensors
gps1_sensor_sptr_ = std::make_shared<mars::GpsSensorClass>("Gps1", core_states_sptr_);
{ // Limit scope of temp variables
Eigen::Matrix<double, 3, 1> gps_meas_std;
gps_meas_std << 2.0, 2.0, 2.0;
gps1_sensor_sptr_->R_ = gps_meas_std.cwiseProduct(gps_meas_std);
GpsSensorData gps_calibration;
gps_calibration.state_.p_ig_ = Eigen::Vector3d(0, 0, 0.10);
Eigen::Matrix<double, 9, 9> gps_cov;
gps_cov.setZero();
gps_cov.diagonal() << 1e-2, 1e-2, 1e-2, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001;
gps_calibration.sensor_cov_ = gps_cov;
gps1_sensor_sptr_->set_initial_calib(std::make_shared<GpsSensorData>(gps_calibration));
// TODO is set here for now, but will be managed by core logic in later versions
gps1_sensor_sptr_->const_ref_to_nav_ = true;
}
// Subscriber
sub_imu_measurement_ = nh.subscribe("imu_in", sub_imu_cb_buffer_size_, &MarsWrapperGps::ImuMeasurementCallback, this);
sub_gps1_measurement_ =
nh.subscribe("gps_in", sub_sensor_cb_buffer_size_, &MarsWrapperGps::GpsMeasurementCallback, this);
// Publisher
pub_ext_core_state_ = nh.advertise<mars_ros::ExtCoreState>("core_ext_state_out", pub_cb_buffer_size_);
pub_ext_core_state_lite_ = nh.advertise<mars_ros::ExtCoreStateLite>("core_ext_state_lite_out", pub_cb_buffer_size_);
pub_core_pose_state_ = nh.advertise<geometry_msgs::PoseStamped>("core_pose_state_out", pub_cb_buffer_size_);
pub_gps_state_ = nh.advertise<geometry_msgs::PoseWithCovarianceStamped>("gps_cal_state_out", pub_cb_buffer_size_);
}
bool MarsWrapperGps::init()
{
core_logic_.core_is_initialized_ = false;
core_logic_.buffer_.ResetBufferData();
gps1_sensor_sptr_->is_initialized_ = false;
common_gps_ref_is_set_ = false;
return true;
}
bool MarsWrapperGps::initServiceCallback(std_srvs::SetBool::Request& /*request*/,
std_srvs::SetBool::Response& /*response*/)
{
init();
ROS_INFO_STREAM("Initialized filter trough ROS Service");
return true;
}
void MarsWrapperGps::configCallback(mars_ros::marsConfig& config, uint32_t /*level*/)
{
// Config parameter overwrite
publish_on_propagation_ = config.pub_on_prop;
core_logic_.verbose_ = config.verbose;
verbose_output_ = config.verbose;
use_ros_time_now_ = config.use_ros_time_now;
if (config.initialize)
{
init();
ROS_INFO_STREAM("Initialized filter trough Reconfigure GUI");
}
config.initialize = false;
}
void MarsWrapperGps::set_common_gps_reference(const GpsCoordinates& reference)
{
if (!common_gps_ref_is_set_)
{
std::cout << "Setting the common GPS reference to: \n" << reference << std::endl;
gps1_sensor_sptr_->set_gps_reference_coordinates(reference);
common_gps_ref_is_set_ = true;
}
}
void MarsWrapperGps::ImuMeasurementCallback(const sensor_msgs::ImuConstPtr& meas)
{
// Map the measutement to the mars type
Time timestamp(meas->header.stamp.toSec());
// Generate a measurement data block
BufferDataType data;
data.set_sensor_data(std::make_shared<IMUMeasurementType>(MarsMsgConv::ImuMsgToImuMeas(*meas)));
// Call process measurement
core_logic_.ProcessMeasurement(imu_sensor_sptr_, timestamp, data);
// Initialize the first time at which the propagation sensor occures
if (!core_logic_.core_is_initialized_)
{
core_logic_.Initialize(p_wi_init_, q_wi_init_);
}
if (publish_on_propagation_)
{
mars::BufferEntryType latest_state;
core_logic_.buffer_.get_latest_state(&latest_state);
mars::CoreStateType latest_core_state = static_cast<mars::CoreType*>(latest_state.data_.core_.get())->state_;
pub_ext_core_state_.publish(
MarsMsgConv::ExtCoreStateToMsg(latest_state.timestamp_.get_seconds(), latest_core_state));
}
}
void MarsWrapperGps::GpsMeasurementCallback(const sensor_msgs::NavSatFixConstPtr& meas)
{
Time timestamp;
if (use_ros_time_now_)
{
timestamp = Time(ros::Time::now().toSec());
}
else
{
timestamp = Time(meas->header.stamp.toSec());
}
// Map the measurement to the mars sensor type
GpsMeasurementType gps_meas = MarsMsgConv::NavSatFixMsgToGpsMeas(*meas);
set_common_gps_reference(gps_meas.coordinates_);
GpsMeasurementUpdate(gps1_sensor_sptr_, gps_meas, timestamp);
}
void MarsWrapperGps::RunCoreStatePublisher()
{
mars::BufferEntryType latest_state;
core_logic_.buffer_.get_latest_state(&latest_state);
mars::CoreStateType latest_core_state = static_cast<mars::CoreType*>(latest_state.data_.core_.get())->state_;
pub_ext_core_state_.publish(MarsMsgConv::ExtCoreStateToMsg(latest_state.timestamp_.get_seconds(), latest_core_state));
pub_ext_core_state_lite_.publish(
MarsMsgConv::ExtCoreStateLiteToMsg(latest_state.timestamp_.get_seconds(), latest_core_state));
pub_core_pose_state_.publish(
MarsMsgConv::ExtCoreStateToPoseMsg(latest_state.timestamp_.get_seconds(), latest_core_state));
}
void MarsWrapperGps::GpsMeasurementUpdate(std::shared_ptr<mars::GpsSensorClass> sensor_sptr,
const GpsMeasurementType& gps_meas, const Time& timestamp)
{
// TMP feedback init pose
// p_wi_init_ = pose_meas.position_;
// q_wi_init_ = pose_meas.orientation_;
// Generate a measurement data block
BufferDataType data;
data.set_sensor_data(std::make_shared<GpsMeasurementType>(gps_meas));
// Call process measurement
if (!core_logic_.ProcessMeasurement(sensor_sptr, timestamp, data))
{
return;
}
// Publish the latest core state
this->RunCoreStatePublisher();
// Publish latest sensor state
mars::BufferEntryType latest_sensor_state;
core_logic_.buffer_.get_latest_sensor_handle_state(sensor_sptr, &latest_sensor_state);
mars::GpsSensorStateType gps_sensor_state = sensor_sptr.get()->get_state(latest_sensor_state.data_.sensor_);
pub_gps_state_.publish(MarsMsgConv::GpsStateToMsg(latest_sensor_state.timestamp_.get_seconds(), gps_sensor_state));
}