#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 ) { std::cout << "bang bang \n " ; 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); Vec accelerationPhaseTime = maxJointVelocity.celldivide(maxJointAcceleration)/sampleTime; Vec jointMovementRemaining = jointMovementAbs - maxJointVelocity.cellmultiply(maxJointVelocity).celldivide(maxJointAcceleration); Vec maxVelocityPhaseThime = jointMovementRemaining.celldivide(maxJointLocalVelocity); Vec minStepsPerJoint = accelerationPhaseTime * 2.0f + maxVelocityPhaseThime; std::cout << minStepsPerJoint << "minStepsPerJoint \n"; std::cout << accelerationPhaseTime << "a time \n"; std::cout << jointMovementRemaining << "reimain movement\n"; std::cout << maxVelocityPhaseThime << "max time \n"; minStepsPerJoint = minStepsPerJoint.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); // percentage of max velocity //Vec currJointMovementOfMax = minStepsPerJoint / minStepsPerJoint.max() ; // s = a* t^2 / 2 => a = s* 2 / t^2 Vec currMaxAcceleration = (maxJointLocalAcceleration).cellmultiply(minStepsPerJoint) / minStepsPerJoint.max(); Vec currMaxVelocity = (maxJointLocalVelocity ).cellmultiply(minStepsPerJoint) / minStepsPerJoint.max(); std::cout << "max velo curr : " << currMaxVelocity << "\n"; for( int i = 0 ; i < this->steps; ++i) { for (int joint = 0 ; joint < SIZE; ++joint) { if (i < accelerationPhaseTime(i)) { this->nodes[i].acceleration(i) = currMaxAcceleration(i); }else if (i < accelerationPhaseTime(i) + jointMovementRemaining(i)) { this->nodes[i].acceleration(i) = 0.0f; }else { this->nodes[i].acceleration(i) = currMaxAcceleration(i)* -1.0f; } this->nodes[i].velocity(i) = velocityLast(i) + jointMovementSgn(i) * currMaxAcceleration(i); } this->nodes[i].jointPos = jointLast + this->nodes[i].velocity; //std::cout << i << this->nodes[i].velocity << this->nodes[i].jointPos <<"\n"; jointLast = this->nodes[i].jointPos; velocityLast = this->nodes[i].velocity; } } }; template class LSPBCartTrajectory: public Trajectory { }; #endif