class DataLogger: """Logs data from the magenetomer for implementing the calibration levels""" def __init__(self, magnetometer=None): if magnetometer == None: self.magnetometer = Magnetometer() else: self.magnetometer = magnetometer self.x = [] self.y = [] self.z = [] def measure(self, num_readings, time_ms_per_reading=0): """time per reading must be more than 7 ms""" if (time_ms_per_reading > 7): time_sleep = time_ms_per_reading - 7 for i in range(num_readings + 2): self.magnetometer.take_measurement() if i > 1: self.x.append(self.magnetometer.x) self.y.append(self.magnetometer.y) self.z.append(self.magnetometer.z) sleep_ms(time_sleep) else: for i in range(num_readings): self.magnetometer.take_measurement() if i > 1: self.x.append(self.magnetometer.x) self.y.append(self.magnetometer.y) self.z.append(self.magnetometer.z) def get_json(self): data["x"] = self.x data["y"] = self.y data["z"] = self.z return ujson.dumps(data) def std_dev(self): mean = sum(self.z) / len(self.z) z = math.sqrt( sum(map(lambda x: (x - mean) * (x - mean), self.z)) / len(self.z)) mean = sum(self.y) / len(self.y) y = math.sqrt( sum(map(lambda x: (x - mean) * (x - mean), self.y)) / len(self.y)) mean = sum(self.x) / len(self.x) x = math.sqrt( sum(map(lambda x: (x - mean) * (x - mean), self.x)) / len(self.x)) return (x, y, z)
class DataLogger: """ Logs data from the magenetometer """ def __init__(self, magnetometer=None): """ Creates a DataLogger instance, optionally uses a Magnetometer instance if given, otherwises creates a new instance :param magnetometer: the optional magnetometer instance it will use to record data """ if magnetometer == None: self.magnetometer = Magnetometer() else: self.magnetometer = magnetometer self.data = [] def measure(self, num_readings, time_ms_per_reading=0): """ Takes reading from the magnetometer and scales to the correct units :param num_readings: number of readings to take :param time_ms_per_reading: time to spend taking each reading :return: no return value """ self.data = [] """time per reading must be more than 7 ms""" if (time_ms_per_reading > 7): time_sleep = time_ms_per_reading - 7 for i in range(num_readings + 2): self.magnetometer.take_measurement() if i > 1: vector = Vector3D() vector.x = self.magnetometer.x vector.y = self.magnetometer.y vector.z = self.magnetometer.z self.data.append(vector) sleep_ms(time_sleep) else: for i in range(num_readings): self.magnetometer.take_measurement() if i > 1: vector = Vector3D() vector.x = self.magnetometer.x vector.y = self.magnetometer.y vector.z = self.magnetometer.z self.data.append(vector) if num_readings >= 10: self.data = self.data[5:] def get_json(self): """ Converts recorded magnetometer data to JSON :return: JSON object containing magnetometer readings """ return ujson.dumps(self.data) def mean(self): """ Calculates the mean of the recorded data :return: Vector3D instance of the mean value recorded in each axis """ if self.data == []: print("Take measurement first using measure()") return 0 self.data_mean = Vector3D() for i in self.data: self.data_mean = self.data_mean + i self.data_mean = self.data_mean.mul(1 / len(self.data)) return self.data_mean def std_dev(self): """ Calculate the standard deviation of the recorded data :return: Vector3D instance of the std deviation value recorded in each axis """ self.mean() res = Vector3D() for i in range(len(self.data)): res = res + (self.data[i] - self.data_mean) * (self.data[i] - self.data_mean) res = res.mul(1 / len(self.data)) res = res.sqrt() self.data_std_dev = res return res