-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.py
41 lines (30 loc) · 1.16 KB
/
helper.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
from __future__ import division
import math
import logging
# this only SEEMS to be unused, but some logging config is run on import.
import config
logger = logging.getLogger(__name__)
def map(x, in_min, in_max, out_min, out_max):
"""
Maps values in one range to a value in another range.
See https://www.arduino.cc/reference/en/language/functions/math/map/
"""
return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
def generate_curve(min_value, max_value, step_size_limit):
"""
Generate a sine curve from the "from" value to the "to" value, not to
exceed the given step size. Purpose of this is to have servos moving
with relatively high rotating mass, without knocking over or de-calibrating
the rig due to torque.
"""
steps = int((max_value - min_value) / step_size_limit)
retval = []
half_range = (max_value - min_value) / 2
for i in range(1, steps):
val = int(min_value + half_range + math.sin(math.pi * i / (steps / 2))
* half_range)
retval.append(val)
return retval
if __name__ == "__main__":
val = generate_curve(-99, 99, 2)
logger.info(val)