def __init__(self, name): map = generate_map(name) self.ctrl_socket = map['ctrl'] self.monitor_socket = map['mon'] self.opcd = OPCD_Interface(map['opcd_ctrl'], 'powerman') bus = SMBus(self.opcd.get('gpio_i2c_bus')) self.gpio_mosfet = GPIO_Bank(bus, self.opcd.get('gpio_i2c_address')) self.power_pin = self.opcd.get('gpio_power_pin') self.cells = self.opcd.get('battery_cells') self.low_cell_voltage_idle = self.opcd.get( 'battery_low_cell_voltage_idle') self.low_cell_voltage_load = self.opcd.get( 'battery_low_cell_voltage_load') self.battery_current_treshold = self.opcd.get( 'battery_current_treshold') self.capacity = self.opcd.get('battery_capacity') self.low_battery_voltage_idle = self.cells * self.low_cell_voltage_idle self.low_battery_voltage_load = self.cells * self.low_cell_voltage_load self.critical = False #self.gpio_mosfet.write() self.warning_started = False # start threads: self.standing = True self.adc_thread = start_daemon_thread(self.adc_reader) self.request_thread = start_daemon_thread(self.request_handler)
def __init__(self, name): # set-up logger: logfile = user_data_dir() + sep + 'PowerMan.log' log_config(filename = logfile, level = DEBUG, format = '%(asctime)s - %(levelname)s: %(message)s') # initialized and load config: log_info('powerman starting up') map = generate_map(name) self.ctrl_socket = map['ctrl'] self.monitor_socket = map['mon'] self.opcd = OPCD_Interface(map['opcd_ctrl'], 'powerman') bus = SMBus(self.opcd.get('gpio_i2c_bus')) self.gpio_mosfet = GPIO_Bank(bus, self.opcd.get('gpio_i2c_address')) self.power_pin = self.opcd.get('gpio_power_pin') self.cells = self.opcd.get('battery_cells') self.low_cell_voltage = self.opcd.get('battery_low_cell_voltage') self.capacity = self.opcd.get('battery_capacity') self.low_battery_voltage = self.cells * self.low_cell_voltage self.critical = False self.gpio_mosfet.write() self.warning_started = False # start threads: self.standing = True self.adc_thread = start_daemon_thread(self.adc_reader) self.emitter_thread = start_daemon_thread(self.power_state_emitter) self.request_thread = start_daemon_thread(self.request_handler) log_info('powerman running')
def __init__(self, name): map = generate_map(name) self.ctrl_socket = map['ctrl'] self.monitor_socket = map['mon'] self.opcd = OPCD_Interface(map['opcd_ctrl'], 'powerman') bus = SMBus(self.opcd.get('gpio_i2c_bus')) self.gpio_mosfet = GPIO_Bank(bus, self.opcd.get('gpio_i2c_address')) self.power_pin = self.opcd.get('gpio_power_pin') self.cells = self.opcd.get('battery_cells') self.low_cell_voltage_idle = self.opcd.get('battery_low_cell_voltage_idle') self.low_cell_voltage_load = self.opcd.get('battery_low_cell_voltage_load') self.battery_current_treshold = self.opcd.get('battery_current_treshold') self.capacity = self.opcd.get('battery_capacity') self.low_battery_voltage_idle = self.cells * self.low_cell_voltage_idle self.low_battery_voltage_load = self.cells * self.low_cell_voltage_load self.critical = False #self.gpio_mosfet.write() self.warning_started = False # start threads: self.standing = True self.adc_thread = start_daemon_thread(self.adc_reader) self.request_thread = start_daemon_thread(self.request_handler)
class PowerMan: def __init__(self, name): map = generate_map(name) self.ctrl_socket = map['ctrl'] self.monitor_socket = map['mon'] self.opcd = OPCD_Interface(map['opcd_ctrl'], 'powerman') bus = SMBus(self.opcd.get('gpio_i2c_bus')) self.gpio_mosfet = GPIO_Bank(bus, self.opcd.get('gpio_i2c_address')) self.power_pin = self.opcd.get('gpio_power_pin') self.cells = self.opcd.get('battery_cells') self.low_cell_voltage_idle = self.opcd.get('battery_low_cell_voltage_idle') self.low_cell_voltage_load = self.opcd.get('battery_low_cell_voltage_load') self.battery_current_treshold = self.opcd.get('battery_current_treshold') self.capacity = self.opcd.get('battery_capacity') self.low_battery_voltage_idle = self.cells * self.low_cell_voltage_idle self.low_battery_voltage_load = self.cells * self.low_cell_voltage_load self.critical = False #self.gpio_mosfet.write() self.warning_started = False # start threads: self.standing = True self.adc_thread = start_daemon_thread(self.adc_reader) self.request_thread = start_daemon_thread(self.request_handler) def battery_warning(self): # do something in order to indicate a low battery: msg = 'CRITICAL WARNING: SYSTEM BATTERY VOLTAGE IS LOW; IMMEDIATE SHUTDOWN REQUIRED OR SYSTEM WILL BE DAMAGED' system('echo "%s" | wall' % msg) beeper_enabled = self.opcd.get('beeper_enabled') while True: if beeper_enabled: self.gpio_mosfet.set_gpio(5, False) sleep(0.1) self.gpio_mosfet.set_gpio(5, True) sleep(0.1) else: sleep(1.0) def adc_reader(self): voltage_adc = ADC(self.opcd.get('voltage_adc')) current_adc = ADC(self.opcd.get('current_adc')) voltage_lambda = eval(self.opcd.get('adc_2_voltage')) current_lambda = eval(self.opcd.get('adc_2_current')) self.consumed = 0.0 vmin = 13.2 vmax = 16.4 self.voltage = voltage_lambda(voltage_adc.read()) batt = min(1.0, max(0.0, (self.voltage - vmin) / (vmax - vmin))) self.consumed = (1.0 - batt) * self.capacity hysteresis = Hysteresis(self.opcd.get('low_voltage_hysteresis')) time_prev = time() while True: sleep(0.2) try: self.voltage = voltage_lambda(voltage_adc.read()) self.current = current_lambda(current_adc.read()) self.consumed += self.current / (3600 * (time() - time_prev)) time_prev = time() if self.voltage < self.low_battery_voltage_idle and self.current < self.battery_current_treshold or self.voltage < self.low_battery_voltage_load and self.current >= self.battery_current_treshold: self.critical = hysteresis.set() else: hysteresis.reset() if self.critical: if not self.warning_started: self.warning_started = True start_daemon_thread(self.battery_warning) voltage = self.voltage current = self.current capacity = self.capacity consumed = self.consumed remaining = self.capacity - self.consumed if remaining < 0: remaining = 0 critical = self.critical state = [self.voltage, # 0 [V] self.current, # 1 [A] remaining, # 2 [Ah] self.critical] # 3 [Bool] except Exception, e: continue self.monitor_socket.send(dumps(state))
class PowerMan: def __init__(self, name): map = generate_map(name) self.ctrl_socket = map['ctrl'] self.monitor_socket = map['mon'] self.opcd = OPCD_Interface(map['opcd_ctrl'], 'powerman') bus = SMBus(self.opcd.get('gpio_i2c_bus')) self.gpio_mosfet = GPIO_Bank(bus, self.opcd.get('gpio_i2c_address')) self.power_pin = self.opcd.get('gpio_power_pin') self.cells = self.opcd.get('battery_cells') self.low_cell_voltage_idle = self.opcd.get( 'battery_low_cell_voltage_idle') self.low_cell_voltage_load = self.opcd.get( 'battery_low_cell_voltage_load') self.battery_current_treshold = self.opcd.get( 'battery_current_treshold') self.capacity = self.opcd.get('battery_capacity') self.low_battery_voltage_idle = self.cells * self.low_cell_voltage_idle self.low_battery_voltage_load = self.cells * self.low_cell_voltage_load self.critical = False #self.gpio_mosfet.write() self.warning_started = False # start threads: self.standing = True self.adc_thread = start_daemon_thread(self.adc_reader) self.request_thread = start_daemon_thread(self.request_handler) def battery_warning(self): # do something in order to indicate a low battery: msg = 'CRITICAL WARNING: SYSTEM BATTERY VOLTAGE IS LOW; IMMEDIATE SHUTDOWN REQUIRED OR SYSTEM WILL BE DAMAGED' system('echo "%s" | wall' % msg) beeper_enabled = self.opcd.get('beeper_enabled') while True: if beeper_enabled: self.gpio_mosfet.set_gpio(5, False) sleep(0.1) self.gpio_mosfet.set_gpio(5, True) sleep(0.1) else: sleep(1.0) def adc_reader(self): voltage_adc = ADC(self.opcd.get('voltage_adc')) current_adc = ADC(self.opcd.get('current_adc')) voltage_lambda = eval(self.opcd.get('adc_2_voltage')) current_lambda = eval(self.opcd.get('adc_2_current')) self.consumed = 0.0 vmin = 13.2 vmax = 16.4 self.voltage = voltage_lambda(voltage_adc.read()) batt = min(1.0, max(0.0, (self.voltage - vmin) / (vmax - vmin))) self.consumed = (1.0 - batt) * self.capacity hysteresis = Hysteresis(self.opcd.get('low_voltage_hysteresis')) time_prev = time() while True: sleep(0.2) try: self.voltage = voltage_lambda(voltage_adc.read()) self.current = current_lambda(current_adc.read()) self.consumed += self.current / (3600 * (time() - time_prev)) time_prev = time() if self.voltage < self.low_battery_voltage_idle and self.current < self.battery_current_treshold or self.voltage < self.low_battery_voltage_load and self.current >= self.battery_current_treshold: self.critical = hysteresis.set() else: hysteresis.reset() if self.critical: if not self.warning_started: self.warning_started = True start_daemon_thread(self.battery_warning) voltage = self.voltage current = self.current capacity = self.capacity consumed = self.consumed remaining = self.capacity - self.consumed if remaining < 0: remaining = 0 critical = self.critical state = [ self.voltage, # 0 [V] self.current, # 1 [A] remaining, # 2 [Ah] self.critical ] # 3 [Bool] except Exception, e: continue self.monitor_socket.send(dumps(state))
class PowerMan: def __init__(self, name): # set-up logger: logfile = user_data_dir() + sep + 'PowerMan.log' log_config(filename = logfile, level = DEBUG, format = '%(asctime)s - %(levelname)s: %(message)s') # initialized and load config: log_info('powerman starting up') map = generate_map(name) self.ctrl_socket = map['ctrl'] self.monitor_socket = map['mon'] self.opcd = OPCD_Interface(map['opcd_ctrl'], 'powerman') bus = SMBus(self.opcd.get('gpio_i2c_bus')) self.gpio_mosfet = GPIO_Bank(bus, self.opcd.get('gpio_i2c_address')) self.power_pin = self.opcd.get('gpio_power_pin') self.cells = self.opcd.get('battery_cells') self.low_cell_voltage = self.opcd.get('battery_low_cell_voltage') self.capacity = self.opcd.get('battery_capacity') self.low_battery_voltage = self.cells * self.low_cell_voltage self.critical = False self.gpio_mosfet.write() self.warning_started = False # start threads: self.standing = True self.adc_thread = start_daemon_thread(self.adc_reader) self.emitter_thread = start_daemon_thread(self.power_state_emitter) self.request_thread = start_daemon_thread(self.request_handler) log_info('powerman running') def battery_warning(self): # do something in order to indicate a low battery: msg = 'CRITICAL WARNING: SYSTEM BATTERY VOLTAGE IS LOW; IMMEDIATE SHUTDOWN REQUIRED OR SYSTEM WILL BE DAMAGED' log_warn(msg) system('echo "%s" | wall' % msg) while True: self.gpio_mosfet.set_gpio(5, False) sleep(0.1) self.gpio_mosfet.set_gpio(5, True) sleep(0.1) def adc_reader(self): voltage_adc = ADC(self.opcd.get('voltage_adc')) current_adc = ADC(self.opcd.get('current_adc')) voltage_lambda = eval(self.opcd.get('adc_2_voltage')) current_lambda = eval(self.opcd.get('adc_2_current')) self.current_integral = 0.0 hysteresis = Hysteresis(self.opcd.get('low_voltage_hysteresis')) while True: sleep(1) try: self.voltage = voltage_lambda(voltage_adc.read()) self.current = current_lambda(current_adc.read()) if self.current < 4.0: self.current = 0.5 self.current_integral += self.current / 3600 print self.voltage, self.low_battery_voltage if self.voltage < self.low_battery_voltage: self.critical = hysteresis.set() else: hysteresis.reset() if self.critical: if not self.warning_started: self.warning_started = True start_daemon_thread(self.battery_warning) except Exception, e: log_err(str(e))