memory optimization
This commit is contained in:
parent
6d07be2015
commit
c9b897d180
1 changed files with 44 additions and 16 deletions
|
@ -252,27 +252,44 @@ void mpuActiveMonitorMode() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct GridCell {
|
// Optimized spatial grid using single array + cell indices
|
||||||
uint8_t particles[10];
|
uint8_t gridParticles[NUM_PARTICLES]; // All particles in grid order
|
||||||
uint8_t count;
|
uint16_t gridCellStart[GRID_SIZE * GRID_SIZE]; // Start index for each cell
|
||||||
};
|
uint8_t gridCellCount[GRID_SIZE * GRID_SIZE]; // Count for each cell
|
||||||
|
|
||||||
GridCell grid[GRID_SIZE][GRID_SIZE];
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
bool blinkState = true;
|
bool blinkState = true;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void buildSpatialGrid() {
|
void buildSpatialGrid() {
|
||||||
memset(grid, 0, sizeof(grid));
|
// Clear cell counts
|
||||||
|
memset(gridCellCount, 0, sizeof(gridCellCount));
|
||||||
|
|
||||||
|
// Count particles per cell
|
||||||
for(int i = 0; i < NUM_PARTICLES; i++) {
|
for(int i = 0; i < NUM_PARTICLES; i++) {
|
||||||
int gx = constrain(particles[i].x / CELL_SIZE, 0, GRID_SIZE-1);
|
int gx = constrain(particles[i].x / CELL_SIZE, 0, GRID_SIZE-1);
|
||||||
int gy = constrain(particles[i].y / CELL_SIZE, 0, GRID_SIZE-1);
|
int gy = constrain(particles[i].y / CELL_SIZE, 0, GRID_SIZE-1);
|
||||||
|
int cellIndex = gy * GRID_SIZE + gx;
|
||||||
if(grid[gx][gy].count < 10) {
|
gridCellCount[cellIndex]++;
|
||||||
grid[gx][gy].particles[grid[gx][gy].count++] = i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate start indices (prefix sum)
|
||||||
|
gridCellStart[0] = 0;
|
||||||
|
for(int i = 1; i < GRID_SIZE * GRID_SIZE; i++) {
|
||||||
|
gridCellStart[i] = gridCellStart[i-1] + gridCellCount[i-1];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset counts for filling
|
||||||
|
memset(gridCellCount, 0, sizeof(gridCellCount));
|
||||||
|
|
||||||
|
// Fill particle indices
|
||||||
|
for(int i = 0; i < NUM_PARTICLES; i++) {
|
||||||
|
int gx = constrain(particles[i].x / CELL_SIZE, 0, GRID_SIZE-1);
|
||||||
|
int gy = constrain(particles[i].y / CELL_SIZE, 0, GRID_SIZE-1);
|
||||||
|
int cellIndex = gy * GRID_SIZE + gx;
|
||||||
|
int insertPos = gridCellStart[cellIndex] + gridCellCount[cellIndex];
|
||||||
|
gridParticles[insertPos] = i;
|
||||||
|
gridCellCount[cellIndex]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,6 +353,10 @@ void setup() {
|
||||||
#endif
|
#endif
|
||||||
attachInterrupt(digitalPinToInterrupt(EXT0_PIN), DMPDataReady, FALLING);
|
attachInterrupt(digitalPinToInterrupt(EXT0_PIN), DMPDataReady, FALLING);
|
||||||
MPUIntStatus = mpu.getIntStatus();
|
MPUIntStatus = mpu.getIntStatus();
|
||||||
|
|
||||||
|
// Initialize sleep timer (start counting from device boot/wake)
|
||||||
|
lastZMot = millis();
|
||||||
|
zMotInterrupt = false;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
@ -380,9 +401,12 @@ void applyPhysics() {
|
||||||
if(gx+dx < 0 || gx+dx >= GRID_SIZE) continue;
|
if(gx+dx < 0 || gx+dx >= GRID_SIZE) continue;
|
||||||
if(gy+dy < 0 || gy+dy >= GRID_SIZE) continue;
|
if(gy+dy < 0 || gy+dy >= GRID_SIZE) continue;
|
||||||
|
|
||||||
GridCell &cell = grid[gx+dx][gy+dy];
|
int cellIndex = (gy+dy) * GRID_SIZE + (gx+dx);
|
||||||
for(int c=0; c<cell.count; c++) {
|
int cellStart = gridCellStart[cellIndex];
|
||||||
const int j = cell.particles[c];
|
int cellCount = gridCellCount[cellIndex];
|
||||||
|
|
||||||
|
for(int c = 0; c < cellCount; c++) {
|
||||||
|
const int j = gridParticles[cellStart + c];
|
||||||
if(j <= i) continue; // Avoid duplicate pairs
|
if(j <= i) continue; // Avoid duplicate pairs
|
||||||
|
|
||||||
const float dx = particles[j].x - particles[i].x;
|
const float dx = particles[j].x - particles[i].x;
|
||||||
|
@ -507,6 +531,10 @@ void reportIMU() {
|
||||||
// Detect sudden changes (ignoring gravity component of ~16384)
|
// Detect sudden changes (ignoring gravity component of ~16384)
|
||||||
float accelDelta = abs(accelMagnitude - lastAccelMagnitude);
|
float accelDelta = abs(accelMagnitude - lastAccelMagnitude);
|
||||||
maxAccelImpulse = max(maxAccelImpulse * 0.9f, accelDelta);
|
maxAccelImpulse = max(maxAccelImpulse * 0.9f, accelDelta);
|
||||||
|
|
||||||
|
// Reset sleep timer on ANY motion/DMP data (device is active)
|
||||||
|
lastZMot = millis();
|
||||||
|
zMotInterrupt = false; // Clear zero motion flag since we have motion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue