-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_measurements.py
84 lines (61 loc) · 2.36 KB
/
arduino_measurements.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
# -*- coding: utf-8 -*-
"""
Created on September 1 15:38 2018
Truss framework created by Máté Szedlák.
Copyright MIT, Máté Szedlák 2016-2018.
"""
import random
from base_objects import Loads
def convert_node_id_to_dof_id(node_list):
dof_list = []
for node in node_list:
remainder = None
if node[-1].upper() == 'X':
remainder = 0
elif node[-1].upper() == 'Y':
remainder = 1
elif node[-1].upper() == 'Z':
remainder = 2
dof = int(node[0:len(node)-1]) * 3 + remainder
dof_list.append(dof)
return dof_list
class ArduinoMeasurements(object):
def __init__(self, node_list):
self.id_list = convert_node_id_to_dof_id(node_list)
self.displacements = []
self.loads = []
# Measure initial distances
self.initial_measurements = self.calibrate()
def read_raw_input(self, fake, random_limit = 0):
"""
Arduino input
:param fake: it will be returned wrapped in an array
:return: fake
"""
if random_limit > 0:
random_input = fake + random.randint(-random_limit*10, +random_limit*10)/10
return [random_input]
else:
return [fake]
def calibrate(self):
return [0]
def update(self, loads, title=''):
"""
One measurement means a displacement along one axis (X/Y/Z)
A measurement value is negative if the displacement's ordinate is lower than at the initial moment,
e.g. when it goes "down".
Other radial displacement shall be divided into X/Y/Z directional components.
"""
# TODO: write function for:
try:
with open("./loads/%s.txt" % title, "r") as sourcefile:
load_input = sourcefile.readline().strip().split(' ')
load = Loads({'forces': [[int(load_input[0]), float(load_input[1])]]})
loads.forces = load.forces
except IOError:
print("The following file could not be opened: ./loads/%s" % title)
print("Please make sure that the load data is available for the program")
raise IOError
measurements = self.read_raw_input(5, 0)
self.displacements = [[self.id_list[i], self.initial_measurements[i] - measurements[i]]
for i in range(len(self.id_list))]