Exemplo n.º 1
0
int main()
{



	if (_is_docked()){
		for(int j=0;(j<10);j++)
			for(int i=0;(i<200);i++)
				_delay_ms(5);
	}
				
	scounter=0;
	//Initialized data buffer
	dataIndex=0;
	dataSubindex=0;
	// Blink green for 5 seconds	

	_wocket_initialize();
		

	AC_NUMS=_SAMPLING_RATE *60;
	power_adc_disable();
  	power_spi_disable();
  	power_timer0_disable();
  	power_timer1_disable();
  	power_twi_disable();


	while(1){
			

			

		//Sample only in the main loop because of p
		if(sampleFlag){			
			power_adc_enable();
			_atmega_adc_turn_on();
			sampleFlag=0;
#ifdef _VERSION ==3




			x=_atmega_a2dConvert10bit(ADC0);
		
			y=_atmega_a2dConvert10bit(ADC1);

			z=_atmega_a2dConvert10bit(ADC2);
			//x=y=z=cc++;
			//if (cc>=1024)
			//	cc=0;

				vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

				if (_wPC>40){	//Skip the first samples						
					if (summary_count==0)
					{
						vmag=vmag/24;
						if (vmag>65535)
							acount[ci]=65535;
						else
							acount[ci]=(unsigned short) vmag;
				 		vmag=0;
						++ci;
						if (ci==AC_BUFFER_SIZE)
							ci=0;
						cseq++;
		
						if (ci==si)
						{
							si++;
							if (si==AC_BUFFER_SIZE)
								si=0;
							sseq++;
						}
						acount[ci]=0;
						summary_count=AC_NUMS;
					}else
						summary_count--;
				}
				else if (_wPC==40)
					vmag=0;

			
#else
			//x=_atmega_a2dConvert10bit(ADC3);
			//y=_atmega_a2dConvert10bit(ADC2);
			//z=_atmega_a2dConvert10bit(ADC1);		
#endif
		

		
			 m_SET_X(data[dataIndex],x,dataSubindex);
			 m_SET_Y(data[dataIndex],y,dataSubindex);
			 m_SET_Z(data[dataIndex],z,dataSubindex);

			 dataSubindex++;
			 if (dataSubindex>=4)
			 	dataSubindex=0;
		

			 
			 //Most of the time the data buffer with 750 will not overflow
			 //and will be enough to transmit the data, data will go from 0 up to a specific
			 //value

			if (_wTM==_TM_Continuous)
			{
									
				switch(dataSubindex){
				case 1:
						m_GET_X(x,data[dataIndex].byte1,data[dataIndex].byte2,0);
						m_GET_Y(y,data[dataIndex].byte2,data[dataIndex].byte3,0);
						m_GET_Z(z,data[dataIndex].byte3,data[dataIndex].byte4,0);
						break;
				case 2:
						m_GET_X(x,data[dataIndex].byte4,data[dataIndex].byte5,1);
						m_GET_Y(y,data[dataIndex].byte6,data[dataIndex].byte7,1);
						m_GET_Z(z,data[dataIndex].byte7,data[dataIndex].byte8,1);
						break;
				case 3:
						m_GET_X(x,data[dataIndex].byte8,data[dataIndex].byte9,2);
						m_GET_Y(y,data[dataIndex].byte9,data[dataIndex].byte10,2);
						m_GET_Z(z,data[dataIndex].byte11,data[dataIndex].byte12,2);
						break;
				case 0:
						m_GET_X(x,data[dataIndex].byte12,data[dataIndex].byte13,3);
						m_GET_Y(y,data[dataIndex].byte13,data[dataIndex].byte14,3);
						m_GET_Z(z,data[dataIndex].byte14,data[dataIndex].byte15,3);
						break;
				}
											
		
				if (justconnected==1)
				{
					_send_tm();
					justconnected=2;
				}		
				//_send_pdu(x,y,z);
				_send_uncompressed_pdu(x, y, z);

				
					//Send summary activity count
				/*	for (int i=0;(i<summaryindex);i++){
						_send_summary_count(acount[i]);
						acount[i]=0;
					}
					if (summaryindex<AC_BUFFER_SIZE){
						acount[0]=acount[summaryindex];
						summaryindex=0;
					}*/

			}
			else 
			{
				if ((dataSubindex==0) && (batch_counter<750))
					batch_counter++;
				if (connected){
					_greenled_turn_on();
						
					gotack=1;
					tester=0;

					if (_wTM==_TM_Continuous)
						continue;
						
					for (int ixz=0;(ixz<100);ixz++) {                                                                                      
       						_bluetooth_transmit_uart0_byte(0xff); 

							
						if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}						
					}

					_send_fv();
					_send_sr();					 
					_send_tm();
					_send_batch_count((batch_counter-1)*4);																	
					_send_acs();
					//Send summary activity count
					/*for (int i=0;(i<summaryindex);i++){
						_send_summary_count(acount[i]);
						acount[i]=0;
					}
					if (summaryindex<AC_BUFFER_SIZE){
						acount[0]=acount[summaryindex];
						summaryindex=0;
					}*/

					
					if ((batch_counter>0) && (batch_counter<750)) // Go from 0 up to batch_counter
					{						
						for (int i=0;(i<(batch_counter-1));i++)
						{
							m_GET_X(x,data[i].byte1,data[i].byte2,0);
							m_GET_Y(y,data[i].byte2,data[i].byte3,0);
							m_GET_Z(z,data[i].byte3,data[i].byte4,0);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[i].byte4,data[i].byte5,1);
							m_GET_Y(y,data[i].byte6,data[i].byte7,1);
							m_GET_Z(z,data[i].byte7,data[i].byte8,1);							
							//_send_uncompressed_pdu(x,y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[i].byte8,data[i].byte9,2);
							m_GET_Y(y,data[i].byte9,data[i].byte10,2);
							m_GET_Z(z,data[i].byte11,data[i].byte12,2);
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[i].byte12,data[i].byte13,3);
							m_GET_Y(y,data[i].byte13,data[i].byte14,3);
							m_GET_Z(z,data[i].byte14,data[i].byte15,3);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							_receive_data();


							if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}
						}

						
						if (batch_counter>0){
						//copy end item into start
						data[0].byte1=data[batch_counter].byte1;
						data[0].byte2=data[batch_counter].byte2;
						data[0].byte3=data[batch_counter].byte3;
						data[0].byte4=data[batch_counter].byte4;
						data[0].byte5=data[batch_counter].byte5;
						data[0].byte6=data[batch_counter].byte6;
						data[0].byte7=data[batch_counter].byte7;
						data[0].byte8=data[batch_counter].byte8;
						data[0].byte9=data[batch_counter].byte9;
						data[0].byte10=data[batch_counter].byte10;
						data[0].byte11=data[batch_counter].byte11;
						data[0].byte12=data[batch_counter].byte12;
						data[0].byte13=data[batch_counter].byte13;
						data[0].byte14=data[batch_counter].byte14;
						data[0].byte15=data[batch_counter].byte15;
						}


					}else{

						int current=dataIndex+1;
						int end =dataIndex;
						if (current>=750)
							current=0;
						while(current!=end)
						{
							m_GET_X(x,data[current].byte1,data[current].byte2,0);
							m_GET_Y(y,data[current].byte2,data[current].byte3,0);
							m_GET_Z(z,data[current].byte3,data[current].byte4,0);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[current].byte4,data[current].byte5,1);
							m_GET_Y(y,data[current].byte6,data[current].byte7,1);
							m_GET_Z(z,data[current].byte7,data[current].byte8,1);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[current].byte8,data[current].byte9,2);
							m_GET_Y(y,data[current].byte9,data[current].byte10,2);
							m_GET_Z(z,data[current].byte11,data[current].byte12,2);
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[current].byte12,data[current].byte13,3);
							m_GET_Y(y,data[current].byte13,data[current].byte14,3);
							m_GET_Z(z,data[current].byte14,data[current].byte15,3);							
							//_send_uncompressed_pdu(x,y, z);
							
							_send_pdu(x,y,z);

							current++;
							if (current==750)
								current=0;

							_receive_data();


							if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}
							
						}

						//copy end item into start
						data[0].byte1=data[end].byte1;
						data[0].byte2=data[end].byte2;
						data[0].byte3=data[end].byte3;
						data[0].byte4=data[end].byte4;
						data[0].byte5=data[end].byte5;
						data[0].byte6=data[end].byte6;
						data[0].byte7=data[end].byte7;
						data[0].byte8=data[end].byte8;
						data[0].byte9=data[end].byte9;
						data[0].byte10=data[end].byte10;
						data[0].byte11=data[end].byte11;
						data[0].byte12=data[end].byte12;
						data[0].byte13=data[end].byte13;
						data[0].byte14=data[end].byte14;
						data[0].byte15=data[end].byte15;
					}


			

					
					batch_counter=0;
					dataIndex=0;
					seconds_passed=0;
					while (seconds_passed<400)
					{
						_delay_ms(5);
						seconds_passed++;
						_receive_data();

						if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}						
					}						
					//connected=0;

					//Don't turn off the radio if a request to switch mode has been received
					if (_wTM==_TM_Continuous)
						_bluetooth_turn_on();	
					else
						_bluetooth_turn_off();		
					
					command_counter=0;
					seconds_disconnected=0;
					_greenled_turn_off();
				
				}
			}
			_atmega_adc_turn_off();
			power_adc_disable();

			if ((dataSubindex==0) && (!connected))
				dataIndex++;			
			if (dataIndex==750)
				dataIndex=0;
			connected=0;			
			
		}	
		
			cli();
			set_sleep_mode(SLEEP_MODE_IDLE);
			//set_sleep_mode(SLEEP_MODE_PWR_SAVE);
    		sleep_enable();
    		sleep_bod_disable(); 	
    		sei();
    		sleep_cpu();
    		sleep_disable();


	}

	return 0;
}
Exemplo n.º 2
0
void _wocket_initialize(void) //This function initializes the wocket
{
	// Disable the watchdog timer. It has to be done at the beginning of the program.
	_atmega_disable_watchdog();
	_atmega_initialize(CPU_CLK_PRESCALAR_1024);
	num_skipped_timer_interrupts = 10;//(F_CPU/1024)/PERFECT_SAMPLING_FREQUENCY;
	
	unsigned short battery = _atmega_a2dConvert10bit(IN_VSENSE_BAT);
	if (battery < 700)
	{// Blink yellow 3times for 5 seconds if the battery is not fully charged 
		for (int i = 0; (i < 3); i++){
			_yellowled_turn_on();		
			for (int j = 0; (j < 200); j++)
				_delay_ms(5);
			_yellowled_turn_off();
			for (int j = 0; (j < 200); j++)
				_delay_ms(5);
		}
	}
	
	if (battery > 100)
	{//Load the status byte from the EEPROM  
		_INITIALIZED = eeprom_read_byte(&_NV_INITIALIZED);		
	}
	else
	{ //turn on the yellow led for 5 seconds then shutdown 
		_yellowled_turn_on();		
		for(int  i = 0; (i < 1000); i++)
			_delay_ms(5);
		_yellowled_turn_off();
		_delay_ms(500);
		_atmega_finalize();
		return;
	}
	
	// If the wocket has been initialized before, read the parameters from EEPROM and blinks green once	
	if (_INITIALIZED == _WOCKET_INITIALIZED)
	{		
		if (battery > 300)
		{
			_SAMPLING_RATE = eeprom_read_byte(&_NV_SAMPLING_RATE);
			_wTM = eeprom_read_byte(&_NV_WTM);

			_wTCNT2 = eeprom_read_byte(&_NV_TCT);
			_wTCNT2_reps = eeprom_read_byte(&_NV_TCTREPS);
			_wTCNT2_last = eeprom_read_byte(&_NV_TCTLAST);

			_wBTCAL100 = eeprom_read_word(&_NV_BTCAL100);
			_wBTCAL80  = eeprom_read_word(&_NV_BTCAL80);
			_wBTCAL60  = eeprom_read_word(&_NV_BTCAL60);
			_wBTCAL40  = eeprom_read_word(&_NV_BTCAL40);
			_wBTCAL20  = eeprom_read_word(&_NV_BTCAL20);
			_wBTCAL10  = eeprom_read_word(&_NV_BTCAL10);

			_wX1G_CAL  = eeprom_read_word(&_NV_X1G_CAL);
			_wXN1G_CAL = eeprom_read_word(&_NV_XN1G_CAL);
			_wY1G_CAL  = eeprom_read_word(&_NV_Y1G_CAL);
			_wYN1G_CAL = eeprom_read_word(&_NV_YN1G_CAL);
			_wZ1G_CAL  = eeprom_read_word(&_NV_Z1G_CAL);
			_wZN1G_CAL = eeprom_read_word(&_NV_ZN1G_CAL);

			_wPDT = eeprom_read_byte(&_NV_PDT);

			_greenled_turn_on();		
			for(int i = 0; (i < 200); i++)
				_delay_ms(10);
			_greenled_turn_off();
		}
	}

		
	// If the wocket has never been initialized, write the default settings and blink green 3 times 
	else
	{ 
		_SAMPLING_RATE = 40; 
		_wTM = _WTM_Continuous;
		//_wTM = _WTM_Burst_60;

		// Calculate the timer variables used to sample at the right frequency
		_wocket_initialize_timer2_interrupt();
		
		// Set the overflow interrupt timer 		
		switch(_wTM)
		{
			case _WTM_Continuous:	
				_MAX_SAMPLING_RATE = 126; //This limitation is due to the definition of SEND_SR  and GET_SR commands
				break;					  // The MCU provided in wocket able to sample at higher rates 								
			case _WTM_Burst_30:    	//a transfer mode that send the burst every 30 secs
				_MAX_SAMPLING_RATE = 80;		
				break;
			case _WTM_Burst_60:		//a transfer mode that send the burst every 60 secs
				_MAX_SAMPLING_RATE = 40;		
				break;
			case _WTM_Burst_90:		//a transfer mode that send the burst every 90 secs
				_MAX_SAMPLING_RATE = 30;		
				break;
			case _WTM_Burst_120:		//a transfer mode that send the burst every 120 secs
				_MAX_SAMPLING_RATE = 20;		
				break;
			default:
				break;
		}
		
		if (_SAMPLING_RATE > _MAX_SAMPLING_RATE)
		{
			_SAMPLING_RATE = _MAX_SAMPLING_RATE;
		
		}
		if (_SAMPLING_RATE < _MIN_SAMPLING_RATE)
		{
			_SAMPLING_RATE = _MIN_SAMPLING_RATE;		
		}
			
		
		if (battery > 300)
		{	
			// Write the parameters to the EEPROM
			eeprom_write_byte(&_NV_SAMPLING_RATE,_SAMPLING_RATE);
				
			eeprom_write_byte(&_NV_TCT,_wTCNT2);
			eeprom_write_byte(&_NV_TCTREPS,_wTCNT2_reps);
			eeprom_write_byte(&_NV_TCTLAST,_wTCNT2_last);			

			eeprom_write_byte(&_NV_WTM,_wTM);
			eeprom_write_byte(&_NV_STATUS_BYTE,0x00);
			eeprom_write_byte(&_NV_SENS,_wSENS);

			//Set default battery calibration values
			eeprom_write_word(&_NV_BTCAL100,_DEFAULTBTCAL100);
			eeprom_write_word(&_NV_BTCAL80, _DEFAULTBTCAL80);
			eeprom_write_word(&_NV_BTCAL60, _DEFAULTBTCAL60);
			eeprom_write_word(&_NV_BTCAL40, _DEFAULTBTCAL40);
			eeprom_write_word(&_NV_BTCAL20, _DEFAULTBTCAL20);
			eeprom_write_word(&_NV_BTCAL10, _DEFAULTBTCAL10);

			_wBTCAL100 = _DEFAULTBTCAL100;
			_wBTCAL80  = _DEFAULTBTCAL80;
			_wBTCAL60  = _DEFAULTBTCAL60;
			_wBTCAL40  = _DEFAULTBTCAL40;
			_wBTCAL20  = _DEFAULTBTCAL20;
			_wBTCAL10  = _DEFAULTBTCAL10;

			//Set default Accelerometer calibration values
			eeprom_write_word(&_NV_X1G_CAL, _DEFAULT_X1G_CAL);
			eeprom_write_word(&_NV_XN1G_CAL,_DEFAULT_XN1G_CAL);
			eeprom_write_word(&_NV_Y1G_CAL, _DEFAULT_Y1G_CAL);
			eeprom_write_word(&_NV_YN1G_CAL,_DEFAULT_YN1G_CAL);
			eeprom_write_word(&_NV_Z1G_CAL, _DEFAULT_Z1G_CAL);
			eeprom_write_word(&_NV_ZN1G_CAL,_DEFAULT_ZN1G_CAL);

			_wX1G_CAL  = _DEFAULT_X1G_CAL;
			_wXN1G_CAL = _DEFAULT_XN1G_CAL;
			_wY1G_CAL  = _DEFAULT_Y1G_CAL;
			_wYN1G_CAL = _DEFAULT_YN1G_CAL;
			_wZ1G_CAL  = _DEFAULT_Z1G_CAL;
			_wZN1G_CAL = _DEFAULT_ZN1G_CAL;

			_wPDT = _DEFAULT_PDT;
			eeprom_write_byte(&_NV_PDT, _wPDT);
		}

		// Set the initialized flag in the status byte
		_INITIALIZED = _WOCKET_INITIALIZED;

		// Write the status byte to the EEPROM		
		eeprom_write_byte(&_NV_INITIALIZED,_INITIALIZED);
				
		// Blink green for 5 seconds	
		for (int i = 0; (i < 3); i++){
			_greenled_turn_on();		
			for(int j = 0;(j < 200); j++)
				_delay_ms(5);
			_greenled_turn_off();
			for(int j = 0; (j < 200); j++)
				_delay_ms(5);
		}		
	}

	_DEFAULT_SHUTDOWN = (unsigned long)_wPDT * (unsigned long)_SAMPLING_RATE * (unsigned long)60;
	_wShutdownTimer = _DEFAULT_SHUTDOWN;
	
    // Enable Timer 2 
    _atmega_enable_timer2(CPU_CLK_PRESCALAR_1024); 
}