-
Notifications
You must be signed in to change notification settings - Fork 1
/
digital_pot_test.py
executable file
·104 lines (89 loc) · 3.99 KB
/
digital_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/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 digital potentiometer.
#
import pytest
import sys, 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 hardware.i2c_scanner import I2CScanner, DeviceNotFound
from hardware.digital_pot import DigitalPotentiometer
#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_digital_potentiometer():
_start_time = dt.now()
try:
# read YAML configuration
_level = Level.INFO
_loader = ConfigLoader(_level)
filename = 'config.yaml'
_config = _loader.configure(filename)
_i2c_scanner = I2CScanner(_config, _level)
if _i2c_scanner.has_hex_address(['0x0E']):
_log.info('using digital potentiometer...')
# configure digital potentiometer for motor speed
# _pot = DigitalPotentiometer(_config, level=_level)
_pot = DigitalPotentiometer(_config, out_min=-180, out_max=180, level=_level)
# _pot.set_output_limits(-0.90, 0.90)
else:
raise Exception('no digital potentiometer available.')
# _log.info('using mock potentiometer...')
# _pot = MockPotentiometer(_config, key_callback, Level.INFO)
# sys.exit(0)
_last_scaled_value = 0.0
_log.info('starting test...')
_hz = 20
_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
# math.isclose(3, 15, abs_tol=0.03 * 255) # 3% on a 0-255 scale
if isclose(_scaled_value, 0.0, abs_tol=0.05 * 90):
_pot.set_black()
_log.info(Fore.YELLOW + Style.DIM + 'scaled value: {:9.6f}'.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 DeviceNotFound as e:
_log.error('no potentiometer found, 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_digital_potentiometer()
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