Parking Finder
The Parking Finder system is a smart parking solution designed to help drivers efficiently locate available
Loading...
Searching...
No Matches
imu.py
Go to the documentation of this file.
1
4
5import threading
6import board
7import adafruit_icm20x
8
9#if need ax,ay,az (or other measurements)=>
10#myimu = IMU()
11#ax, ay, az = myimu.get_accel()
12#print(myimu) => should be used in a while True loop
13#once application closes make sure run myimu.stop() for clean stop
14
15class IMU:
16 def __init__(self):
17
18 i2c = board.I2C()
19 self.sensor = adafruit_icm20x.ICM20948(i2c)
20 #self.rate = rate_hz
21 self.running = True
22
23 self.accel = self.sensor.acceleration# m/s^2
24 self.gyro = self.sensor.gyro #rad/s
25 self.magnetic = self.sensor.magnetic # uT
26
27 # new data flag
28 self.new_data = False
29
30 # start background thread
31 self.thread = threading.Thread(target=self.update_loop, daemon=True)
32 self.thread.start()
33
34 def __str__(self):
35 return (
36 f"Acceleration: X:{self.accel[0]:.3f}, Y:{self.accel[1]:.3f}, Z:{self.accel[2]:.3f} m/s^2\n"
37 f"Gyroscope: X:{self.gyro[0]:.3f}, Y:{self.gyro[1]:.3f}, Z:{self.gyro[2]:.3f} rads/s\n"
38 f"Magnetometer: X:{self.magnetic[0]:.3f}, Y:{self.magnetic[1]:.3f}, Z:{self.magnetic[2]:.3f} uT\n"
39 )
40
41 def update_loop(self):
42 #period = 1 / self.rate
43 while self.running:
44 self.accel = self.sensor.acceleration
45 self.gyro = self.sensor.gyro
46 self.magnetic = self.sensor.magnetic
47 self.new_data = True
48 #time.sleep(period)
49
50 def get_accel(self):
51 return self.accel
52
53 def get_gyro(self):
54 return self.gyro
55
56 def get_magn(self):
57 self.new_data = False
58 return self.magnetic
59
60 def stop(self):
61 self.running = False
62 self.thread.join()
Definition imu.py:15
bool running
Definition imu.py:21
thread
Definition imu.py:31
gyro
Definition imu.py:24
accel
Definition imu.py:23
bool new_data
Definition imu.py:28
magnetic
Definition imu.py:25
update_loop(self)
Definition imu.py:41
sensor
Definition imu.py:19