main/ims_sensorshoe.c
/*
 * Task to calibrate and process sensor measurements.
 * After calibration, measurements are sent to UDP class for transmission
 */
Task to calibrate and process sensor measurements.
After calibration, measurements are sent to UDP class for transmission
Task to calibrate and process sensor measurements . After calibration measurements are sent to UDP class for transmission
void sensor_eval_task(void *arg) {
	for(;;){
		if(xQueueReceive( globalPtrs->adc_q, in, pdMS_TO_TICKS(2000))) {
			if((xEventGroupGetBits(globalPtrs->system_event_group ) & SEND_RAW_DATA_ONLY) > 0) {
				xQueueSend( globalPtrs->udp_tx_q, (void *) in, ( TickType_t ) 0); 
			}
			else if((xEventGroupGetBits(globalPtrs->system_event_group ) & CALIBRATING) > 0) {
				if(!calibrate_running) {
					for (int ii = 0; ii < ADCBUFSIZE; ++ii ){
						max [ii] = 0;
						min [ii] = 0xFFFF;
						thresh[ii] = 0xFFFF;
					}
					calibrate_running = true;
				}
				for(int ii = 0; ii < ADCBUFSIZE; ++ii) {
					if(in->data[ii] > max[ii])
						max[ii] = in->data[ii];
					else if(in->data[ii] < min[ii])
						min[ii] = in->data[ii];
				}
			}
			else {
				if(calibrate_running || ((xEventGroupGetBits(globalPtrs->system_event_group) & NEW_THRESHOLD) > 0)) {
					calibrate_running = false;
					xEventGroupClearBits(globalPtrs->system_event_group, NEW_THRESHOLD);
					set_flash_uint8( threshold, "threshold" );
					float temp_thresh = (float)threshold / 100;
					for(int ii = 0; ii < ADCBUFSIZE; ++ii) {
						thresh[ii] = (uint16_t)(((float) (max[ii] - min[ii])) * temp_thresh) + min[ii];
					}
					ESP_LOGI(TAG,"thresh:%d,%d,%d,%d",thresh[0],thresh[1],thresh[2],thresh[3]);
					storeCalibration();
				}
				out->data = 0;
				for(int jj = 0; jj < ADCBUFSIZE; jj++){
					if(in->data[jj] > thresh[jj]){
						out->data |= ( 1 << jj );	
					}
				}
				out->nodeid = in->nodeid;
				out->counter = in->counter;
				xQueueSend( globalPtrs->udp_tx_q, (void *) out, ( TickType_t ) 0); 
			}
		}
	}
}
void sensor_eval_task ( void * arg ) { for ( ; ; ) { if ( xQueueReceive ( globalPtrs -> adc_q , in , pdMS_TO_TICKS ( 2000 ) ) ) { if ( ( xEventGroupGetBits ( globalPtrs -> system_event_group ) & SEND_RAW_DATA_ONLY ) > 0 ) { xQueueSend ( globalPtrs -> udp_tx_q , ( void * ) in , ( TickType_t ) 0 ) ; } else if ( ( xEventGroupGetBits ( globalPtrs -> system_event_group ) & CALIBRATING ) > 0 ) { if ( ! calibrate_running ) { for ( int ii = 0 ; ii < ADCBUFSIZE ; ++ ii ) { max [ ii ] = 0 ; min [ ii ] = 0xFFFF ; thresh [ ii ] = 0xFFFF ; } calibrate_running = true ; } for ( int ii = 0 ; ii < ADCBUFSIZE ; ++ ii ) { if ( in -> data [ ii ] > max [ ii ] ) max [ ii ] = in -> data [ ii ] ; else if ( in -> data [ ii ] < min [ ii ] ) min [ ii ] = in -> data [ ii ] ; } } else { if ( calibrate_running || ( ( xEventGroupGetBits ( globalPtrs -> system_event_group ) & NEW_THRESHOLD ) > 0 ) ) { calibrate_running = false ; xEventGroupClearBits ( globalPtrs -> system_event_group , NEW_THRESHOLD ) ; set_flash_uint8 ( threshold , " " ) ; float temp_thresh = ( float ) threshold / 100 ; for ( int ii = 0 ; ii < ADCBUFSIZE ; ++ ii ) { thresh [ ii ] = ( uint16_t ) ( ( ( float ) ( max [ ii ] - min [ ii ] ) ) * temp_thresh ) + min [ ii ] ; } ESP_LOGI ( TAG , " " , thresh [ 0 ] , thresh [ 1 ] , thresh [ 2 ] , thresh [ 3 ] ) ; storeCalibration ( ) ; } out -> data = 0 ; for ( int jj = 0 ; jj < ADCBUFSIZE ; jj ++ ) { if ( in -> data [ jj ] > thresh [ jj ] ) { out -> data |= ( 1 << jj ) ; } } out -> nodeid = in -> nodeid ; out -> counter = in -> counter ; xQueueSend ( globalPtrs -> udp_tx_q , ( void * ) out , ( TickType_t ) 0 ) ; } } } }
Task to calibrate and process sensor measurements.
Task to calibrate and process sensor measurements .
//			ESP_LOGI(TAG,"recv nodeid: %d, counter: %d", in->nodeid, in->counter); //			ESP_LOGI(TAG,"data:%d,%d,%d,%d thresh:%d,%d,%d,%d", in->data[0],in->data[1],in->data[2],in->data[3],thresh[0],thresh[1],thresh[2],thresh[3]); //If raw data mode is set, send raw adc data directly over udp //dont wait if queue is full //reset all calibration arrays //calibrate mode running //					ESP_LOGI(TAG,"data:%d,%d,%d,%d max:%d,%d,%d,%d  min:%d,%d,%d,%d", in.data[0],in.data[1],in.data[2],in.data[3],max[0],max[1],max[2],max[3],min[0],min[1],min[2],min[3]); //ESP_LOGI(TAG,"max:%d,%d,%d,%d  min:%d,%d,%d,%d",max[0],max[1],max[2],max[3],min[0],min[1],min[2],min[3]); //calculate threshold //ESP_LOGI(TAG, "End calibration or threshold change: threshold = %d", threshold); //TODO: save max, min and thresh values to flash //calibration not running, apply threshold to sensor values and send to udp task //enable a bit if the sensor measurement is above the threshold //				ESP_LOGI(TAG,"%c%c%c%c%c%c%c%c", BYTE_TO_BINARY(out->data)); //dont wait if queue is full