23#include "modules/external_command/external_command_demo/proto/sweeper_custom_command.pb.h"
31 : command_id_(0), module_name_(
"demo") {}
34 action_command_client_ =
37 chassis_command_client_ =
40 free_space_command_client_ =
43 lane_follow_command_client_ =
46 "/apollo/external_command/lane_follow");
47 path_follow_command_client_ =
50 "/apollo/external_command/path_follow");
51 speed_command_client_ =
54 "/apollo/external_command/speed");
55 valet_parking_command_client_ =
58 "/apollo/external_command/valet_parking");
62 "/apollo/external_command/command_status");
64 "/apollo/modules/external_command/external_command_demo/conf/"
72 struct pollfd fd = {STDIN_FILENO, POLLIN, revent};
73 switch (poll(&fd, 1, 100)) {
75 std::cout <<
"Failed to read keyboard" << std::endl;
81 std::cin.getline(data, 50);
82 std::string input_command_string = data;
83 if (input_command_string ==
"pull_over") {
87 }
else if (input_command_string ==
"stop") {
90 }
else if (input_command_string ==
"start") {
93 }
else if (input_command_string ==
"clear") {
97 }
else if (input_command_string ==
"manual") {
101 }
else if (input_command_string ==
"auto") {
105 }
else if (input_command_string ==
"vin") {
108 }
else if (input_command_string ==
"enter_mission") {
112 }
else if (input_command_string ==
"exit_mission") {
116 }
else if (input_command_string ==
"chassis") {
117 SendVehicleSignalCommand();
118 }
else if (input_command_string ==
"custom_chassis") {
119 SendCustomChassisCommand();
120 }
else if (input_command_string.find(
"set_speed") != std::string::npos) {
121 auto index = input_command_string.find(
"set_speed");
122 std::string speed_value_string = input_command_string.substr(
123 index + std::string(
"set_speed").length(),
124 input_command_string.length());
125 if (!speed_value_string.empty()) {
126 double speed_value = std::atof(speed_value_string.c_str());
127 SendSpeedCommand(speed_value);
129 AWARN <<
"Input format is invalid, please input format like: "
132 }
else if (input_command_string ==
"increase_speed") {
133 double speed_factor = 1.2;
134 SendSpeedFactorCommand(speed_factor);
135 }
else if (input_command_string ==
"decrease_speed") {
136 double speed_factor = 0.8;
137 SendSpeedFactorCommand(speed_factor);
138 }
else if (input_command_string ==
"restore_speed") {
140 }
else if (input_command_string ==
"lane") {
143 way_point.set_x(0.0);
144 way_point.set_y(0.0);
145 way_point.set_heading(0.0);
146 std::vector<apollo::external_command::Pose> way_points;
147 way_points.emplace_back(way_point);
149 end_pose.set_x(10.0);
151 end_pose.set_heading(0.0);
152 SendLaneFollowCommand(way_points, end_pose,
154 }
else if (input_command_string ==
"path_loc") {
155 SendPathFollowCommandWithLocationRecord(
157 }
else if (input_command_string ==
"path_path") {
158 SendPathFollowCommandWithPathRecord(
160 }
else if (input_command_string ==
"valet_parking") {
161 std::string parking_spot_id =
"451089045";
162 SendValetParkingCommand(parking_spot_id, demo_config_.
target_speed());
163 }
else if (input_command_string.find(
"command_status") !=
167 const size_t start_pos = std::string(
"command_status=").length();
168 const size_t end_pos = input_command_string.length();
169 if (end_pos <= start_pos) {
171 <<
"Please check command status with format: command_status=XX!"
175 std::string command_id_string =
176 input_command_string.substr(start_pos, end_pos);
177 command_id_string.erase(
178 std::remove_if(command_id_string.begin(), command_id_string.end(),
180 command_id_string.end());
181 uint64_t
id = std::atoi(command_id_string.c_str());
182 CheckCommandStatus(
id);
183 }
else if (input_command_string ==
"free1") {
185 end_pose.set_x(437556.02);
186 end_pose.set_y(4432540.34);
187 end_pose.set_heading(1.8);
188 std::vector<apollo::external_command::Point> way_points;
193 point1.set_x(437536.29);
194 point1.set_y(4432560.69);
195 point2.set_x(437536.29);
196 point2.set_y(4432510.69);
197 point3.set_x(437576.29);
198 point3.set_y(4432510.69);
199 point4.set_x(437576.29);
200 point4.set_y(4432560.69);
201 way_points.emplace_back(point1);
202 way_points.emplace_back(point2);
203 way_points.emplace_back(point3);
204 way_points.emplace_back(point4);
206 SendFreespaceCommand(way_points, end_pose);
208 std::cout <<
"Invalid input!" << input_command_string << std::endl;
214void ExternalCommandDemo::SendActionCommand(
216 auto command = std::make_shared<apollo::external_command::ActionCommand>();
217 FillCommandHeader(command);
218 command->set_command(action_command_type);
219 std::cout <<
"Sending action command: " << command->DebugString()
221 auto response = action_command_client_->SendRequest(command);
222 if (
nullptr == response) {
223 std::cout <<
"Command sending failed, please check the service is on!\n"
226 std::cout <<
"******Finish sending command.******\n" << std::endl;
230void ExternalCommandDemo::SendVehicleSignalCommand() {
232 auto command = std::make_shared<apollo::external_command::ChassisCommand>();
233 FillCommandHeader(command);
234 command->mutable_basic_signal()->set_turn_signal(
236 std::cout <<
"Sending chassis command: " << command->DebugString()
238 auto response = chassis_command_client_->SendRequest(command);
239 if (
nullptr == response) {
240 std::cout <<
"Command sending failed, please check the service is on!\n"
243 std::cout <<
"******Finish sending command.******\n" << std::endl;
247void ExternalCommandDemo::SendCustomChassisCommand() {
249 auto command = std::make_shared<apollo::external_command::ChassisCommand>();
250 FillCommandHeader(command);
252 auto custom_operation = command->mutable_custom_operation();
255 sweeper_command.set_is_turn_on_brush(
true);
256 sweeper_command.set_sweeping_speed(2.0);
257 custom_operation->PackFrom(sweeper_command);
258 std::cout <<
"Sending custom chassis command: " << command->DebugString()
260 auto response = chassis_command_client_->SendRequest(command);
261 if (
nullptr == response) {
262 std::cout <<
"Command sending failed, please check the service is on!\n"
265 std::cout <<
"******Finish sending command.******\n" << std::endl;
269void ExternalCommandDemo::SendPathFollowCommandWithPathRecord(
270 const std::string& record_path) {
275 std::shared_ptr<apollo::external_command::PathFollowCommand>
276 path_follow_command =
277 std::make_shared<apollo::external_command::PathFollowCommand>();
278 ReadPathFromPathRecord(record_path, path_follow_command->mutable_way_point());
280 FillCommandHeader(path_follow_command);
282 auto path_boundary = path_follow_command->mutable_boundary_with_width();
286 path_follow_command->set_target_speed(demo_config_.
target_speed());
287 auto response = path_follow_command_client_->SendRequest(path_follow_command);
288 if (
nullptr == response) {
289 std::cout <<
"Command sending failed, please check the service is on!\n"
292 std::cout <<
"******Finish sending command.******\n" << std::endl;
296void ExternalCommandDemo::SendPathFollowCommandWithLocationRecord(
297 const std::string& record_dir) {
300 std::shared_ptr<apollo::external_command::PathFollowCommand>
301 path_follow_command =
302 std::make_shared<apollo::external_command::PathFollowCommand>();
303 std::vector<std::string> record_files =
305 std::sort(record_files.begin(), record_files.end());
306 std::string dir_prefix = record_dir +
'/';
307 for (
const auto file_name : record_files) {
308 ReadPathFromLocationRecord(dir_prefix + file_name,
309 path_follow_command->mutable_way_point());
313 FillCommandHeader(path_follow_command);
315 auto path_boundary = path_follow_command->mutable_boundary_with_width();
319 path_follow_command->set_target_speed(demo_config_.
target_speed());
320 auto response = path_follow_command_client_->SendRequest(path_follow_command);
321 if (
nullptr == response) {
322 std::cout <<
"Command sending failed, please check the service is on!\n"
325 std::cout <<
"******Finish sending command.******\n" << std::endl;
329void ExternalCommandDemo::SendSpeedCommand(
double speed) {
330 auto command = std::make_shared<apollo::external_command::SpeedCommand>();
331 FillCommandHeader(command);
332 command->set_target_speed(speed);
333 std::cout <<
"Sending speed command: " << command->DebugString() << std::endl;
334 auto response = speed_command_client_->SendRequest(command);
335 if (
nullptr == response) {
336 std::cout <<
"Command sending failed, please check the service is on!\n"
339 std::cout <<
"******Finish sending command.******\n" << std::endl;
343void ExternalCommandDemo::SendSpeedFactorCommand(
double speed_factor) {
344 auto command = std::make_shared<apollo::external_command::SpeedCommand>();
345 FillCommandHeader(command);
346 command->set_target_speed_factor(speed_factor);
347 std::cout <<
"Sending speed factor command: " << command->DebugString()
349 auto response = speed_command_client_->SendRequest(command);
350 if (
nullptr == response) {
351 std::cout <<
"Command sending failed, please check the service is on!\n"
354 std::cout <<
"******Finish sending command.******\n" << std::endl;
358void ExternalCommandDemo::RestoreSpeed() {
359 auto command = std::make_shared<apollo::external_command::SpeedCommand>();
360 FillCommandHeader(command);
361 command->set_is_restore_target_speed(
true);
362 std::cout <<
"Restore speed: " << command->DebugString() << std::endl;
363 auto response = speed_command_client_->SendRequest(command);
364 if (
nullptr == response) {
365 std::cout <<
"Command sending failed, please check the service is on!\n"
368 std::cout <<
"******Finish sending command.******\n" << std::endl;
372void ExternalCommandDemo::SendLaneFollowCommand(
373 const std::vector<apollo::external_command::Pose>& way_points,
376 std::make_shared<apollo::external_command::LaneFollowCommand>();
377 FillCommandHeader(command);
379 for (
const auto& point : way_points) {
380 auto way_point = command->add_way_point();
381 way_point->CopyFrom(point);
384 command->mutable_end_pose()->CopyFrom(end);
385 if (target_speed > 0.0) {
386 command->set_target_speed(target_speed);
388 std::cout <<
"Sending lane follow command: " << command->DebugString()
390 auto response = lane_follow_command_client_->SendRequest(command);
391 if (
nullptr == response) {
392 std::cout <<
"Command sending failed, please check the service is on!\n"
395 std::cout <<
"******Finish sending command.******\n" << std::endl;
399void ExternalCommandDemo::SendFreespaceCommand(
400 const std::vector<apollo::external_command::Point>& way_points,
402 auto command = std::make_shared<apollo::external_command::FreeSpaceCommand>();
403 FillCommandHeader(command);
405 auto roi_point = command->mutable_drivable_roi();
406 for (
const auto& point : way_points) {
407 roi_point->add_point()->CopyFrom(point);
410 command->mutable_parking_spot_pose()->CopyFrom(end);
411 std::cout <<
"Sending lane follow command: " << command->DebugString()
413 auto response = free_space_command_client_->SendRequest(command);
414 if (
nullptr == response) {
415 std::cout <<
"Command sending failed, please check the service is on!\n"
418 std::cout <<
"******Finish sending command.******\n" << std::endl;
421void ExternalCommandDemo::SendValetParkingCommand(
422 const std::string& parking_spot_id,
double target_speed) {
424 std::make_shared<apollo::external_command::ValetParkingCommand>();
425 FillCommandHeader(command);
426 command->set_parking_spot_id(parking_spot_id);
427 if (target_speed > 0.0) {
428 command->set_target_speed(target_speed);
430 std::cout <<
"Sending valet parking command: " << command->DebugString()
432 auto response = valet_parking_command_client_->SendRequest(command);
433 if (
nullptr == response) {
434 std::cout <<
"Command sending failed, please check the service is on!\n"
437 std::cout <<
"******Finish sending command.******\n" << std::endl;
441void ExternalCommandDemo::ReadPathFromPathRecord(
442 const std::string& record_file,
443 google::protobuf::RepeatedPtrField<apollo::external_command::Point>*
445 std::cout <<
"ReadPathFromPathRecord: " << record_file << std::endl;
447 if (!reader.IsValid()) {
448 std::cout <<
"Fail to open " << record_file << std::endl;
454 while (reader.ReadMessage(&message)) {
456 if (planning_trajectory.ParseFromString(message.
content)) {
463 for (
const auto input_point : trajectory_points) {
464 auto output_point = waypoints->Add();
465 output_point->set_x(input_point.path_point().x());
466 output_point->set_y(input_point.path_point().y());
470void ExternalCommandDemo::CheckCommandStatus(
const uint64_t command_id) {
472 std::make_shared<apollo::external_command::CommandStatusRequest>();
473 FillCommandHeader(command);
474 command->set_command_id(command_id);
475 std::cout <<
"Sending check command: " << command->DebugString() << std::endl;
476 auto response = status_client_->SendRequest(command);
477 if (
nullptr == response) {
478 std::cout <<
"Check status failed!\n" << std::endl;
480 std::cout << response->DebugString();
481 std::cout <<
"******Finish checking command.******\n" << std::endl;
485void ExternalCommandDemo::ReadPathFromLocationRecord(
486 const std::string& record_file,
487 google::protobuf::RepeatedPtrField<apollo::external_command::Point>*
489 std::cout <<
"ReadPathFromLocationRecord: " << record_file << std::endl;
491 if (!reader.IsValid()) {
492 std::cout <<
"Fail to open " << record_file << std::endl;
499 bool is_last_poistion_set =
false;
500 while (reader.ReadMessage(&message)) {
501 if (message.
channel_name ==
"/apollo/localization/pose") {
502 if (localization.ParseFromString(message.
content)) {
504 if (!is_last_poistion_set) {
505 last_x = position.
x();
506 last_y = position.y();
507 is_last_poistion_set =
true;
513 auto output_point = waypoints->Add();
514 output_point->set_x(position.x());
515 output_point->set_y(position.y());
516 last_x = position.x();
517 last_y = position.y();
bool Proc() override
The Proc logic of the component, which called by the CyberRT frame.
std::shared_ptr< Node > node_
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
std::vector< std::string > ListSubPaths(const std::string &directory_path, const unsigned char d_type)
List sub-paths.
T abs(const T &x)
Get absolute value of x
Basic data struct of record message.
std::string content
The content of the message.
std::string channel_name
The channel name of the message.
optional string file_of_path_follow_with_planning_record
optional double min_distance_error
optional string file_of_path_follow_with_localization_record
optional double right_path_width
optional double left_path_width
optional double target_speed
optional apollo::localization::Pose pose
optional apollo::common::PointENU position
repeated apollo::common::TrajectoryPoint trajectory_point