-
Notifications
You must be signed in to change notification settings - Fork 79
/
sensor.cpp
162 lines (142 loc) · 4.64 KB
/
sensor.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
// Copyright 2021, Steve Macenski
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include <fstream>
#include <sstream>
#include "ros2_ouster/client/client.h"
#include "ros2_ouster/exception.hpp"
#include "ros2_ouster/interfaces/metadata.hpp"
#include "ros2_ouster/sensor.hpp"
namespace sensor
{
Sensor::Sensor()
: SensorInterface() {}
Sensor::~Sensor()
{
_ouster_client.reset();
_lidar_packet.clear();
_imu_packet.clear();
}
void Sensor::reset(
ros2_ouster::Configuration & config,
rclcpp_lifecycle::LifecycleNode::SharedPtr node)
{
_ouster_client.reset();
configure(config, node);
}
void Sensor::configure(
ros2_ouster::Configuration & config,
rclcpp_lifecycle::LifecycleNode::SharedPtr node)
{
RCLCPP_INFO(
node->get_logger(),
"Configuring Ouster driver node.");
// Check the validity of some of the retrieved parameters
if (!ouster::sensor::lidar_mode_of_string(config.lidar_mode)) {
throw ros2_ouster::OusterDriverException(
"Invalid lidar mode: " + config.lidar_mode);
exit(-1);
}
if (!ouster::sensor::timestamp_mode_of_string(config.timestamp_mode)) {
throw ros2_ouster::OusterDriverException(
"Invalid timestamp mode: " + config.timestamp_mode);
exit(-1);
}
// Report to the user whether automatic address detection is being used, and
// what the source / destination IPs are
RCLCPP_INFO(
node->get_logger(),
"Connecting to sensor at %s.", config.lidar_ip.c_str());
if (config.computer_ip == "") {
RCLCPP_INFO(
node->get_logger(),
"Sending data from sensor to computer using automatic address detection");
} else {
RCLCPP_INFO(
node->get_logger(),
"Sending data from sensor to %s.", config.computer_ip.c_str());
}
_ouster_client = ouster::sensor::init_client(
config.lidar_ip,
config.computer_ip,
ouster::sensor::lidar_mode_of_string(config.lidar_mode),
ouster::sensor::timestamp_mode_of_string(config.timestamp_mode),
config.lidar_port,
config.imu_port);
if (!_ouster_client) {
throw ros2_ouster::OusterDriverException(
std::string("Failed to create connection to lidar."));
}
setMetadata(config.lidar_port, config.imu_port, config.timestamp_mode);
_lidar_packet.resize(this->getPacketFormat().lidar_packet_size + 1);
_imu_packet.resize(this->getPacketFormat().imu_packet_size + 1);
}
ouster::sensor::client_state Sensor::get()
{
const ouster::sensor::client_state state = ouster::sensor::poll_client(*_ouster_client);
if (state == ouster::sensor::client_state::EXIT) {
throw ros2_ouster::OusterDriverException(
std::string(
"Failed to get valid sensor data "
"information from lidar, returned exit!"));
} else if (state & ouster::sensor::client_state::CLIENT_ERROR) {
throw ros2_ouster::OusterDriverException(
std::string(
"Failed to get valid sensor data "
"information from lidar, returned error!"));
}
return state;
}
uint8_t * Sensor::readLidarPacket(const ouster::sensor::client_state & state)
{
if (state & ouster::sensor::client_state::LIDAR_DATA &&
ouster::sensor::read_lidar_packet(
*_ouster_client, _lidar_packet.data(),
this->getPacketFormat()))
{
return _lidar_packet.data();
}
return nullptr;
}
uint8_t * Sensor::readImuPacket(const ouster::sensor::client_state & state)
{
if (state & ouster::sensor::client_state::IMU_DATA &&
ouster::sensor::read_imu_packet(
*_ouster_client, _imu_packet.data(),
this->getPacketFormat()))
{
return _imu_packet.data();
}
return nullptr;
}
void Sensor::setMetadata(
int lidar_port, int imu_port,
const std::string & timestamp_mode)
{
if (_ouster_client) {
_metadata = ros2_ouster::Metadata(
ouster::sensor::parse_metadata(
ouster::sensor::get_metadata(*_ouster_client)),
imu_port, lidar_port, timestamp_mode);
}
ros2_ouster::populate_missing_metadata_defaults(_metadata);
}
ros2_ouster::Metadata Sensor::getMetadata()
{
return _metadata;
}
ouster::sensor::packet_format Sensor::getPacketFormat()
{
return ouster::sensor::get_format(getMetadata());
}
} // namespace sensor