Main Page | File List | Globals

robot.c

Go to the documentation of this file.
00001 //
00002 // C main: robot
00003 //
00004 // Description: Program of the leobot
00005 //
00006 //
00007 // Author: Leonhard Klein <leoklein@gmx.net>, (C) 2004
00008 #include <avr/io.h>
00009 #include <avr/interrupt.h>
00010 #include <avr/signal.h>
00011 #include <avr/eeprom.h>
00012 
00013 #include "robotsound.h"
00014 #include "robotu.h"
00015 #include "robotdo.h"
00016 #include "basic.h"
00017 #include "ir.h"
00018 
00019 
00020 SIGNAL(SIG_ADC) {
00021   static u08 irfrcount;
00022   static s16 irposstart;
00023   switch (admodus) {
00024   case irpos:                                      /* startup free running 4,8kHz */
00025     irfrcount = 0;
00026     admodus = irfr2;
00027     irposstart = ADC;
00028     return;
00029   case irfr2:                                      /* normal free running */
00030     irfrcount++;
00031     if (irfrcount < 20) {
00032       PORTB ^= _BV(PB4);
00033       return;
00034     } else {
00035       irpos_value = ADC - irposstart;
00036       PORTB &= ~_BV(PB4);
00037     }
00038     break;
00039   case irstation:
00040     irstation_value = ADC;
00041     break;
00042   case light:
00043     light_value = ADC >> 2;
00044     break;
00045   case vcc:
00046     vcc_value = 1000 - ADC;
00047     break;
00048   case vcco:
00049     vcco_value = ADC;
00050     break;
00051   }
00052   ADCSRA = 0;                                                           /* disable AD-Converter */
00053   admodus = none;
00054 }
00055 
00056 SIGNAL(SIG_OVERFLOW0) {                                                  /* Timer0 Overflow for threating */
00057   static u08 odp;
00058   struct measuretype mmodus;
00059   cli();
00060   if ((modus == ir_r)|(modus == ir_s)) {                                /* common check for common timing */
00061     TCNT0 = 255-111;                                                    /* 13.888 = 36000Hz */
00062     do_ir();
00063     sei();
00064     return;
00065   }
00066   if (modus == radar) {
00067     if (radar_value++ == 0)
00068       PORTD |= _BV(PD5);                                                /* set INIT */
00069     if ((radar_value > 1000) || (PINC & _BV(PC2))) {                    /* got ECHO or timeout */
00070       TCCR0 = _BV(CS01);                                                /* 2kHz - normal operation */
00071       modus = oldmodus;
00072       PORTD &= ~_BV(PD5);
00073       PORTD |= _BV(PD6);                                                /* switch radar off */
00074     }
00075     sei();
00076     return;
00077   }
00078   /* normal operation */
00079   time ++;
00080   decode_position();
00081   mmodus = measuremodus[modus];
00082   if                        (time % _BV(5) ==  0)   playi();          /* 1,935kHz / 32  = 60Hz  */
00083   if                        (time % _BV(7) ==  1)   drive();          /* 1,935kHz / 128 = 12Hz */
00084   if ((mmodus.light)     & ((time % _BV(8) ==  2))) start_ad(light);  /* 1,935kHz / 256 = 6Hz */
00085   if ((mmodus.vcc)       & ((time % _BV(8) ==  4))) start_ad(vcc);
00086   if ((mmodus.vcco)      & ((time % _BV(8) ==  6))) start_ad(vcco);
00087   if ((mmodus.irstation) & ((time % _BV(6) ==  8))) start_ad(irstation);
00088   if ((mmodus.radar)     & ((time % _BV(10)==  9))) {
00089     start_radar();
00090     return;
00091   }
00092   if ((mmodus.irpos)     & ((time % _BV(7) == 10))) start_ad(irpos);  /* 10: leave maximum time to rest for irstation */
00093   if ((mmodus.irr)       & (ISIRON)) {
00094     start_ir_r();
00095     return;
00096   }
00097   switch (modus) {
00098   case irstring_s:
00099     do_ir_string();
00100     break;
00101   case boredom:
00102     do_boredom();
00103     break;
00104   case sleep:
00105     do_sleep();
00106     break;
00107   case connected:
00108     do_connected();
00109     break;
00110   case searching:
00111     do_searching();
00112     break;
00113   case flee:
00114     do_flee();
00115     break;
00116   case empty:
00117     do_empty();
00118     break;
00119 #ifdef SCRIPT
00120   case script:
00121     do_script();
00122     break;
00123 #endif
00124 #ifdef IRCONT
00125   case ircont:
00126     do_ircont();
00127     break;
00128 #endif
00129   default: SETMODUS (boredom);
00130   }
00131   if (vcc_value < BATLOW) needpower = 1;
00132   if (vcc_value < BATEMPTY) {            /* rescuemodus */
00133     RESETP;
00134     modus = empty;
00135   }
00136   olddippos = DIPPOS;
00137   TCNT0 = 0;
00138   sei();
00139 }
00140 
00141 int main(void) {
00142   u16 i;
00143 #ifndef TEST
00144   for (i=0; i < 60000;i++) _NOP;
00145 #endif
00146   admodus = none;
00147   vcc_value = BATFULL;
00148   light_value = 255;
00149   start_sound(start_up, 1);
00150   switch (olddippos = DIPPOS) {
00151   case 0: modus = boredom; break;
00152   case 1: modus = connected; break;
00153   case 2: modus = ircont; break;
00154   case 3: modus = script; break;
00155   }
00156   /*  init_pins */
00157   DDRB  = _BV(DDB0)|_BV(DDB1)|_BV(DDB2)|_BV(DDB3)|_BV(DDB4);
00158   DDRD  = _BV(DDD5)|_BV(DDD6)|_BV(DDD7);
00159   PORTD = _BV(PD0)|_BV(PD1)|_BV(PD2)|_BV(PD3);          /* enable pull-up's for angle sensors */
00160   /*  init_int */
00161   TIMSK = TIMSK | _BV(TOIE0);
00162   TCNT0 = 0;
00163   TCCR0 = _BV(CS01);                                    /* Clock / 8 intervall is 4m/256*8~=2k */
00164   /*  init_pwm */
00165   TCCR1A= _BV(COM1A1)|_BV(COM1B1)|_BV(WGM10);           /* PWM 8bit*/
00166   TCCR1B= _BV(CS11)|_BV(CS10);                          /*  /64 */
00167   TCNT1 = 0;
00168   OCR1A = 0;
00169   OCR1B = 0;                                            /* reset timers */
00170 
00171   sei();                                   /* enable interrupts */
00172   for (;;)
00173     ;                                      /* loop forever */
00174 }

Generated on Wed Feb 2 20:03:50 2005 for Robot by doxygen 1.3.6