comms not ready

Dependencies:   NRF2401P mbed-rtos mbed

Committer:
newc4420
Date:
Fri Jun 12 08:57:26 2015 +0000
Revision:
0:05989ea2d008
Child:
1:51a5cfbfd25c
Working code, may have superfluous code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
newc4420 0:05989ea2d008 1 #include "mbed.h"
newc4420 0:05989ea2d008 2 #include "rtos.h"
newc4420 0:05989ea2d008 3
newc4420 0:05989ea2d008 4 /*Program to open locker when a signal level (lock) is sent in from an external controller.
newc4420 0:05989ea2d008 5 System is split into threads so main loop can run even when waiting in door unlock function.
newc4420 0:05989ea2d008 6
newc4420 0:05989ea2d008 7 The circuit uses a metal contact to detect if the door is open/closed(latch engaged or not) and sends out an external signal.
newc4420 0:05989ea2d008 8 Main loop checks if door needs to be unlocked(from external signal) and if door is open.
newc4420 0:05989ea2d008 9 Door unlock solenoid is controlled by function unlock*/
newc4420 0:05989ea2d008 10
newc4420 0:05989ea2d008 11 /*function definitions*/
newc4420 0:05989ea2d008 12 void unlock (void const *args); //function statement to unlock door,
newc4420 0:05989ea2d008 13
newc4420 0:05989ea2d008 14 /*set pins*/
newc4420 0:05989ea2d008 15 DigitalOut latch(D0); //signal out to open door
newc4420 0:05989ea2d008 16 DigitalIn lock(D1); //signal in from interface to unlock door
newc4420 0:05989ea2d008 17 DigitalIn doorclosed(D2); //detect if door is closed(logic 1 for door closed)
newc4420 0:05989ea2d008 18 DigitalOut flagdoor(D3); //flag out if door is open
newc4420 0:05989ea2d008 19 PwmOut alarm(PTE20); //alarm via pulse width modulated signal on pin PTE20
newc4420 0:05989ea2d008 20
newc4420 0:05989ea2d008 21 //testing parameters
newc4420 0:05989ea2d008 22 DigitalOut led(LED1); //light to denote unlock thread
newc4420 0:05989ea2d008 23 DigitalOut led2(LED2); //light to denote cycle
newc4420 0:05989ea2d008 24
newc4420 0:05989ea2d008 25 //global flags used in both treads, hence volatile
newc4420 0:05989ea2d008 26 volatile int unlockflag = 0; //internal flag to denote need to unlock door
newc4420 0:05989ea2d008 27 volatile int alarmflag = 1; //alarm activated flag
newc4420 0:05989ea2d008 28
newc4420 0:05989ea2d008 29 volatile int justarmed = 0; //flag to remember if alarm has just been armed to not run unlocking process
newc4420 0:05989ea2d008 30 int main()
newc4420 0:05989ea2d008 31 {
newc4420 0:05989ea2d008 32 //initital set up for system
newc4420 0:05989ea2d008 33 led=1;
newc4420 0:05989ea2d008 34 alarmflag = 1; //initially alarm armed
newc4420 0:05989ea2d008 35 unlockflag = 0; //initially no call to unlock
newc4420 0:05989ea2d008 36 alarm = 0; //initialise alarm as not sounding
newc4420 0:05989ea2d008 37 latch = 1; //solenoid initially locked
newc4420 0:05989ea2d008 38
newc4420 0:05989ea2d008 39 Thread Tunlock(unlock); //set up unlock function as a thread, thread should not run while unlockflag ==0
newc4420 0:05989ea2d008 40
newc4420 0:05989ea2d008 41
newc4420 0:05989ea2d008 42 //main loop that runs forever, checking for external flags and if door open/closed and to fire off alarm(status thread)
newc4420 0:05989ea2d008 43 while(true)
newc4420 0:05989ea2d008 44 {
newc4420 0:05989ea2d008 45 led2 = !led2;
newc4420 0:05989ea2d008 46 //check if need to unlock door(external flag, lock), setting internal flag(unlockflag) so unlock function will run accordingly
newc4420 0:05989ea2d008 47 if (lock == 0 && alarmflag ==1 && justarmed == 0) {
newc4420 0:05989ea2d008 48 unlockflag = 1;
newc4420 0:05989ea2d008 49 Thread :: wait(100);
newc4420 0:05989ea2d008 50 }
newc4420 0:05989ea2d008 51 //after one cycle from arming alarm, turn off justarmed flag and wait for more than a second before testing for next unlock signal
newc4420 0:05989ea2d008 52 else if(justarmed ==1 && alarmflag==1)
newc4420 0:05989ea2d008 53 {
newc4420 0:05989ea2d008 54 justarmed = 0;
newc4420 0:05989ea2d008 55 Thread :: wait(1100);
newc4420 0:05989ea2d008 56 }
newc4420 0:05989ea2d008 57
newc4420 0:05989ea2d008 58 //check if door is open
newc4420 0:05989ea2d008 59 if (doorclosed == 1) { //if door is open(electric contact lost), send out flag(logic 1)
newc4420 0:05989ea2d008 60 flagdoor = 0;
newc4420 0:05989ea2d008 61 } else {
newc4420 0:05989ea2d008 62 flagdoor = 1;
newc4420 0:05989ea2d008 63 }
newc4420 0:05989ea2d008 64 Thread :: wait(200); //only check door status every 0.2s
newc4420 0:05989ea2d008 65
newc4420 0:05989ea2d008 66 //check if door broken into(alarm active, no unlock signal and door open)
newc4420 0:05989ea2d008 67 if(alarmflag == 1 && lock == 1 && doorclosed == 0)
newc4420 0:05989ea2d008 68 {
newc4420 0:05989ea2d008 69 alarm = 0.5; //alarm goes off, doesnt stop until investigated by operator
newc4420 0:05989ea2d008 70 }
newc4420 0:05989ea2d008 71 }
newc4420 0:05989ea2d008 72 }
newc4420 0:05989ea2d008 73
newc4420 0:05989ea2d008 74 /*function to unlock door to be called in thread. Note time limit on solenoid open to prevent overheating
newc4420 0:05989ea2d008 75 (should put spring to force door open once unlocked) unlock protocol thread*/
newc4420 0:05989ea2d008 76 void unlock (void const *args)
newc4420 0:05989ea2d008 77 {
newc4420 0:05989ea2d008 78 (void)args; //to note thread function has no input parameter
newc4420 0:05989ea2d008 79
newc4420 0:05989ea2d008 80
newc4420 0:05989ea2d008 81 while (true) {
newc4420 0:05989ea2d008 82 //unlock process should only run code below if unlockflag == 1, checks every cycle, does nothing if internal flag(unlockflag)==0
newc4420 0:05989ea2d008 83 if (unlockflag == 1)
newc4420 0:05989ea2d008 84 {
newc4420 0:05989ea2d008 85 alarmflag = 0; //deactivate alarm
newc4420 0:05989ea2d008 86 latch = 0; //open latch via relay activating solenoid
newc4420 0:05989ea2d008 87 led = 0; //test light on to tell if solenoid should be on
newc4420 0:05989ea2d008 88 Thread :: wait(5000); //latch stays open(solenoid activated) for 5 seconds
newc4420 0:05989ea2d008 89 latch = 1; //turn off solenoid, let latch closed
newc4420 0:05989ea2d008 90 led = 1; //turn off test light
newc4420 0:05989ea2d008 91 unlockflag = 0; //reset internal flag for door lock
newc4420 0:05989ea2d008 92
newc4420 0:05989ea2d008 93 //wait for door to be closed, do other loop checks
newc4420 0:05989ea2d008 94 while (doorclosed == 0)
newc4420 0:05989ea2d008 95 {
newc4420 0:05989ea2d008 96 Thread :: wait(100); //wait 0.5s, allowing other threads to run
newc4420 0:05989ea2d008 97 }
newc4420 0:05989ea2d008 98 //if door is closed and alarm not activated, activate alarm and remember that alarm is just armed
newc4420 0:05989ea2d008 99 if (doorclosed == 1 && alarmflag==0 && justarmed ==0)
newc4420 0:05989ea2d008 100 {
newc4420 0:05989ea2d008 101 alarmflag = 1;
newc4420 0:05989ea2d008 102 justarmed = 1;
newc4420 0:05989ea2d008 103 }
newc4420 0:05989ea2d008 104 else
newc4420 0:05989ea2d008 105 {
newc4420 0:05989ea2d008 106 Thread :: wait(200); //do main thread checks while waiting(superfluous code from previous iteration?)
newc4420 0:05989ea2d008 107 }
newc4420 0:05989ea2d008 108
newc4420 0:05989ea2d008 109 }
newc4420 0:05989ea2d008 110 }
newc4420 0:05989ea2d008 111 }