100% found this document useful (1 vote)
708 views7 pages

Elevator Program in Python (To Be Run On BeagleBoneBlack)

This document contains Python code to simulate the control of a 4-floor elevator system using LEDs and buttons connected to a Raspberry Pi. It initializes GPIO pins for LEDs and buttons, defines functions for turning LEDs on/off and monitoring button presses. The main loop waits for button presses, moves the "elevator" up or down by turning LEDs on/off in sequence, and tracks the current floor position.

Uploaded by

sandy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
708 views7 pages

Elevator Program in Python (To Be Run On BeagleBoneBlack)

This document contains Python code to simulate the control of a 4-floor elevator system using LEDs and buttons connected to a Raspberry Pi. It initializes GPIO pins for LEDs and buttons, defines functions for turning LEDs on/off and monitoring button presses. The main loop waits for button presses, moves the "elevator" up or down by turning LEDs on/off in sequence, and tracks the current floor position.

Uploaded by

sandy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

//elevator.

py

import sys
import time
import select
from liftpins import *
from gpio import *
def liftLEDExit (gpio):
gpioSetVal(gpio, val="0")
gpioUnexport(gpio)
return

def liftLEDInit (gpio):


gpioExport(gpio)
gpioSetDir(gpio, flag="out")
gpioSetVal(gpio, val="0")
return
def liftLEDOn (gpio):
gpioSetVal(gpio, val="1")
return
def liftLEDOff (gpio):
gpioSetVal(gpio, val="0")
return
def liftButtonExit (gpio):
gpioUnexport(gpio)
return

def liftButtonInit (gpio):


gpioExport(gpio)
gpioSetDir(gpio, flag="in")
gpioSetEdge(gpio, flag="falling")
return
def liftInitAll():
for i in range(0, NO_OF_DIR_LEDS):
liftLEDInit(str(dir_leds[i]))

for i in range(0, NO_OF_FLOORS):


liftLEDInit(str(pos_leds[i]))
liftLEDInit(str(lift_leds[i]))
liftButtonInit(str(lift_buttons[i]))
return

def liftExitAll():

for i in range(0, NO_OF_DIR_LEDS):


liftLEDExit(str(dir_leds[i]))

for i in range(0, NO_OF_FLOORS):


liftLEDExit(str(pos_leds[i]))
liftLEDExit(str(lift_leds[i]))
liftButtonExit(str(lift_buttons[i]))
print "\n=== Demonstration END ===\n"
return
def liftDefaultPos():
liftLEDOn(str(pos_leds[DEFAULT_LIFT_POS]))
return
def liftDirUp():
for i in range(0, NO_OF_DIR_LEDS):
liftLEDOn(str(dir_leds[i]))
time.sleep(0.5)
for i in range(0, NO_OF_DIR_LEDS):
liftLEDOff(str(dir_leds[i]))
return
def liftDirDown():
for i in range(NO_OF_DIR_LEDS, 0, -1):
liftLEDOn(str(dir_leds[i-1]))
time.sleep(0.5)
for i in range(0, NO_OF_DIR_LEDS):
liftLEDOff(str(dir_leds[i]))
return
def GetButtonVal():
try:
fo0 = open(SYSFS_GPIO_DIR + "/gpio" + str(LIFT_BUTTON_0) +
"/value" ,"r")
fo0.read(2)
floor_set[0]["fd"] = fo0
fo1 = open(SYSFS_GPIO_DIR + "/gpio" + str(LIFT_BUTTON_1) +
"/value" ,"r")
fo1.read(2)
floor_set[1]["fd"] = fo1
fo2 = open(SYSFS_GPIO_DIR + "/gpio" + str(LIFT_BUTTON_2) +
"/value" ,"r")
fo2.read(2)
floor_set[2]["fd"] = fo2

fo3 = open(SYSFS_GPIO_DIR + "/gpio" + str(LIFT_BUTTON_3) +


"/value" ,"r")
fo3.read(2)
floor_set[3]["fd"] = fo3
print "\nWaiting for button press ..."
r, w, ex = select.select([], [], [fo0, fo1, fo2, fo3])

for i in range(len(floor_set)):
if floor_set[i]["fd"] in ex:
print "LIFT button is pressed for floor # %d" % i
liftLEDOn(str(floor_set[i] ["led"]))
time.sleep(1)
but = i
fo = floor_set[i]["fd"]
fo.seek(0, 0);
str1 = fo.read(1)
#print "Button Pressed ! Value = ", str1
fo0.close()
fo1.close()
fo2.close()
fo3.close()
return but

except IOError:
return
try:

print "\nLift Operation Simulation using Python\n"


print "-----------------------------------------------\n"
liftInitAll()
liftDefaultPos()
cur_flr = DEFAULT_LIFT_POS

while True:
new_flr = GetButtonVal()
if new_flr > cur_flr:
tmp = cur_flr
print "LIFT going UP to floor #%d" %new_flr
while (tmp != new_flr):
liftDirUp()
time.sleep(0.01)
liftLEDOff(str(pos_leds[tmp]))
tmp += 1
liftLEDOn(str(pos_leds[tmp]))
time.sleep(0.5)
elif new_flr < cur_flr:
tmp = cur_flr
print "LIFT going DOWN to floor #%d" %new_flr
while (tmp != new_flr):
liftDirDown()
time.sleep(0.01)

liftLEDOff(str(pos_leds[tmp]))
tmp -= 1
liftLEDOn(str(pos_leds[tmp]))
time.sleep(0.5)

cur_flr = new_flr
liftLEDOff(str(lift_leds[new_flr]))
time.sleep(1)

liftExitAll()
exit()
except KeyboardInterrupt:
liftExitAll()
print "Program Exit due to CTRL-C"
exit()
sys.exit(0)

//gpio.py

import sys
import os
SYSFS_GPIO_DIR = "/sys/class/gpio"
def gpioUnexport (gpio):
try:
fo = open(SYSFS_GPIO_DIR + "/unexport","w")
fo.write(gpio)
fo.close()
return
except IOError:
return
def gpioExport (gpio):
try:
fo = open(SYSFS_GPIO_DIR + "/export","w")
fo.write(gpio)
fo.close()
return
except IOError:
return
def gpioSetDir (gpio, flag):
try:
fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/direction"
,"w")

fo.write(flag)
fo.close()
return
except IOError:
return
def gpioSetVal (gpio, val):
try:
fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/value" ,"w")
fo.write(val)
fo.close()
return
except IOError:
return
def gpioSetEdge (gpio, flag):
try:
fo = open(SYSFS_GPIO_DIR + "/gpio" + gpio + "/edge" ,"w")
fo.write(flag)
fo.close()
return
except IOError:
return

//Liftpins.py
LED_1
LED_2
LED_3
LED_4
LED_5
LED_6
LED_7
LED_8

=
=
=
=
=
=
=
=

(0
(0
(0
(0
(1
(1
(0
(1

*
*
*
*
*
*
*
*

32)
32)
32)
32)
32)
32)
32)
32)

+
+
+
+
+
+
+
+

3
23
2
26
17
15
15
14

LED_9 =
LED_10
LED_11
LED_12
LED_13
LED_14
LED_15

(0 * 32) + 30
=
(2 * 32) + 2
=
(1 * 32) + 28
=
(2 * 32) + 3
=
(0 * 32) + 31
=
(2 * 32) + 5
=
(1 * 32) + 18

SW_1
SW_2

(0 * 32) + 14
(0 * 32) + 27

=
=

SW_3
SW_4

=
=

(0 * 32) + 22
(2 * 32) + 1

# DIRECTIN LEDS: to represent lift direction (up or down)


LIFT_DIR_1
=
LED_9
LIFT_DIR_2
=
LED_10
LIFT_DIR_3
=
LED_11
LIFT_DIR_4
=
LED_12
LIFT_DIR_5
=
LED_13
LIFT_DIR_6
=
LED_14
LIFT_DIR_7
=
LED_15
# POSITION LEDS: LEDs to indicate the current position of Lift
LIFT_POS_0
=
LED_5
LIFT_POS_1
=
LED_6
LIFT_POS_2
=
LED_7
LIFT_POS_3
=
LED_8
# LIFT BUTTONS:
LIFT_BUTTON_0
LIFT_BUTTON_1
LIFT_BUTTON_2
LIFT_BUTTON_3
# LIFT LEDS:
LIFT_LED_0
LIFT_LED_1
LIFT_LED_2
LIFT_LED_3

corresponding to each floor of the Lift


=
SW_1
=
SW_2
=
SW_3
=
SW_4

indication for BUTTON Press on each floor


=
LED_1
=
LED_2
=
LED_3
=
LED_4

# An array of DIRECTIN LEDS


dir_leds = [
LIFT_DIR_1,
LIFT_DIR_2,
LIFT_DIR_3,
LIFT_DIR_4,
LIFT_DIR_5,
LIFT_DIR_6,
LIFT_DIR_7
]
# An array of POSITION LEDS
pos_leds = [
LIFT_POS_0,
LIFT_POS_1,
LIFT_POS_2,
LIFT_POS_3
]

lift_leds = [
LIFT_LED_0,
LIFT_LED_1,
LIFT_LED_2,
LIFT_LED_3
]
lift_buttons = [
LIFT_BUTTON_0,
LIFT_BUTTON_1,
LIFT_BUTTON_2,
LIFT_BUTTON_3
]
NO_OF_FLOORS
=
NO_OF_DIR_LEDS
=
DEFAULT_LIFT_POS =

4
7
0

floor_set = [
{"fd":-1, "button":LIFT_BUTTON_0, "led":LIFT_LED_0},
{"fd":-1, "button":LIFT_BUTTON_1, "led":LIFT_LED_1},
{"fd":-1, "button":LIFT_BUTTON_2, "led":LIFT_LED_2},
{"fd":-1, "button":LIFT_BUTTON_3, "led":LIFT_LED_3}
]

You might also like