code:ground_control
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
code:ground_control [2008/05/01 00:10] – laurenceb | code:ground_control [2008/11/13 09:50] (current) – laurenceb | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | 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. | + | ===== Input capture based version ===== |
+ | This uses the input capture function on timer1 (atmega168). The ICNC1 bit in the TCCR1B register need to be set to enable oversampling to give more noise tolerance. A second function running approx every 20ms is needed to decrement counter by two and check if counter> | ||
+ | |||
+ | ==== Header ==== | ||
+ | <code c> | ||
+ | void enable_ground_control(); | ||
+ | void disable_ground_control(); | ||
+ | #define PULSE_MIN (s16)(0.001*(float)F_CPU/ | ||
+ | #define PULSE_MAX (s16)(0.002*(float)F_CPU/ | ||
+ | #define GROUND_LED_ON PORTD|=(1<< | ||
+ | #define GROUND_LED_OFF PORTD& | ||
+ | #define GROUND_LED_SET PORTD& | ||
+ | void run_ground_control(); | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====Main code=== | ||
+ | |||
+ | <code c> | ||
+ | #ifdef GROUND | ||
+ | #include " | ||
+ | extern volatile u08 counter; | ||
+ | extern volatile u16 diff; | ||
+ | |||
+ | |||
+ | ISR(TIMER1_CAPT_vect) | ||
+ | { | ||
+ | static u16 start; | ||
+ | if(TCCR1B& | ||
+ | { | ||
+ | TCCR1B& | ||
+ | start=ICR1; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | TCCR1B|=0x40; | ||
+ | if(!(counter& | ||
+ | { | ||
+ | diff=ICR1; | ||
+ | if(start> | ||
+ | { | ||
+ | diff+=TOP_COUNT; | ||
+ | } | ||
+ | diff-=start; | ||
+ | if(diff> | ||
+ | { | ||
+ | if((counter& | ||
+ | { | ||
+ | counter+=3; | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | counter& | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void run_ground_control() | ||
+ | { | ||
+ | if((~0x80& | ||
+ | { | ||
+ | counter|=0x80; | ||
+ | OCR1B=diff; | ||
+ | counter& | ||
+ | GROUND_LED_ON; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | GROUND_LED_OFF; | ||
+ | } | ||
+ | if(counter& | ||
+ | counter-=2; | ||
+ | } | ||
+ | |||
+ | void enable_ground_control() | ||
+ | { | ||
+ | TIMSK1|=(1<< | ||
+ | counter=0; | ||
+ | } | ||
+ | |||
+ | void disable_ground_control() | ||
+ | { | ||
+ | TIMSK1& | ||
+ | GROUND_LED_OFF; | ||
+ | counter=0; | ||
+ | diff=0; | ||
+ | } | ||
+ | #endif | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Pin change based version ===== | ||
+ | |||
+ | |||
+ | 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 is 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 ===== | ===== ground.h ===== |
code/ground_control.1209600606.txt.gz · Last modified: 2008/07/19 23:31 (external edit)