#ifndef _LSPBTRAJCETORY_H_ #define _LSPBTRAJCETORY_H_ #include #include "Trajectroy.h" #include "sgn.h" template class LSPBJointTrajectory: public Trajectory { public: LSPBJointTrajectory( float sampleTimeMs, Vec maxJointVelocity, Vec maxJointAcceleration, Vec jointStart, Vec jointEnd ) { float MStoSec = 1000.0f; float sampleTime = sampleTimeMs / MStoSec; // calculate maximum velocity and acceleration Vec maxJointLocalVelocity = maxJointVelocity * sampleTime; Vec maxJointLocalAcceleration = maxJointAcceleration * sampleTime; std::cout << maxJointLocalVelocity << ", " << maxJointLocalAcceleration << "\n"; // calculate delta movement Vec jointMovement = jointEnd - jointStart; Vec jointMovementAbs = jointMovement.abs(); Vec jointMovementSgn = jointMovement.sgn(); // calculate sample count // calculate number of movement steps // one joint has to reach maxvelocity the others are stepped down to // calculate time if acceleration is enouth to reach max speed Vec maxVeloReachable = (jointMovementAbs.celldivide(maxJointAcceleration)*2.0f).sqrt().cellmultiply(maxJointVelocity); maxJointLocalVelocity = (maxVeloReachable/sampleTime).cellmax(maxJointLocalVelocity); // v ^ // | // v_bang| /\ <- bang bang trajectory // | / \ // v_lspb| _____\___ <- lspb trajectory // | / . . // | / . . \ // | / . . \ // |/________.__.___\____> t // | t1| t2 | t3| // |tx| // // t1/t3 = acceleration phase // t2 = constant speed phase // // t = t1 + t2 +t3 // // v_lspb = percent * v_bang // // t = t_bang + t_x // t = t_bang + (v_bang - v_lspb)/a * 2 // // t = t_bang + (t_bang/2 * a - percent *t_bang/2*a) / a *2 // t = t_bang + (1- percent) * (t_bang/2 * a) / a *2 // t = t_bang + (1- percent) * t_bang // t = (2-percent) * t_bang // calculate time for bang bang Vec accelerationPhaseTime = maxJointVelocity.celldivide(maxJointAcceleration)/sampleTime; Vec timeMaxAcceleration = ((jointMovementAbs/2.0f).celldivide(maxJointAcceleration) * 2.0f).sqrt() * 2.0f / sampleTime; Vec timeMaxVelocity = ((jointMovementAbs/2.0f).celldivide(maxJointVelocity) * 2.0f)*2.0f / sampleTime; Vec timeBangBang = timeMaxAcceleration.cellmax(timeMaxVelocity); std::cout << timeBangBang << "timeBangBang\n"; float steps_bangbang = timeBangBang.ceil().max(); // calculate maxAcceleration for bangbang Vec currMaxAcceleration = (jointMovementAbs * 2.0f ).celldivide(steps_bangbang).celldivide(steps_bangbang)*2.0f; std::cout << "currMaxAcceleration : " << currMaxAcceleration << "\n"; float percentLSPB = 0.7f; Vec timeLSPB = timeBangBang * (1 + 0.25f * (1.0f - percentLSPB)); float timeBlend = ((timeBangBang /2.0f) * percentLSPB).ceil().max(); std::cout << timeBlend << "timeBlend \n"; std::cout << timeLSPB << "max time \n"; Vec minStepsPerJoint = timeLSPB.ceil(); this->steps = minStepsPerJoint.max(); if (this->steps == 0) this->steps +=1; this->nodes = (struct Trajectory::trajectoryNode* ) calloc(sizeof(struct Trajectory::trajectoryNode),this->steps); Vec jointLast = jointStart; Vec velocityLast(0.0f); int count = 0; for( int i = 0 ; i < this->steps; ++i) { if (i < timeBlend) { count += 1; this->nodes[i].acceleration = currMaxAcceleration; }else if (i < this->steps - timeBlend ) { this->nodes[i].acceleration = Vec(0.0f); }else { count -= 1; this->nodes[i].acceleration = currMaxAcceleration* -1.0f; } this->nodes[i].velocity = jointMovementSgn.cellmultiply(currMaxAcceleration) * count; this->nodes[i].jointPos = jointLast + this->nodes[i].velocity; jointLast = this->nodes[i].jointPos; velocityLast = this->nodes[i].velocity; } } }; template class LSPBCartTrajectory: public Trajectory { }; #endif