YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
robotAction.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include <yarp/os/Log.h>
7#include <yarp/os/LogStream.h>
8#include <algorithm>
9
10#include <fstream>
11
12using namespace std;
13using namespace yarp::os;
14
15#include "robotAction.h"
16
17// ******************** ACTION FRAME
21
23{
24 counter = as.counter;
25 time = as.time;
26 q_joints=as.q_joints;
27}
28
30{
31 if (this == &as)
32 {
33 return *this;
34 }
35 counter = as.counter;
36 time = as.time;
37 q_joints=as.q_joints;
38 return *this;
39}
40
44
45// ******************** ACTION CLASS
47{
48 if (action_frames_vector.size() == 0)
49 return 0;
50 return action_frames_vector[0].q_joints.size();
51}
52
54{
55 speed_factor = 1.0;
56 forever = false;
57 current_frame = 0;
59}
60
65
67{
68 std::deque<action_frame>::iterator action_frames_it;
70 {
71 string s = "(" +
72 std::to_string(action_frames_it->counter) + " " +
73 std::to_string(action_frames_it->time) + ") ";
74 for (int i = 0; i < action_frames_it->q_joints.size(); i++)
75 {
76 s += std::to_string(action_frames_it->q_joints[i]);
77 }
78 yInfo() << s;
79 }
80}
81
82
83std::string getFileExtension(const std::string& filename)
84{
85 //Finds the last occurrence of the dot
86 std::size_t dotPos = filename.find_last_of('.');
87 if (dotPos != std::string::npos) {
88 //Returns the file extension (after the dot)
89 return filename.substr(dotPos + 1);
90 }
91 //If the dot is not found, return an empty string
92 return "";
93}
94
95bool action_class::openFile(string filename, size_t njoints, double timestep)
96{
97 std::ifstream file(filename);
98 if (!file.is_open())
99 {
100 yError() << "Cannot find file" << filename;
101 return false;
102 }
103
104 std::string line;
105 //std::getline(file, line); //Removes the remaining part of the line
106
107 size_t linecount = 1;
108 size_t wallcount = 0;
109 double walltime = 0;
110 while (std::getline(file, line))
111 {
112 if (timestep == -1)
113 {
114 if(!parseCommandLineVarTime(line, njoints))
115 {
116 yError ("error parsing file, line %u\n", linecount);
117 return false;
118 };
119 }
120 else
121 {
122 if (!parseCommandLineFixTime(line, njoints, wallcount, walltime))
123 {
124 yError("error parsing file, line %u\n", linecount);
125 return false;
126 }
128 wallcount++;
129 linecount++;
130 }
131 }
132
133 file.close();
134 return true;
135}
136
137//FORMAT FILE
138//0.0 0.0 0.0 0.0
139bool action_class::parseCommandLineFixTime(std::string command_line, size_t njoints, size_t wallCount, double wallTime)
140{
141 std::istringstream iss(command_line);
142
144 tmp_frame.counter = wallCount;
145 tmp_frame.time = wallTime;
146
147 double value = 0.0;
148 std::vector<double> tempElements;
149 while (iss >> value)
150 {
151 tempElements.push_back(value);
152 }
153
154 if (tempElements.size() != njoints)
155 {
156 yError("Invalid number of elements, expected: %u, read: %u", njoints, tempElements.size());
157 return false;
158 }
159 for (size_t i = 0; i < njoints;i++)
160 {
161 tmp_frame.q_joints.push_back(tempElements[i]);
162 }
163
164 //insert the new frame in the list
166
167 return true;
168}
169
170//FORMAT FILE
171//111 1.01222 0.0 0.0 0.0 0.0
172bool action_class::parseCommandLineVarTime(std::string command_line, size_t njoints)
173{
174 std::istringstream iss(command_line);
175
177
178 iss >> tmp_frame.counter;
179 iss >> tmp_frame.time;
180
181 double value = 0.0;
182 std::vector<double> tempElements;
183 while (iss >> value)
184 {
185 tempElements.push_back(value);
186 }
187
188 if (tempElements.size() != njoints)
189 {
190 yError("Invalid number of elements, expected: %u, read: %u", njoints, tempElements.size());
191 return false;
192 }
193 for (size_t i = 0; i << njoints;i++)
194 {
195 tmp_frame.q_joints[i]= tempElements[i];
196 }
197
198 //insertion of the new parsed frame in the vector of frames based on the timestamp
199 std::deque<action_frame>::iterator action_frames_it;
201 {
202 if (tmp_frame.time > action_frames_it->time)
203 {
204 break;
205 }
207 }
208 return true;
209}
210
211std::vector<double> action_class::interpolate_joints(const std::vector<double>& q1, const std::vector<double>& q2, double t1, double t2, double t)
212{
213 std::vector<double> q_interp(q1.size());
214 double alpha = (t - t1) / (t2 - t1);
215 for (size_t i = 0; i < q1.size(); ++i)
216 {
217 q_interp[i] = q1[i] + alpha * (q2[i] - q1[i]);
218 }
219 return q_interp;
220}
221
223{
224 if (action_frames_vector.empty())
225 {
226 yError("The vector of actions frames is empty!");
227 return;
228 }
229 if (timestep <= 0)
230 {
231 yError("The timestep cannot be <=0 !");
232 return;
233 }
234
235 std::deque<action_frame> interpolated_frames;
236 double t_min = action_frames_vector.front().time;
237 double t_max = action_frames_vector.back().time;
238
239 // Generates new timestamps with the given constant time step
240 size_t c = 0;
241 for (double t = t_min; t <= t_max; t += timestep)
242 {
243 // Find the closest frames on the left and on the right
244 auto it = std::lower_bound(action_frames_vector.begin(), action_frames_vector.end(), t,
245 [](const action_frame& frame, double time)
246 {
247 return frame.time < time;
248 });
249
250 if (it == action_frames_vector.begin())
251 {
252 interpolated_frames.push_back(*it);
253 continue;
254 }
255
256 if (it == action_frames_vector.end())
257 {
259 break;
260 }
261
262 auto it_prev = std::prev(it);
263
264 // Interpolates joints values
266 new_frame.counter = c++;
267 new_frame.time = t;
268 new_frame.q_joints = interpolate_joints(it_prev->q_joints, it->q_joints, it_prev->time, it->time, t);
269
271 }
272
274}
#define yInfo(...)
Definition Log.h:319
#define yError(...)
Definition Log.h:361
void interpolate_action_frames(double timestep)
size_t get_njoints()
size_t current_frame
Definition robotAction.h:50
double speed_factor
Definition robotAction.h:52
bool openFile(std::string filename, size_t njoints, double timestep=-1)
std::deque< action_frame > action_frames_vector
Definition robotAction.h:53
action_frame & operator=(const action_frame &as)
std::vector< double > q_joints
Definition robotAction.h:34
size_t counter
Definition robotAction.h:32
A mini-server for performing network communication in the background.
void close() override
Stop port activity.
STL namespace.
An interface to the operating system, including Port based communication.
std::string getFileExtension(const std::string &filename)