-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi_ekf_node.cpp
188 lines (165 loc) · 6.03 KB
/
wifi_ekf_node.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
#include <ros/ros.h>
#include <sensor_msgs/Imu.h>
#include <sensor_msgs/PointCloud.h>
#include <nav_msgs/Path.h>
#include <nav_msgs/Odometry.h>
#include <geometry_msgs/PoseStamped.h>
#include <sensor_msgs/Imu.h>
#include <sensor_msgs/PointCloud.h>
#include "wifi_ekf.h"
//simulation
#define G_CNT 0
//experiments
//#define G_CNT 500
#include <queue>
using namespace std;
queue<sensor_msgs::Imu> imu_buf;
queue<sensor_msgs::PointCloud> wifi_buf;
ros::Publisher pub_path;
ros::Publisher pub_odometry;
ros::Publisher pub_pose;
//ros::Publisher pub_ap;
ros::Publisher pub_v;
ros::Publisher pub_p;
nav_msgs::Path path;
int gravity_cnt = 0;
Eigen::Vector3d sum_g(0.0, 0.0, 0.0);
bool filter_start = false;
WifiEkf wifi_ekf;
void imu_callback(const sensor_msgs::ImuConstPtr &imu_msg)
{
Eigen::Vector3d acc(imu_msg->linear_acceleration.x,
imu_msg->linear_acceleration.y,
imu_msg->linear_acceleration.z);
if (gravity_cnt < G_CNT)
{
sum_g += acc;
gravity_cnt++;
return;
}
else if (gravity_cnt == G_CNT)
{
sum_g += acc;
gravity_cnt++;
//for simulation
wifi_ekf.init(imu_msg->header.stamp.toSec(), Eigen::Vector3d(0.0, 0.0, -9.8));
//for experiments
//wifi_ekf.init(imu_msg->header.stamp.toSec(), sum_g/gravity_cnt);
filter_start = true;
return;
}
imu_buf.push(*imu_msg);
}
void wifi_callback(const sensor_msgs::PointCloudConstPtr &wifi_msg)
{
wifi_buf.push(*wifi_msg);
}
void process()
{
if (!filter_start || imu_buf.empty() || wifi_buf.empty())
return;
if (wifi_buf.front().header.stamp.toSec() < imu_buf.front().header.stamp.toSec())
{
wifi_buf.pop();
return;
}
if (wifi_buf.front().header.stamp.toSec() >= imu_buf.back().header.stamp.toSec())
{
return;
}
sensor_msgs::PointCloud wifi_msg = wifi_buf.front();
wifi_buf.pop();
ROS_DEBUG("IMU integration");
double check_t = 0.0;
while (imu_buf.front().header.stamp.toSec() <= wifi_msg.header.stamp.toSec())
{
check_t = imu_buf.front().header.stamp.toSec();
wifi_ekf.predict(check_t,
Eigen::Vector3d(imu_buf.front().linear_acceleration.x,
imu_buf.front().linear_acceleration.y,
imu_buf.front().linear_acceleration.z),
Eigen::Vector3d(imu_buf.front().angular_velocity.x,
imu_buf.front().angular_velocity.y,
imu_buf.front().angular_velocity.z)
);
imu_buf.pop();
}
ROS_DEBUG("Time shift: %f", std::fabs(check_t - wifi_msg.header.stamp.toSec()));
Eigen::Vector3d disp_wifi;
disp_wifi(0) = wifi_msg.points[0].x;
disp_wifi(1) = wifi_msg.points[0].y;
disp_wifi(2) = wifi_msg.points[0].z;
ROS_INFO_STREAM("published disp: " << disp_wifi.transpose());
wifi_ekf.update(disp_wifi);
ROS_INFO_STREAM("wifi position: " << wifi_ekf.p.transpose());
ROS_INFO_STREAM("wifi velocity: " << wifi_ekf.v.transpose());
//ROS_INFO_STREAM("ap: " << wifi_ekf.ap.transpose());
ROS_INFO_STREAM("gravity: " << wifi_ekf.g.transpose() << " norm: " << wifi_ekf.g.norm());
ROS_INFO_STREAM("cov: " << wifi_ekf.P.diagonal().transpose());
geometry_msgs::Point32 p_v;
p_v.x = wifi_ekf.v(0);
p_v.y = wifi_ekf.v(1);
p_v.z = wifi_ekf.v(2);
pub_v.publish(p_v);
geometry_msgs::Point32 p_p;
p_p.x = wifi_ekf.p(0);
p_p.y = wifi_ekf.p(1);
p_p.z = wifi_ekf.p(2);
pub_p.publish(p_p);
nav_msgs::Odometry odometry;
//sensor_msgs::PointCloud point_cloud;
//geometry_msgs::Point32 p_ap;
//p_ap.x = wifi_ekf.ap(0);
//p_ap.y = wifi_ekf.ap(1);
//p_ap.z = wifi_ekf.ap(2);
//point_cloud.header.stamp = ros::Time::now();
//point_cloud.header.frame_id = "world";
//point_cloud.points.push_back(p_ap);
//pub_ap.publish(point_cloud);
//point_cloud.points.clear();
odometry.header.stamp = ros::Time::now();
odometry.header.frame_id = "world";
odometry.pose.pose.position.x = wifi_ekf.p(0);
odometry.pose.pose.position.y = wifi_ekf.p(1);
odometry.pose.pose.position.z = wifi_ekf.p(2);
odometry.pose.pose.orientation.x = Eigen::Quaterniond(wifi_ekf.q).x();
odometry.pose.pose.orientation.y = Eigen::Quaterniond(wifi_ekf.q).y();
odometry.pose.pose.orientation.z = Eigen::Quaterniond(wifi_ekf.q).z();
odometry.pose.pose.orientation.w = Eigen::Quaterniond(wifi_ekf.q).w();
odometry.twist.twist.linear.x = wifi_ekf.v(0);
odometry.twist.twist.linear.y = wifi_ekf.v(1);
odometry.twist.twist.linear.z = wifi_ekf.v(2);
pub_odometry.publish(odometry);
geometry_msgs::PoseStamped pose_stamped;
pose_stamped.header.stamp = ros::Time::now();
pose_stamped.header.frame_id = "world";
pose_stamped.pose = odometry.pose.pose;
path.poses.push_back(pose_stamped);
pub_path.publish(path);
pub_pose.publish(pose_stamped);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "wifi_ekf");
ros::NodeHandle n("~");
//ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug);
pub_path = n.advertise<nav_msgs::Path>("path", 1000);
pub_odometry = n.advertise<nav_msgs::Odometry>("odometry", 1000);
pub_pose = n.advertise<geometry_msgs::PoseStamped>("pose", 1000);
//pub_ap = n.advertise<sensor_msgs::PointCloud>("ap", 1000);
pub_v = n.advertise<geometry_msgs::Point32>("velocity", 1000);
pub_p = n.advertise<geometry_msgs::Point32>("position", 1000);
path.header.frame_id = "world";
ros::Subscriber sub_imu = n.subscribe("/data_generator/imu", 1000, imu_callback);
//ros::Subscriber sub_imu = n.subscribe("/imu_3dm_gx4/imu", 1000, imu_callback);
ros::Subscriber sub_wifi = n.subscribe("/data_generator/wifi", 1000, wifi_callback);
//ros::Subscriber sub_wifi = n.subscribe("/wifi", 1000, wifi_callback);
ros::Rate r(1000);
while (ros::ok())
{
process();
ros::spinOnce();
r.sleep();
}
//ros::spin();
}