libmove3d-planners
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Groups Pages
kinotrajectory.hpp
1 /*
2  * Adaptation of the code of Tobias Kunz for use in Move3d
3  *
4  *
5  * Copyright (c) 2012, Georgia Tech Research Corporation
6  * All rights reserved.
7  *
8  * Author: Tobias Kunz <tobias@gatech.edu>
9  * Date: 05/2012
10  *
11  * Humanoid Robotics Lab Georgia Institute of Technology
12  * Director: Mike Stilman http://www.golems.org
13  *
14  * Algorithm details and publications:
15  * http://www.golems.org/node/1570
16  *
17  * This file is provided under the following "BSD-style" License:
18  * Redistribution and use in source and binary forms, with or
19  * without modification, are permitted provided that the following
20  * conditions are met:
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  * * Redistributions in binary form must reproduce the above
24  * copyright notice, this list of conditions and the following
25  * disclaimer in the documentation and/or other materials provided
26  * with the distribution.
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
28  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
29  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
35  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #pragma once
43 #include <Eigen/Core>
44 #include "kinopath.hpp"
45 
46 #include "API/Device/robot.hpp"
47 #include "API/Trajectory/trajectory.hpp"
48 #include "API/ConfigSpace/configuration.hpp"
49 #include <vector>
50 
51 #include "API/Trajectory/kinotrajectorygenomstruct.h"
52 
53 
54 struct KinoROSPoint{
55  double time_from_start;
56  std::vector<double> positions;
57  std::vector<double> velocities;
58  //vector<double> accelerations;
59 };
60 
61 namespace API{
63  {
64  public:
65  // Generates a time-optimal trajectory
66  KinoTrajectory(const KinoPath &path, const Eigen::VectorXd &maxVelocity, const Eigen::VectorXd &maxAcceleration, double timeStep = 0.001, double initialVel = 0);
67 
68  ~KinoTrajectory(void);
69 
70  // Call this method after constructing the object to make sure the trajectory generation succeeded without errors.
71  // If this method returns false, all other methods have undefined behavior.
72  bool isValid() const;
73 
74  // Returns the optimal duration of the trajectory
75  double getDuration() const;
76 
77  // Return the position/configuration or velocity vector of the robot for a given point in time within the trajectory.
78  Eigen::VectorXd getPosition(double time) const;
79  Eigen::VectorXd getVelocity(double time) const;
80  std::list<Eigen::VectorXd> getWaypointsAfter(double time) const;
81 
82  KinoPath getPath() const { return path;};
83  Eigen::VectorXd getMaxVelocity() const { return maxVelocity;};
84  Eigen::VectorXd getMaxAcceleration() const { return maxAcceleration;};
85  double getTimeStep() const { return timeStep;};
86  double getPathVelocity(double time) const;
87 
88  double getFactor() const { return factor;};
89  void setFactor(double factor) { this->factor = factor;};
90 
91 
92  //Creation of KinoTrajectory from libmove3d and libmove3d-planners trajectories
93  static KinoTrajectory* createKinoTrajectory(API::Trajectory* move3dTrajectory, std::vector<unsigned int> dofs, double timeStep = 0.001);
94  static KinoTrajectory* createKinoTrajectory(p3d_traj* p3dTrajectory, std::vector<unsigned int> dofs, double timeStep = 0.001);
95 
96  //Creation of genom struct and of KinoTrajectory from libmove3d trajectory
97  static KinoGenomTrajectory createKinoGenomTrajectory(p3d_traj* p3dTrajectory, std::vector<unsigned int> dofs, double timeStep = 0.001);
98  static KinoTrajectory* createKinoTrajectory(KinoGenomTrajectory genomTrajectory);
99 
100  //Modification of an existing KinoTrajectory
101  static KinoTrajectory* factorKinoTrajectory(KinoTrajectory* originalTrajectory, double factor = 1.0); //Changing the whole trajectory speed
102  static KinoTrajectory* changeKinoTrajectory(KinoTrajectory* originalTrajectory, API::Trajectory* move3dTrajectory, std::vector<unsigned int> dofs, double timeChange); //Switching from a trajectory to another at a given time
103  static KinoTrajectory* modulateKinoTrajectory(KinoTrajectory* originalTrajectory, double timeChange, double factor = 1.0);//Changing the speed of the trajectory at a given time
104 
105 
106  //Exports
107  std::vector<KinoROSPoint> toROS(unsigned int intSteps=0) const; //the whole trajectory ready to be use in a Joint Trajectory Action goal
108  KinoROSPoint toROSAt(double timeStamp) const; //the configuration, velocity and acceleration of the robot at a given time
109  API::Trajectory * toP3DTraj(Robot* robot, unsigned int intSteps = 0) const;//the whole trajectory ready to be use in libmove3d-planners, not very useful due to the absence of time handling in move3d
110 
111  void toGazebo(unsigned int intSteps = 0) const; //A cpp text output to be use in the ROS tutorial http://wiki.ros.org/pr2_controllers/Tutorials/Moving the arm using the Joint Trajectory Action
112  void print( unsigned int intSteps = 0) const; //A csv text output
113 
114  // Outputs the phase trajectory and the velocity limit curve in 2 files for debugging purposes.
115  void outputPhasePlaneKinoTrajectory() const;
116 
117  private:
118  struct KinoTrajectoryStep {
119  KinoTrajectoryStep() {}
120  KinoTrajectoryStep(double pathPos, double pathVel) :
121  pathPos(pathPos),
122  pathVel(pathVel)
123  {}
124  double pathPos;
125  double pathVel;
126  double time;
127  };
128 
129  void compute(double initialVel = 0.0);
130  bool getNextSwitchingPoint(double pathPos, KinoTrajectoryStep &nextSwitchingPoint, double &beforeAcceleration, double &afterAcceleration);
131  bool getNextAccelerationSwitchingPoint(double pathPos, KinoTrajectoryStep &nextSwitchingPoint, double &beforeAcceleration, double &afterAcceleration);
132  bool getNextVelocitySwitchingPoint(double pathPos, KinoTrajectoryStep &nextSwitchingPoint, double &beforeAcceleration, double &afterAcceleration);
133  bool integrateForward(std::list<KinoTrajectoryStep> &trajectory, double acceleration);
134  void integrateBackward(std::list<KinoTrajectoryStep> &startKinoTrajectory, double pathPos, double pathVel, double acceleration);
135  double getMinMaxPathAcceleration(double pathPosition, double pathVelocity, bool max);
136  double getMinMaxPhaseSlope(double pathPosition, double pathVelocity, bool max);
137  double getAccelerationMaxPathVelocity(double pathPos) const;
138  double getVelocityMaxPathVelocity(double pathPos) const;
139  double getAccelerationMaxPathVelocityDeriv(double pathPos);
140  double getVelocityMaxPathVelocityDeriv(double pathPos);
141 
142  std::list<KinoTrajectoryStep>::const_iterator getKinoTrajectorySegment(double time) const;
143 
144  static double deltaValue(double q1, double q2);
145 
146  KinoPath path;
147  Eigen::VectorXd maxVelocity;
148  Eigen::VectorXd maxAcceleration;
149  unsigned int n;
150  bool valid;
151  std::list<KinoTrajectoryStep> trajectory;
152  std::list<KinoTrajectoryStep> endTrajectory; // non-empty only if the trajectory generation failed.
153 
154  static const double eps;
155  const double timeStep;
156 
157  mutable double cachedTime;
158  mutable std::list<KinoTrajectoryStep>::const_iterator cachedTrajectorySegment;
159 
160  double factor;
161  };
162 }
Definition: kinopath.hpp:76
Definition: kinotrajectorygenomstruct.h:11
This class holds a the robot represented by a kinematic chain.
Definition: robot.hpp:42
Definition: kinotrajectory.hpp:62
Definition: trajectory.hpp:40
Definition: kinotrajectory.hpp:54