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
32 changes: 27 additions & 5 deletions examples/omniscan450Example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from builtins import input

import math
import signal
import sys
from pathlib import Path
Expand Down Expand Up @@ -111,19 +112,32 @@ def signal_handler(sig, frame):
# Running omniscan450Example.py from existing log file
if args.log is not None and not new_log:
with open(log_path, 'rb') as f:
count = 0
overall_sum = 0
while True:
data = Omniscan450.read_packet(f)

if data is None:
break # EOF or bad packet

print(f"ID: {data.message_id}\tName: {data.name}")
# print(f"ID: {data.message_id}\tName: {data.name}")
if data.message_id == definitions.OMNISCAN450_OS_MONO_PROFILE:
count += 1
# # print(data)

# Printing the same results as if directly connected to the Omniscan
scaled_result = Omniscan450.scale_power(data)
print(f"Average power: {sum(scaled_result) / len(scaled_result)}")

linear_powers = [10 ** (db / 10) for db in scaled_result]

avg_linear_power = sum(linear_powers) / len(linear_powers) if linear_powers else 0

avg_power_db = 10 * math.log10(avg_linear_power) if avg_linear_power > 0 else float('-inf')

print(f"Average power (linearized): {avg_power_db:.2f} dB")
overall_sum += avg_power_db
# print(f"Average power: {sum(scaled_result) / len(scaled_result)}")
print(f"Final average power: {overall_sum / count if count > 0 else 0:.2f} dB")

# Connected to physical omniscan
else:
Expand All @@ -150,10 +164,10 @@ def signal_handler(sig, frame):
myOmniscan450.control_os_ping_params(enable=1)

# For a custom ping rate
custom_msec_per_ping = Omniscan450.calc_msec_per_ping(1000) # 1000 Hz
custom_msec_per_ping = Omniscan450.calc_msec_per_ping(20)

# To find pulse length percent
custom_pulse_length = Omniscan450.calc_pulse_length_pc(0.2) # 0.2%
custom_pulse_length = Omniscan450.calc_pulse_length_pc(0.2)

## Set these attributes like this
# myOmniscan450.control_os_ping_params(
Expand All @@ -174,7 +188,15 @@ def signal_handler(sig, frame):
if data:
scaled_result = Omniscan450.scale_power(data)
try:
print(f"Average power: {sum(scaled_result) / len(scaled_result)}")
linear_powers = [10 ** (db / 10) for db in scaled_result]

avg_linear_power = sum(linear_powers) / len(linear_powers) if linear_powers else 0

avg_power_db = 10 * math.log10(avg_linear_power) if avg_linear_power > 0 else float('-inf')

print(f"Average power (linearized): {avg_power_db:.2f} dB")

# print(f"Average power: {sum(scaled_result) / len(scaled_result)}")
except ZeroDivisionError:
print("Length of scaled_result is 0")
elif not data:
Expand Down
12 changes: 0 additions & 12 deletions examples/surveyor240Example.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,6 @@ def signal_handler(sig, frame):
roll = math.atan2(vector[1], vector[2])
print(f"Pitch: {pitch}\tRoll: {roll}")

# if data.message_id == definitions.SURVEYOR240_YZ_POINT_DATA:
# # Display YZ point data in a table
# yz_data = Surveyor240.create_yz_point_data(data)
# print(f"Length of yz_data: {len(yz_data)}\tNum_points: {data.num_points}")
# print("Index\tY\tZ")
# for i in range(0, len(yz_data), 2):
# print(f"{i//2}\t{yz_data[i]:.2f}\t{yz_data[i+1]:.2f}")
# print(f"Temperature: {(data.water_degC * 9/5) + 32} F")
# print(f"Temperature: {data.water_degC} C")
# print(f"Pressure: {data.water_bar} Bar")

# if data.message_id == definitions.SURVEYOR240_ATOF_POINT_DATA:
# # Just an example packet, could check for other packet types and
# # show results from those too
Expand Down Expand Up @@ -201,7 +190,6 @@ def signal_handler(sig, frame):
# Set multiple packets to listen for
data = mySurveyor240.wait_message([definitions.SURVEYOR240_ATOF_POINT_DATA,
definitions.SURVEYOR240_ATTITUDE_REPORT,
definitions.SURVEYOR240_YZ_POINT_DATA,
definitions.SURVEYOR240_WATER_STATS])

if data:
Expand Down
2 changes: 1 addition & 1 deletion generate/templates/omniscan450.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class Omniscan450(PingDevice):
save_name = dt.strftime("%Y-%m-%d-%H-%M")

if log_directory is None:
project_root = Path.cwd().parent
project_root = Path.cwd()
self.log_directory = project_root / "logs/omniscan"
else:
self.log_directory = Path(log_directory)
Expand Down
8 changes: 7 additions & 1 deletion generate/templates/s500.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ from brping import pingmessage
import time
import struct
import socket
import math
from datetime import datetime, timezone
from pathlib import Path

Expand Down Expand Up @@ -158,6 +159,11 @@ class S500(PingDevice):
except ConnectionResetError as e:
raise ConnectionError("Socket connection was reset: %s" % str(e))

# Calculate the milliseconds per ping from a ping rate
@staticmethod
def calc_msec_per_ping(ping_rate):
return math.floor(1000.0 / ping_rate)

# Converts power results to correct format
@staticmethod
def scale_power(msg):
Expand Down Expand Up @@ -276,7 +282,7 @@ class S500(PingDevice):
save_name = dt.strftime("%Y-%m-%d-%H-%M")

if log_directory is None:
project_root = Path.cwd().parent
project_root = Path.cwd()
self.log_directory = project_root / "logs/s500"
else:
self.log_directory = Path(log_directory)
Expand Down
13 changes: 5 additions & 8 deletions generate/templates/surveyor240.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ from brping import pingmessage
import time
import struct
import socket
import math
from datetime import datetime, timezone
from pathlib import Path

Expand Down Expand Up @@ -232,7 +233,7 @@ class Surveyor240(PingDevice):
save_name = dt.strftime("%Y-%m-%d-%H-%M")

if log_directory is None:
project_root = Path.cwd().parent
project_root = Path.cwd()
self.log_directory = project_root / "logs/surveyor"
else:
self.log_directory = Path(log_directory)
Expand Down Expand Up @@ -387,14 +388,10 @@ class Surveyor240(PingDevice):

return tuple(atof_list)

# Creates yz_point_data and fills it with the correct data from the message
# Calculate the milliseconds per ping from a ping rate
@staticmethod
def create_yz_point_data(msg):
raw_array = msg.yz_point_data

yz_list = struct.unpack('<' + 'f' * int(msg.num_points) * 2, raw_array)
return yz_list

def calc_msec_per_ping(ping_rate):
return math.floor(1000.0 / ping_rate)

if __name__ == "__main__":
import argparse
Expand Down