新建远程控制车辆插件
(1)执行下面命令,新建插件文件夹remote control command demo
代码:buildtool create --template component remote_control_command_demo
执行成功后,执行Is 命令可以看到当前目录下生成了remote control command demo文件夹,如下图所示;
查看目录结构:代码:tree remote_control_command_demo
生成的目录结构如下:
remote_control_command_demo
|-- BUILD
|-- conf
| |-- remote_control_command_demo.conf -----------加载全局参数的文件
| `-- remote_control_command_demo.pb.txt -----------proto的配置文件
|-- cyberfile.xml -----------编译的加载文件
|-- dag
| `-- remote_control_command_demo.dag -----------启动的dag文件
|-- launch
| `-- remote_control_command_demo.launch -----------启动的launch文件
|-- proto
| |-- BUILD -----------proto的编译文件
| `-- remote_control_command_demo.proto -----------proto的定义文件
|-- remote_control_command_demo.cc -----------功能实现文件
`-- remote_control_command_demo.h -----------功能定义文件
(2)插件已就位,启动在线编辑器,此处略过~
(3)将下列文件中的代码copy至remote control command demo对应的文件中
remote control command demo.cc:
代码:
#include "remote_control_command_demo/remote_control_command_demo.h"
#include <poll.h>
#include <cctype>
#include "cyber/common/file.h"
#include "cyber/record/record_reader.h"
using apollo::external_command::CommandStatus;
RemoteCotrolCommandDemo::RemoteCotrolCommandDemo() : command_id_(0), module_name_("demo") {}
bool RemoteCotrolCommandDemo::Init() {
action_command_client_
= std::make_shared<apollo::common::ClientWrapper<apollo::external_command::ActionCommand, CommandStatus>>(
node_, "/apollo/external_command/action");
free_space_command_client_ = std::make_shared<
apollo::common::ClientWrapper<apollo::external_command::FreeSpaceCommand, CommandStatus>>(
node_, "/apollo/external_command/free_space");
lane_follow_command_client_ = std::make_shared<
apollo::common::ClientWrapper<apollo::external_command::LaneFollowCommand, CommandStatus>>(
node_, "/apollo/external_command/lane_follow");
return true;
}
bool RemoteCotrolCommandDemo::Proc() {
int8_t revent = 0; // short
struct pollfd fd = {STDIN_FILENO, POLLIN, revent};
switch (poll(&fd, 1, 100)) {
case -1:
std::cout << "Failed to read keyboard" << std::endl;
return false;
case 0:
return true;
default:
char data[50];
std::cin.getline(data, 50);
std::string input_command_string = data;
if (input_command_string == "pull_over") {
// Pull over.
// SendActionCommand(
// apollo::external_command::ActionCommandType::PULL_OVER);
auto command = std::make_shared<apollo::external_command::ActionCommand>();
FillCommandHeader(command);
command->set_command(apollo::external_command::ActionCommandType::PULL_OVER);
std::cout << "Sending action command: " << command->DebugString() << std::endl;
auto response = action_command_client_->SendRequest(command);
if (nullptr == response) {
std::cout << "Command sending failed, please check the service is on!\n" << std::endl;
} else {
std::cout << "******Finish sending command.******\n" << std::endl;
}
//
} else if (input_command_string == "stop") {
// Stop planning.
// SendActionCommand(apollo::external_command::ActionCommandType::STOP);
auto command = std::make_shared<apollo::external_command::ActionCommand>();
FillCommandHeader(command);
command->set_command(apollo::external_command::ActionCommandType::STOP);
std::cout << "Sending action command: " << command->DebugString() << std::endl;
auto response = action_command_client_->SendRequest(command);
if (nullptr == response) {
std::cout << "Command sending failed, please check the service is on!\n" << std::endl;
} else {
std::cout << "******Finish sending command.******\n" << std::endl;
}
//
} else if (input_command_string == "start") {
// Start planning.
// SendActionCommand(apollo::external_command::ActionCommandType::START);
auto command = std::make_shared<apollo::external_command::ActionCommand>();
FillCommandHeader(command);
command->set_command(apollo::external_command::ActionCommandType::START);
std::cout << "Sending action command: " << command->DebugString() << std::endl;
auto response = action_command_client_->SendRequest(command);
if (nullptr == response) {
std::cout << "Command sending failed, please check the service is on!\n" << std::endl;
} else {
std::cout << "******Finish sending command.******\n" << std::endl;
}
//
} else if (input_command_string == "free1") {
apollo::external_command::Pose end_pose;
end_pose.set_x(437556.02);
end_pose.set_y(4432540.34);
end_pose.set_heading(1.8);
std::vector<apollo::external_command::Point> way_points;
apollo::external_command::Point point1;
apollo::external_command::Point point2;
apollo::external_command::Point point3;
apollo::external_command::Point point4;
point1.set_x(437536.29);
point1.set_y(4432560.69);
point2.set_x(437536.29);
point2.set_y(4432510.69);
point3.set_x(437576.29);
point3.set_y(4432510.69);
point4.set_x(437576.29);
point4.set_y(4432560.69);
way_points.emplace_back(point1);
way_points.emplace_back(point2);
way_points.emplace_back(point3);
way_points.emplace_back(point4);
SendFreespaceCommand(way_points, end_pose);
} else {
std::cout << "Invalid input!" << input_command_string << std::endl;
}
}
return true;
}
void RemoteCotrolCommandDemo::SendFreespaceCommand(
const std::vector<apollo::external_command::Point>& way_points,
const apollo::external_command::Pose& end) {
auto command = std::make_shared<apollo::external_command::FreeSpaceCommand>();
FillCommandHeader(command);
// Copy way_points
auto roi_point = command->mutable_drivable_roi();
for (const auto& point : way_points) {
roi_point->add_point()->CopyFrom(point);
}
// Copy end point
command->mutable_parking_spot_pose()->CopyFrom(end);
std::cout << "Sending lane follow command: " << command->DebugString() << std::endl;
auto response = free_space_command_client_->SendRequest(command);
if (nullptr == response) {
std::cout << "Command sending failed, please check the service is on!\n" << std::endl;
} else {
std::cout << "******Finish sending command.******\n" << std::endl;
}
}
remote control command demo.h:
代码:
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "modules/common_msgs/external_command_msgs/action_command.pb.h"
#include "modules/common_msgs/external_command_msgs/chassis_command.pb.h"
#include "modules/common_msgs/external_command_msgs/command_status.pb.h"
#include "modules/common_msgs/external_command_msgs/free_space_command.pb.h"
#include "modules/common_msgs/external_command_msgs/lane_follow_command.pb.h"
#include "modules/common_msgs/external_command_msgs/path_follow_command.pb.h"
#include "modules/common_msgs/external_command_msgs/speed_command.pb.h"
#include "modules/common_msgs/external_command_msgs/valet_parking_command.pb.h"
#include "modules/common_msgs/planning_msgs/planning.pb.h"
#include "cyber/component/timer_component.h"
#include "cyber/cyber.h"
#include "modules/common/service_wrapper/client_wrapper.h"
#include "modules/common/util/message_util.h"
class RemoteCotrolCommandDemo final : public apollo::cyber::TimerComponent {
public:
RemoteCotrolCommandDemo();
bool Init() override;
bool Proc() override;
private:
template <typename T>
void FillCommandHeader(const std::shared_ptr<T>& command);
void SendFreespaceCommand(
const std::vector<apollo::external_command::Point>& way_points,
const apollo::external_command::Pose& end);
std::shared_ptr<apollo::common::ClientWrapper<
apollo::external_command::ActionCommand,
apollo::external_command::CommandStatus>>
action_command_client_;
std::shared_ptr<apollo::common::ClientWrapper<
apollo::external_command::FreeSpaceCommand,
apollo::external_command::CommandStatus>>
free_space_command_client_;
std::shared_ptr<apollo::common::ClientWrapper<
apollo::external_command::LaneFollowCommand,
apollo::external_command::CommandStatus>>
lane_follow_command_client_;
uint64_t command_id_;
const std::string module_name_;
};
template <typename T>
void RemoteCotrolCommandDemo::FillCommandHeader(const std::shared_ptr<T>& command) {
apollo::common::util::FillHeader(module_name_, command.get());
command->set_command_id(++command_id_);
}
CYBER_REGISTER_COMPONENT(RemoteCotrolCommandDemo);
cyberfile.xml:
代码:
<package format="2">
<name>external-command-demo</name>
<version>local</version>
<description> The package contains temp component for converting routing message to external command. </description>
<maintainer email="apollo-support@baidu.com">Apollo</maintainer>
<license>Apache License 2.0</license>
<url type="website">https://www.apollo.auto/</url>
<url type="repository">https://github.com/ApolloAuto/apollo</url>
<url type="bugtracker">https://github.com/ApolloAuto/apollo/issues</url>
<type>module</type>
<src_path url="https://github.com/ApolloAuto/apollo">//remote_control_command_demo</src_path>
<depend repo_name="com_github_gflags_gflags" lib_names="gflags">3rd-gflags</depend>
<depend type="binary" repo_name="common" lib_names="common">common</depend>
<depend repo_name="routing" type="binary">routing</depend>
</package>
BUILD:
代码:
load("//tools:cpplint.bzl", "cpplint")
load("//tools:apollo_package.bzl", "apollo_cc_binary", "apollo_cc_library", "apollo_cc_test", "apollo_component", "apollo_package")
package(default_visibility = ["//visibility:public"])
apollo_component(
name = "libremote_control_command_demo_component.so",
hdrs = ["remote_control_command_demo.h"],
srcs = ["remote_control_command_demo.cc"],
copts = ["-DMODULE_NAME=\\\"external_command_demo\\\""],
deps = [
"//cyber",
"//modules/common/util:message_util",
"//modules/common_msgs/external_command_msgs:command_status_cc_proto",
"//modules/common_msgs/external_command_msgs:action_command_cc_proto",
"//modules/common_msgs/external_command_msgs:chassis_command_cc_proto",
"//modules/common_msgs/external_command_msgs:free_space_command_cc_proto",
"//modules/common_msgs/external_command_msgs:lane_follow_command_cc_proto",
"//modules/common_msgs/external_command_msgs:path_follow_command_cc_proto",
"//modules/common_msgs/external_command_msgs:speed_command_cc_proto",
"//modules/common_msgs/external_command_msgs:valet_parking_command_cc_proto",
"//modules/common_msgs/planning_msgs:planning_cc_proto",
"//modules/common/service_wrapper:client_wrapper",
],
)
filegroup(
name = "conf",
srcs = glob([
"conf/*",
"dag/*",
"launch/*"
]),
)
cpplint()
apollo_package()
dag/remote control command demo.dag:
代码:
module_config {
module_library : "remote_control_command_demo/libremote_control_command_demo_component.so"
timer_components {
class_name : "RemoteCotrolCommandDemo"
config {
name: "remote_control_command_demo_component"
interval: 200 # milliseconds
}
}
}
launch/remote control command demo.launch:
代码:
<cyber>
<module>
<name>remote_control_command_demo</name>
<dag_conf>remote_control_command_demo/dag/remote_control_command_demo.dag</dag_conf>
<process_name>remote_control_command_demo</process_name>
</module>
</cyber>
conf/remote control command demo.conf:
代码:
--flagfile=/apollo/modules/common/data/global_flagfile.txt
(4)代码文件拷贝完毕后,点击左上角保存代码文件;
(5)点击上一个网页链接,返回操作终端,
(6)执行下面指令,执行代码编译操作;
buildtool init
buildtool build -p remote_control_command_demo
(7)如下图所示,正常情况会编译成功;如果编译失败,请检查代码的替换内容是否正确(检查完毕后保存后,重新执行5编译操作指令即可);
启动远程控制车辆模块
(1)在终端中,执行下面命令启动远程控制车辆模块,执行成功后,如下图所示。
远程命令模块代码:
mainboard -d remote_control_command_demo/dag/remote_control_command_demo.dag
(2)执行成功后,如下图所示,此进程会被单独的挂起。
在新的终端中启动Dreamview,此步骤略过
发送远程控制指令
(1)点击DV左下角运行按钮,保证车辆沿着当前的轨迹线行驶,切换至远程控制终端,在终端输入"stop"指令,车辆会由“LANE FOLLOW”场景进入"EMERGENCY STOP”场景(如DV所示)车辆会在当前位置缓慢停止;
切换至DV界面,可以发现车辆已经执行急刹指令:
(2)继续在终端输入"start"指令,车辆会再次进入“LANE FOLLOW”场景,车辆会继续沿着当前的的routing线行驶,如下图DV界面所示;
(4)继续在终端输入"start"指令,车辆会由"PULL OVER”场景再次进入“LANE FOLLOW”场景,车辆会继续沿着当前的的routing线行驶;
(5)当我们向终端发送错误的指令时,例如"aaaa",系统无法识别指令,会提醒我们是无效的指令,对当前车辆的运行场景无影响(大胆的操作吧!)