-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.py
73 lines (64 loc) · 3.06 KB
/
logger.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
"""
logger.py contains the implementation of Logger,
which is responsible for writing the time, state, and sensor values
to a CSV for later inspection.
"""
import csv
from sensor_manager import SensorManager
from proportional_controller import Proportional_Controller
from servo import Servo
from utils import HEADERS
class Logger:
def __init__(self, name: str, sensor_manager: SensorManager, servo: Servo, proportional_controller: Proportional_Controller):
self.name = name
self.sensor_manager = sensor_manager
self.servo = servo
self.proportional_controller = proportional_controller
self._initialize_csv()
def _initialize_csv(self):
"""
The _initialize_csv function is called internally exactly once to load the csv writer
and write the titles of each column. Note that there is no deinit function ... the file
is not gracefully closed. We simply pray that this has no impact.
"""
self.file = open(self.name, "w")
self.writer = csv.writer(self.file)
self.writer.writerow(HEADERS)
def log(self):
"""
The log function adds a row to the csv with the current sensor readings. Note that the
log function simply records values and does not directly read them from the sensor
(nor does it recalculate the time).
"""
row = [
self.sensor_manager.readings-1,
"%.4f" % self.sensor_manager.time,
self.sensor_manager.state.name,
"%.4f" % self.sensor_manager.altitude,
"%.4f" % self.sensor_manager.altitude_bmp,
"%.4f" % self.sensor_manager.acceleration_acce_x,
"%.4f" % self.sensor_manager.acceleration_acce_y,
"%.4f" % self.sensor_manager.acceleration_acce_z,
"%.4f" % self.sensor_manager.acceleration_imu_x,
"%.4f" % self.sensor_manager.acceleration_imu_y,
"%.4f" % self.sensor_manager.acceleration_imu_z,
"%.4f" % self.sensor_manager.linacceleration_imu_x,
"%.4f" % self.sensor_manager.linacceleration_imu_y,
"%.4f" % self.sensor_manager.linacceleration_imu_z,
"%.4f" % self.sensor_manager.eulerangle_imu_x,
"%.4f" % self.sensor_manager.eulerangle_imu_y,
"%.4f" % self.sensor_manager.eulerangle_imu_z,
"%.4f" % self.sensor_manager.gravity_imu_x,
"%.4f" % self.sensor_manager.gravity_imu_y,
"%.4f" % self.sensor_manager.gravity_imu_z,
"%.4f" % self.sensor_manager.kalman_acceleration,
"%.4f" % self.sensor_manager.kalman_velocity,
"%.4f" % self.sensor_manager.kalman_altitude,
"%.4f" % self.sensor_manager.orientation_beta,
# "%.4f" % self.sensor_manager.predicted_apogee
"%.4f" % self.servo.angle,
"%.4f" % self.proportional_controller.servo_target_angle,
"%.4f" % self.proportional_controller.apogee_projected,
"%.4f" % self.proportional_controller.apogee_error
]
csv.writer(self.file).writerow(row)