#ifndef _BANGBANGTRAJCETORY_H_ #define _BANGBANGTRAJCETORY_H_ #include "Trajectroy.h" #include "vec.h" template class BangBangJointTrajectory : public Trajectory { public: BangBangJointTrajectory( 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 minBangBangTime = maxJointLocalVelocity.celldivide(maxJointLocalAcceleration) / sampleTime; //TODO check if mintime is neceesary std::cout << minBangBangTime << "minBangBangTime \n "; Vec minStepsPerJoint = jointMovementAbs.celldivide(maxJointLocalVelocity)*2.0f; minStepsPerJoint = minStepsPerJoint.ceil(); std::cout << minStepsPerJoint << "minStepsPerJoint \n"; 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 float floaterror = 1.0f; Vec currMaxAcceleration = (jointMovementAbs * 2.0f ).celldivide(this->steps).celldivide(this->steps)*2.0f*floaterror; std::cout << "currMaxAcceleration : " << currMaxAcceleration << "\n"; float count = 0.0f; for( int i = 0 ; i < this->steps; ++i) { // acceleration phase if (i < this->steps /2 ) { this->nodes[i].acceleration = currMaxAcceleration ; count +=1.0f; } // deacceleration phase else { this->nodes[i].acceleration = currMaxAcceleration * -1.0f; count -=1.0f; } this->nodes[i].velocity = jointMovementSgn.cellmultiply(currMaxAcceleration) * count; this->nodes[i].jointPos = jointLast + this->nodes[i].velocity; std::cout << count << this->nodes[i].velocity << this->nodes[i].jointPos <<"\n"; jointLast = this->nodes[i].jointPos; velocityLast = this->nodes[i].velocity; } } private: protected: }; template class BangBangCartTrajectory: public Trajectory { }; #endif