int main(void) { int numBlinks = 10; gpioConfig(N_LED_BLUE_PORT, N_LED_BLUE_PIN, GPIO_OUTPUT | GPIO_LOW); gpioConfig(N_LED_GREEN_PORT, N_LED_GREEN_PIN, GPIO_OUTPUT | GPIO_LOW); gpioConfig(N_LED_YELLOW_PORT, N_LED_YELLOW_PIN, GPIO_OUTPUT | GPIO_LOW); for (;;) { /* -------- 100 MHz (external clock) ----------- * Configure the Multipurpose Clock Generator output to use the external * clock locked with a PLL at the maximum frequency of 100MHZ * * For PLL, the dividers must be set first. * * System: 100 MHz * Bus: 50 MHz * Flexbus: 50 MHz * Flash: 25 MHz */ clockSetDividers(DIVIDE_BY_1, DIVIDE_BY_2, DIVIDE_BY_4, DIVIDE_BY_4); clockConfigMcgOut(MCG_PLL_EXTERNAL_100MHZ); flashLed(N_LED_BLUE_PORT, N_LED_BLUE_PIN, numBlinks); /* -------- 24 MHz (slow internal clock) ----------- * Configure the Multipurpose Clock Generator output to use the internal * 4MHz clock locked with FLL (internal clocks can only be locked with * the FLL) at 24MHz. * * For FLL, the clock dividers be set before or after. (but not before * PLL) * * System: 24 MHz * Bus: 3 MHz * Flexbus: 3 MHz * Flash: 3 MHz */ clockConfigMcgOut(MCG_FLL_INTERNAL_24MHZ); clockSetDividers(DIVIDE_BY_1, DIVIDE_BY_8, DIVIDE_BY_8, DIVIDE_BY_8); flashLed(N_LED_GREEN_PORT, N_LED_GREEN_PIN, numBlinks); /* -------- 6 MHz (slow internal clock) ----------- * With the output still at 24MHz, the dividers are quartered. Therefore, * the new output will be 6MHz. * * System: 6 MHz * Bus: 1.5 MHz * Flexbus: 1.5 MHz * Flash: 1.5 MHz */ clockSetDividers(DIVIDE_BY_4, DIVIDE_BY_16, DIVIDE_BY_16, DIVIDE_BY_16); flashLed(N_LED_YELLOW_PORT, N_LED_YELLOW_PIN, numBlinks); } return 0; }
//////////////////////////////////////////////////////////// // LOAD PORT INFO FROM EEPROM void loadPortInfo(byte forceReload) { byte cs = 0; for(int i=0; i<NUM_PORTS; ++i) { PORT_INFO *p = port[i]; memset(p, 0, sizeof(PORT_INFO)); int eepromAddr = 16 * i; p->cfg.triggerChannel = eeprom_read(eepromAddr + 0); p->cfg.triggerNote = eeprom_read(eepromAddr + 1); p->cfg.durationMax = (int)eeprom_read(eepromAddr + 2) << 8; p->cfg.durationMax |= (int)eeprom_read(eepromAddr + 3); p->cfg.durationModulator = eeprom_read(eepromAddr + 4); p->cfg.dutyMax = eeprom_read(eepromAddr + 5); p->cfg.dutyModulator = eeprom_read(eepromAddr + 6); byte flags = eeprom_read(eepromAddr + 7); p->cfg.invert = !!(flags & EEPROM_FLAG_INVERT); // default to full duration/duty p->status.duration = p->cfg.durationMax; p->status.duty = p->cfg.dutyMax; calcCheckSum((byte*)&p->cfg, sizeof(PORT_CONFIG), &cs); } if(forceReload || eeprom_read(EEPROM_ADDR_CHECKSUM) != cs) { // failed checksum (possibly no previous stored config) flashLed(10); initPortInfo(); savePortInfo(); } }
//////////////////////////////////////////////////////////// // ENTRY POINT void main() { int i; // osc control / 8MHz / internal osccon = 0b01110001; // timer0... configure source and prescaler cmcon0 = 7; // enable serial receive interrupt intcon = 0b11000000; pie1.5 = 1; // configure io trisa = 0b00001000; trisc = 0b01000000; ansel = 0b00000000; porta=0; portc=0; init_usart(); flashLed(3); // loop forever for(;;) { if(bSynchCount) { if(!--bSynchCount) P_SYNCH = 0; } if(bRestartCount) { if(!--bRestartCount) P_RESTART = 0; } } }
void bot1(void) { /* Puts bot in motion (`forward()`) */ forward(); /* Loops until collision (boolean is set in ISR) */ while (!hitWall) { for (int i(0); i < NUM_BUMPERS; ++i) { if (digitalRead(bumpers[i])) { hitWall = true; } } delay(1); } Serial.println("Hit wall"); /* Moves backward until red and stops */ actionUntilColor(RED, backward); turn(-80); /* negative to turn right */ actionUntilColor(RED, turnRight); /* Flash a red LED */ flashLed(RED_LED); /* pauses for 1 second to flash */ /* Follows red until centre yellow */ while (!followColorUntilColor(RED, YELLOW)) {} Serial.println("Found yellow"); forward(); delay(PUSH_CONST); turn(-20); stop(); /* Stops on yellow, flashing yellow LED twice */ flashLed(YELLOW_LED); /* pauses for 1 second to flash */ flashLed(YELLOW_LED); /* pauses for 1 second to flash */ /* experimentally necessary to avoid thick black line */ forward(); delay(75); /* Follows yellow */ Serial.println("Following yellow until red"); while (!followColorUntilColor(YELLOW, RED)) {} forward(); delay(PUSH_CONST); stop(); /* Follows red */ Serial.println("Following red until yellow"); while (!followColorUntilColor(RED, YELLOW)) {} /* Stops on yellow, turns on yellow LED, turns 180 degrees */ Serial.println("Turning until red"); stop(); digitalWrite(YELLOW_LED, HIGH); turn(220); /* ensures we escape red and actually turn */ actionUntilColor(RED, turnLeft); /* Follows red back to the middle */ while (!followColorUntilColor(RED, YELLOW)) {} Serial.println("Found yellow and about to communicate"); forward(); delay(PUSH_CONST); stop(); /* Communicates to Bot 2: `START` */ transmit(startMsg, MSG_LEN, TIMEOUT); Serial.println("flashing green LED"); /* Flashes green LED twice */ flashLed(GREEN_LED); /* pauses for 1 second to flash */ flashLed(GREEN_LED); /* pauses for 1 second to flash */ /* Follows yellow */ while (!followColorUntilColor(YELLOW, RED)) {} forward(); delay(PUSH_CONST); stop(); /* Follows red, stops on yellow */ while (!followColorUntilColor(RED, YELLOW)) {} /* Turns on green LED */ digitalWrite(GREEN_LED, HIGH); /* Waits for `TOXIC` */ Serial.println("Receiving toxic message"); turnLeft(45); while (!receive(recMsg, MSG_LEN)) {} stop(); delay(2 * TIMEOUT); /* don't double receive a message */ /* Flash yellow LED continuously */ digitalWrite(YELLOW_LED, HIGH); /* Waits for `STOP_YELLOW` */ turnLeft(45); while (!receive(recMsg, MSG_LEN)) {} /* Turns off yellow LED */ digitalWrite(YELLOW_LED, LOW); delay(TIMEOUT); /* don't double receive a message */ /* Waits for `DONE` */ Serial.println("Receiving toxic message"); turnLeft(45); while (!receive(recMsg, MSG_LEN)) {} stop(); /* Flashes green LED */ while (1) { flashLed(GREEN_LED); } }
void bot2(void) { /* Waits for `START` */ Serial.println("Waiting for start"); while (!receive(recMsg, MSG_LEN)) {} delay(500); /* Flash green LED twice */ Serial.println("Lit green led"); flashLed(GREEN_LED); /* pauses for 1 second to flash */ flashLed(GREEN_LED); /* pauses for 1 second to flash */ /* Puts bot in motion (`forward()`) */ forward(); /* Loops until collision (boolean is set in ISR) */ while (!hitWall) { for (int i = 0; i < NUM_BUMPERS; ++i) { if (digitalRead(bumpers[i])) { hitWall = true; } } delay(1); } Serial.println("Hit wall"); /* Moves backward until blue and stops */ actionUntilColor(BLUE, backward); turn(80); /* positive to turn left */ actionUntilColor(BLUE, turnLeft); /* Stops on blue, flashing a blue LED */ flashLed(BLUE_LED); /* Turns a predetermined angle to the right, and follows blue */ while (!followColorUntilColor(BLUE, YELLOW)) {} Serial.println("Found yellow"); forward(); delay(PUSH_CONST); stop(); /* Stops on yellow, flashing yellow LED twice */ flashLed(YELLOW_LED); flashLed(YELLOW_LED); /* Follows yellow */ Serial.println("Following yellow until blue"); while (!followColorUntilColor(YELLOW, BLUE)) {} forward(); delay(PUSH_CONST); stop(); /* Follows blue */ Serial.println("Following blue until yellow"); while (!followColorUntilColor(BLUE, YELLOW)) {} /* Stops on yellow, turns on yellow LED, turns 180 degrees */ Serial.println("Turning until blue"); stop(); digitalWrite(YELLOW_LED, HIGH); turn(-180); actionUntilColor(BLUE, turnRight); /* Communicates to Bot 1: `TOXIC` */ transmit(startMsg, MSG_LEN, TIMEOUT); /* Flash yellow continuously */ flashLed(YELLOW_LED); digitalWrite(YELLOW_LED, HIGH); /* Follows blue, stops on yellow */ while (!followColorUntilColor(BLUE, YELLOW)) {} forward(); delay(PUSH_CONST); stop(); /* Communicates to Bot 1: `STOP_YELLOW` */ transmit(startMsg, MSG_LEN, TIMEOUT); /* Stop flashing yellow LED */ digitalWrite(YELLOW_LED, LOW); /* Follows yellow until blue */ while (!followColorUntilColor(YELLOW, BLUE)) {} forward(); delay(PUSH_CONST); stop(); /* Follows blue, stops on yellow */ while (!followColorUntilColor(BLUE, YELLOW)) {} stop(); /* Communicates to Bot 1: `DONE` */ transmit(doneMsg, MSG_LEN, TIMEOUT); /* Flashes green LED */ while (1) { flashLed(GREEN_LED); } }
void flashBlue() { flashLed(0xff,0x00,0xCC); }
void flashRed() { flashLed(0xff,0x00,0x00); }
void flashOrange() { flashLed(0xff,0x66,0x00); }
void flashGreen() { flashLed(0x00,0xff,0x00); }