Go Down

Topic: a[i] = b * c[i]; Comes With Errors Inside For & While Loops (Read 711 times) previous topic - next topic

CCCanyon

int basicThrust;
int motor[6];
const float ax[6] = {@*&#^%&};
const float ay[6] = {^%$#&*%};
const float az[6] = {%$#^%&*};
int axScaler;
int ayScaler;
int azScaler;

I'm using a nano board, tying to write a hexacopter flight-controller myself.
Above are variables' types, and the following code inside void loop comes with error:

test_fly_004.ino: In function 'void loop()':
test_fly_004.ino:127:1: error: unable to find a register to spill in class 'POINTER_REGS'

Code: [Select]

  for(int k=0; i<=5; i++){
  motor[i] = basicThrust;
  motor[i] += (axScaler) * (ax[i]);
  motor[i] += (ayScaler) * (ay[i]);
  motor[i] += (azScaler) * (az[i]);
  }


Changed to while loop won't help either, IDE detects the same error.
Code: [Select]

  int k=0;
  while(i<=5){
  motor[i] = basicThrust;
  motor[i] += (axScaler) * (ax[i]);
  motor[i] += (ayScaler) * (ay[i]);
  motor[i] += (azScaler) * (az[i]);
  i++;
  }


I also checked IDE doesn't mixed " * multiplication " with " * dereference operator" by changing * into +.

But when I remove one of the variables:

Code: [Select]

  for(int k=0; i<=5; i++){
  motor[i] = basicThrust;
  motor[i] += (ax[i]);
  motor[i] += (ay[i]);
  motor[i] += (az[i]);
  }


Code: [Select]

  for(int k=0; i<=5; i++){
  motor[i] = basicThrust;
  motor[i] += (axScaler);
  motor[i] += (ayScaler);
  motor[i] += (azScaler);
  }


No errors now.

Does it mean I have to create another variable to temporarily store (axScaler) * (ax) , then add it on?Besides, I'd like to know why it happens.
Thanks.

jremington

Post all your code, using code tags.

Is this supposed to represent foul language? Either way, it won't compile.
Quote
const float ax[6] = {@*&#^%&};

econjack

There are a number of things that are, at best, fuzzy. First, what's this about:

Code: [Select]

const float ax[6] = {@*&#^%&};
const float ay[6] = {^%$#&*%};
const float az[6] = {%$#^%&*};


Those don't look like floating point constants. In the following expression:

Code: [Select]

 motor[i] += (axScaler) * (ax[i]);


You should post all of your code, too.

el_supremo

Code: [Select]
for(int k=0; i<=5; i++){
Why are you initializing 'k' but incrementing 'i' and also using 'i' as the array indices?

Pete
Don't send me technical questions via Private Message.

econjack

For some reason, part of my post was lost. The expression:

Code: [Select]

 motor[i] += (axScaler) * (ax[i]);


needs a cast. If you are truncating the float, then use:

Code: [Select]

 motor[i] += axScaler * (int) ax[i];


If you need the fractional component in the calculation, use these casts:

Code: [Select]

 motor[i] += (int) ( (float) axScaler * ax[i]);



Your original code is trying to pour four bytes of float information into a 2 byte int bucket, which runs the risk of losing data.

CCCanyon

Sorry for stupid mistakes while posting the question.

"#^%$$#" looks like this:
Code: [Select]

const float ax[6] = {-0.166666, 0.166666,-0.333333, 0.333333,-0.166666, 0.166666};
const float ay[6] = {-0.288675,-0.288675, 0.0, 0.0, 0.288675, 0.288675};
const float az[6] = { 1.0,-1.0,-1.0, 1.0, 1.0,-1.0};

And only "i", no "k"

Thank you econjack. I tried both, same error popped up.

and here's my code:
Code: [Select]

#include<adxl345.h>
#include<Wire.h>
#include<Servo.h>

int PPMcounter = 0;
unsigned long Stopwatch;
unsigned long Laststopwatch = 0;
float axScaler;
float ayScaler;
float azScaler;

const float ax[6] = {-0.166666, 0.166666,-0.333333, 0.333333,-0.166666, 0.166666};
const float ay[6] = {-0.288675,-0.288675, 0.0, 0.0, 0.288675, 0.288675};
const float az[6] = { 1.0,-1.0,-1.0, 1.0, 1.0,-1.0};

unsigned int Pulsewidth;
int Ch1pw;
int Ch2pw;
int Ch3pw;
int Ch4pw;
int Ch5pw;

int motor[6] = {900,900,900,900,900,900};

int Vacc[3];
adxl345 ACC;

unsigned long basicThrust = 1070;


Servo ESC1, ESC2, ESC3, ESC4, ESC5, ESC6;

void setup() {
  ACC.StartACC();
  ESC1.attach(11);
  ESC2.attach(10);
  ESC3.attach(9);
  ESC4.attach(6);
  ESC5.attach(5);
  ESC6.attach(3);
 
  ESC1.writeMicroseconds(motor[0]);
  ESC2.writeMicroseconds(motor[1]);
  ESC3.writeMicroseconds(motor[2]);
  ESC4.writeMicroseconds(motor[3]);
  ESC5.writeMicroseconds(motor[4]);
  ESC6.writeMicroseconds(motor[5]);
 
 
  delay(4000);
 
  attachInterrupt(0, PPM, CHANGE);
}

void loop() {
  ACC.GetVacc(Vacc);
 
  basicThrust = basicThrust+((Ch3pw - 1468)/100);
  basicThrust = constrain(basicThrust, 1000, 2000);
 
  axScaler = ((Ch1pw-1488)*15)/50;
  //axScaler += (Vacc[1])*(-10);
 
  ayScaler = ((1488-Ch2pw)*15)/50;
  //ayScaler += (Vacc[0])*(10);
 
  azScaler = ((Ch4pw-1484)*15)/120;
 
  int k=0;
  while(k<=5){
  motor[k] = basicThrust;// + axScaler*(-1/6) + ayScaler*(-1/34640) + azScaler*( 1);
  motor[k] += (axScaler) * ((int)ax[k]);
  motor[k] += (ayScaler) * ((int)ay[k]);
  motor[k] += (azScaler) * ((int)az[k]);
  k++;
  }
  if(Ch5pw > 1500)
  {
    motor[0] = 1000;
    motor[1] = 1000;
    motor[2] = 1000;
    motor[3] = 1000;
    motor[4] = 1000;
    motor[5] = 1000;
    basicThrust = 1000;
  }
 
 
  delay(50);
  noInterrupts();
  ESC1.writeMicroseconds(motor[0]);
  ESC2.writeMicroseconds(motor[1]);
  ESC3.writeMicroseconds(motor[2]);
  ESC4.writeMicroseconds(motor[3]);
  ESC5.writeMicroseconds(motor[4]);
  ESC6.writeMicroseconds(motor[5]);
  interrupts();
}
void PPM()
{
  Stopwatch = micros();
  Pulsewidth = Stopwatch - Laststopwatch;
  Laststopwatch = Stopwatch;
 
 
  if(Pulsewidth > 4000 )
    {
      PPMcounter = 0;
    }
   
   else if( PPMcounter == 1)
      {
        Ch1pw = Pulsewidth;
      }
   else if( PPMcounter == 2)
      {
        Ch2pw = Pulsewidth;
      }
   else if( PPMcounter == 3)
      {
        Ch3pw = Pulsewidth;
      }
   else if( PPMcounter == 4)
      {
        Ch4pw = Pulsewidth;
      }
   else
      {
        Ch5pw = Pulsewidth;
      }
   PPMcounter++;   

}

econjack

In expressions like these:

Code: [Select]

  basicThrust = constrain(basicThrust, 1000, 2000);
 
  axScaler = ((Ch1pw-1488)*15)/50;


all of the numeric constants are treated as int, which screws up the math. Use a type modifier to make it known to the compiler what the data type is, or use a decimal value for floats:

Code: [Select]

  basicThrust = constrain(basicThrust, 1000L, 2000L);   // Notice the 'L' so the compiler knows its a long
 
  axScaler = (( (float) Ch1pw-1488.0)*15.0)/50.0;


See if that helps.

CCCanyon

Thank you econjack. That will make my code more decent, since there will be more and more math.
"a = b * c"inside for/while still won't work. I'll forget about simplicity and use "d = b; d *= c; a += d "

nickgammon

Guess what?  [i]  renders your code in italics, and also makes it gibberish.

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up