示例#1
0
文件: task_adc.c 项目: glockwork/dfu
void vTaskAdc( void * args )
{
    static portTickType prevTime;
    static portTickType pointTime;
    static TData * st;
    int32_t i;
    st = data();
    filterInit();
    prevTime = xTaskGetTickCount();
    
    pointTime = 0;
    for ( ;; )
    {

        // Get ADC data.
        i = adc();
        // Filter ADC data.
        i = filter( i );
        // Put ADC data.
        adcSetData( i );
        vTaskDelayUntil( &prevTime, ADC_INTERVAL );
        pointTime += ADC_INTERVAL;
        if ( pointTime >= PLOT_TIME_PER_PT )
        {
            pointTime = 0;
            plotPlaceCurrentData();
        }
    }
}
示例#2
0
void writeKFValues() {
	Kalman filterx = filterInit(1);//Create Kalman Object X
	//Kalman filtery = filterInit(2);//Create Kalman Object Y
	analogCalibrate(1);//Calibrate Analog Port
	file = fopen("kf.csv", "w");
	while(1){
		//accelRead(KalmanObject)
		fprintf(file, "%d,%d\n", analogReadCalibrated(1), accelRead(filterx));
		printf("RawX: %d   \t FilteredX: %d\n\r",analogReadCalibrated(1),accelRead(filterx));
		//printf("RawY: %d\t FilteredY: %d\n\r",analogReadCalibrated(2),accelRead(filtery));
		delay(2);
	}
}
int main(void)
{
	/*Ждем пока все включится*/
	_delay_ms(100);
	
	/*Настраиваем порты ввода-вывода*/
	DDRB = 1<<PORTB0|1<<PORTB1|1<<PORTB2|1<<PORTB3|1<<PORTB4|1<<PORTB5|1<<PORTB6|1<<PORTB7;
	DDRC = 1<<PORTC0|1<<PORTC1|1<<PORTC2|0<<PORTC3|0<<PORTC4|0<<PORTC5|0<<PORTC6|0<<PORTC7;
	DDRD = 0<<PORTD0|0<<PORTD1|0<<PORTD2|0<<PORTD3|1<<PORTD4|0<<PORTD5|0<<PORTD6|1<<PORTD7;

	PORTB = 1;
	PORTD = 1 << PORTD2;
    
	/*Тяга двигателей на минимум*/
    for(uint8_t k = 0; k < CHANNELS_COUNT; ++k)
    {
        counter[k] = LOW;
    }
	
	/*Настраиваем I2C*/
	TWSR = 0x00;
	TWBR = ((F_CPU / I2C_SPEED) - 16) / 2;  
	_delay_us(10);

	/*Включаем Таймер0*/
	TCCR0 = 1<<CS02 | 0<<CS01 | 0<<CS00;
	
	/*Включаем Таймер1*/
	OCR1A=HIGH; //TOP
	TCCR1A=0<<COM1A1|0<<COM1A0|1<<COM1B1|0<<COM1B0|0<<FOC1A|0<<FOC1B|1<<WGM11|1<<WGM10;
	TCCR1B=0<<ICNC1|0<<ICES1|1<<WGM13|1<<WGM12|0<<CS12|0<<CS11|1<<CS10;
	TIMSK= 1<<TOIE2 | 1<<OCIE1A|1<<OCIE1B|0<<TOIE1|1<<TOIE0|0<<OCIE0;
	OCR1B=LOW;
	
	/*Включаем АЦП*/
	ADC_Init();   
    
	/*Включаем прерывание INT0(высотомер)*/
	INT0_Init();
	
	/*Разрешаем работу прерываний*/
	sei();
	
	/*Настраиваем Modbus*/
	eMBErrorCode eStatus = eMBInit( MB_RTU, 0x01, 0, 57600, MB_PAR_NONE );
	eStatus = eMBEnable();
    
	/*Настраиваем сенсоры*/    
	SensorsInit();
	
	/*Загружаем в Holding Registers и в массив параметров значения из EEPROM*/
	ModbusInitValues();
	
	filterInit();	
		
	while(1)
	{
		/*Актуализируем значения Modbus-регистров в соответствии со значениями параметров*/
		ModbusLoader();
		/*Актуализируем значения параметров в соответствии со значениями Holding Registers*/
		ModbusSaver();
		
		/*Итерация Modbus*/
		eMBPoll();
		
		/*Ресурсоемкий расчет курса*/		
		Course_Calc();
	}
}
/**
 * \brief main function of the data acquisition system
 * \author Jürgen Funck
 * \date 2010-03-24
 */
int main(void) {
	cli();	// disable interrupts globally
 	
	// set clock prescaler
	CLKPR = (1<<CLKPCE);
	CLKPR = 0;
	_delay_ms(2);


	// initialisation
	DDRC |= (1<<PC2);
	progState_t state = IDLE;
	adcInit();					// adc
	filterInit();	 			// filter 
	serialInit(BAUD_115200); 	// serial port
	state = IDLE;				// state

	serialSendString("Hello World!\n");

	// set sleep-mode
	set_sleep_mode(SLEEP_MODE_IDLE);

	sei(); // enable interrupts globally

	// main-loop
	int16_t val = 0;

	while(1) {
// ACQUIRE		
		switch(state) {
			case ANA_MEAS:
				if(adcIsRunning() > 0) {
					state = ANA_MEAS;
				}
				else {
					state = FILT;
				}
				break;
// FILTER			
		        case FILT:
				if(filterWaitingVals() > FILTER_ORD) {
					if(decimation==OFF) {
						val = filterFIR();//Identity();
					}
					else {
						val = filterFIRDecim();
					}
					while(serialSendWord(val)){}
				}
				else {
					filterReset();
					state = IDLE;
				}
				break;
			case IDLE: 
			default:
// WAIT FOR COMMANDS
				// check incomming messages
				state = checkMessages();

				if(state == IDLE) {
					// sleep until the next interrupt
					sleep_enable();
					sleep_cpu();
					sleep_disable();
				}
		}

	}

	return 0;
 }