Thursday 12 January 2017

Simulation of Lift Using Led, Switches and Beagle Bone Black

Introduction

This is a simulation of working of lift using beagle bone black and python. The position of lift will be indicated using led. Switches will be used as lift buttons.



Language & Software Requirements

Language: Python
Text Editor: nano, gedit

Note: You cannot use gedit through terminal after ssh into beagle bone black.

Hardware Requirements

Beagle Bone Black
Male-Male Connectors
Male-Female Connectors
Led
Bread Board
Resistors
Computer
OR Gate 7432
3 Swiches


Connections


Or Gate Connections

We need to use 3 gates out of 4 for each of the Led.
For each gate the configuration will be as follows
Input1-Ground
Input2-GPIO output pin
Ouput- Goes to Led.

Led Connections

Each Led must be connected to resister of appropriate resistance & Ground. The other terminal must be connected to Output from Led.
Led will go high whenever the output is high else it will remain low.

Switch Connections

One terminal goes to VCC(3.3V), the other goes to GPIO input.

Code


Configuration of GPIO pins

Three pins are set to input for detecting switch voltage levels to identify the pressing of a floor button. Similarly three pins are required to glow the leds. These must be set as outputs.

Polling


Each of the input pins need to repeatedly checked for the voltage levels. In case the voltage level is high. It must be responded to.
Continuous polling can be implemented through a infinite while loop.

Variables

Two variables will be required one is for the current floor and the other is for the destination floor. The current floor will be initialized to the first floor. The value of the destination floor will be set on floor press.

Conditions

The following condition need to be considered the lift goes up, ie current floor is less than the destination floor.
Second condition is for the lift going down, ie the current floor is below the destination floor.

Sleep Function

A sleep call is used to show the location of the lift as it passes through each floor.

 

Code


import Adafruit_BBIO.GPIO as GPIO
import time
GPIO.cleanup()
GPIO.setup("P8_07",GPIO.IN)     #first floor switch
GPIO.setup("P8_09",GPIO.IN)     #second floor switch
GPIO.setup("P8_11",GPIO.IN)     #third floor switch
GPIO.setup("P8_08",GPIO.OUT)    #first floor led        
GPIO.setup("P8_10",GPIO.OUT)    #second floor led
GPIO.setup("P8_12",GPIO.OUT)    #third floor led

GPIO.output("P8_08",GPIO.HIGH)  #lift assumed at first floor init
GPIO.output("P8_10",GPIO.LOW)   #set the remaining led to zero
GPIO.output("P8_12",GPIO.LOW)
arr = ["P8_08","P8_10","P8_12"]
cfloor=1                        #current floor
dfloor=1                        #destination floor
while True:                     #continuous polling
                                
        

        if (GPIO.input("P8_07")==1):
                dfloor=1
                print "First Floor Button Pressed"
        if (GPIO.input("P8_09")==1):
                dfloor=2
                print "Second Floor Button Pressed"
        if (GPIO.input("P8_11")==1):
                dfloor=3
                print "Third Floor Button Presssed"

        #if floor button was pressed it was detected

        if cfloor>dfloor:       #lift goes down
                print "The lift must go down ", cfloor," to ",dfloor
   for i in range (cfloor , dfloor-1,-1):
                        GPIO.output(arr[cfloor-1],GPIO.LOW)
                        cfloor=i
                        GPIO.output(arr[cfloor-1],GPIO.HIGH)
                        time.sleep(2)

        if cfloor<dfloor:       #lift goes up
                for i in range (cfloor , dfloor+1 , 1):
                        print "The lift must go up ", cfloor," to ",dfloor
                        GPIO.output(arr[cfloor-1],GPIO.LOW)
                        cfloor=i
                        GPIO.output(arr[cfloor-1],GPIO.HIGH)
                        time.sleep(2)



Flaws in the Code

The lift button are not polled when the lift is moving from current floor to destination floor.

Flaw Corrections- Multi threaded Approach

The following correction could be possibly implemented to represent lift behavior in real life and rectify the above flaw.

A multi-threaded approach is necessary.
A polling threads needs to be made which repeatedly polls the status of lift button. Whenever a button is pressed the current condition of the lift must be checked and a flag should be set against the corresponding floor.

Another movement threads needs to implemented. This thread will be started by the polling thread only if it isn't running. This thread once started will run only if there exists at least one floor to visit. After visiting each floor, the flags for the corresponding floor should be reset.








No comments:

Post a Comment