forked from dhague/vpower
-
Notifications
You must be signed in to change notification settings - Fork 9
/
LinearInterpolationPowerCalculator.py
38 lines (33 loc) · 1.34 KB
/
LinearInterpolationPowerCalculator.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
import os, sys
import csv
from AbstractPowerCalculator import AbstractPowerCalculator
from functions import interp
if getattr(sys, 'frozen', False):
# If we're running as a pyinstaller bundle
SCRIPT_DIR = os.path.dirname(sys.executable)
else:
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
class LinearInterpolationPowerCalculator(AbstractPowerCalculator):
def __init__(self):
super(LinearInterpolationPowerCalculator, self).__init__()
self.wheel_circumference = 2.105 # default value - can be overridden in config.py
xp = [0]
yp = [0]
curve_file = '%s/curve.csv' % SCRIPT_DIR
if os.path.isfile(curve_file):
with open(curve_file, 'r') as fd:
reader = csv.reader(fd)
next(reader, None)
for line in reader:
xp.append(int(line[0]))
yp.append(int(line[1]))
else:
xp.extend([5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60])
yp.extend([30, 60, 90, 125, 160, 200, 230, 280, 325, 375, 430, 490])
def power_from_speed(self, revs_per_sec):
kms_per_rev = self.wheel_circumference / 1000.0
speed = revs_per_sec * 3600 * kms_per_rev
power = int(interp(self.xp, self.yp, speed))
return power
def set_wheel_circumference(self, circumference):
self.wheel_circumference = circumference