Control Libraries 6.3.4
Loading...
Searching...
No Matches
Trajectory.hpp
1#pragma once
2
3#include <chrono>
4#include <deque>
5#include "state_representation/State.hpp"
6
7namespace state_representation {
8template<class StateT>
9class Trajectory : public State {
10private:
11 std::deque<StateT> points_;
12 std::deque<std::chrono::nanoseconds> times_;
13 std::string reference_frame_;
14 std::vector<std::string> joint_names_;
15
16public:
20 explicit Trajectory();
21
26 explicit Trajectory(const std::string& name);
27
31 const std::string get_reference_frame() const;
32
36 virtual void set_reference_frame(const std::string& reference_frame);
37
41 const std::vector<std::string>& get_joint_names() const;
42
46 void set_joint_names(unsigned int nb_joints);
47
51 void set_joint_names(const std::vector<std::string>& joint_names);
52
56 void initialize();
57
61 template<typename DurationT>
62 void add_point(const StateT& new_point, const std::chrono::duration<int64_t, DurationT>& new_time);
63
67 template<typename DurationT>
68 void insert_point(const StateT& new_point, const std::chrono::duration<int64_t, DurationT>& new_time, int pos);
69
73 void delete_point();
74
78 void clear();
79
83 const std::deque<StateT>& get_points() const;
84
89 const StateT& get_point(unsigned int index) const;
90
95 StateT& get_point(unsigned int index);
96
100 const std::deque<std::chrono::nanoseconds>& get_times() const;
101
105 int get_size() const;
106
110 const std::pair<StateT, std::chrono::nanoseconds> operator[](unsigned int idx) const;
111
115 std::pair<StateT, std::chrono::nanoseconds> operator[](unsigned int idx);
116
117};
118
119template<class StateT>
121 State(StateType::TRAJECTORY) {
122 this->initialize();
123}
124
125template<class StateT>
126Trajectory<StateT>::Trajectory(const std::string& name):
127 State(StateType::TRAJECTORY, name),
128 reference_frame_("") {
129 this->initialize();
130}
131
132template<class StateT>
133inline const std::string Trajectory<StateT>::get_reference_frame() const {
134 return this->reference_frame_;
135}
136
137template<class StateT>
138inline void Trajectory<StateT>::set_reference_frame(const std::string& reference_frame) {
139 this->reference_frame_ = reference_frame;
140}
141
142template<class StateT>
143inline const std::vector<std::string>& Trajectory<StateT>::get_joint_names() const {
144 return this->joint_names_;
145}
146
147template<class StateT>
148inline void Trajectory<StateT>::set_joint_names(unsigned int nb_joints) {
149 this->joint_names_.resize(nb_joints);
150 for (unsigned int i = 0; i < nb_joints; i++) {
151 this->joint_names_[i] = "joint_" + std::to_string(i + 1);
152 }
153}
154
155template<class StateT>
156inline void Trajectory<StateT>::set_joint_names(const std::vector<std::string>& joint_names) {
157 this->joint_names_ = joint_names;
158}
159
160template<class StateT>
162 this->State::initialize();
163 this->points_.clear();
164 this->times_.clear();
165}
166
167template<class StateT>
168template<typename DurationT>
169void Trajectory<StateT>::add_point(const StateT& new_point, const std::chrono::duration<int64_t, DurationT>& new_time) {
170 this->set_filled();
171 this->points_.push_back(new_point);
172
173 if (!this->times_.empty()) {
174 auto const previous_time = this->times_.back();
175 this->times_.push_back(previous_time + new_time);
176 } else {
177 this->times_.push_back(new_time);
178 }
179}
180
181template<class StateT>
182template<typename DurationT>
183void Trajectory<StateT>::insert_point(const StateT& new_point,
184 const std::chrono::duration<int64_t, DurationT>& new_time,
185 int pos) {
186 this->set_filled();
187
188 auto it_points = this->points_.begin();
189 auto it_times = this->times_.begin();
190 std::advance(it_points, pos);
191 std::advance(it_times, pos);
192
193 this->points_.insert(it_points, new_point);
194
195 auto previous_time = this->times_[pos - 1];
196 this->times_.insert(it_times, previous_time + new_time);
197
198 for (unsigned int i = pos + 1; i <= this->points_.size(); i++) {
199 this->times_[i] += new_time;
200 }
201}
202
203template<class StateT>
205 this->set_filled();
206 if (!this->points_.empty()) {
207 this->points_.pop_back();
208 }
209 if (!this->times_.empty()) {
210 this->times_.pop_back();
211 }
212}
213
214template<class StateT>
216 this->points_.clear();
217 this->times_.clear();
218}
219
220template<class StateT>
221inline const std::deque<StateT>& Trajectory<StateT>::get_points() const {
222 return this->points_;
223}
224
225template<class StateT>
226const StateT& Trajectory<StateT>::get_point(unsigned int index) const {
227 return this->points_[index];
228}
229
230template<class StateT>
231StateT& Trajectory<StateT>::get_point(unsigned int index) {
232 return this->points_[index];
233}
234
235template<class StateT>
236inline const std::deque<std::chrono::nanoseconds>& Trajectory<StateT>::get_times() const {
237 return this->times_;
238}
239
240template<class StateT>
242 return this->points_.size();
243}
244
245template<class StateT>
246const std::pair<StateT, std::chrono::nanoseconds> Trajectory<StateT>::operator[](unsigned int idx) const {
247 return std::make_pair(this->points_[idx], this->times_[idx]);
248}
249
250template<class StateT>
251std::pair<StateT, std::chrono::nanoseconds> Trajectory<StateT>::operator[](unsigned int idx) {
252 this->set_filled();
253 return std::make_pair(this->points_[idx], this->times_[idx]);
254}
255}
Abstract class to represent a state.
Definition: State.hpp:25
virtual void initialize()
Initialize the State to a zero value.
Definition: State.cpp:61
virtual void set_reference_frame(const std::string &reference_frame)
Setter of the reference frame.
Definition: Trajectory.hpp:138
void initialize()
Initialize trajectory.
Definition: Trajectory.hpp:161
const std::vector< std::string > & get_joint_names() const
Getter of the names attribute.
Definition: Trajectory.hpp:143
void set_joint_names(unsigned int nb_joints)
Setter of the names attribute from the number of joints.
Definition: Trajectory.hpp:148
const StateT & get_point(unsigned int index) const
Get the trajectory point at given index.
Definition: Trajectory.hpp:226
void delete_point()
Delete last point and corresponding time from trajectory.
Definition: Trajectory.hpp:204
const std::deque< std::chrono::nanoseconds > & get_times() const
Get attribute list of trajectory times.
Definition: Trajectory.hpp:236
const std::pair< StateT, std::chrono::nanoseconds > operator[](unsigned int idx) const
Operator overload for returning a single trajectory point and corresponding time.
Definition: Trajectory.hpp:246
void insert_point(const StateT &new_point, const std::chrono::duration< int64_t, DurationT > &new_time, int pos)
Insert new point and corresponding time to trajectory between two already existing points.
Definition: Trajectory.hpp:183
int get_size() const
Get attribute number of point in trajectory.
Definition: Trajectory.hpp:241
const std::deque< StateT > & get_points() const
Get attribute list of trajectory points.
Definition: Trajectory.hpp:221
Trajectory()
Empty constructor.
Definition: Trajectory.hpp:120
void add_point(const StateT &new_point, const std::chrono::duration< int64_t, DurationT > &new_time)
Add new point and corresponding time to trajectory.
Definition: Trajectory.hpp:169
const std::string get_reference_frame() const
Getter of the reference frame as const reference.
Definition: Trajectory.hpp:133
void clear()
Clear trajectory.
Definition: Trajectory.hpp:215
Core state variables and objects.
StateType
The class types inheriting from State.
Definition: StateType.hpp:13