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 {
|
||||
uint8_t particles[10];
|
||||
uint8_t count;
|
||||
};
|
||||
|
||||
GridCell grid[GRID_SIZE][GRID_SIZE];
|
||||
// Optimized spatial grid using single array + cell indices
|
||||
uint8_t gridParticles[NUM_PARTICLES]; // All particles in grid order
|
||||
uint16_t gridCellStart[GRID_SIZE * GRID_SIZE]; // Start index for each cell
|
||||
uint8_t gridCellCount[GRID_SIZE * GRID_SIZE]; // Count for each cell
|
||||
|
||||
#ifdef DEBUG
|
||||
bool blinkState = true;
|
||||
#endif
|
||||
|
||||
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++) {
|
||||
int gx = constrain(particles[i].x / CELL_SIZE, 0, GRID_SIZE-1);
|
||||
int gy = constrain(particles[i].y / CELL_SIZE, 0, GRID_SIZE-1);
|
||||
|
||||
if(grid[gx][gy].count < 10) {
|
||||
grid[gx][gy].particles[grid[gx][gy].count++] = i;
|
||||
int cellIndex = gy * GRID_SIZE + gx;
|
||||
gridCellCount[cellIndex]++;
|
||||
}
|
||||
|
||||
// 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
|
||||
attachInterrupt(digitalPinToInterrupt(EXT0_PIN), DMPDataReady, FALLING);
|
||||
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(gy+dy < 0 || gy+dy >= GRID_SIZE) continue;
|
||||
|
||||
GridCell &cell = grid[gx+dx][gy+dy];
|
||||
for(int c=0; c<cell.count; c++) {
|
||||
const int j = cell.particles[c];
|
||||
int cellIndex = (gy+dy) * GRID_SIZE + (gx+dx);
|
||||
int cellStart = gridCellStart[cellIndex];
|
||||
int cellCount = gridCellCount[cellIndex];
|
||||
|
||||
for(int c = 0; c < cellCount; c++) {
|
||||
const int j = gridParticles[cellStart + c];
|
||||
if(j <= i) continue; // Avoid duplicate pairs
|
||||
|
||||
const float dx = particles[j].x - particles[i].x;
|
||||
|
@ -507,6 +531,10 @@ void reportIMU() {
|
|||
// Detect sudden changes (ignoring gravity component of ~16384)
|
||||
float accelDelta = abs(accelMagnitude - lastAccelMagnitude);
|
||||
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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue