Elevator Program in Python (To Be Run On BeagleBoneBlack)
Elevator Program in Python (To Be Run On BeagleBoneBlack)
py
import sys
import time
import select
from liftpins import *
from gpio import *
def liftLEDExit (gpio):
gpioSetVal(gpio, val="0")
gpioUnexport(gpio)
return
def liftExitAll():
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:
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
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}
]