-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
163 lines (129 loc) · 3.81 KB
/
main.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <visualization_msgs/msg/marker_array.hpp>
#include <rclcpp/rclcpp.hpp>
#include <cmath>
double wrap_to_2pi(double theta)
{
const double divisor = std::floor(theta/(2*M_PI));
return theta - 2*M_PI*divisor;
}
class MarkerTest : public rclcpp::Node
{
public:
using Marker = visualization_msgs::msg::Marker;
using Markers = visualization_msgs::msg::MarkerArray;
using Point = geometry_msgs::msg::Point;
MarkerTest()
: rclcpp::Node("marker_test")
{
_publisher = create_publisher<Markers>(
"/markers", rclcpp::SystemDefaultsQoS());
using namespace std::chrono_literals;
_timer = create_wall_timer(50ms, [&](){ make_markers(); });
}
private:
void make_markers()
{
Markers msg;
msg.markers.push_back(make_circle());
msg.markers.push_back(make_spot());
_publisher->publish(msg);
}
Marker make_circle()
{
const auto now = get_clock()->now();
Marker circle;
circle.header.frame_id = "/map";
circle.header.stamp = get_clock()->now();
circle.id = 0;
circle.ns = "marker_test";
circle.type = Marker::LINE_STRIP;
circle.action = Marker::ADD;
circle.pose.position.x = 0.0;
circle.pose.position.y = 0.0;
circle.pose.position.z = 0.0;
circle.pose.orientation.w = 1.0;
circle.pose.orientation.x = 0.0;
circle.pose.orientation.y = 0.0;
circle.pose.orientation.z = 0.0;
const double resolution = 5.0*M_PI/180.0;
const double initial_theta = wrap_to_2pi(2.0*M_PI*now.seconds()/period);
for (double theta = initial_theta; theta <= 2*M_PI; theta += resolution)
{
Point p;
p.x = radius*std::cos(theta);
p.y = radius*std::sin(theta);
p.z = 0.0;
circle.points.push_back(p);
}
Point p;
p.x = radius;
p.y = 0.0;
p.z = 0.0;
circle.points.push_back(p);
const double thick = 0.1;
circle.scale.x = thick;
circle.scale.y = thick;
circle.scale.z = thick;
circle.color.r = 0.0;
circle.color.g = 1.0;
circle.color.b = 0.0;
circle.color.a = 0.5;
circle.lifetime.sec = 3.0;
return circle;
}
Marker make_spot()
{
const auto now = get_clock()->now();
Marker spot;
spot.header.frame_id = "/map";
spot.header.stamp = now;
spot.id = 1;
spot.ns = "marker_test";
spot.type = Marker::CYLINDER;
spot.action = Marker::ADD;
const double height = 1.0;
const double theta = wrap_to_2pi(2*M_PI*now.seconds()/period);
spot.pose.position.x = radius*std::cos(theta);
spot.pose.position.y = radius*std::sin(theta);
spot.pose.position.z = height/2.0;
spot.pose.orientation.w = 1.0;
spot.pose.orientation.x = 0.0;
spot.pose.orientation.y = 0.0;
spot.pose.orientation.z = 0.0;
spot.scale.x = 0.5;
spot.scale.y = 0.5;
spot.scale.z = height;
spot.color.r = 1.0;
spot.color.g = 1.0;
spot.color.b = 0.0;
spot.color.a = 1.0;
spot.lifetime.sec = 3.0;
return spot;
}
rclcpp::TimerBase::SharedPtr _timer;
rclcpp::Publisher<Markers>::SharedPtr _publisher;
const double radius = 5.0;
const double period = 50.0;
};
int main(int argc, char* argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MarkerTest>());
rclcpp::shutdown();
}