20#include <unordered_map>
22#include <boost/algorithm/string.hpp>
23#include <boost/format.hpp>
34 AINFO <<
"start to init MotionService.";
35 vehicle_planemotion_ =
new PlaneMotion(motion_buffer_size_);
40 AINFO <<
"load lane detection component proto param failed";
44 std::string camera_names_str = motion_service_param.
camera_names();
45 boost::algorithm::split(camera_names_, camera_names_str,
46 boost::algorithm::is_any_of(
","));
48 std::string input_camera_channel_names_str =
50 boost::algorithm::split(input_camera_channel_names_,
51 input_camera_channel_names_str,
52 boost::algorithm::is_any_of(
","));
53 if (input_camera_channel_names_.size() != camera_names_.size()) {
54 AERROR <<
"wrong input_camera_channel_names_.size(): "
55 << input_camera_channel_names_.size();
60 const std::string &channel_name_img = input_camera_channel_names_[0];
61 const std::string &camera_name = camera_names_[0];
62 std::function<void(
const ImageMsgType &)> camera_callback =
63 std::bind(&MotionServiceComponent::OnReceiveImage,
this,
64 std::placeholders::_1, camera_name);
65 auto camera_reader =
node_->CreateReader(channel_name_img, camera_callback);
68 const std::string &channel_name_local =
71 std::bind(&MotionServiceComponent::OnLocalization,
this,
72 std::placeholders::_1);
73 auto localization_reader =
74 node_->CreateReader(channel_name_local, localization_callback);
79 AINFO <<
"init MotionService success.";
84void MotionServiceComponent::OnReceiveImage(
const ImageMsgType &message,
85 const std::string &camera_name) {
86 std::lock_guard<std::mutex> lock(mutex_);
87 const double curr_timestamp = message->measurement_time() + timestamp_offset_;
88 ADEBUG <<
"image received: camera_name: " << camera_name
89 <<
" image ts: " << curr_timestamp;
90 camera_timestamp_ = curr_timestamp;
95void MotionServiceComponent::OnLocalization(
97 std::lock_guard<std::mutex> lock(mutex_);
98 ADEBUG <<
"localization received: localization ts: "
99 << message->measurement_time();
100 const auto &velocity = message->pose().linear_velocity();
103 float velx =
static_cast<float>(velocity.x());
104 float vely =
static_cast<float>(velocity.y());
105 float velz =
static_cast<float>(velocity.z());
107 static_cast<float>(sqrt(velx * velx + vely * vely + velz * velz));
111 double timestamp_diff = 0;
116 vehicle_status.
time_d = 0;
121 static_cast<float>(message->pose().angular_velocity_vrf().y());
123 static_cast<float>(-message->pose().angular_velocity_vrf().x());
125 static_cast<float>(message->pose().angular_velocity_vrf().z());
126 timestamp_diff = message->measurement_time() - pre_timestamp_;
127 vehicle_status.
time_d = timestamp_diff;
128 vehicle_status.
time_ts = message->measurement_time();
131 pre_timestamp_ = message->measurement_time();
135 double camera_timestamp = 0;
136 camera_timestamp = camera_timestamp_;
137 ADEBUG <<
"motion processed for camera timestamp: " << camera_timestamp;
140 if (std::abs(camera_timestamp - pre_camera_timestamp_) <
141 std::numeric_limits<double>::epsilon()) {
142 ADEBUG <<
"Motion_status: accum";
146 }
else if (camera_timestamp > pre_camera_timestamp_) {
147 ADEBUG <<
"Motion_status: accum_push";
149 pre_camera_timestamp_, camera_timestamp,
151 PublishEvent(camera_timestamp);
153 ADEBUG <<
"Motion_status: pop";
160 pre_camera_timestamp_ = camera_timestamp;
165void MotionServiceComponent::PublishEvent(
const double timestamp) {
167 std::shared_ptr<apollo::perception::MotionService> motion_service_msg(
172 header->set_timestamp_sec(timestamp);
173 header->set_module_name(
"motion_service");
177 for (
int k =
static_cast<int>(motion_buffer.size()) - 1; k >= 0; k--) {
179 motion_service_msg->add_vehicle_status();
180 ConvertVehicleMotionToMsgOut(motion_buffer.at(k), v_status_msg);
182 writer_->Write(motion_service_msg);
186void MotionServiceComponent::ConvertVehicleMotionToMsgOut(
188 v_status_msg->set_roll_rate(vs.roll_rate);
189 v_status_msg->set_pitch_rate(vs.pitch_rate);
190 v_status_msg->set_yaw_rate(vs.yaw_rate);
191 v_status_msg->set_velocity(vs.velocity);
192 v_status_msg->set_velocity_x(vs.velocity_x);
193 v_status_msg->set_velocity_y(vs.velocity_y);
194 v_status_msg->set_velocity_z(vs.velocity_z);
195 v_status_msg->set_time_ts(vs.time_ts);
196 v_status_msg->set_time_d(vs.time_d);
198 motion_msg->set_m00(vs.motion(0, 0));
199 motion_msg->set_m01(vs.motion(0, 1));
200 motion_msg->set_m02(vs.motion(0, 2));
201 motion_msg->set_m03(vs.motion(0, 3));
202 motion_msg->set_m10(vs.motion(1, 0));
203 motion_msg->set_m11(vs.motion(1, 1));
204 motion_msg->set_m12(vs.motion(1, 2));
205 motion_msg->set_m13(vs.motion(1, 3));
206 motion_msg->set_m20(vs.motion(2, 0));
207 motion_msg->set_m21(vs.motion(2, 1));
208 motion_msg->set_m22(vs.motion(2, 2));
209 motion_msg->set_m23(vs.motion(2, 3));
210 motion_msg->set_m30(vs.motion(3, 0));
211 motion_msg->set_m31(vs.motion(3, 1));
212 motion_msg->set_m32(vs.motion(3, 2));
213 motion_msg->set_m33(vs.motion(3, 3));
218 std::lock_guard<std::mutex> lock(motion_mutex_);
223 return pre_camera_timestamp_;
bool GetProtoConfig(T *config) const
std::shared_ptr< Node > node_
base::MotionBuffer GetMotionBuffer()
double GetLatestTimestamp()
bool GetMotionInformation(double timestamp, base::VehicleStatus *vs)
bool find_motion_with_timestamp(double timestamp, base::VehicleStatus *vs)
base::MotionBuffer get_buffer()
void add_new_motion(double pre_image_timestamp, double image_timestamp, int motion_operation_flag, base::VehicleStatus *vehicledata)
Math-related util functions.
boost::circular_buffer< VehicleStatus > MotionBuffer
std::shared_ptr< localization::LocalizationEstimate > LocalizationMsgType
std::shared_ptr< apollo::drivers::Image > ImageMsgType
optional string input_camera_channel_names
optional string input_localization_channel_name
optional string output_topic_channel_name
optional string camera_names