-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessIMU.cpp
More file actions
68 lines (53 loc) · 2.25 KB
/
processIMU.cpp
File metadata and controls
68 lines (53 loc) · 2.25 KB
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
#include <nlohmann/json.hpp>
#include <Eigen/Core>
#include <iostream>
#include <fstream>
using json = nlohmann::json;
void readIMU(const std::string &infile, Eigen::VectorXd &t, Eigen::VectorXd &ax, Eigen::VectorXd &ay,
Eigen::VectorXd &az, Eigen::VectorXd &wx, Eigen::VectorXd &wy, Eigen::VectorXd &wz)
{
std::ifstream ifs(infile, std::ifstream::in);
std::vector<double> _t, _ax, _ay, _az, _wx, _wy, _wz;
std::string line;
while (std::getline(ifs, line)) {
json data = json::parse(line);
if (!data.contains("msg")) {
continue;
}
if (data["msg"].get<std::string>().compare("imu_raw") == 0) {
double stamp = data["stamp"].get<double>();
unsigned long seq = data["seq"].get<unsigned long>();
_t.push_back(data["stamp"].get<double>());
_ax.push_back(data["ax"].get<double>());
_ay.push_back(data["ay"].get<double>());
_az.push_back(data["az"].get<double>());
_wx.push_back(data["wx"].get<double>());
_wy.push_back(data["wy"].get<double>());
_wz.push_back(data["wz"].get<double>());
}
}
t = Eigen::Map<Eigen::VectorXd>(_t.data(), _t.size());
ax = Eigen::Map<Eigen::VectorXd>(_ax.data(), _ax.size());
ay = Eigen::Map<Eigen::VectorXd>(_ay.data(), _ay.size());
az = Eigen::Map<Eigen::VectorXd>(_az.data(), _az.size());
wx = Eigen::Map<Eigen::VectorXd>(_wx.data(), _wx.size());
wy = Eigen::Map<Eigen::VectorXd>(_wy.data(), _wy.size());
wz = Eigen::Map<Eigen::VectorXd>(_wz.data(), _wz.size());
}
int main(int argc, char *argv[])
{
if (argc <= 1) {
std::cout << "Input file required!" << std::endl;
std::cout << "Usage: processIMU <input file>" << std::endl;
return -1;
}
Eigen::VectorXd t, ax, ay, az, wx, wy, wz;
readIMU(std::string(argv[1]), t, ax, ay, az, wx, wy, wz);
std::cout << "Loaded " << t.size() << " IMU messages." << std::endl;
// process IMU data
// compute norm of the acceleration vector
//a = np.sqrt(ax*ax + ay*ay + az*az)
Eigen::VectorXd a = (ax.array() * ax.array() + ay.array() * ay.array() + az.array() * az.array()).cwiseSqrt();
std::cout << "a = " << std::endl << a << std::endl;
return 0;
}