Skip to content

Commit

Permalink
Chase a heap corruption leak
Browse files Browse the repository at this point in the history
Signed-off-by: John Walicki <johnwalicki@gmail.com>
  • Loading branch information
johnwalicki committed Apr 13, 2021
1 parent a68dcdc commit f32794c
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions WatsonIoT/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,12 +565,11 @@ void Send10Seconds2Cloud() {
acceleration["y"].add(AccelRecord.y);
acceleration["z"].add(AccelRecord.z);
}

}

// Serialize the History Json object into a string to be transmitted
//serializeJson(historydoc,Serial); // print to console
static char historymsg[16384];;
static char historymsg[16384];
serializeJson(historydoc, historymsg, 16383);

int jsonSize = measureJson(historydoc);
Expand All @@ -585,6 +584,7 @@ void Send10Seconds2Cloud() {
NeoPixelStatus( LED_CONNECTED ); // Success - blink cyan
}

mqtt.setBufferSize( 2000 ); // reset the MQTT buffer size
historydoc.clear();
}

Expand Down Expand Up @@ -848,17 +848,25 @@ void loop() {
adxstatus = adxl355.getStatus();

if (adxstatus & Adxl355::STATUS_VALUES::FIFO_FULL) {
// Keep track of the heap in case heap fragmentation returns
//Serial.println( xPortGetFreeHeapSize() );
int numEntriesFifo = adxl355.readFifoEntries( (long *)fifoOut ) ;
if ( numEntriesFifo != -1 ) {
// Generate an array of json objects that contain x,y,z arrays of 32 floats.
// [{"x":[],"y":[],"z":[]},{"x":[],"y":[],"z":[]}]
JsonObject acceleration = traces.createNestedObject();

// Declare one AccelReading structure for this iteration of loop()
// so it doesn't need to go in and out of scope in various for() loops below
// typedef struct AccelXYZ {
// double x; double y; double z;
// } AccelReading ;
AccelReading AccelRecord;

// [{"x":[9.479,0],"y":[0.128,-1.113],"z":[-0.185,123.321]},{"x":[9.479,0],"y":[0.128,-1.113],"z":[-0.185,123.321]}]
double gal;
double x, y, z;
for (int i = 0; i < numEntriesFifo; i++) {
AccelReading AccelRecord;
gal = adxl355.valueToGals(fifoOut[i][0]);
x = round(gal*1000)/1000;
acceleration["x"].add(x);
Expand All @@ -878,13 +886,14 @@ void loop() {
}

// Do some STA / LTA math here...
// ...
char mathmsg[65];
sprintf(mathmsg, "Calculating STA/LTA from %d accelerometer readings", StaLtaQue.getCount());
//Serial.println(mathmsg);
if( StaLtaQue.isFull() ) {
/////////////////// find offset ////////////////
int queCount = StaLtaQue.getCount( );

for (int idx = 0; idx < queCount; idx++) {
AccelReading AccelRecord;
if( StaLtaQue.peekIdx( &AccelRecord, idx) ) {
sample[0] = AccelRecord.x;
sample[1] = AccelRecord.y;
Expand All @@ -897,13 +906,12 @@ void loop() {
for (int j = 0; j < 3; j++) {
offset[j] = sampleSUM[j] / (QUE_len);
}

/////////////////// find lta /////////////////
sampleSUM[0] = 0;
sampleSUM[1] = 0;
sampleSUM[2] = 0;
for (int idx = 0; idx < LTA_len; idx++) {
AccelReading AccelRecord;
if( StaLtaQue.peekIdx( &AccelRecord, idx) ) {
sampleABS[0] = abs( AccelRecord.x - offset[0] );
sampleABS[1] = abs( AccelRecord.y - offset[1] );
Expand All @@ -916,13 +924,12 @@ void loop() {
for (int j = 0; j < 3; j++) {
ltav[j] = sampleSUM[j] / (LTA_len);
}

//////////////////// find sta ///////////////////////
sampleSUM[0] = 0;
sampleSUM[1] = 0;
sampleSUM[2] = 0;
for (int idx = LTA_len-STA_len ; idx < LTA_len; idx++) {
AccelReading AccelRecord;
if( StaLtaQue.peekIdx( &AccelRecord, idx) ) {
sampleABS[0] = abs( AccelRecord.x - offset[0] );
sampleABS[1] = abs( AccelRecord.y - offset[1] );
Expand All @@ -939,15 +946,14 @@ void loop() {
if ( stalta[j] >= thresh ) {
// Whoa - STA/LTA algorithm detected some anomalous shaking
Serial.printf("STA/LTA = %f = %f / %f (%i)\n", stalta[j], stav[j], ltav[j], j );
bPossibleEarthQuake = true ;
bPossibleEarthQuake = true ;
}
}
}

//// find STA/LTA for the other 31 samples but without doing the summing again

for (int idx = LTA_len+1; idx < QUE_len; idx++) {
AccelReading AccelRecord;
if( StaLtaQue.peekIdx( &AccelRecord, idx) ) {
sample[0] = AccelRecord.x;
sample[1] = AccelRecord.y;
Expand Down Expand Up @@ -981,34 +987,41 @@ void loop() {
}
}

// If STA/LTA algorithm detected some anomalous shaking
if( bPossibleEarthQuake ) {
if( numSecsOfAccelReadings > 0 ) {
SendLiveData2Cloud();
numSecsOfAccelReadings-- ;
bPossibleEarthQuake=false;
} else if( bPossibleEarthQuake ) {
// The STA/LTA algorithm detected some anomalous shaking
// If this is continued shaking, the above SendLiveData2Cloud()
// function has already sent current accelerometer data
// so don't send it again.
bPossibleEarthQuake=false;

// Start sending 5 minutes of live accelerometer data
Serial.println("Start sending 5 minutes of live accelerometer data");
numSecsOfAccelReadings = 300 ;

// Send the previous 10 seconds of history to the cloud
Send10Seconds2Cloud();
}
char mathmsg[65];
sprintf(mathmsg, "%d accelerometer readings on the StaLta Queue", StaLtaQue.getCount());
Serial.println(mathmsg);
// When the math is done, drop 32 records off the queue

// When this loop is done, drop 32 records off the queue
if( StaLtaQue.isFull() ) {
for( int i=0; i < 32; i++ )
StaLtaQue.drop();
}

if( numSecsOfAccelReadings > 0 ) {
SendLiveData2Cloud();
numSecsOfAccelReadings-- ;
}
// Clear & Reset JsonArrays
jsonTraces.clear();
traces = jsonTraces.to<JsonArray>();

//Switch the direction of the LEDs
breathedirection = breathedirection ? false : true;
}

// Keep track of the heap in case heap fragmentation returns
//Serial.println( xPortGetFreeHeapSize() );
}
}

Expand Down

0 comments on commit f32794c

Please sign in to comment.