//polls for the end of an adc conversion event. Then processe buffer to extract the averaged //value. It takes this value and averages it with the existing value in an 8 position buffer //which serves as a super fast place for other code to retrieve ADC values // This is only used when RAWADC is not defined void sys_io_adc_poll() { if (obufn != bufn) { uint32_t tempbuff[8] = {0,0,0,0,0,0,0,0}; //make sure its zero'd //the eight or four enabled adcs are interleaved in the buffer //this is a somewhat unrolled for loop with no incrementer. it's odd but it works for (int i = 0; i < 256;) { tempbuff[3] += adc_buf[obufn][i++]; tempbuff[2] += adc_buf[obufn][i++]; tempbuff[1] += adc_buf[obufn][i++]; tempbuff[0] += adc_buf[obufn][i++]; } //now, all of the ADC values are summed over 32/64 readings. So, divide by 32/64 (shift by 5/6) to get the average //then add that to the old value we had stored and divide by two to average those. Lots of averaging going on. for (int j = 0; j < 4; j++) { adc_values[j] += (tempbuff[j] >> 6); adc_values[j] = adc_values[j] >> 1; } for (int i = 0; i < NUM_ANALOG; i++) { int val; val = getRawADC(i); // addNewADCVal(i, val); // adc_out_vals[i] = getADCAvg(i); adc_out_vals[i] = val; } obufn = bufn; }
/* Poll the ADC for altitude values and put the * latest values on the altitude queue. */ static void vGetAltitude(void *pvParameters) { unsigned int groundValue = getRawADC(); unsigned int maxValue = groundValue - 270; unsigned int rawADCvalue; float altitude; xQueueHandle xOLEDQueue = ( xQueueHandle ) pvParameters; portTickType xLastWakeTime = xTaskGetTickCount(); /* Define a period of 2 milliseconds (500Hz) */ const portTickType xPeriod = ( 3 / portTICK_RATE_MS ); for ( ;; ) { vTaskDelayUntil( &xLastWakeTime, xPeriod ); rawADCvalue = getRawADC(); //Dynamically adjust the ground voltage value (differs on each helicopter). if (rawADCvalue > groundValue) { groundValue = rawADCvalue; } //Dynamically adjust the max altitude voltage value. if (rawADCvalue < (groundValue - 320)) { rawADCvalue = maxValue; } //Convert the voltage values to a percentage of the total height. altitude = 100.0f * (groundValue - rawADCvalue) / (groundValue - maxValue); /* Uncomment to send the current altitude to the OLED */ //xQueueMessage message = { CURRENT_ALTITUDE, altitude }; //xQueueSendToBack( xOLEDQueue, &message, 0 ); xAltMessage altMessage = { altitude }; xQueueSendToBack( xAltQueue, &altMessage, 0 ); } }