/*---------------------------------------------------------------------------- Thread 3 'lights': executes if current time is between start & end time *---------------------------------------------------------------------------*/ void lights (void const *argument) { /* traffic light operation */ SetLights (RED, 1); /* RED & STOP lights on */ SetLights (STOP, 1); SetLights (YELLOW, 0); SetLights (GREEN, 0); SetLights (WALK, 0); while (1) { /* endless loop */ osDelay(500); /* wait for timeout: 500ms */ if (!signalon ()) { /* if traffic signal time over */ tid_blinking = osThreadCreate(osThread(blinking), NULL); /* start blinking */ return; /* STOP lights */ } SetLights (YELLOW, 1); osDelay(500); /* wait for timeout: 500ms */ SetLights (RED, 0); /* GREEN light for cars */ SetLights (YELLOW, 0); SetLights (GREEN, 1); osSignalClear(tid_lights, 0x0010); osDelay(500); /* wait for timeout: 500ms */ osSignalWait(0x0010, 7500); /* wait for event with timeout */ SetLights (YELLOW, 1); /* timeout value: 7.5s */ SetLights (GREEN, 0); osDelay(500); /* wait for timeout: 500ms */ SetLights (RED, 1); /* RED light for cars */ SetLights (YELLOW, 0); osDelay(500); /* wait for timeout: 500ms */ SetLights (STOP, 0); /* GREEN light for WALKers */ SetLights (WALK, 1); osDelay(2500); /* wait for timeout: 2.5s */ SetLights (STOP, 1); /* RED light for WALKers */ SetLights (WALK, 0); } }
/* * Larm - ansvarig för att aktivera olika larmindikationer (kamera, led, siren). */ void larm(int currentTime){ //Larmstate signalerar om larmet ska slås på if(larmState) { if((currentTime - outportTime[15]) > outportLength[15]) { outportTime[15] = currentTime; sendPulse(100000,100000 , 15); //Skicka puls till kamera } signalon(DE2_PIO_JP1_OUT3_5_BASE, 3); //Slå på signal till LED } else { signaloff(DE2_PIO_JP1_OUT3_5_BASE, 2); //Slå av kamera signaloff(DE2_PIO_JP1_OUT3_5_BASE, 3); //Slå av LED signaloff(DE2_PIO_JP1_OUT3_5_BASE, 4); //Slå av Siren } //Larmstate ska bara slå på siren om switch 1 är på if(larmState && buttonState[1]) { if((currentTime - outportTime[17]) > outportLength[17]) { outportTime[17] = currentTime; sendPulse(10000,10000 , 17); //Skicka puls till siren } } else if(larmState && !buttonState[1]) { signaloff(DE2_PIO_JP1_OUT3_5_BASE, 4); } }
/* * sendPulse - Används för att skicka puls till samtliga utportar. Argument anger hur * länge pulsen ska vara hög respektive låg samt vilken port signalen skickas till. */ void sendPulse(int highLength, int lowLength, int port) { int adress; int addressPort; if(port <=4){ addressPort = port; adress = DE2_PIO_JP1_OUT1_5_BASE; } else if(port > 4 && port < 13){ addressPort = port - 5; adress = DE2_PIO_JP1_OUT2_8_BASE; } else { addressPort = port - 13; adress = DE2_PIO_JP1_OUT3_5_BASE; } if(outportState[port]) { outportLength[port] = highLength; signalon(adress, addressPort); } else { outportLength[port] = lowLength; signaloff(adress, addressPort); } outportState[port] = !outportState[port]; }
/*---------------------------------------------------------------------------- Thread 2 'blinking': runs if current time is outside start & end time *---------------------------------------------------------------------------*/ void blinking (void const *argument) { /* blink YELLOW light */ SetLights (RED, 0); /* all lights off */ SetLights (YELLOW, 0); SetLights (GREEN, 0); SetLights (STOP, 0); SetLights (WALK, 0); while (1) { /* endless loop */ SetLights (YELLOW, 1); /* YELLOW light on */ osDelay(300); /* wait for timeout: 300ms */ SetLights (YELLOW, 0); /* YELLOW light off */ osDelay(300); /* wait for timeout: 300ms */ if (signalon ()) { /* if blinking time over */ tid_lights = osThreadCreate(osThread(lights), NULL); /* start lights */ return; /* and STOP blinking */ } } }
/* * pingDetect_obstacles - funktion ansvarig för att undvika hinder. När låga värden * på pingsensorerna upptäcks så läggs en ny sekvens körinstruktioner in. */ void pingDetect_obstacles(int inportLength, int inportLength2, int inportLength4) { if((inportLength < 150)) { signalon(DE2_PIO_REDLED18_BASE, 10); int delayLength = 1000*(rand() % 25 + 50); //clearDriveList(); listDelay = 0; struct driveNode* firstNode = malloc(sizeof (*driveList)); driveList = firstNode; (*driveList).driveState = turn_left; (*driveList).driveTime = delayLength; (*driveList).next = malloc(sizeof (*driveList)); driveList = driveList->next; (*driveList).driveState = stop; (*driveList).driveTime = 600000; (*driveList).next = malloc(sizeof (*driveList)); driveList = driveList->next; (*driveList).driveState = drive_forward; (*driveList).driveTime = -1; (*driveList).next = NULL; driveList = firstNode; } else if((inportLength2 < 150)) { signalon(DE2_PIO_REDLED18_BASE, 13); int delayLength = 1000*(rand() % 25 + 50); //clearDriveList(); listDelay = 0; struct driveNode* firstNode = malloc(sizeof (*driveList)); driveList = firstNode; (*driveList).driveState = turn_right; (*driveList).driveTime = delayLength; (*driveList).next = malloc(sizeof (*driveList)); driveList = driveList->next; (*driveList).driveState = stop; (*driveList).driveTime = 600000; (*driveList).next = malloc(sizeof (*driveList)); driveList = driveList->next; (*driveList).driveState = drive_forward; (*driveList).driveTime = -1; (*driveList).next = NULL; driveList = firstNode; } if((inportLength4 < 200)) { signalon(DE2_PIO_REDLED18_BASE, 10); int delayLength = 1000*(rand() % 25 + 50); //clearDriveList(); listDelay = 0; struct driveNode* firstNode = malloc(sizeof (*driveList)); driveList = firstNode; (*driveList).driveState = drive_backward; (*driveList).driveTime = 50000; (*driveList).next = malloc(sizeof (*driveList)); driveList = driveList->next; (*driveList).driveState = turn_right; (*driveList).driveTime = delayLength; (*driveList).next = malloc(sizeof (*driveList)); driveList = driveList->next; (*driveList).driveState = stop; (*driveList).driveTime = 600000; (*driveList).next = malloc(sizeof (*driveList)); driveList = driveList->next; (*driveList).driveState = drive_forward; (*driveList).driveTime = -1; (*driveList).next = NULL; driveList = firstNode; } else { signaloff(DE2_PIO_REDLED18_BASE, 10); signaloff(DE2_PIO_REDLED18_BASE, 13); signaloff(DE2_PIO_REDLED18_BASE, 15); } }