Control Libraries 7.4.0
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 reset();
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() {
122 this->set_type(StateType::TRAJECTORY);
123 this->reset();
124}
125
126template<class StateT>
127Trajectory<StateT>::Trajectory(const std::string& name):
128 State(name),
129 reference_frame_("") {
130 this->set_type(StateType::TRAJECTORY);
131 this->reset();
132}
133
134template<class StateT>
135inline const std::string Trajectory<StateT>::get_reference_frame() const {
136 return this->reference_frame_;
137}
138
139template<class StateT>
140inline void Trajectory<StateT>::set_reference_frame(const std::string& reference_frame) {
141 this->reference_frame_ = reference_frame;
142}
143
144template<class StateT>
145inline const std::vector<std::string>& Trajectory<StateT>::get_joint_names() const {
146 return this->joint_names_;
147}
148
149template<class StateT>
150inline void Trajectory<StateT>::set_joint_names(unsigned int nb_joints) {
151 this->joint_names_.resize(nb_joints);
152 for (unsigned int i = 0; i < nb_joints; i++) {
153 this->joint_names_[i] = "joint_" + std::to_string(i + 1);
154 }
155}
156
157template<class StateT>
158inline void Trajectory<StateT>::set_joint_names(const std::vector<std::string>& joint_names) {
159 this->joint_names_ = joint_names;
160}
161
162template<class StateT>
164 this->State::reset();
165 this->points_.clear();
166 this->times_.clear();
167}
168
169template<class StateT>
170template<typename DurationT>
171void Trajectory<StateT>::add_point(const StateT& new_point, const std::chrono::duration<int64_t, DurationT>& new_time) {
172 this->set_empty(false);
173 this->points_.push_back(new_point);
174
175 if (!this->times_.empty()) {
176 auto const previous_time = this->times_.back();
177 this->times_.push_back(previous_time + new_time);
178 } else {
179 this->times_.push_back(new_time);
180 }
181}
182
183template<class StateT>
184template<typename DurationT>
185void Trajectory<StateT>::insert_point(const StateT& new_point,
186 const std::chrono::duration<int64_t, DurationT>& new_time,
187 int pos) {
188 this->set_empty(false);
189
190 auto it_points = this->points_.begin();
191 auto it_times = this->times_.begin();
192 std::advance(it_points, pos);
193 std::advance(it_times, pos);
194
195 this->points_.insert(it_points, new_point);
196
197 auto previous_time = this->times_[pos - 1];
198 this->times_.insert(it_times, previous_time + new_time);
199
200 for (unsigned int i = pos + 1; i <= this->points_.size(); i++) {
201 this->times_[i] += new_time;
202 }
203}
204
205template<class StateT>
207 this->set_empty(false);
208 if (!this->points_.empty()) {
209 this->points_.pop_back();
210 }
211 if (!this->times_.empty()) {
212 this->times_.pop_back();
213 }
214}
215
216template<class StateT>
218 this->points_.clear();
219 this->times_.clear();
220}
221
222template<class StateT>
223inline const std::deque<StateT>& Trajectory<StateT>::get_points() const {
224 return this->points_;
225}
226
227template<class StateT>
228const StateT& Trajectory<StateT>::get_point(unsigned int index) const {
229 return this->points_[index];
230}
231
232template<class StateT>
233StateT& Trajectory<StateT>::get_point(unsigned int index) {
234 return this->points_[index];
235}
236
237template<class StateT>
238inline const std::deque<std::chrono::nanoseconds>& Trajectory<StateT>::get_times() const {
239 return this->times_;
240}
241
242template<class StateT>
244 return this->points_.size();
245}
246
247template<class StateT>
248const std::pair<StateT, std::chrono::nanoseconds> Trajectory<StateT>::operator[](unsigned int idx) const {
249 return std::make_pair(this->points_[idx], this->times_[idx]);
250}
251
252template<class StateT>
253std::pair<StateT, std::chrono::nanoseconds> Trajectory<StateT>::operator[](unsigned int idx) {
254 this->set_empty(false);
255 return std::make_pair(this->points_[idx], this->times_[idx]);
256}
257}
Abstract class to represent a state.
Definition State.hpp:25
void set_type(const StateType &type)
Setter of the state type attribute.
Definition State.cpp:41
virtual void reset()
Reset the object to a post-construction state.
Definition State.cpp:77
virtual void set_reference_frame(const std::string &reference_frame)
Setter of the reference frame.
const std::vector< std::string > & get_joint_names() const
Getter of the names attribute.
void set_joint_names(unsigned int nb_joints)
Setter of the names attribute from the number of joints.
const StateT & get_point(unsigned int index) const
Get the trajectory point at given index.
void delete_point()
Delete last point and corresponding time from trajectory.
const std::deque< std::chrono::nanoseconds > & get_times() const
Get attribute list of trajectory times.
const std::pair< StateT, std::chrono::nanoseconds > operator[](unsigned int idx) const
Operator overload for returning a single trajectory point and corresponding time.
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.
int get_size() const
Get attribute number of point in trajectory.
const std::deque< StateT > & get_points() const
Get attribute list of trajectory points.
void reset()
Initialize trajectory.
void add_point(const StateT &new_point, const std::chrono::duration< int64_t, DurationT > &new_time)
Add new point and corresponding time to trajectory.
const std::string get_reference_frame() const
Getter of the reference frame as const reference.
void clear()
Clear trajectory.
Core state variables and objects.