ocra-recipes
Doxygen documentation for the ocra-recipes repository
TrajectoryThread.h
Go to the documentation of this file.
1 
9 /*
10  * This file is part of ocra-recipes.
11  * Copyright (C) 2016 Institut des Systèmes Intelligents et de Robotique (ISIR)
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26 
27 #ifndef TRAJECTORY_THREAD_H
28 #define TRAJECTORY_THREAD_H
29 
30 #include <iostream>
31 
33 
34 #include <yarp/os/Time.h>
35 #include <yarp/os/RateThread.h>
36 #include <Eigen/Dense>
37 #include <Eigen/Lgsm>
38 
40 #include <ocra/util/Macros.h>
42 #include <ocra/control/TaskState.h>
43 
44 
45 namespace ocra_recipes
46 {
48 {
53 };
54 
56 {
67 };
68 
69 class TrajectoryThread : public yarp::os::RateThread
70 {
72 
73 public:
74  // TrajectoryThread();
75  TrajectoryThread(int period, const std::string& taskPortName, const TRAJECTORY_TYPE = MIN_JERK, const TERMINATION_STRATEGY _terminationStrategy = STOP_THREAD);
76  TrajectoryThread(int period, const std::string& taskPortName, const Eigen::MatrixXd& waypoints, const TRAJECTORY_TYPE = MIN_JERK, const TERMINATION_STRATEGY _terminationStrategy = STOP_THREAD);
77  TrajectoryThread(int period, const std::string& taskPortName, const std::list<Eigen::VectorXd>& waypoints, const TRAJECTORY_TYPE = MIN_JERK, const TERMINATION_STRATEGY _terminationStrategy = STOP_THREAD);
79 
85  virtual bool threadInit();
86 
90  virtual void threadRelease();
91 
102  virtual void run();
103 
107  void pause();
108 
112  void unpause();
113 
114 
115  // Setters
116  void setMaxVelocity(double maxVel);
117  void setMaxVelocity(Eigen::VectorXd maxVel);
118  void setMaxAcceleration(double maxAcc);
119  void setMaxAcceleration(Eigen::VectorXd maxAcc);
120 
128  bool setDisplacement(double dispDouble);
129 
137  bool setDisplacement(const Eigen::VectorXd& displacementVector);
138 
147  bool setTrajectoryWaypoints(const Eigen::MatrixXd& userWaypoints, bool containsStartingWaypoint=false);
148 
157  bool setTrajectoryWaypoints(const std::list<Eigen::VectorXd>& waypointList, bool containsStartingWaypoint=false);
158 
164  void setTerminationStrategy(const TERMINATION_STRATEGY newTermStrat){terminationStrategy = newTermStrat;}
165 
171  void setGoalErrorThreshold(const double newErrorThresh){errorThreshold = newErrorThresh;}
172 
178  void setUseVarianceModulation(bool newVarMod){useVarianceModulation = newVarMod;}
179 
185  bool goalAttained();
186 
190  double getDiffError();
191 
192  double getDuration();
193  std::list<Eigen::VectorXd> getWaypointList();
194 
195  void returnToHome();
197 
198  Eigen::MatrixXd getWaypoints(){return allWaypoints;}
199 
200  #if USING_SMLT
201  // Setters
202  void setMeanWaypoints(std::vector<bool>& isMeanWaypoint);
203  void setVarianceWaypoints(std::vector<bool>& isVarWaypoint);
204  void setOptimizationWaypoints(std::vector<bool>& isOptWaypoint);
205  void setDofToOptimize(std::vector<Eigen::VectorXi>& dofToOptimize);
206 
207  // Getters
208  Eigen::VectorXd getBayesianOptimizationVariables();
209  #endif
210 
211  void setMaxVelocityAndAcceleration(double maxVel, double maxAcc);
212  void setMaxVelocityAndAcceleration(const Eigen::VectorXd& maxVel, const Eigen::VectorXd& maxAcc);
213 
214  ocra::Trajectory::Ptr getTrajectory(){return trajectory;}
215 
216 protected:
217 
218  std::shared_ptr<TaskConnection> task;
220 
221  void init();
222 
224  Eigen::VectorXd varianceToWeights(Eigen::VectorXd& desiredVariance, const double beta = 1.0);
225  // void getTaskWeightDimension();
226  void flipWaypoints();
227  void cycleWaypoints();
228 
230 
231  Eigen::MatrixXd userWaypoints;
232  std::list<Eigen::VectorXd> userWaypointList;
233 
236 
237  std::shared_ptr<ocra::Trajectory> trajectory;
238 
239 
242  Eigen::VectorXd desiredVariance;
243  Eigen::ArrayXd varianceThresh;
244 
245  Eigen::VectorXd startStateVector;
246  Eigen::VectorXd goalStateVector;
247  Eigen::MatrixXd allWaypoints;
248  std::list<Eigen::VectorXd> allWaypointList;
250 
251  Eigen::VectorXd desiredState;
252  yarp::os::Bottle desStateBottle;
253 
255 
259 
261  bool isPaused;
263 
265  double pauseTime;
266 
268 
269 
270 private:
271  ocra::TaskState matrixToTaskState(const Eigen::MatrixXd& desMat);
272  const int POS_COL = 0;
273  const int VEL_COL = 1;
274  const int ACC_COL = 2;
275 
276  Eigen::VectorXd getCurrentTaskStateAsVector();
277 
278 };
279 } // namespace ocra_recipes
280 #endif // TRAJECTORY_THREAD_H
ocra::Task::META_TASK_TYPE taskType
std::list< Eigen::VectorXd > userWaypointList
void setUseVarianceModulation(bool newVarMod)
TrajectoryThread(int period, const std::string &taskPortName, const TRAJECTORY_TYPE=MIN_JERK, const TERMINATION_STRATEGY _terminationStrategy=STOP_THREAD)
#define DEFINE_CLASS_POINTER_TYPEDEFS(Class)
Definition: Macros.h:8
std::shared_ptr< TaskConnection > task
ocra::Trajectory::Ptr getTrajectory()
bool setDisplacement(double dispDouble)
META_TASK_TYPE
Definition: Task.h:44
void setMaxVelocityAndAcceleration(double maxVel, double maxAcc)
bool setTrajectoryWaypoints(const Eigen::MatrixXd &userWaypoints, bool containsStartingWaypoint=false)
std::shared_ptr< ocra::Trajectory > trajectory
std::list< Eigen::VectorXd > allWaypointList
void setTerminationStrategy(const TERMINATION_STRATEGY newTermStrat)
void setMaxAcceleration(double maxAcc)
std::list< Eigen::VectorXd > getWaypointList()
Eigen::VectorXd varianceToWeights(Eigen::VectorXd &desiredVariance, const double beta=1.0)
TERMINATION_STRATEGY terminationStrategy
void setGoalErrorThreshold(const double newErrorThresh)