Consider this question closed, found my answer through the answers I had
I am trying to make a position profile curve depending on Looptime, accelerationloops,Setposition,Currentposition(position) and velocity. The curve should look like this:
Is someone able to check what is wrong with my code?
float array[30];
struct Formula {
int LoopTime = 1000;
int AccelerationLoops = 11;
float VelocityLoop = 0;
float VelocityFactor = 0;
int TotalLoops = 0;
float DeltaPosition = 0;
int Setpoint = 0;
int Iteration = -1;
float EndOne = 1.25;
}Formula;
float SetPosition = 1404;
float Position = 0;
float Velocity = 36;
void setup() {
Serial.begin(115200);
for(Formula.Iteration ; Formula.Iteration <= Formula.TotalLoops; Formula.Iteration++){
ProgFormula();
Serial.println(Formula.Iteration);
if(Formula.Iteration >= 0) {array[Formula.Iteration] = Formula.Setpoint;}
}
for(Formula.Iteration = 0 ; Formula.Iteration <= Formula.TotalLoops; Formula.Iteration++){
//Serial.println(array[Formula.Iteration]);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
void ProgFormula(){
if(Formula.Iteration == -1) {
Formula.DeltaPosition = SetPosition - Position;
Formula.VelocityLoop = Velocity / (1000 / Formula.LoopTime);
Formula.TotalLoops = Formula.DeltaPosition / Formula.VelocityLoop + Formula.AccelerationLoops;
Formula.VelocityFactor = Formula.VelocityLoop * 0.5 / Formula.AccelerationLoops;
Formula.EndOne = (Formula.VelocityFactor*pow(Formula.AccelerationLoops,2));
}
else if (Formula.Iteration >= 0 && Formula.Iteration <= Formula.AccelerationLoops) {
Formula.Setpoint = Formula.VelocityFactor * pow(Formula.Iteration,2);
}
else if (Formula.Iteration > Formula.AccelerationLoops && Formula.Iteration <= (Formula.TotalLoops - Formula.AccelerationLoops)) {
Formula.Setpoint = Formula.VelocityLoop * Formula.Iteration - Formula.EndOne;
}
else if (Formula.Iteration >= (Formula.TotalLoops - Formula.AccelerationLoops) && Formula.Iteration <= Formula.TotalLoops) {
Formula.Setpoint = -Formula.VelocityFactor * pow((Formula.TotalLoops - Formula.Iteration),2) + Formula.DeltaPosition;
}
else {
Formula.Setpoint = Formula.Setpoint;
}
}
array[Formula.Iteration] = Formula.Setpoint
yields undefined bahavior when the index is −1.double
is the same size asfloat
.