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 "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::string dir_prefix = record_dir +
'/';
306 for (
const auto file_name : record_files) {
307 ReadPathFromLocationRecord(dir_prefix + file_name,
308 path_follow_command->mutable_way_point());
312 FillCommandHeader(path_follow_command);
314 auto path_boundary = path_follow_command->mutable_boundary_with_width();
318 path_follow_command->set_target_speed(demo_config_.
target_speed());
319 auto response = path_follow_command_client_->SendRequest(path_follow_command);
320 if (
nullptr == response) {
321 std::cout <<
"Command sending failed, please check the service is on!\n"
324 std::cout <<
"******Finish sending command.******\n" << std::endl;
328void ExternalCommandDemo::SendSpeedCommand(
double speed) {
329 auto command = std::make_shared<apollo::external_command::SpeedCommand>();
330 FillCommandHeader(command);
331 command->set_target_speed(speed);
332 std::cout <<
"Sending speed command: " << command->DebugString() << std::endl;
333 auto response = speed_command_client_->SendRequest(command);
334 if (
nullptr == response) {
335 std::cout <<
"Command sending failed, please check the service is on!\n"
338 std::cout <<
"******Finish sending command.******\n" << std::endl;
342void ExternalCommandDemo::SendSpeedFactorCommand(
double speed_factor) {
343 auto command = std::make_shared<apollo::external_command::SpeedCommand>();
344 FillCommandHeader(command);
345 command->set_target_speed_factor(speed_factor);
346 std::cout <<
"Sending speed factor command: " << command->DebugString()
348 auto response = speed_command_client_->SendRequest(command);
349 if (
nullptr == response) {
350 std::cout <<
"Command sending failed, please check the service is on!\n"
353 std::cout <<
"******Finish sending command.******\n" << std::endl;
357void ExternalCommandDemo::RestoreSpeed() {
358 auto command = std::make_shared<apollo::external_command::SpeedCommand>();
359 FillCommandHeader(command);
360 command->set_is_restore_target_speed(
true);
361 std::cout <<
"Restore speed: " << command->DebugString() << std::endl;
362 auto response = speed_command_client_->SendRequest(command);
363 if (
nullptr == response) {
364 std::cout <<
"Command sending failed, please check the service is on!\n"
367 std::cout <<
"******Finish sending command.******\n" << std::endl;
371void ExternalCommandDemo::SendLaneFollowCommand(
372 const std::vector<apollo::external_command::Pose>& way_points,
375 std::make_shared<apollo::external_command::LaneFollowCommand>();
376 FillCommandHeader(command);
378 for (
const auto& point : way_points) {
379 auto way_point = command->add_way_point();
380 way_point->CopyFrom(point);
383 command->mutable_end_pose()->CopyFrom(end);
384 if (target_speed > 0.0) {
385 command->set_target_speed(target_speed);
387 std::cout <<
"Sending lane follow command: " << command->DebugString()
389 auto response = lane_follow_command_client_->SendRequest(command);
390 if (
nullptr == response) {
391 std::cout <<
"Command sending failed, please check the service is on!\n"
394 std::cout <<
"******Finish sending command.******\n" << std::endl;
398void ExternalCommandDemo::SendFreespaceCommand(
399 const std::vector<apollo::external_command::Point>& way_points,
401 auto command = std::make_shared<apollo::external_command::FreeSpaceCommand>();
402 FillCommandHeader(command);
404 auto roi_point = command->mutable_drivable_roi();
405 for (
const auto& point : way_points) {
406 roi_point->add_point()->CopyFrom(point);
409 command->mutable_parking_spot_pose()->CopyFrom(end);
410 std::cout <<
"Sending lane follow command: " << command->DebugString()
412 auto response = free_space_command_client_->SendRequest(command);
413 if (
nullptr == response) {
414 std::cout <<
"Command sending failed, please check the service is on!\n"
417 std::cout <<
"******Finish sending command.******\n" << std::endl;
420void ExternalCommandDemo::SendValetParkingCommand(
421 const std::string& parking_spot_id,
double target_speed) {
423 std::make_shared<apollo::external_command::ValetParkingCommand>();
424 FillCommandHeader(command);
425 command->set_parking_spot_id(parking_spot_id);
426 if (target_speed > 0.0) {
427 command->set_target_speed(target_speed);
429 std::cout <<
"Sending valet parking command: " << command->DebugString()
431 auto response = valet_parking_command_client_->SendRequest(command);
432 if (
nullptr == response) {
433 std::cout <<
"Command sending failed, please check the service is on!\n"
436 std::cout <<
"******Finish sending command.******\n" << std::endl;
440void ExternalCommandDemo::ReadPathFromPathRecord(
441 const std::string& record_file,
442 google::protobuf::RepeatedPtrField<apollo::external_command::Point>*
444 std::cout <<
"ReadPathFromPathRecord: " << record_file << std::endl;
446 if (!reader.IsValid()) {
447 std::cout <<
"Fail to open " << record_file << std::endl;
453 while (reader.ReadMessage(&message)) {
455 if (planning_trajectory.ParseFromString(message.
content)) {
462 for (
const auto input_point : trajectory_points) {
463 auto output_point = waypoints->Add();
464 output_point->set_x(input_point.path_point().x());
465 output_point->set_y(input_point.path_point().y());
469void ExternalCommandDemo::CheckCommandStatus(
const uint64_t command_id) {
471 std::make_shared<apollo::external_command::CommandStatusRequest>();
472 FillCommandHeader(command);
473 command->set_command_id(command_id);
474 std::cout <<
"Sending check command: " << command->DebugString() << std::endl;
475 auto response = status_client_->SendRequest(command);
476 if (
nullptr == response) {
477 std::cout <<
"Check status failed!\n" << std::endl;
479 std::cout << response->DebugString();
480 std::cout <<
"******Finish checking command.******\n" << std::endl;
484void ExternalCommandDemo::ReadPathFromLocationRecord(
485 const std::string& record_file,
486 google::protobuf::RepeatedPtrField<apollo::external_command::Point>*
488 std::cout <<
"ReadPathFromLocationRecord: " << record_file << std::endl;
490 if (!reader.IsValid()) {
491 std::cout <<
"Fail to open " << record_file << std::endl;
498 bool is_last_poistion_set =
false;
499 while (reader.ReadMessage(&message)) {
500 if (message.
channel_name ==
"/apollo/localization/pose") {
501 if (localization.ParseFromString(message.
content)) {
503 if (!is_last_poistion_set) {
504 last_x = position.
x();
505 last_y = position.y();
506 is_last_poistion_set =
true;
512 auto output_point = waypoints->Add();
513 output_point->set_x(position.x());
514 output_point->set_y(position.y());
515 last_x = position.x();
516 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.
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