Esempio n. 1
0
void main()
{
	initialize_ports();
	
	for(;;) {

		//
		// update leds based on switches
		//
		
		update_ds1to4();
		
		//
		// buzz buzzer
		//
		
		if(sw(S1)) {
			costate {
				buzzon();
				waitfor(DelayMs(10));
				buzzoff();
				waitfor(DelayMs(200));
			}
		} else {
			buzzoff();
		}
		
		//
		// flash leds
		//
		
		costate {
			waitfor(DelayMs(500));
			ledoff(DS5);
			waitfor(DelayMs(1000));
			ledon(DS5);
		}

		costate {
			waitfor(DelayMs(1000));
			ledoff(DS6);
			waitfor(DelayMs(500));
			ledon(DS6);
		}
		
		costate {
			waitfor(DelayMs(250));
			ledoff(DS7);
			waitfor(DelayMs(1250));
			ledon(DS7);
		}
		
		costate {
			waitfor(DelayMs(750));
			ledoff(DS8);
			waitfor(DelayMs(900));
			ledon(DS8);
		}
	}
Esempio n. 2
0
///////////////////////////////////////////////////////////
// set DS1 and DS2 leds to stay on and off at intervals
///////////////////////////////////////////////////////////
cofunc flashled[2](int led, int ontime, int offtime)
{
	ledon(led);
	waitfor(DelayMs(ontime));
	ledoff(led);
	waitfor(DelayMs(offtime));
}
Esempio n. 3
0
/* Blinks the ACT LED. WARNING: NEVER RETURNS (INFINITE LOOP)*/
void ledblink()
{
	while(1)
	{
		ledon();
		Wait(500000);
		ledoff();
		Wait(500000);
	}
}
Esempio n. 4
0
int main()
{
	
	uint8_t k=0;uint8_t l=0;
	while(1){
	for(l=0;l<=8;l++)
		for(k=0;k<=8;k++)
		  ledon(l,k);
		
}
}
Esempio n. 5
0
void ledflash( uint32_t period , int duty )
{
	if ( gettime() % period > (period*duty)/16 )
	{
		ledon(LEDALL);
	}
	else
	{
		ledoff(LEDALL);
	}
	
	
}
Esempio n. 6
0
/* main loop */
int
main(void)
{
	input_line = (char *)malloc(6 * sizeof(char));

	led_setperm();

	set_time = 200000;

	while (strncmp(input_line, "quit", 4) != 0)
	{
		input_line = readline("Led Control> ");

		/* I know instruction parsing is really lame :/ */
		if (!strncmp(input_line, "help", 4))
			help();
		if (!strncmp(input_line, "ledon", 5))
			ledon();
		if (!strncmp(input_line, "ledoff", 6))
			ledoff();
		if (!strncmp(input_line, "settime", 7))
			settime();
		if (!strncmp(input_line, "volume", 6))
			volume();
		if (!strncmp(input_line, "bin", 3))
			bin();
		if (!strncmp(input_line, "slide", 5))
			slide();
		if (!strncmp(input_line, "blink", 5))
			blink();
		if (!strncmp(input_line, "bislide", 7))
			bislide();
		if (!strncmp(input_line, "biblink", 7))
			biblink();
		if (!strncmp(input_line, "grow", 4))
			grow();
		if (!strncmp(input_line, "center", 6))
			center();
		if (!strncmp(input_line, "side", 4))
			side();
		if (!strncmp(input_line, "biside", 6))
			biside();
		if (!strncmp(input_line, "demo", 4))
			demo();
	}

	led_off_all();

	exit(0);
}
Esempio n. 7
0
void failloop(int val)
{
	for (int i = 0; i <= 3; i++)
	  {
		  pwm_set(i, 0);
	  }

	while (1)
	  {
		  for (int i = 0; i < val; i++)
		    {
			    ledon(255);
			    delay(200000);
			    ledoff(255);
			    delay(200000);
		    }
		  delay(800000);
	  }

}
Esempio n. 8
0
/* initialize hardware */
void
init(void)
{
  led_init();
  ledon();

  strobo_init();
  strobooff();

  door_init();
  doordisengage();

  ring_init();

  /* timer initialization
   *
   * CTC mode
   * clocked from system clock
   * prescaler /1024
   * compare value 10
   *
   * this gives about 100 Hz at 1 MHz system clock
   *
   * we also enable the compare match interrupt
   * 
   */
  TCNT2 =  0;
  OCR2  = 10;
  TCCR2 = _BV(WGM21)|_BV(CS22)|_BV(CS21)|_BV(CS20);
  TIMSK = _BV(OCIE2);

  /* initialize usart */
  uinit(9600);

  /* enable interrupts */
  sei();
}
Esempio n. 9
0
void rider (int select,int kleur,int tijd)	//Select : 1=aan-links,2=uit-links,3=aan-rechts,4=uit-rechts,5=loop-links,6=loop-rechts,7=rider-links,8=rider-rechts	Kleur : 1=blauw,2=groen,3=cyaan,4=rood,5=paars,6=geel,7=wit
{
	switch (select)
	{
		case 1 :	ledon (1,kleur);	waitms(tijd);	ledon (2,kleur);	waitms(tijd);	ledon (3,kleur);	waitms(tijd);	ledon (4,kleur);	waitms(tijd);	break;
		case 2 :	ledoff(1,kleur);	waitms(tijd);	ledoff(2,kleur);	waitms(tijd);	ledoff(3,kleur);	waitms(tijd);	ledoff(4,kleur);	waitms(tijd);	break;
		case 3 :	ledon (4,kleur);	waitms(tijd);	ledon (3,kleur);	waitms(tijd);	ledon (2,kleur);	waitms(tijd);	ledon (1,kleur);	waitms(tijd);	break;
		case 4 :	ledoff(4,kleur);	waitms(tijd);	ledoff(3,kleur);	waitms(tijd);	ledoff(2,kleur);	waitms(tijd);	ledoff(1,kleur);	waitms(tijd);	break;
		
		case 5 :	ledon (1,kleur);	waitms(tijd);	ledoff(1,kleur);	ledon (2,kleur);	waitms(tijd);	ledoff(2,kleur);
					ledon (3,kleur);	waitms(tijd);	ledoff(3,kleur);	ledon (4,kleur);	waitms(tijd);	ledoff(4,kleur);									break;
					
		case 6 :	ledon (4,kleur);	waitms(tijd);	ledoff(4,kleur);	ledon (3,kleur);	waitms(tijd);	ledoff(3,kleur);
					ledon (2,kleur);	waitms(tijd);	ledoff(2,kleur);	ledon (1,kleur);	waitms(tijd);	ledoff(1,kleur);									break;
					
		case 7 :	ledon (1,kleur);	waitms(tijd);	ledon (2,kleur);	waitms(tijd);	ledon (3,kleur);	waitms(tijd);	ledon (4,kleur);	waitms(tijd);
					ledoff(1,kleur);	waitms(tijd);	ledoff(2,kleur);	waitms(tijd);	ledoff(3,kleur);	waitms(tijd);	ledoff(4,kleur);	waitms(tijd);	break;
					
		case 8 :	ledon (4,kleur);	waitms(tijd);	ledon (3,kleur);	waitms(tijd);	ledon (2,kleur);	waitms(tijd);	ledon (1,kleur);	waitms(tijd);
					ledoff(4,kleur);	waitms(tijd);	ledoff(3,kleur);	waitms(tijd);	ledoff(2,kleur);	waitms(tijd);	ledoff(1,kleur);	waitms(tijd);	break;
					
		default:	return; 																																		break;
	}
}
Esempio n. 10
0
int main(void)
{
	DDRD = 255;
	DDRB  |=  (1<<0);	DDRB  |=  (1<<1);	DDRB  |=  (1<<2);	DDRB  |=  (1<<3);	DDRB  &= ~(1<<4);	DDRB  &= ~(1<<5);

	PORTD = 0;
	PORTB &= ~(1<<0);	PORTB &= ~(1<<1);	PORTB &= ~(1<<2);	PORTB &= ~(1<<3);	PORTB &= ~(1<<4);	PORTB &= ~(1<<5);
	
	int knop1,knop2,select=0,kleur=0,tijd=400,a,b,rgb[3]={1,2,4},c;
	
	while(1)
	{
		knop1 = PINB&(1<<in1);	//==16 is hoog
		knop2 = PINB&(1<<in2);	//==32 is hoog
		
		if      ((knop1==16)&&(knop2==32))
		{
			for (a=0;a<3;a+=1)
			{
				for (b=0;b<10;b+=1)
				{
					ledon (7,rgb[a]);	waitms (50);	dark();		waitms (50);
				}
			}
			
			for (b=0;b<10;b+=1)
			{
				for (a=0;a<3;a+=1)
				{
					ledon (7,rgb[a]);	waitms (50);	dark();		waitms (50);
				}
			}
			
			for (c=120;c>=0;c-=20)
			{
				ledon (1,1);	waitms (c);		dark();
				ledon (2,2);	waitms (c);		dark();
				ledon (3,4);	waitms (c);		dark();
				ledon (4,1);	waitms (c);		dark();
				ledon (1,2);	waitms (c);		dark();
				ledon (2,4);	waitms (c);		dark();
				ledon (3,1);	waitms (c);		dark();
				ledon (4,2);	waitms (c);		dark();
				ledon (1,4);	waitms (c);		dark();
				ledon (2,1);	waitms (c);		dark();
				ledon (3,2);	waitms (c);		dark();
				ledon (4,4);	waitms (c);		dark();
			}
			
			for (c=0;c<=120;c+=20)
			{
				ledon (1,1);	waitms (c);		dark();
				ledon (2,2);	waitms (c);		dark();
				ledon (3,4);	waitms (c);		dark();
				ledon (4,1);	waitms (c);		dark();
				ledon (1,2);	waitms (c);		dark();
				ledon (2,4);	waitms (c);		dark();
				ledon (3,1);	waitms (c);		dark();
				ledon (4,2);	waitms (c);		dark();
				ledon (1,4);	waitms (c);		dark();
				ledon (2,1);	waitms (c);		dark();
				ledon (3,2);	waitms (c);		dark();
				ledon (4,4);	waitms (c);		dark();
			}
			
			fadein (7,7,500);
			fadeout(7,7,500);
			
			dark();
		}
		
		else if ((knop1==16)&&(knop2!=32))
		{
			ledon (5,1);	ledon (6,2);	waitms (tijd);	dark();
			ledon (5,2);	ledon (6,4);	waitms (tijd);	dark();
			ledon (5,4);	ledon (6,1);	waitms (tijd);	dark();
			ledon (5,1);	ledon (6,2);	waitms (tijd);	dark();
			ledon (5,2);	ledon (6,4);	waitms (tijd);	dark();
			ledon (5,4);	ledon (6,1);	waitms (tijd);	dark();
			dark();
		}
		
		else if ((knop1!=16)&&(knop2==32))
		{
			for (kleur=1;kleur<8;kleur+=1)
			{
				fadein (7,kleur,700);
				fadeout(7,kleur,700);
				waitms(600);
			}
			dark();
		}
		
		else
		{
			fadein (7,1,700);
			fadeout(7,1,700);
			waitms (600);
			dark();
		}
	}
}
Esempio n. 11
0
/* ... the usual ... */
int
main(void)
{
  presses    = 0;
  firstpress = 0;
  lastpress  = 0;
  lockdown   = LOCKED_BY_DEFAULT;
  strobo     = STROBE_BY_DEFAULT;

  /* initialize hardware */
  init();

  utx('I');

  /* main loop */
  while(1) {
    int event = waitevent();

    switch(event) {

    case EVENT_RING:
      utx('R');
      if(!lockdown) {
	/* if this is the first press, remember the time and enable signals */
	if(!presses) {
	  firstpress = gettick();
	}

        /* blink the led */
	ledoff();
	waitticks(BLINKTIME);
        ledon();

	/* if the number of presses reaches the limit, open the door */
	presses++;

        /* record time of last keypress */
        lastpress = gettick();

	/* if pressed often enough */
	if(presses >= PRESSES) {
          strobooff();
	  opendoor();
	  presses    = 0;
	  firstpress = 0;
          lastpress  = 0;
	}
      }
      break;

    case EVENT_TIMEOUT:
      utx('T');

      strobooff();

      /* reset the sequence detector */
      presses    = 0;
      firstpress = 0;
      lastpress  = 0;
      break;

    case EVENT_STROBE:
      if(strobo)
        stroboon();
      break;

    case EVENT_CMD_OPEN:
      opendoor();
      break;

    case EVENT_CMD_LOCK:
      utx('L');
      lockdown = TRUE;
      break;

    case EVENT_CMD_UNLOCK:
      utx('U');
      lockdown = FALSE;
      break;

    case EVENT_CMD_STROBO_ENABLE:
      utx('S');
      strobo = TRUE;
      break;

    case EVENT_CMD_STROBO_DISABLE:
      utx('s');
      strobooff();
      strobo = FALSE;
      break;
    }
  }
}
Esempio n. 12
0
void gyro_cal(void)
{
int data[6];
	
unsigned long time = gettime();
unsigned long timestart = time;
unsigned long timemax = time;
unsigned long lastlooptime;

	
// 2 and 15 seconds
while ( time - timestart < 2e6  &&  time - timemax < 15e6 )
	{	
		
		unsigned long looptime; 
		looptime = time - lastlooptime;
		lastlooptime = time;
		if ( looptime == 0 ) looptime = 1;
		
		//softi2c_readdata( 0x68 , 67 , data, 6 );

 i2c_readdata( 67 , data, 6 );
		
		gyro[0] = (int16_t) ((data[2]<<8) + data[3]);
		gyro[1] = (int16_t) ((data[0]<<8) + data[1]);
		gyro[2] = (int16_t) ((data[4]<<8) + data[5]);	
		
if ( (time - timestart)%200000 > 100000) 
{
	ledon(B00000101);
	ledoff(B00001010);
}
else 
{
	ledon(B00001010);
	ledoff(B00000101);
}
		 for ( int i = 0 ; i < 3 ; i++)
			{
				if ( fabs(gyro[i]) > 100 ) 
				{
					count = 0;
					timestart = gettime();
				}
				else
				{
					lpf( &gyrocal[i] , gyro[i], lpfcalc( (float) looptime , 0.5 * 1e6) );
					count++;
				}
				
			}
					
		time = gettime();
	}

if ( count < 100 )
{
	for ( int i = 0 ; i < 3; i++)
	{
		gyrocal[i] = 0;
	}
}
	
#ifdef SERIAL	
printf("gyro calibration  %f %f %f \n "   , gyrocal[0] , gyrocal[1] , gyrocal[2]);
#endif
	
}
Esempio n. 13
0
int main(void)
{

	clk_init();

	gpio_init();

#ifdef SERIAL
	serial_init();
#endif

	i2c_init();

	spi_init();

	pwm_init();

	pwm_set(MOTOR_FL, 0);	// FL
	pwm_set(MOTOR_FR, 0);
	pwm_set(MOTOR_BL, 0);	// BL
	pwm_set(MOTOR_BR, 0);	// FR

	time_init();


	if (RCC_GetCK_SYSSource() == 8)
	  {
          
	  }
	else
	  {
		  failloop(5);
	  }

	sixaxis_init();

	if (sixaxis_check())
	  {
#ifdef SERIAL
		  printf(" MPU found \n");
#endif
	  }
	else
	  {
#ifdef SERIAL
		  printf("ERROR: MPU NOT FOUND \n");
#endif
		  failloop(4);
	  }

	adc_init();

	rx_init();

	int count = 0;
	vbattfilt = 0.0;

	while (count < 64)
	  {
		  vbattfilt += adc_read(1);
		  count++;
	  }
       // for randomising MAC adddress of ble app - this will make the int = raw float value        
		random_seed =  *(int *)&vbattfilt ; 
		random_seed = random_seed&0xff;
      
	vbattfilt = vbattfilt / 64;

#ifdef SERIAL
	printf("Vbatt %2.2f \n", vbattfilt);
#ifdef NOMOTORS
	printf("NO MOTORS\n");
#warning "NO MOTORS"
#endif
#endif

#ifdef STOP_LOWBATTERY
// infinite loop
	if (vbattfilt < STOP_LOWBATTERY_TRESH)
		failloop(2);
#endif

// loads acc calibration and gyro dafaults
	loadcal();

	gyro_cal();

	rgb_init();
	
	imu_init();
	
	extern unsigned int liberror;
	if (liberror)
	  {
#ifdef SERIAL
		  printf("ERROR: I2C \n");
#endif
		  failloop(7);
	  }


	lastlooptime = gettime();
	extern int rxmode;
	extern int failsafe;

	float thrfilt;

//
//
//              MAIN LOOP
//
//


	checkrx();

	while (1)
	  {
		  // gettime() needs to be called at least once per second 
		  maintime = gettime();
		  looptime = ((uint32_t) (maintime - lastlooptime));
		  if (looptime <= 0)
			  looptime = 1;
		  looptime = looptime * 1e-6f;
		  if (looptime > 0.02f)	// max loop 20ms
		    {
			    failloop(3);
			    //endless loop                  
		    }
		  lastlooptime = maintime;

		  if (liberror > 20)
		    {
			    failloop(8);
			    // endless loop
		    }

		  sixaxis_read();

		  control();

// battery low logic
				
		float hyst;
		float battadc = adc_read(1);
vbatt = battadc;
		// average of all 4 motor thrusts
		// should be proportional with battery current			
		extern float thrsum; // from control.c
		// filter motorpwm so it has the same delay as the filtered voltage
		// ( or they can use a single filter)		
		lpf ( &thrfilt , thrsum , 0.9968f);	// 0.5 sec at 1.6ms loop time	
		
		lpf ( &vbattfilt , battadc , 0.9968f);		

#ifdef AUTO_VDROP_FACTOR

static float lastout[12];
static float lastin[12];
static float vcomp[12];
static float score[12];
static int current_index = 0;

int minindex = 0;
float min = score[0];

{
	int i = current_index;

	vcomp[i] = vbattfilt + (float) i *0.1f * thrfilt;
		
	if ( lastin[i] < 0.1f ) lastin[i] = vcomp[i];
	float temp;
	//	y(n) = x(n) - x(n-1) + R * y(n-1) 
	//  out = in - lastin + coeff*lastout
		// hpf
	 temp = vcomp[i] - lastin[i] + FILTERCALC( 1000*12 , 1000e3) *lastout[i];
		lastin[i] = vcomp[i];
		lastout[i] = temp;
	 lpf ( &score[i] , fabsf(temp) , FILTERCALC( 1000*12 , 10e6 ) );

	}
	current_index++;
	if ( current_index >= 12 ) current_index = 0;

	for ( int i = 0 ; i < 12; i++ )
	{
	 if ( score[i] < min )  
		{
			min = score[i];
			minindex = i;
		}
}

#undef VDROP_FACTOR
#define VDROP_FACTOR  minindex * 0.1f
#endif

		if ( lowbatt ) hyst = HYST;
		else hyst = 0.0f;

		vbatt_comp = vbattfilt + (float) VDROP_FACTOR * thrfilt;

		if ( vbatt_comp <(float) VBATTLOW + hyst ) lowbatt = 1;
		else lowbatt = 0;
		

// led flash logic              

		  if (rxmode != RX_MODE_BIND)
		    {
					// non bind                    
			    if (failsafe)
			      {
				      if (lowbatt)
					      ledflash(500000, 8);
				      else
					      ledflash(500000, 15);
			      }
			    else
			      {
				      if (lowbatt)
					      ledflash(500000, 8);
				      else
					{
						if (ledcommand)
						  {
							  if (!ledcommandtime)
								  ledcommandtime = gettime();
							  if (gettime() - ledcommandtime > 500000)
							    {
								    ledcommand = 0;
								    ledcommandtime = 0;
							    }
							  ledflash(100000, 8);
						  }
						else
						{
							if ( aux[LEDS_ON] )
							ledon( 255);
							else 
							ledoff( 255);
						}
					}
			      }
		    }
		  else
		    {		// bind mode
			    ledflash(100000 + 500000 * (lowbatt), 12);
		    }

// rgb strip logic   
#if (RGB_LED_NUMBER > 0)				
	extern void rgb_led_lvc( void);
	rgb_led_lvc( );
#endif
				
#ifdef BUZZER_ENABLE
	buzzer();
#endif

#ifdef FPV_ON
			static int fpv_init = 0;
			if ( rxmode == RX_MODE_NORMAL && ! fpv_init ) {
				fpv_init = gpio_init_fpv();
			}
			if ( fpv_init ) {
				if ( failsafe ) {
					GPIO_WriteBit( FPV_PIN_PORT, FPV_PIN, Bit_RESET );
				} else {
					GPIO_WriteBit( FPV_PIN_PORT, FPV_PIN, aux[ FPV_ON ] ? Bit_SET : Bit_RESET );
				}
			}
#endif

	checkrx();
				
					
	// loop time 1ms                
	while ((gettime() - maintime) < (1000 - 22) )
			delay(10);



	}			// end loop


}
Esempio n. 14
0
int main(void)
{

	clk_init();

	gpio_init();

#ifdef SERIAL
	serial_init();
#endif

	i2c_init();

	spi_init();

	pwm_init();

	pwm_set(MOTOR_FL, 0);	// FL
	pwm_set(MOTOR_FR, 0);
	pwm_set(MOTOR_BL, 0);	// BL
	pwm_set(MOTOR_BR, 0);	// FR

	time_init();


#ifdef SERIAL
	printf("\n clock source:");
#endif
	if (RCC_GetCK_SYSSource() == 8)
	  {
#ifdef SERIAL
		  printf(" PLL \n");
#endif
	  }
	else
	  {
#ifdef SERIAL
		  if (RCC_GetCK_SYSSource() == 0)
			  printf(" HSI \n");
		  else
			  printf(" OTHER \n");
#endif
		  failloop(5);
	  }

	sixaxis_init();

	if (sixaxis_check())
	  {
#ifdef SERIAL
		  printf(" MPU found \n");
#endif
	  }
	else
	  {
#ifdef SERIAL
		  printf("ERROR: MPU NOT FOUND \n");
#endif
		  failloop(4);
	  }

	adc_init();

	rx_init();

	int count = 0;
	float vbattfilt = 0.0;

	while (count < 64)
	  {
		  vbattfilt += adc_read(1);
		  count++;
	  }
	vbattfilt = vbattfilt / 64;

#ifdef SERIAL
	printf("Vbatt %2.2f \n", vbattfilt);
#ifdef NOMOTORS
	printf("NO MOTORS\n");
#warning "NO MOTORS"
#endif
#endif

#ifdef STOP_LOWBATTERY
// infinite loop
	if (vbattfilt < STOP_LOWBATTERY_TRESH)
		failloop(2);
#endif

// loads acc calibration and gyro dafaults
	loadcal();

	gyro_cal();

	imu_init();
	
	extern unsigned int liberror;
	if (liberror)
	  {
#ifdef SERIAL
		  printf("ERROR: I2C \n");
#endif
		  failloop(7);
	  }


	lastlooptime = gettime();
	extern int rxmode;
	extern int failsafe;

	float thrfilt;

//
//
//              MAIN LOOP
//
//


	checkrx();

	while (1)
	  {
		  // gettime() needs to be called at least once per second 
		  maintime = gettime();
		  looptime = ((uint32_t) (maintime - lastlooptime));
		  if (looptime <= 0)
			  looptime = 1;
		  looptime = looptime * 1e-6f;
		  if (looptime > 0.02f)	// max loop 20ms
		    {
			    failloop(3);
			    //endless loop                  
		    }
		  lastlooptime = maintime;

		  if (liberror > 20)
		    {
			    failloop(8);
			    // endless loop
		    }

		  sixaxis_read();

		  control();

// battery low logic

		  float battadc = adc_read(1);

		  // average of all 4 motor thrusts
		  // should be proportional with battery current                  
		  extern float thrsum;	// from control.c
		  // filter motorpwm so it has the same delay as the filtered voltage
		  // ( or they can use a single filter)           
		  lpf(&thrfilt, thrsum, 0.9968);	// 0.5 sec at 1.6ms loop time   


		  lpf(&vbattfilt, battadc, 0.9968);

		  if (vbattfilt + VDROP_FACTOR * thrfilt < VBATTLOW)
			  lowbatt = 1;
		  else
			  lowbatt = 0;

// led flash logic              

		  if (rxmode != RX_MODE_BIND)
		    {		// non bind                    
			    if (failsafe)
			      {
				      if (lowbatt)
					      ledflash(500000, 8);
				      else
					      ledflash(500000, 15);
			      }
			    else
			      {
				      if (lowbatt)
					      ledflash(500000, 8);
				      else
					{
						if (ledcommand)
						  {
							  if (!ledcommandtime)
								  ledcommandtime = gettime();
							  if (gettime() - ledcommandtime > 500000)
							    {
								    ledcommand = 0;
								    ledcommandtime = 0;
							    }
							  ledflash(100000, 8);
						  }
						else
							ledon(255);
					}
			      }
		    }
		  else
		    {		// bind mode
			    ledflash(100000 + 500000 * (lowbatt), 12);
		    }


		  checkrx();
#ifdef DEBUG
		  elapsedtime = gettime() - maintime;
#endif
// loop time 1ms                
		  while ((gettime() - maintime) < 1000)
			  delay(10);



	  }			// end loop


}
Esempio n. 15
0
int main(void)
{

	clk_init();
	
	delay(1000);
	
  gpio_init();

#ifdef SERIAL	
	serial_init();
#endif

	i2c_init();
	
	spi_init();
	
	pwm_init();

	pwm_set( MOTOR_FL , 0);   // FL
	pwm_set( MOTOR_FR , 0);	 
	pwm_set( MOTOR_BL , 0);   // BL
	pwm_set( MOTOR_BR , 0);   // FR

  time_init();
	

#ifdef SERIAL	
	printf( "\n clock source:" );
	#endif
	if (  RCC_GetCK_SYSSource() == 8)  
	{
		#ifdef SERIAL	
		printf( " PLL \n" );
		#endif
	}
	else 
	{
	#ifdef SERIAL	
	if (  RCC_GetCK_SYSSource() == 0)  printf( " HSI \n" );
	else  printf( " OTHER \n" );
	#endif
  failloop( 5 );
	}

	sixaxis_init();
	
	if ( sixaxis_check() ) 
	{
		#ifdef SERIAL	
		printf( " MPU found \n" );
		#endif
	}
	else 
	{
		#ifdef SERIAL	
		printf( "ERROR: MPU NOT FOUND \n" );	
		#endif
		failloop(4);
	}
	
	adc_init();
	
	rx_init();
	
int count = 0;
	
while ( count < 64 )
{
	vbattfilt += adc_read(1);
	count++;
}
 vbattfilt = vbattfilt/64;	

#ifdef SERIAL	
		printf( "Vbatt %2.2f \n", vbattfilt );
		#ifdef NOMOTORS
    printf( "NO MOTORS\n" );
		#warning "NO MOTORS"
		#endif
#endif
	
#ifdef STOP_LOWBATTERY
// infinite loop
if ( vbattfilt < (float) STOP_LOWBATTERY_TRESH) failloop(2);
#endif

	gyro_cal();

	
extern unsigned int liberror;
if ( liberror ) 
{
	  #ifdef SERIAL	
		printf( "ERROR: I2C \n" );	
		#endif
		failloop(7);
}


 static unsigned lastlooptime; 
 lastlooptime = gettime();
 extern int rxmode;
 extern int failsafe;

 float thrfilt;

//
//
// 		MAIN LOOP
//
//

#ifdef DEBUG
static float timefilt;
#endif

	while(1)
	{
		// gettime() needs to be called at least once per second 
		unsigned long time = gettime(); 
		looptime = ((uint32_t)( time - lastlooptime));
		if ( looptime <= 0 ) looptime = 1;
		looptime = looptime * 1e-6f;
		if ( looptime > 0.02f ) // max loop 20ms
		{
			failloop( 3);	
			//endless loop			
		}
	
		#ifdef DEBUG				
		totaltime += looptime;
		lpf ( &timefilt , looptime, 0.998 );
		#endif
		lastlooptime = time;
		
		if ( liberror > 20) 
		{
			failloop(8);
			// endless loop
		}
		
		checkrx();
		
		gyro_read();
		
		control();
		
// battery low logic
		static int lowbatt = 0;
		float hyst;
		float battadc = adc_read(1);

		// average of all 4 motor thrusts
		// should be proportional with battery current			
		extern float thrsum; // from control.c
		// filter motorpwm so it has the same delay as the filtered voltage
		// ( or they can use a single filter)		
		lpf ( &thrfilt , thrsum , 0.9968f);	// 0.5 sec at 1.6ms loop time	
		
		lpf ( &vbattfilt , battadc , 0.9968f);		

		if ( lowbatt ) hyst = HYST;
		else hyst = 0.0f;
		
		if ( vbattfilt + (float) VDROP_FACTOR * thrfilt <(float) VBATTLOW + hyst ) lowbatt = 1;
		else lowbatt = 0;
		
// led flash logic		
		if ( rxmode == 0)
		{// bind mode
		ledflash ( 100000+ 500000*(lowbatt) , 12);
		}else
		{// non bind
		if ( failsafe) 
		{
		if ( lowbatt )
				ledflash ( 500000 , 8);
		else
				ledflash ( 500000, 15);	
			
		}
			else
			{					
			if ( lowbatt) 
				 ledflash ( 500000 , 8);	
			else ledon( 255);	
			} 		
		}

// the delay is required or it becomes endless loop ( truncation in time routine)
while ( (gettime() - time) < 1000 ) delay(10); 		

		
	}// end loop
	

}