Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions electrical/calibrate_mag_gyro.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def magnetometer_calibrate():

# could maybe automate when we detect that the hard offset values stop changing
print("Magnetometer Calibration")
print("Start moving the board in all directions")
print("Start SLOWLY moving the board in all directions")
print("When the magnetic Hard Offset values stop")
print("changing, press ENTER to go to the next step")
print("Press ENTER to continue...")
Expand All @@ -94,7 +94,6 @@ def magnetometer_calibrate():
min_x = min(min_x, mag_x)
min_y = min(min_y, mag_y)
min_z = min(min_z, mag_z)

max_x = max(max_x, mag_x)
max_y = max(max_y, mag_y)
max_z = max(max_z, mag_z)
Expand All @@ -105,9 +104,15 @@ def magnetometer_calibrate():
HI_offset_z = (max_z + min_z) / 2

# Get the soft-iron offset values
SI_offset_x = (max_x - min_x) / 2
SI_offset_y = (max_y - min_y) / 2
SI_offset_z = (max_z - min_z) / 2
mag_scale_x = (max_x - min_x) / 2
mag_scale_y = (max_y - min_y) / 2
mag_scale_z = (max_z - min_z) / 2

avg_rad = (mag_scale_x + mag_scale_y + mag_scale_z) / 3.0

SI_offset_x = avg_rad/(mag_scale_x)
SI_offset_y = avg_rad/(mag_scale_y)
SI_offset_z = avg_rad/(mag_scale_z)

print(
"Hard-Iron Offset: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} uT".format(
Expand Down Expand Up @@ -135,6 +140,8 @@ def magnetometer_calibrate():
)
)

return hard_off, soft_off

#########################
# Gyroscope Calibration #
#########################
Expand Down Expand Up @@ -182,6 +189,7 @@ def gyro_calibrate():
noise_y = max_y - min_y
noise_z = max_z - min_z


print(
"Zero Rate Offset: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} rad/s".format(
offset_x, offset_y, offset_z
Expand All @@ -201,3 +209,7 @@ def gyro_calibrate():
offset_x, offset_y, offset_z
)
)


while True:
magnetometer_calibrate()
34 changes: 34 additions & 0 deletions electrical/gps_nmea_parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import serial
import pynmea2


"""
This code is mainly for extracting pure longitude and latitude data from
a the serial port our ublox C099-F9P gps reciever is connected to.

Note that the C099-F9P module MUST be configured to output NMEA messages,
which may be of type GGC, RMC, etc.
More on GPS messaging protocol can be read here: https://cdn.sparkfun.com/assets/f/7/4/3/5/PM-15136.pdf
"""

class GPSSerialReader:

def __init__(self, port, br):
# port is the physical port that the GPS module is plugged into on board
# port starts at 0, on linux is of form '/dev/ttyS*' where * is the number referring to port
self.port = port
# higher baud rate is usually better for more accurate positioning
self.baud_rate = br


def read_serial(self):
with serial.Serial(self.port, self.baud_rate, timeout=1) as ser:
while True:
line = ser.readline().decode('ascii', errors='replace')
if line.startswith('$G'):
try:
msg = pynmea2.parse(line)
# Assuming the message is of type GGA or RMC which contains lat/long
print(f"Latitude: {msg.latitude}, Longitude: {msg.longitude}")
except pynmea2.ParseError as e:
print(f"Parse error: {e}")
Loading