code:ground_control
This is an old revision of the document!
This AVR code constantly checks the PWM input for pulse lenght. If a stream of 10 pulses are detected that match the criterion, the input PWM in routed to the output. A single failing PWM pulse will cause control to revert back to internal guidance. Useful for UAVs, allowing control to be regained from the ground in the event of a faulty control algorthym.
ground.h
#define pwm_record_factor (u08)F_CPU/500.0/256.0 #define lower_pwm_bound pwm_record_factor*4.25 //valid pwm limits #define upper_pwm_bound pwm_record_factor*1.75 #define GROUND void enable_ground_control(); void disable_ground_control();
ground.c
#include "main.h" #include "ground.h" volatile u08 pwm_counter=0x00; volatile u08 pwm_period=0x00; ISR(PCINT1_vect) { if(PORTC & 0x01) //start of C.0 pulse { TCNT0=0x00; //reset timer at start of C.0 pulse } else //end of C.0 pulse { if(TCNT0>upper_pwm_bound && TCNT0<lower_pwm_bound) //valid pwm { if(pwm_counter<0x0A) { pwm_counter++; } else { pwm_period=TCNT0; //store the pwm period, the kalman filter will like it! } } else { sbi(TCCR1A,COM1A1); //reenable the PWM pwm_counter=0x00; } } if(pwm_counter==0x0A) //a stream of valid pwm { cbi(TCCR1A,COM1A1); //turn off PWM output to servo PORTB=(PORTB & ~0x03)|(PORTC & 0x03); //move PORTC 0,1 to PORTB 0,1 } } ISR(TIMER0_OVF_vect) { sbi(TCCR1A,COM1A1); //reenable the PWM pwm_counter=0x00; //with valid pwm, there will be no overflows } void enable_ground_control() { sbi(TIMSK0, TOIE0); //Timer0 interrupts on sbi(PCICR, PCIE1); //Pin change interrupts on } void disable_ground_control() { cbi(TIMSK0, TOIE0); //Timer0 interrupts on cbi(PCICR, PCIE1); pwm_counter=0x00; //Pin change interrupts on }
code/ground_control.1208625337.txt.gz · Last modified: 2008/07/19 23:31 (external edit)