-
Notifications
You must be signed in to change notification settings - Fork 1
/
mock_pot_test.py
executable file
·90 lines (75 loc) · 3.18 KB
/
mock_pot_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
#!/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-10-05
# modified: 2021-08-07
#
# Tests the mocked digital potentiometer.
#
import pytest
import sys, time, 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 mock.potentiometer import MockPotentiometer
_log = Logger('test', Level.INFO)
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
def key_callback(event):
_log.info('callback on event: {}'.format(event))
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
@pytest.mark.unit
def test_motors():
_start_time = dt.now()
try:
# read YAML configuration
_level = Level.INFO
_loader = ConfigLoader(_level)
filename = 'config.yaml'
_config = _loader.configure(filename)
_log.info('creating message bus...')
_pot = MockPotentiometer(_config, key_callback, Level.INFO)
_pot.enable()
_last_scaled_value = 0.0
_log.info('starting test...')
_hz = 2
_rate = Rate(_hz, Level.ERROR)
while True:
_scaled_value = _pot.get_scaled_value(False)
if _scaled_value != _last_scaled_value: # if not the same as last time
if isclose(_scaled_value, 0.0, abs_tol=0.05):
_pot.set_black()
_log.info(Fore.YELLOW + Style.DIM + 'velocity: {:5.2f}.'.format(_scaled_value))
else:
_pot.set_rgb(_pot.value)
_log.info(Fore.YELLOW + Style.NORMAL + 'velocity: {:5.2f}.'.format(_scaled_value))
_last_scaled_value = _scaled_value
_rate.wait()
except KeyboardInterrupt:
_log.info('Ctrl-C caught; exiting...')
except Exception as e:
_log.error('{} encountered, exiting: {}'.format(type(e), e))
finally:
pass
_elapsed_ms = round((dt.now() - _start_time).total_seconds() * 1000.0)
_log.info(Fore.YELLOW + 'complete: elapsed: {:d}ms'.format(_elapsed_ms))
# ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
def main():
try:
test_motors()
except Exception as e:
print(Fore.RED + 'error in motor test: {}'.format(e) + Style.RESET_ALL)
traceback.print_exc(file=sys.stdout)
finally:
pass
if __name__== "__main__":
main()
#EOF