-
Notifications
You must be signed in to change notification settings - Fork 1
/
battery_check_test.py
executable file
·120 lines (98 loc) · 4.01 KB
/
battery_check_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2020-2021 by Murray Altheim. All rights reserved. This file is part
# of the Robot Operating System project, released under the MIT License. Please
# see the LICENSE file included as part of this package.
#
# author: Murray Altheim
# created: 2020-03-16
# modified: 2021-08-11
#
# Tests the ADS1015 ADC as a battery check device.
#
import pytest
import sys, signal, traceback
from datetime import datetime as dt
from math import isclose
from colorama import init, Fore, Style
init()
from core.rate import Rate
from core.logger import Logger, Level
from core.config_loader import ConfigLoader
from core.message_bus import MessageBus
from core.message_factory import MessageFactory
from hardware.i2c_scanner import I2CScanner, DeviceNotFound
from hardware.battery import BatteryCheck
_message_bus = None
_log = Logger('test', Level.INFO)
# exception handler ............................................................
def signal_handler(signal, frame):
global _message_bus
print(Fore.RED + 'Ctrl-C caught: exiting...' + Style.RESET_ALL)
if _message_bus:
_message_bus.disable()
_message_bus.close()
print(Fore.CYAN + 'exit.' + Style.RESET_ALL)
sys.exit(0)
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
@pytest.mark.unit
def test_battery_check(config):
global _message_bus, _message_factory
_start_time = dt.now()
try:
_log.info('using ADS1015 ADC...')
_battery = BatteryCheck(config, _message_bus, _message_factory, Level.INFO)
_log.info('starting test...')
# now in main application loop until quit or Ctrl-C...
_log.info(Fore.YELLOW + 'enabling message bus...')
_message_bus.enable()
# _battery.enable()
if _message_bus and _message_bus.enabled:
_log.info(Fore.YELLOW + 'disabling and closing message bus...')
_message_bus.disable()
_message_bus.close()
# _hz = 1
# _rate = Rate(_hz, Level.ERROR)
# while True:
# _log.info(Fore.BLACK + '...')
# _rate.wait()
_elapsed_ms = round(( dt.now() - _start_time ).total_seconds() * 1000.0)
_log.info(Fore.YELLOW + 'complete: elapsed: {:d}ms'.format(_elapsed_ms))
except KeyboardInterrupt:
_log.info('Ctrl-C caught; exiting...')
except Exception as e:
_log.error('{} encountered, exiting: {}'.format(type(e), e))
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
def main():
global _message_bus, _message_factory
signal.signal(signal.SIGINT, signal_handler)
try:
# read YAML configuration
_level = Level.INFO
_loader = ConfigLoader(_level)
filename = 'config.yaml'
_config = _loader.configure(filename)
_i2c_scanner = I2CScanner(_config, _level)
if not _i2c_scanner.has_hex_address(['0x48']):
raise DeviceNotFound('no ADS1015 available.')
_log.info('creating message bus...')
_message_bus = MessageBus(_config, _level)
_log.info('creating message factory...')
_message_factory = MessageFactory(_message_bus, _level)
test_battery_check(_config)
except KeyboardInterrupt:
print(Style.BRIGHT + 'caught Ctrl-C; exiting...')
except DeviceNotFound as e:
_log.error('no potentiometer found, exiting.')
except Exception as e:
print(Fore.RED + 'error in motor test: {}'.format(e) + Style.RESET_ALL)
traceback.print_exc(file=sys.stdout)
finally:
_log.info('exit.')
if _message_bus:
_log.info('finally calling close...')
_message_bus.close()
if __name__== "__main__":
main()
#EOF