Parking Finder
The Parking Finder system is a smart parking solution designed to help drivers efficiently locate available
Loading...
Searching...
No Matches
localization.py
Go to the documentation of this file.
1
3
4import numpy as np
5from imu import IMU
6from gps import GPS
7from test_ekf import EKF
8
9
10def blend_angle(ekf, old_angle, new_angle, alpha):
11 diff = ekf.normalize_angle(new_angle - old_angle)
12 return ekf.normalize_angle(old_angle + alpha * diff)
13
14# This function converts latitude and longitude to local Cartesian coordinates (x, y) in meters relative to an origin point (lat0, lon0).
15def latlon_toxy(lat, lon, lat0, lon0): # lat0 and lon0 are the initial starting GPS coordinates
16 R = 6378137 # Earth radius in meters
17
18 lat = np.radians(lat)
19 lon = np.radians(lon)
20
21 lat0 = np.radians(lat0)
22 lon0 = np.radians(lon0)
23
24 x = (lon - lon0) * R * np.cos(lat0)
25 y = (lat - lat0) * R
26
27 return x, y
28
29# This function calculates the yaw (heading) of the car. It uses pitch and roll to compensate for possible tilted angle of the IMU.
30def tilt_compensated_yaw(accel, mag):
31 ax, ay, az = accel
32 mx, my, mz = mag
33
34 # Normalize accelerometer
35 norm_a = np.sqrt(ax**2 + ay**2 + az**2)
36 norm_m = np.sqrt(mx**2 + my**2 + mz**2)
37
38 if norm_a < 1e-6 or norm_m < 1e-6:
39 return None
40
41 ax /= norm_a
42 ay /= norm_a
43 az /= norm_a
44
45 # Pitch and roll
46 pitch = np.arcsin(-ax) # rotation around y-axis
47 roll = np.arctan2(ay, az) # rotation around x-axis
48
49 # Tilt compensation for magnetometer
50 mx_comp = mx * np.cos(pitch) + mz * np.sin(pitch)
51 my_comp = mx * np.sin(roll) * np.sin(pitch) + my * np.cos(roll) - mz * np.sin(roll) * np.cos(pitch)
52
53 # Yaw calculation
54 yaw = np.arctan2(-my_comp, mx_comp) # rotation around z-axis, negative sign depends on sensor frame
55
56 return yaw