KenYEAH! 2k15


1 week Microcontroller Lab at Oxford University

Locker

Import programlocker4

comms not ready

Task

The locker team is tasked with securing the locker, following the instructions of the user via the interface, setting the battery to charge and relaying information between the battery and the interface.

Overview

The locker is secured by a solenoid controlled latch. There is also an alarm system which can be armed or disarmed. The armed alarm sounds off if a metal contact is disconnected while the alarm is armed. The locking mechanism and the alarm only unlocks when instructed to by the interface.

<battery systems work in progress> Battery charger to be connected to 5V source through relay 3. Communication with battery to receive battery data.

Securing the locker

The locker is secured via a solenoid controlled latch(catching the locker door). When an unlock pulse is sent from the interface, the mbed turns off the alarm(sets alarmflag = 0), then activates the solenoid to pull the latch to unlock the door(function unlock() ). To prevent overheating, the solenoid only activates for a limited time, within which the user must open the door/springs will force the door to open. The solenoid is also controlled via a relay as the mbed cannot supply the power the solenoid needs.

/media/uploads/Vauvz/11267462_925847570791204_97751234_o_jIFFRBx.jpg

There is a electrical contact surface where contact is established when the door is locked and disconnected if the door is opened. This allows the locker system to know if the door is open(via setting flag doorclosed=0). If the door is opened while the alarm is activated(alarmflag==1&&doorclosed==0), the loss of electrical contact sets off the alarm. This contact also allows the locker system to inform the interface system if the door is opened.

/media/uploads/Vauvz/11416675_925847567457871_564665441_o.jpg

Build (v3.3)

Circuit

/media/uploads/Vauvz/11428881_925847574124537_1064349468_o.jpg

- 1 relay assembly; solenoid connected to relay 5, ground connected to mbed GND pin, VCC connected to mbed 5V(running solenoid via relay) solenoid powered by 8.5V source through the relay

- 2 metal contacts; one connected to high(3V3), other connected to pin D2 via pull-up resistor.(check if door is closed)

- 1 speaker connected to mbed pin PTE20

mbed

/media/uploads/Vauvz/11420277_925847577457870_1868089561_o.jpg

- 2 digital-in pins on the mbed:

1) "lock" on pin D1 is the flag from the interface to unlock the door. (logic 0 to unlock)

2) "doorclosed" on pin D2 is connected to a pull-down resistor and the metal contacts on the door.(logic 1 for closed door)

- 2 digital-out pins on the mbed:

1) "latch" on pin D0 controls the solenoid via relay.(logic 0 to activate solenoid)

2)"flagdoor" on pin D3 to the interface, informing it if the door is open(logic 1 for opened door)

-1 PWMOut on the mbed: "alarm" on pin PTE20, powers speaker for alarm on demand.

Code(v3.3)

initialisation

Setting mbed pins for input and output:

setting mbed pins

/*set pins*/
DigitalOut latch(D0);       //signal out to open door
DigitalIn lock(D1);       //signal in from interface to unlock door
DigitalIn doorclosed(D2);   //detect if door is closed(logic 1 for door closed)
DigitalOut flagdoor(D3);    //flag out if door is open
PwmOut alarm(PTE20);        //alarm via pulse width modulated signal on pin PTE20

Global variable/flags, initial values:

Flags

//global flags used in threads, hence volatile
volatile int unlockflag = 0;             //internal flag to denote need to unlock door
volatile int alarmflag = 1;              //alarm activated flag
volatile int justarmed = 0;             //flag to remember if alarm has just been armed to not run unlocking process

These global flags are used by the various threads to control the output and state of the system.

-unlockflag is set to 1 when request to unlock is sent.

-alarmflag is set to 1 when alarm is armed.

-justarmed is set to 1 when the alarm has just been reset after the locker has been opened.

Running The mbed continuously runs two threads, the main thread which monitors the status of the system and external flags, and the Tunlock thread which controls the entire unlock process.

Thread main():

Main thread

