Compiler Problem with Calculation within If Statement?

Hi,

I’m getting an Compiler Error, but cannot understand why.
Does maybe somebody has an Idea what i’ve coded wrong?

When i’m removing the Calculation included within if Statement it compiles without Errors.

Compiler Error:
exit status 1
unable to find a register to spill in class ‘POINTER_REGS’

When compiling following Code:

const int           cAxisCount                  =2;                           //Total Count of used Axis
      double        dSpeed[cAxisCount]          ={10,10};
      double        dPositionCommand[cAxisCount],dPositionSetpoint[cAxisCount];

void setup()
{}

void loop()
{
  //Calculate Speed
  for(int i=0; i<cAxisCount; i++)
  {
    //Speed
    if(dPositionCommand[i] > dPositionSetpoint[i] + dSpeed[i])
      dPositionSetpoint[i] = dPositionSetpoint[i] + dSpeed[i];
    else if(dPositionCommand[i] < dPositionSetpoint[i] - dSpeed[i])
      dPositionSetpoint[i] = dPositionSetpoint[i] - dSpeed[i];
    else
      dPositionSetpoint[i] = dPositionCommand[i];
  }
}

IDE version?

You can make “i” global:

const int cAxisCount = 2;                          //Total Count of used Axis
double dSpeed[cAxisCount] = {10.0, 10.0};
double dPositionCommand[cAxisCount];
double dPositionSetpoint[cAxisCount];
int i;

void setup()
{
}

void loop()
{
  //Calculate Speed
  for ( i = 0; i < cAxisCount; i++)
  {
    //Speed
    if (dPositionCommand[i] > dPositionSetpoint[i] + dSpeed[i])
      dPositionSetpoint[i] = dPositionSetpoint[i] + dSpeed[i];
    else if (dPositionCommand[i] < dPositionSetpoint[i] - dSpeed[i])
      dPositionSetpoint[i] = dPositionSetpoint[i] - dSpeed[i];
    else
    {
      dPositionSetpoint[i] = dPositionCommand[i];
    }
  }
}

IDE 1.6.5

I can reproduce that with 1.6.7.

How does making "i" global work? (which it does)

See Bug 60040 - AVR: error: unable to find a register to spill in class 'POINTER_REGS' - looks like a known bug.

maxx8888:
Does maybe somebody has an Idea what i’ve coded wrong?

Nothing. Compiler errors are rare, but you found one. :slight_smile:

By typing things on the keyboard? :slight_smile: I don’t know… also this works:

void loop()
{
  static int i;
  //Calculate Speed
  for ( i = 0; i < cAxisCount; i++)

@maxx8888, code organized like that cries out for a struct / class.

In the meantime, some basic reduction overcomes the problem…

const int           cAxisCount                  =2;                           //Total Count of used Axis
      double        dSpeed[cAxisCount]          ={10,10};
      double        dPositionCommand[cAxisCount],dPositionSetpoint[cAxisCount];

void setup()
{}

void loop()
{
  //Calculate Speed
  for(int i=0; i<cAxisCount; i++)
  {
    double ps;

    ps = dPositionSetpoint[i];
    
    //Speed
    if(dPositionCommand[i] > ps + dSpeed[i])
      ps = ps + dSpeed[i];
    else if(dPositionCommand[i] < ps - dSpeed[i])
      ps = ps - dSpeed[i];
    else
      ps = dPositionCommand[i];

    dPositionSetpoint[i] = ps;
  }
}

I suspect a Pascal “with” would eliminate the problem but the language lacks it.

Fully reduced…

const int           cAxisCount                  =2;                           //Total Count of used Axis
      double        dSpeed[cAxisCount]          ={10,10};
      double        dPositionCommand[cAxisCount],dPositionSetpoint[cAxisCount];

void setup()
{}

void loop()
{
  //Calculate Speed
  for(int i=0; i<cAxisCount; i++)
  {
    double pc;
    double ps;
    double s;

    pc = dPositionCommand[i];
    ps = dPositionSetpoint[i];
    s = dSpeed[i];
    
    //Speed
    if(pc > ps + s)
      ps = ps + s;
    else if(pc < ps - s)
      ps = ps - s;
    else
      ps = pc;

    dPositionSetpoint[i] = ps;
  }
}

Wow :o.

Thanks for all the answers!

Ok, then it is really a compiler Bug and i need todo one of the proposed workarounds.

I'm trying to control 2 DC Motors with the Codepart i've posted. In order to keep fast response on the Motors i'm trying to run the full Arduino loop in less than 1msec.

I tried to avoid thinks like Variable Declaration within For Loops. I thought this would make things slower. Or is this a wrong assumption?

Thererfore the static "i" Variable would even speed things up, right? :-)

Hmm, just need to consider Serial Communication and Encoder Interrupts, which are also using i as variable in for Loops...

maxx8888: Thererfore the static "i" Variable would even speed things up, right? :-)

Wrong. Automatic variables are either the same performance as static variables or better performance.