/******************************************************************************* AlphaBot - Utils Utils.c *******************************************************************************/ #include #include "AlphaBot.h" //------------------------------------------------------------------------------ typedef struct { unsigned int uiFrequency, uiDuration; } SoundBit; ulong ulTickCount = 0; char cSpeed[2]; ulong ulMileage[2]; ulong ulSpeed_PrevTick[2], ulSpeed_CurTick[2]; uint uiSpeed_PrevCount[2], uiSpeed_CurCount[2]; uint uiSpeed_StalledSheelCounter[2]; _idat char cHeadPosition = 0; _idat uchar ucLrSensorDistance = 0; bit bButtonState = 0; //------------------------------------------------------------------------------ void _interrupt(3) _using(1) TickCounter(void) //-- every 10 ms { TIMER_RELOAD; //-- Reload the timer immediately ++ulTickCount; if( abs(cSpeed[LEFT]) > 0 ) uiSpeed_StalledSheelCounter[LEFT]++; else uiSpeed_StalledSheelCounter[LEFT]=0; if( abs(cSpeed[RIGHT]) > 0 ) uiSpeed_StalledSheelCounter[RIGHT]++; else uiSpeed_StalledSheelCounter[RIGHT]=0; } // interrupt TickCounter //----------------------------------------------------------------------------------------- /* char sign( long lParam ) { if( lParam < 0 ) return -1; else return 1; } // sign //------------------------------------------------------------------------------ */ void Delay( unsigned int uiMSecs ) { _idat uchar i; while( uiMSecs-- ) for( i=0; i < 230; i++ ) { _nop(); _nop(); } } // Delay //------------------------------------------------------------------------------ void SetBeeperFreq( unsigned int uiFreq ) { ulong ulVal = 65536 - 1843200 / uiFreq; RCAP2H = ulVal / 256; RCAP2L = ulVal % 256; } // SetBeeperFreq //------------------------------------------------------------------------------ void Beep( unsigned int uiFreq, unsigned int uiMsec ) { // set beeper frequency SetBeeperFreq(uiFreq); //-- beep for specified preiod of time BEEPER_ON; Delay(uiMsec); BEEPER_OFF; } // Beep //------------------------------------------------------------------------------ void SetTwrColor(TwrColor eColor) { /* LED_TWR_YELLOW = (eColor & 0x01) != 0 ? LED_ON : LED_OFF; LED_TWR_BLUE = (eColor & 0x02) != 0 ? LED_ON : LED_OFF; LED_TWR_RED = (eColor & 0x04) != 0 ? LED_ON : LED_OFF; */ switch( eColor ) { case TWR_OFF: LED_TWR_YELLOW=LED_OFF; LED_TWR_RED=LED_OFF; LED_TWR_BLUE=LED_OFF; break; case TWR_YELLOW:LED_TWR_YELLOW=LED_ON; LED_TWR_RED=LED_OFF; LED_TWR_BLUE=LED_OFF; break; case TWR_BLUE: LED_TWR_YELLOW=LED_OFF; LED_TWR_RED=LED_OFF; LED_TWR_BLUE=LED_ON; break; case TWR_RED: LED_TWR_YELLOW=LED_OFF; LED_TWR_RED=LED_ON; LED_TWR_BLUE=LED_OFF; break; case TWR_GREEN: LED_TWR_YELLOW=LED_ON; LED_TWR_RED=LED_OFF; LED_TWR_BLUE=LED_ON; case TWR_PURPLE:LED_TWR_YELLOW=LED_OFF; LED_TWR_RED=LED_ON; LED_TWR_BLUE=LED_ON; case TWR_ORANGE:LED_TWR_YELLOW=LED_ON; LED_TWR_RED=LED_ON; LED_TWR_BLUE=LED_OFF; default: // TWR_ON = 0x07 // 111 LED_TWR_YELLOW=LED_ON; LED_TWR_RED=LED_ON; LED_TWR_BLUE=LED_ON; } } // SetTwrColor //------------------------------------------------------------------------------ rom const SoundBit Sounds[][11] = { // SND_OK { {700,120}, {600,160}, {0} }, // SND_ERROR { {150,1000}, {0} }, // SND_RND { {1680,350}, {1440,225}, {1520,225}, {1320,75}, {1120,75}, {960,75}, {1680,350}, {800,225}, {1680,175}, {0} }, // SND_CALL { {1760,100}, {1400,50}, {1400,150}, {1680,75}, {1520,25}, {1200,225}, {960,25}, {1720,100}, {1520,125}, {880,50}, {0} }, // SND_CONFUSED { {1120,250}, {960,500}, {1680,350}, {800,75}, {0} }, // SND_2WAY { {880,50}, {960,50}, {1040,50}, {1120,50}, {1760,50}, {1600,50}, {1440,50}, {1360,50}, {1200,50}, {1120,50}, {0} }, // SND_PHONE, { {1800,35}, {1680,27}, {1800,35}, {1680,27}, {1800,35}, {1680,27}, {1800,35}, {1680,27}, {0} }, // SND_DEFAULT { {850,80}, {900,80}, {1250,120}, {0} } }; // Sounds void Sound( SoundType eSound ) { uchar i; BEEPER_ON; for( i=0; Sounds[eSound][i].uiFrequency != 0; i++ ) // Beep( Sounds[eSound][i].uiFrequency, Sounds[eSound][i].uiDuration ); { SetBeeperFreq(Sounds[eSound][i].uiFrequency); Delay(Sounds[eSound][i].uiDuration); } BEEPER_OFF; } // Sound //------------------------------------------------------------------------------ void Show( ShowMode eShowMode ) { _idat uint i; switch( eShowMode ) { case S_START: //-- 3 short beeps & blinks Delay(1000); SetTwrColor(TWR_YELLOW); Beep(850,70); SetTwrColor(TWR_OFF); Delay(200); SetTwrColor(TWR_RED); Beep(900,70); SetTwrColor(TWR_OFF); Delay(200); SetTwrColor(TWR_BLUE); Beep(1250,150); SetTwrColor(TWR_OFF); Delay(500); break; case S_DIAG_BEGIN: Beep(800,250); break; case S_DIAG_ERROR: SetTwrColor(TWR_RED); Sound(SND_ERROR); SetTwrColor(TWR_OFF); break; case S_DIAG_OK: SetTwrColor(TWR_GREEN); Sound(SND_OK); SetTwrColor(TWR_OFF); break; case S_BUMP_LEFT: case S_BUMP_RIGHT: case S_BUMP_REAR: SetTwrColor(TWR_PURPLE); Beep(350,100); SetTwrColor(TWR_OFF); break; case S_BUMP_IR_SHORT: SetTwrColor(TWR_PURPLE); Delay(100); SetTwrColor(TWR_OFF); break; case S_IR_LONG_SCAN: SetTwrColor(TWR_YELLOW); Beep(250,30); SetTwrColor(TWR_OFF); break; case S_GOTHERE: SetTwrColor(TWR_BLUE); Sound(SND_CALL); SetTwrColor(TWR_OFF); break; case S_TIME2SLEEP: BEEPER_ON; for( i=4000; i >= 150; i-=200) { SetTwrColor(TWR_YELLOW); SetBeeperFreq(i); SetTwrColor(TWR_OFF); Delay(120); } BEEPER_OFF; break; case S_SNORE: for( i=200; i >= 150; i-=10 ) { SetTwrColor(TWR_GREEN); Beep(i,100); SetTwrColor(TWR_OFF); Delay(100); } break; case S_WAKEUP: SetTwrColor(TWR_PURPLE); Beep(500,50); SetTwrColor(TWR_OFF); Delay(220); SetTwrColor(TWR_PURPLE); Beep(500,80); SetTwrColor(TWR_OFF); Delay(60); SetTwrColor(TWR_PURPLE); Beep(500,80); SetTwrColor(TWR_OFF); Delay(80); SetTwrColor(TWR_PURPLE); Beep(1200,400);SetTwrColor(TWR_OFF); break; case S_WAITING: //-- Generic "wait-do-nothing", before sensor/diagnostic re-checks. for( i=0; i<3; i++ ) { SetTwrColor(TWR_PURPLE); Delay(500); SetTwrColor(TWR_OFF); Delay(500); } break; case S_RANDOM: i = 2 + rand() % 10; while( --i > 0 ) { SetTwrColor( rand() % (TWR_ON+1) ); Beep( 200+rand()%1500, 30 + rand()%200 ); } SetTwrColor(TWR_OFF); break; case S_TILTED: for(i = 0; i< 3; i++ ) { SetTwrColor(TWR_RED); LED_HEART_OFF; Beep( 1000, 70 ); SetTwrColor(TWR_OFF); LED_HEART_ON; Delay(70); } break; case S_STALLED: SetTwrColor(TWR_RED); Sound(SND_2WAY); SetTwrColor(TWR_OFF); break; case S_DANCE_STEP: SetTwrColor(TWR_BLUE); Sound(SND_OK); SetTwrColor(TWR_OFF); break; case S_DANCE_END: SetTwrColor(TWR_RED); Sound(SND_PHONE); SetTwrColor(TWR_RED); break; } } // Show //----------------------------------------------------------------------------------------- void HeartBeat(void) { static unsigned int uiCount = 0; if( uiCount == 0 ) { LED_HEART = ~LED_HEART; uiCount = rand() % 4000; } else --uiCount; } // HeartBeat //------------------------------------------------------------------------------