#ifndef _TRAJECTORY_H_ #define _TRAJECTORY_H_ #include #include #include #include #include #include #include "Vec.h" #include "Mat.h" template class Trajectory; // all types of trajectories /// START OF TRAJECTORY LIST #include "LinearTrajectory.h" #include "LSPBTrajectory.h" #include "BangBangTrajectory.h" #include "FivePolyTrajectory.h" enum TrajectoryType { TrajectoryDefault = 0, TrajectoryJointLinear = 0, TrajectoryCartLinear, TrajectoryJointBangBang, TrajectoryCartBangBang, TrajectoryJointFivePoly, TrajectoryCartFivePoly, TrajectoryJointLSPB, TrajectoryCartLSPB }; static struct { enum TrajectoryType type; std::string str; } TrajectoryTypeStr[] = { { .type = TrajectoryJointLinear , .str="JointLinear" }, { .type = TrajectoryJointLSPB , .str="JointLSPB" }, { .type = TrajectoryJointBangBang, .str="JointBangBang" }, { .type = TrajectoryJointFivePoly, .str="JointFivePoly" }, { .type = TrajectoryCartLinear , .str="CartLinear" }, { .type = TrajectoryCartLSPB , .str="CartLSPB" }, { .type = TrajectoryCartBangBang, .str="CartBangBang" }, { .type = TrajectoryCartFivePoly, .str="CartFivePoly" }, }; /// END OF TRAJECTORY LIST static std::string toString(enum TrajectoryType type) { int items = sizeof(TrajectoryTypeStr) / sizeof(TrajectoryTypeStr[0]); for (int i = 0 ; i < items ; ++i) { if (TrajectoryTypeStr[i].type == type) return ""+ TrajectoryTypeStr[i].str; } return "default"; } static enum TrajectoryType toEnum(std::string name) { int items = sizeof(TrajectoryTypeStr) / sizeof(TrajectoryTypeStr[0]); for (int i = 0 ; i < items ; ++i) { if (TrajectoryTypeStr[i].str == name) return TrajectoryTypeStr[i].type; } return TrajectoryDefault; } enum MovementType { MovementJointBased= 0, MovementCartBased }; template class Trajectory { public: Trajectory() { movementType = MovementJointBased; steps = 0; currentStep = 0; totalTime = 0; nodes = NULL; } Trajectory( float sampleTimeMs, Vec maxJointVelocity, Vec maxJointAcceleration, Vec jointStart, Vec jointEnd ) { movementType = MovementJointBased; steps = 1; currentStep = 0; totalTime = steps * sampleTimeMs; //this->nodes = new struct trajectoryNode [steps]; this->nodes = (struct Trajectory::trajectoryNode* ) calloc(sizeof(struct Trajectory::trajectoryNode),this->steps); nodes[0].jointPos = jointEnd; nodes[0].velocity = (jointEnd-jointStart)/sampleTimeMs; nodes[0].acceleration = Vec(9999.0f); // unused (void)jointStart; (void)maxJointVelocity; (void)maxJointAcceleration; } static class Trajectory* GetTrajectory( enum TrajectoryType type, float sampleTimeMs, Vec maxJointVelocity, Vec maxJointAcceleration, Vec jointStart, Vec jointEnd ) { class Trajectory* retval = NULL; switch ( type ) { case TrajectoryJointLinear: retval = (class Trajectory*) new LinearJointTrajectory(sampleTimeMs, maxJointVelocity, maxJointAcceleration, jointStart, jointEnd); break; case TrajectoryJointBangBang: retval = (class Trajectory*) new BangBangJointTrajectory(sampleTimeMs, maxJointVelocity, maxJointAcceleration, jointStart, jointEnd); break; case TrajectoryJointLSPB: retval = (class Trajectory*) new LSPBJointTrajectory(sampleTimeMs, maxJointVelocity, maxJointAcceleration, jointStart, jointEnd); break; case TrajectoryJointFivePoly: break; case TrajectoryCartLSPB: case TrajectoryCartLinear: case TrajectoryCartBangBang: case TrajectoryCartFivePoly: // TODO implement carthesian movement retval = (class Trajectory*) new LinearJointTrajectory(sampleTimeMs, maxJointVelocity, maxJointAcceleration, jointStart, jointEnd); break; } return retval; } virtual ~Trajectory() { free (nodes); } unsigned int getSteps() { return steps; } unsigned int getRemainingSteps() { if (steps >= currentStep ) return steps - currentStep; else return 0; } unsigned int getCurrentStep() { return currentStep; } enum MovementType getMovementType() { return movementType; } Vec getNextJointPos() { Vec retval(0.0f); unsigned int pos = currentStep; if (steps == 0) return retval; if (pos >= steps) { pos = steps-1; } if (nodes != NULL && movementType == MovementJointBased) { retval = nodes[pos].jointPos; } // increment step currentStep ++; return retval; } MatCarthesian getNextCartPos () { MatCarthesian retval(0.0f,1.0f); unsigned int pos = currentStep; if (steps == 0) return retval; if (pos >= steps) { pos = steps-1; } if (nodes != NULL && movementType == MovementCartBased) { retval = nodes[pos].cartPos; } // increment step currentStep ++; return retval; } Vec getJointPos (unsigned int step) { Vec retval(0.0f); if (steps == 0) return retval; if (step >= steps) { return retval; } if (nodes != NULL) { retval = nodes[step].jointPos; } return retval; } MatCarthesian getCartPos (unsigned int step) { MatCarthesian retval(0.0f, 1.0f); if (steps == 0) return retval; if (step >= steps) { return retval; } if (nodes != NULL) { retval = nodes[step].cartPos; } return retval; } Vec getJointVelocity (unsigned int step) { Vec retval(0.0f); if (steps == 0) return retval; if (step >= steps) { return retval; } if (nodes != NULL) { retval = nodes[step].velocity; } return retval; } Vec getJointAcceleration(unsigned int step) { Vec retval(0.0f); if (steps == 0) return retval; if (step >= steps) { return retval; } if (nodes != NULL) { retval = nodes[step].acceleration; } return retval; } struct trajectoryNode { Vec jointPos; MatCarthesian cartPos; Vec velocity; Vec acceleration; }; void saveToFile(std::string filename) { std::ofstream file; file.open(filename.c_str()); for (unsigned int currentStep = 0 ; currentStep < steps ; ++ currentStep) { for (unsigned int currentJoint = 0 ; currentJoint < SIZE; ++ currentJoint) { if (currentJoint != 0) file << ", "; file << std::fixed << std::setprecision(20) << nodes[currentStep].jointPos(currentJoint); } file << "\n"; } file.close(); } protected: static const unsigned int defaultSteps = 100; unsigned int steps; unsigned int currentStep; float totalTime; struct trajectoryNode* nodes; enum MovementType movementType; }; #endif