# Strange phenomenon when declaring double

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;

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;
}
}
``````
• In setup(), `array[Formula.Iteration] = Formula.Setpoint` yields undefined bahavior when the index is −1. – Edgar Bonet Jun 24 '15 at 13:26
• Bit of a shame that i missed that... Edited my code, what is the reason my double values of the array are just integers with extra zero's? – KoenR Jun 24 '15 at 13:34
• In avr-gcc, `double` is the same size as `float`. – Ignacio Vazquez-Abrams Jun 24 '15 at 13:46
• But in what way will that effect my results? – KoenR Jun 24 '15 at 13:49
• Please be more specific about what the problem is: What behavior do do expect from your code? What behavior do you have instead? – Edgar Bonet Jun 24 '15 at 13:51

OK, this may not be a proper answer. I'm just having a hard time reading your code, and I cannot understand why you make it so complicated. If you know the acceleration profile, computing the position profile is just a matter of integrating twice. Here is an example, not for Arduino but for a PC. It should not be very different on an Arduino, you just wouldn't use `printf()`:

``````/* Compute position for any acceleration profile. */
void compute_profile(float (*acceleration)(int), int max)
{
float x = 0, v = 0;

for (int t = 0; t <= max; t++) {
v += acceleration(t);
x += v;
printf("%d\t%f\n", t, x);
}
}

/* Your particular acceleration profile. */
float VttgJ(int t)
{
if (t <  7) return t;
if (t < 13) return 12 - t;
if (t < 40) return 0;
if (t < 46) return 39 - t;
else        return t - 51;
}

int main(void)
{
compute_profile(VttgJ, 50);
return 0;
}
``````
• The user inputs the following: Position and Velocity. They both need to be controlled. Because this isn't possible I have build three different formules by integrating the velocity curve. The system is able to measure a position and has a constant looptime. My code is rather complex and can probably a lot shorter. Maybe a modification of your code will be a lot smaller. – KoenR Jun 24 '15 at 14:41