#ifndef _TRAJECTORY_H_ #define _TRAJECTORY_H_ #include "vec.h" #include #include template class Trajectory; // all types of trajectories /// START OF TRAJECTORY LIST #include "LinearTrajectory.h" enum TrajectoryType { TrajectoryDefault = 0, TrajectoryJointLinear = 0, TrajectoryCartLinear, TrajectoryJointBangBang, TrajectoryCartBangBang, TrajectoryJointFivePoly, TrajectoryCartFivePoly, TrajectoryJointLSPB, TrajectoryCartLSPB }; static struct { enum TrajectoryType type; std::string str; } TrajectoryTypeStr[] = { { .type = TrajectoryDefault , .str="default" }, { .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]); std::cout <<"items "<< items <<"\n"; for (int i = 0 ; i < items ; ++i) { if (TrajectoryTypeStr[i].str == name) return TrajectoryTypeStr[i].type; } return TrajectoryDefault; } template class Trajectory* calculateTrajectory(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 TrajectoryCartLinear: case TrajectoryJointBangBang: retval = (class Trajectory*) new LinearJointTrajectory( sampleTimeMs, maxJointVelocity, maxJointAcceleration, jointStart, jointEnd); break; case TrajectoryCartBangBang: case TrajectoryJointFivePoly: case TrajectoryCartFivePoly: case TrajectoryJointLSPB: case TrajectoryCartLSPB: break; } return retval; } 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; } virtual ~Trajectory() { std::cout << " test1 \n "; std::cout << nodes[steps-1].jointPos << "\n"; free (nodes); std::cout << " test3 \n "<= 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; } Mat getNextCartPos () { Mat 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 && movementType == MovementJointBased) { retval = nodes[step].jointPos; } return retval; } Mat getCartPos (unsigned int step) { Vec retval(0.0f); if (steps == 0) return retval; if (step >= steps) { return retval; } if (nodes != NULL && movementType == MovementJointBased) { 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 && movementType == MovementCartBased) { 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 && movementType == MovementCartBased) { retval = nodes[step].acceleration; } return retval; } struct trajectoryNode { Vec jointPos; Mat cartPos; Vec velocity; Vec acceleration; }; protected: static const unsigned int defaultSteps = 100; unsigned int steps; unsigned int currentStep; float totalTime; struct trajectoryNode* nodes; enum MovementType movementType; }; #endif