while(true) 
    {
        led2 = !led2;
        //check if need to unlock door(external flag, lock), setting internal flag(unlockflag) so unlock function will run accordingly
        if (lock == 0 && alarmflag ==1 && justarmed == 0) {            
            unlockflag = 1;
            Thread :: wait(100);
        }
            //urn off justarmed flag and wait for more a second after unlock cycle before testing for next unlock signal or brek-in
            else if(justarmed ==1 && alarmflag==1)
            {
                justarmed = 0;
                Thread :: wait(1000);       //maybe unneeded as this statement only happens after an unlock cycle
            }    

        //check if door is open
        if (doorclosed == 1) {      //if door is open(electric contact lost), send out flag(logic 1)
            flagdoor = 0;
        } else {
            flagdoor = 1;
        }
        Thread :: wait(200);        //only check door status every 0.2s
        
        //check if door broken into(alarm active, no unlock signal and door open)
        if(alarmflag == 1 && lock == 1 && doorclosed == 0)
        {
            alarm = 0.5;      //alarm goes off, doesnt stop until investigated by operator
        }
    }

This thread runs continuously , checking if a signal to unlock has been received, if the door is open, and whether the locker is broken into.

Main cycle:

1) If unlock signal(lock ==0) and that a unlock cycle is not running already(alarmflag == 1) or just been run(justarmed ==0), run the unlock cycle in thread Tunlock. Else if an unlock cycle has just been completed(alarmflag ==1 && justarmed ==1), reset just armed and wait to prevent double-running from the same unlock pulse.

2) Detect if door is opened/closed and inform interface. If door is open(doorclosed ==0), send out flag to interface(flagdoor = 1).

3) Sets off alarm if the alarm is armed and the door is opened. If door is open(doorclosed==0), no unlock signal(lock==1) and alarm armed(alarmflag ==1), set off the alarm(alarm ==0.5).

Thread Tunlock:

Unlock thread

void unlock (void const *args)
{
    (void)args;     //to note thread function has no input parameter
    
   
    while (true) {
    //unlock process should only run code below if unlockflag == 1, checks every cycle, does nothing if internal flag(unlockflag)==0
        if (unlockflag == 1) 
        {      
            alarmflag = 0;  //deactivate alarm
            latch = 0;      //open latch via relay activating solenoid
            led = 0;        //test light on to tell if solenoid should be on
            Thread :: wait(5000);   //latch stays open(solenoid activated) for 5 seconds
            latch = 1;      //turn off solenoid, let latch closed
            led = 1;        //turn off test light
            unlockflag = 0;     //reset internal flag for door lock
            
            //wait for door to be closed, do other loop checks
            while (doorclosed == 0)
            {
                Thread :: wait(100);    //wait 0.5s, allowing other threads to run
            }
            //if door is closed and alarm not activated, activate alarm and remember that alarm is just armed
                if (doorclosed == 1 && alarmflag==0 && justarmed ==0)
                {
                    alarmflag = 1;
                    justarmed = 1;
                }    
                    else
                    {
                        Thread :: wait(200);            //do main thread checks while waiting(superfluous code from previous iteration?)
                    }
           
        }    
    }
}

The thread Tunlock runs function unlock() continuously but does nothing if unlockflag ==0. If unlockflag ==1, the program runs through an unlock cycle, deactivating the alarm, .

Unlock cycle:

1) Deactivate the alarm by setting alarmflag=0.

2) Activates the solenoid via a relay, then sets the thread to wait for 5s, allowing other threads to run and giving enough time for the door to open. After 5s it deactivates the solenoid.

3) Sets unlockflag =0.

4) Runs other threads while waiting for door to close.

5) If door is closed, activate alarm (set alarmflag =1). The unlock cycle then ends and the thread now waits for the next instance of unlockflag ==1.

code (v4), following is work in progress

battery communication cycle

Thread Tbattcomms

In this thread, the mbed checks the wireless channel shared with the battery for incoming information. Prints off ID and status of memory. <however version causes solenoid to not work>


All wikipages