-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
picomotodash_utils.py
74 lines (48 loc) · 1.57 KB
/
picomotodash_utils.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
# -*- coding: utf-8 -*-
"""Pico Motorcycle Dashboard UTILS
"""
__author__ = "Salvatore La Bua"
# Utility functions
log_file = open("log.csv", "a")
def _log(data):
log_file.write(str(data) + "\n")
log_file.flush()
def map_range(value, in_range, out_range):
(a, b), (c, d) = in_range, out_range
return (value - a) / (b - a) * (d - c) + c
def moving_avg(value, array, n):
if len(array) >= n:
array.pop(0)
array.append(value)
return sum(array) / len(array)
def normalise_avg(avg, headings, neopixel_ring=None):
# Check whether the array contains both positive and negative numbers
has_positive = any(num > 270 for num in headings)
has_negative = any(num < 90 for num in headings)
if avg > 180:
np_val = 360 - avg
else:
np_val = avg
if has_positive and has_negative:
if neopixel_ring is not None:
neopixel_ring.set_np(0, (0, 0, 32))
tmp_headings = []
for h in headings:
if h > 270:
h = h - 360
tmp_headings.append(h)
avg = ((sum(tmp_headings) / len(tmp_headings)) + 360) % 360
return avg
if neopixel_ring is not None:
neopixel_ring.set_np(0, (0, round(map_range(np_val, (180, 0), (0, 32))), 0))
return avg
def read_adc(adc):
return adc.read_u16()
def read_builtin_temp(adc):
CONVERSION_FACTOR = 3.3 / 65535
reading = read_adc(adc) * CONVERSION_FACTOR
return 27 - (reading - 0.706) / 0.001721
def read_gps(gps):
gps.update_gps(verbose=False)
def read_mpu(mpu):
mpu.update_mpu()