#include <rmvl/lpss/robot.hpp>
using namespace std::chrono_literals;
public:
ControllerNode() :
lpss::
async::Node(
"controller") {
_planner.load("/path/to/robot.urdf");
init_state.
name = {
"joint1",
"joint2",
"joint3"};
init_state.
effort = {0.0, 0.0, 0.0};
_planner.update(init_state);
_state_sub = this->createSubscriber<msg::JointState>("/robot/cur/joints", [this](const msg::JointState &state) {
_fb_state = state;
_planner.update(state);
});
_cmd_pub = this->createPublisher<msg::JointState>("/robot/tar/joints");
_control_timer = this->createTimer(10ms, [this]() {
_planner.update(_fb_state);
if (!_trajectory_received) {
msg::JointState target;
target.
name = {
"joint1",
"joint2",
"joint3"};
target.
position = {1.57, 0.785, -0.785};
target.
effort = {0.0, 0.0, 0.0};
auto traj = _planner.plan(target);
if (!traj.points.empty()) {
if (!_controller.submit(traj)) {
printf("轨迹提交失败\n");
return;
}
_trajectory_received = true;
}
}
auto cmd = _controller.sample(_fb_state);
_cmd_pub->publish(cmd);
});
}
private:
lpss::RobotPlanner _planner;
lpss::RobotController _controller{{"joint1", "joint2", "joint3"}};
msg::JointState _fb_state;
lpss::async::Publisher<msg::JointState>::ptr _cmd_pub;
lpss::async::Subscriber<msg::JointState>::ptr _state_sub;
lpss::async::Timer::ptr _control_timer;
bool _trajectory_received = false;
};
int main() {
auto node = std::make_shared<ControllerNode>();
node->spin();
return 0;
}
轻量级发布订阅服务异步节点
定义 node.hpp:424
JointState 消息类型:sensor/JointState
定义 joint_state.hpp:32
std::vector< double > velocity
定义 joint_state.hpp:37
std::vector< std::string > name
定义 joint_state.hpp:35
std::vector< double > position
定义 joint_state.hpp:36
std::vector< double > effort
定义 joint_state.hpp:38
轻量发布订阅服务框架命名空间,包含节点、发布者、订阅者等相关定义
定义 base.hpp:20