error correction

This commit is contained in:
Thomas Klaehn 2016-12-19 16:52:19 +01:00 committed by Thomas Klaehn
parent 371108fd7e
commit b0a3f6b711
4 changed files with 47 additions and 14 deletions

View File

@ -16,9 +16,8 @@ class mqtt:
def connect(self): def connect(self):
if not self.__is_connected: if not self.__is_connected:
self.__client.on_connect = self.__on_connect self.__client.on_connect = self.__on_connect
result = self.__client.connect(self.__hostname, self.__port, self.__keepalive) self.__client.connect(self.__hostname, self.__port, self.__keepalive)
result |= self.__client.loop_start() self.__client.loop_start()
if 0 == result:
self.__is_connected = True self.__is_connected = True
return True return True
return False return False
@ -27,6 +26,7 @@ class mqtt:
if self.__is_connected: if self.__is_connected:
self.__client.loop_stop() self.__client.loop_stop()
self.__client.disconnect() self.__client.disconnect()
self.__is_connected = False
return True return True
return False return False

View File

@ -1,4 +1,4 @@
from gpio import gpio.gpio from gpio.gpio import gpio
class engine: class engine:
def __init__(self, gpio_1 = 13, gpio_2 = 19): def __init__(self, gpio_1 = 13, gpio_2 = 19):

32
src/gate/gate_handler.py Normal file
View File

@ -0,0 +1,32 @@
'''
Created on Dec 19, 2016
@author: klaehn
'''
from engine.engine import engine
from power_sensor.power_sensor import power_sensor
from time import time
import numpy as np
class gate_handler:
def __init__(self):
self.__engine = engine(13, 19)
self.__power_sensor = power_sensor(1, 0x40)
self.__max_current_up = 315
self.__max_current_down = 280
self.__max_time_open = 250
self.__max_time_close = 250
self.__timeout_open = 0
self.__timeout_close = 0
return
def open(self):
self.__timeout_open = time() + self.__max_time_open
self.__engine.run_up()
power = np.array([])
timestamps = np.array([])
while(time() < self.__max_time_open):
timestamps = np.append(timestamps, time())
power = np.append(power, self.__power_sensor.power_mw())

View File

@ -8,7 +8,7 @@ import time
class gate_guard: class gate_guard:
def __init__(self): def __init__(self):
self.__light_read_delay_s = 6 self.__light_read_delay_s = 60
self.__consequtive_light_reads = 10 self.__consequtive_light_reads = 10
self.__light_lx_close = 0 self.__light_lx_close = 0
self.__light_lx_open = 0 self.__light_lx_open = 0
@ -24,6 +24,7 @@ class gate_guard:
self.__comserver.connect() self.__comserver.connect()
self.__comserver.transmit(topic, payload) self.__comserver.transmit(topic, payload)
self.__comserver.disconnect() self.__comserver.disconnect()
self.__gate_state.set_state("close")
return None return None
def open_gate(self): def open_gate(self):
@ -32,6 +33,7 @@ class gate_guard:
self.__comserver.connect() self.__comserver.connect()
self.__comserver.transmit(topic, payload) self.__comserver.transmit(topic, payload)
self.__comserver.disconnect() self.__comserver.disconnect()
self.__gate_state.set_state("open")
return None return None
def handle_gate_state(self, light_avg=0): def handle_gate_state(self, light_avg=0):
@ -39,22 +41,21 @@ class gate_guard:
if light_avg <= self.__light_lx_close: if light_avg <= self.__light_lx_close:
self.close_gate() self.close_gate()
elif self.__gate_state.get_state() == "close": elif self.__gate_state.get_state() == "close":
if light_avg >= self.__light_lx_open: if light_avg > self.__light_lx_open:
self.open_gate() self.open_gate()
elif self.__gate_state.get_state() == "unknown": elif self.__gate_state.get_state() == "unknown":
'''TODO: bring gate in a defined position''' '''TODO: bring gate in a defined position'''
def run(self): def run(self):
if self.__gate_state.get_state() == "unknown": if self.__gate_state.get_state() == "unknown":
self.handle_gate_state() self.__gate_state.set_state("open")
try: try:
while True: while True:
light_data.push(light_sensor.read()) rd = self.__light_sensor.read()
light_avg = light_data.average() self.__light_data.push(rd)
if light_avg: light_avg = self.__light_data.average()
print "light average: " + str(light_avg) if light_avg != None:
self.handle_gate_state(light_avg) self.handle_gate_state(light_avg)
time.sleep(self.__light_read_delay_s) time.sleep(self.__light_read_delay_s)