Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rafa-martin committed Feb 4, 2025
1 parent ccefd39 commit 716514d
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions script/record_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@
from robotnik_msgs.msg import RecordStatus
from robotnik_msgs.srv import Record, RecordResponse

# Auxiliary functions
#
def _safe_get_param(param_name, default_value):
if rospy.has_param(param_name):
return rospy.get_param(param_name)
else:
rospy.logwarn(f"Parameter '{param_name}' not found in param server. Using default value: {default_value}")
return default_value

def _clamp(name, value, min_value, max_value):
if value < min_value:
rospy.logwarn(f"Paremeter '{name}' with value {value} is lower than the minimum value allowed: {min_value}. Setting it to the minimum value.")
return min_value
elif value > max_value:
rospy.logwarn(f"Paremeter '{name}' with value {value} is higher than the maximum value allowed: {max_value}. Setting it to the maximum value.")
return max_value
else:
return value

def callBackRecordService(request):
global action
Expand All @@ -26,10 +44,10 @@ def callBackRecordService(request):
global wave_interface
response = RecordResponse()

disk_capacity, disk_usage, free_disk_space = shutil.disk_usage(folder_path)
disk_total, disk_usage, _ = shutil.disk_usage(folder_path)

if (request.action.upper() == request.ACTION_RECORD):
if (disk_usage * 100 / disk_capacity) >= max_disk_usage:
if (disk_usage * 100 / disk_total) >= max_disk_usage:
response.success = False
response.message = "Insufficient disk space to start recording"
return response
Expand Down Expand Up @@ -98,17 +116,13 @@ def record():
global wave_interface
global estimated_compressed_audio_size_bytes
global action
global disk_capacity
global disk_usage
global free_disk_space


disk_capacity, disk_usage, free_disk_space = shutil.disk_usage(folder_path)
disk_total, _, disk_free = shutil.disk_usage(folder_path)

l, data = inp.read()
estimated_raw_audio_size = l * 2
estimated_compressed_audio_size_bytes = estimated_raw_audio_size / compression_ratio
theoretical_remaining_space_percentage = (free_disk_space - estimated_compressed_audio_size_bytes) * 100 / disk_capacity
theoretical_remaining_space_percentage = (disk_free - estimated_compressed_audio_size_bytes) * 100 / disk_total

if (100 - theoretical_remaining_space_percentage) >= max_disk_usage_recording:
a = numpy.fromstring(data, dtype='int16')
Expand All @@ -133,17 +147,13 @@ def record():
global inp
global wave_interface
global time_start_record
global disk_capacity
global disk_usage
global break_recording
global free_disk_space
global wav_header_size
global compression_ratio
global max_disk_usage_recording
global max_disk_usage

max_disk_usage = rospy.get_param('~max_disk_usage' , 85.)
max_disk_usage_recording = rospy.get_param('~max_disk_usage_recording', 90.)

# Variables
wav_header_size = 44
max_time_record = 0
folder_path = ""
Expand All @@ -161,9 +171,11 @@ def record():
elif file_format == "wav":
compression_ratio = 1

disk_capacity = 0.
disk_usage = 0.
free_disk_space = 0.
# Get parameters
max_disk_usage = _safe_get_param('~max_disk_usage', 85.)
max_disk_usage_recording = _safe_get_param('~max_disk_usage_recording', 90.)
_clamp('max_disk_usage', max_disk_usage, 0., 100.)
_clamp('max_disk_usage_recording', max_disk_usage_recording, 0., 100.)

# get params name
device = rospy.get_param('~device', device)
Expand Down Expand Up @@ -195,7 +207,7 @@ def record():
record_state.recording = True
record_state.state_description += " " + str(int(seconds_diff.to_sec())) + " seg."
record_state.recording_time = int(seconds_diff.to_sec())
rospy.loginfo(record_state.state_description)
rospy.loginfo_throttle(1, "Recording time: " + str(int(seconds_diff.to_sec())) + " seg.")
else:
record_state.state_description = action
record_state.recording = False
Expand Down

0 comments on commit 716514d

Please sign in to comment.