-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathmain.cpp
More file actions
68 lines (56 loc) · 1.89 KB
/
main.cpp
File metadata and controls
68 lines (56 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// C++ robot control example
// Subscribes to robot pose and publishes twist commands
#include <lcm/lcm-cpp.hpp>
#include <cmath>
#include <cstdio>
#include <thread>
#include <atomic>
#include <chrono>
#include "geometry_msgs/PoseStamped.hpp"
#include "geometry_msgs/Twist.hpp"
class RobotController {
public:
RobotController() : lcm_(), running_(true) {}
void onPose(const lcm::ReceiveBuffer*, const std::string&,
const geometry_msgs::PoseStamped* msg) {
const auto& pos = msg->pose.position;
const auto& ori = msg->pose.orientation;
printf("[pose] x=%.2f y=%.2f z=%.2f | qw=%.2f\n",
pos.x, pos.y, pos.z, ori.w);
}
void run() {
lcm_.subscribe("/odom#geometry_msgs.PoseStamped", &RobotController::onPose, this);
printf("Robot control started\n");
printf("Subscribing to /odom, publishing to /cmd_vel\n");
printf("Press Ctrl+C to stop.\n\n");
// Publisher thread
std::thread pub_thread([this]() {
double t = 0;
while (running_) {
geometry_msgs::Twist twist;
twist.linear.x = 0.5;
twist.linear.y = 0;
twist.linear.z = 0;
twist.angular.x = 0;
twist.angular.y = 0;
twist.angular.z = std::sin(t) * 0.3;
lcm_.publish("/cmd_vel#geometry_msgs.Twist", &twist);
printf("[twist] linear=%.2f angular=%.2f\n", twist.linear.x, twist.angular.z);
t += 0.1;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
});
// Handle incoming messages
while (lcm_.handle() == 0) {}
running_ = false;
pub_thread.join();
}
private:
lcm::LCM lcm_;
std::atomic<bool> running_;
};
int main() {
RobotController controller;
controller.run();
return 0;
}