|
@ -16,106 +16,77 @@ public: |
|
|
Vec<float,SIZE> jointEnd |
|
|
Vec<float,SIZE> jointEnd |
|
|
) |
|
|
) |
|
|
{ |
|
|
{ |
|
|
|
|
|
std::cout << "bang bang \n " ; |
|
|
float MStoSec = 1000.0f; |
|
|
float MStoSec = 1000.0f; |
|
|
|
|
|
float sampleTime = sampleTimeMs / MStoSec; |
|
|
// calculate maximum velocity and acceleration |
|
|
// calculate maximum velocity and acceleration |
|
|
Vec<float, SIZE> maxJointLocalVelocity = maxJointVelocity * (sampleTimeMs / MStoSec); |
|
|
|
|
|
Vec<float, SIZE> maxJointLocalAcceleration = maxJointAcceleration * (sampleTimeMs / MStoSec); |
|
|
|
|
|
std::cout << "max : " << maxJointLocalVelocity << "\n"; |
|
|
|
|
|
|
|
|
Vec<float, SIZE> maxJointLocalVelocity = maxJointVelocity * sampleTime; |
|
|
|
|
|
Vec<float, SIZE> maxJointLocalAcceleration = maxJointAcceleration * sampleTime; |
|
|
|
|
|
std::cout << maxJointLocalVelocity << ", " << maxJointLocalAcceleration << "\n"; |
|
|
|
|
|
|
|
|
// calculate delta movement |
|
|
// calculate delta movement |
|
|
Vec<float,SIZE> jointMovement = jointEnd - jointStart; |
|
|
Vec<float,SIZE> jointMovement = jointEnd - jointStart; |
|
|
Vec<float,SIZE> jointMovementAbs = jointMovement.abs(); |
|
|
Vec<float,SIZE> jointMovementAbs = jointMovement.abs(); |
|
|
|
|
|
Vec<float,SIZE> jointMovementSgn = jointMovement.sgn(); |
|
|
|
|
|
|
|
|
|
|
|
// calculate sample count |
|
|
|
|
|
|
|
|
// calculate number of movement steps |
|
|
// 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<float,SIZE> maxVeloReachable = (jointMovementAbs.celldivide(maxJointAcceleration)*2.0f).sqrt().cellmultiply(maxJointVelocity); |
|
|
|
|
|
|
|
|
// 2 same acceleration and deaceleration phases |
|
|
|
|
|
|
|
|
maxJointLocalVelocity = (maxVeloReachable/sampleTime).cellmax(maxJointLocalVelocity); |
|
|
|
|
|
|
|
|
Vec<float,SIZE> minStepsPerJoint(0.0f); |
|
|
|
|
|
// check |
|
|
|
|
|
for (int j=0; j<SIZE; j++) |
|
|
|
|
|
{ |
|
|
|
|
|
// only need acceleration and deacceleration phase |
|
|
|
|
|
if (maxJointLocalVelocity(j) > jointMovementAbs(j)/2.0f) |
|
|
|
|
|
{ |
|
|
|
|
|
// s = (a * t^2 )/2 |
|
|
|
|
|
// => t = sqrt( s * 2 / a) |
|
|
|
|
|
minStepsPerJoint(j) = sqrt(jointMovementAbs(j) / maxJointLocalAcceleration (j))*2.0f; |
|
|
|
|
|
} else |
|
|
|
|
|
{ |
|
|
|
|
|
// 2 speedup and slow down phases |
|
|
|
|
|
minStepsPerJoint(j) = maxJointLocalVelocity(j)/maxJointLocalAcceleration(j) * 2.0f ; |
|
|
|
|
|
|
|
|
Vec<float,SIZE> accelerationPhaseTime = maxJointVelocity.celldivide(maxJointAcceleration)/sampleTime; |
|
|
|
|
|
Vec<float,SIZE> jointMovementRemaining = jointMovementAbs - maxJointVelocity.cellmultiply(maxJointVelocity).celldivide(maxJointAcceleration); |
|
|
|
|
|
Vec<float,SIZE> maxVelocityPhaseThime = jointMovementRemaining.celldivide(maxJointLocalVelocity); |
|
|
|
|
|
|
|
|
// one phase with constant velocity |
|
|
|
|
|
float remainingjointMovementAbs = jointMovementAbs(j); |
|
|
|
|
|
// v * t = s |
|
|
|
|
|
remainingjointMovementAbs -= maxJointLocalVelocity(j) * minStepsPerJoint(j) * sampleTimeMs; |
|
|
|
|
|
|
|
|
Vec<float,SIZE> 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(); |
|
|
|
|
|
|
|
|
// s / v = t |
|
|
|
|
|
minStepsPerJoint(j) += remainingjointMovementAbs/maxJointLocalVelocity(j); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::cout << "minsteps : " << minStepsPerJoint << "\n"; |
|
|
|
|
|
std::cout << "steps : " << minStepsPerJoint.max() << "\n"; |
|
|
|
|
|
this->steps = ceil(minStepsPerJoint.max()); |
|
|
|
|
|
std::cout << "steps : " << this->steps << "\n"; |
|
|
|
|
|
|
|
|
this->steps = minStepsPerJoint.max(); |
|
|
if (this->steps == 0) |
|
|
if (this->steps == 0) |
|
|
this->steps +=1; |
|
|
this->steps +=1; |
|
|
|
|
|
|
|
|
this->nodes = (struct Trajectory<SIZE>::trajectoryNode* ) calloc(sizeof(struct Trajectory<SIZE>::trajectoryNode),this->steps); |
|
|
this->nodes = (struct Trajectory<SIZE>::trajectoryNode* ) calloc(sizeof(struct Trajectory<SIZE>::trajectoryNode),this->steps); |
|
|
|
|
|
|
|
|
Vec<float,SIZE> jointLast = jointStart; |
|
|
Vec<float,SIZE> jointLast = jointStart; |
|
|
|
|
|
Vec<float,SIZE> velocityLast(0.0f); |
|
|
|
|
|
|
|
|
// calculate thime of max speed reaching |
|
|
|
|
|
Vec<float,SIZE> deltaToMaxSpeed(0.0f); |
|
|
|
|
|
Vec<float,SIZE> currJointLocalAcceleration(0.0f); |
|
|
|
|
|
for (int j=0; j<SIZE; j++) |
|
|
|
|
|
{ |
|
|
|
|
|
// ceil steps/2 - sqrt(steps^2 - jointmovementabs/maxaccelaration) |
|
|
|
|
|
deltaToMaxSpeed(j) = ceil( this->steps /2.0f - sqrt((this->steps/2.0f)*(this->steps/2.0f) - jointMovementAbs(j)/maxJointAcceleration(j))); |
|
|
|
|
|
if (deltaToMaxSpeed(j) <= 0.0f) |
|
|
|
|
|
{ |
|
|
|
|
|
currJointLocalAcceleration(j) = 0.0f; |
|
|
|
|
|
} else |
|
|
|
|
|
{ |
|
|
|
|
|
currJointLocalAcceleration(j) = -jointMovementAbs(j)/(deltaToMaxSpeed(j)*deltaToMaxSpeed(j)-this->steps*deltaToMaxSpeed(j)); |
|
|
|
|
|
} |
|
|
|
|
|
maxJointLocalVelocity(j) = deltaToMaxSpeed(j)*currJointLocalAcceleration(j); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Vec<float,SIZE> currentInk(0.0f); |
|
|
|
|
|
Vec<float,SIZE> currentInkLast(0.0f); |
|
|
|
|
|
Vec<float,SIZE> currentDist(0.0f); |
|
|
|
|
|
Vec<float,SIZE> currentDistLast(0.0f); |
|
|
|
|
|
|
|
|
// percentage of max velocity |
|
|
|
|
|
//Vec<float,SIZE> currJointMovementOfMax = minStepsPerJoint / minStepsPerJoint.max() ; |
|
|
|
|
|
// s = a* t^2 / 2 => a = s* 2 / t^2 |
|
|
|
|
|
Vec<float,SIZE> currMaxAcceleration = (maxJointLocalAcceleration).cellmultiply(minStepsPerJoint) / minStepsPerJoint.max(); |
|
|
|
|
|
Vec<float,SIZE> currMaxVelocity = (maxJointLocalVelocity ).cellmultiply(minStepsPerJoint) / minStepsPerJoint.max(); |
|
|
|
|
|
std::cout << "max velo curr : " << currMaxVelocity << "\n"; |
|
|
|
|
|
|
|
|
for (int currStep=0; currStep<this->steps; currStep++) |
|
|
|
|
|
|
|
|
for( int i = 0 ; i < this->steps; ++i) |
|
|
{ |
|
|
{ |
|
|
for (int currJoint=0; currJoint<SIZE; currJoint++) |
|
|
|
|
|
|
|
|
for (int joint = 0 ; joint < SIZE; ++joint) |
|
|
{ |
|
|
{ |
|
|
if (currStep+1 <= this->steps/2.0f) |
|
|
|
|
|
|
|
|
if (i < accelerationPhaseTime(i)) |
|
|
{ |
|
|
{ |
|
|
currentInk(currJoint) = std::min(currentInkLast(currJoint)+maxJointLocalAcceleration(currJoint),maxJointLocalVelocity(currJoint)); |
|
|
|
|
|
}else if (currStep+1 > this->steps-deltaToMaxSpeed(currJoint)) |
|
|
|
|
|
|
|
|
this->nodes[i].acceleration(i) = currMaxAcceleration(i); |
|
|
|
|
|
}else if (i < accelerationPhaseTime(i) + jointMovementRemaining(i)) |
|
|
{ |
|
|
{ |
|
|
currentInk(currJoint) = std::max(currentInkLast(currJoint)-maxJointLocalAcceleration(currJoint),0.0f); |
|
|
|
|
|
|
|
|
this->nodes[i].acceleration(i) = 0.0f; |
|
|
}else |
|
|
}else |
|
|
{ |
|
|
{ |
|
|
currentInk(currJoint) = currentInkLast(currJoint); |
|
|
|
|
|
} |
|
|
|
|
|
currentDist(currJoint) = currentDistLast(currJoint) + sgn(jointMovement(currJoint))*currentInk(currJoint); |
|
|
|
|
|
this->nodes[currStep].jointPos(currJoint) += sgn(jointMovement(currJoint))*currentInk(currJoint); |
|
|
|
|
|
this->nodes[currStep].velocity(currJoint) = currentInk(currJoint); |
|
|
|
|
|
this->nodes[currStep].acceleration(currJoint) = currentInkLast(currJoint) - currentInk(currJoint); |
|
|
|
|
|
|
|
|
|
|
|
currentDistLast(currJoint) = currentDist(currJoint); |
|
|
|
|
|
currentInkLast(currJoint) = currentInk(currJoint); |
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
// last step myth be to wide so cut it to the wanted position |
|
|
|
|
|
this->nodes[this->steps-1].jointPos = jointEnd; |
|
|
|
|
|
this->nodes[this->steps-1].velocity = maxJointLocalVelocity; // TODO this is not the truth |
|
|
|
|
|
this->nodes[this->steps-1].acceleration = Vec<float,SIZE>(0.0f); |
|
|
|
|
|
|
|
|
//std::cout << i << this->nodes[i].velocity << this->nodes[i].jointPos <<"\n"; |
|
|
|
|
|
jointLast = this->nodes[i].jointPos; |
|
|
|
|
|
velocityLast = this->nodes[i].velocity; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|