theFPVgeek / aeroquad

Automatically exported from code.google.com/p/aeroquad
0 stars 0 forks source link

Variables used in for loops are declared global #70

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The variables channel, axis and motor are declared global in AeroQuad.h and 
used as for loop control variables all over the code. These must be declared 
local, which saves another ~400-500 byte. Best way for the compiler is to 
declare them in the for loop, e.g.
    for (byte motor=FRONT; motor < LASTMOTOR; motor++)
    for (byte channel = ROLL; channel < LASTCHANNEL; channel++) {
    for (byte axis = ROLL; axis < LASTAXIS; axis++) {

Original issue reported on code.google.com by al...@arcor.de on 27 Dec 2010 at 4:00

GoogleCodeExporter commented 8 years ago
When this local declaration is done in pid.h, the compiler makes 
zeroIntegralError() inline and it unrolls the code, so it gets quite large. 
Prevent this by forcing zeroIntegralError() to be noinline.

void zeroIntegralError() __attribute__ ((noinline));
void zeroIntegralError() {
  for (byte axis = ROLL; axis < LASTLEVELAXIS; axis++)
    PID[axis].integratedError = 0;
}

Original comment by al...@arcor.de on 27 Dec 2010 at 4:06

GoogleCodeExporter commented 8 years ago
Pretty amazing!  Those updates saved about 700 bytes.  Do you think these 
updates cause the code to run slower though?

Original comment by CaranchoEngineering@gmail.com on 7 Jan 2011 at 7:42

GoogleCodeExporter commented 8 years ago
The fix causes the code to run faster, it takes longer to increment and use a 
global integer than a byte register, but not so much, maybe it saves 0.5 µs 
per usage. For the overall speed of this has just a minor effect, as the serial 
and eeprom stuff does not run in the main loop and is relative slow anyway.

Original comment by al...@arcor.de on 7 Jan 2011 at 9:52