Commits

iorodeo committed 20d4777

Added settings and timing file creation to the lia_avi_writer. Modified the web
interface so that the timing and settings files are deleted when the avi file
is deleted.

Comments (0)

Files changed (5)

lia_avi_writer/nodes/lia_avi_writer.py

 import redis
 import lia_config
 from lia_web_interface import db_tools
+from lia_web_interface import file_tools
 
 # Messages
 from sensor_msgs.msg import Image
         # Current pulse parameters 
         self.pulse_channel = 'a'
         self.pulse_controller = None
+        self.timing_fid = None
 
         self.lock = threading.Lock()
         self.redis_db = redis.Redis('localhost', db=lia_config.redis_db)
                     self.done = False
                     self.recording_message = 'recording'
 
-                    # Setup pulse controller 
+                    # Get trial values and setup pulse controller 
                     trial_values = db_tools.get_dict(self.redis_db, 'trial_values')
                     self.pulse_controller = Pulse_Controller(
                             trial_values,
                             self.pulse_channel,
                             self.set_current_proxy,
                             )
-                            
+
+                    # Write settings file and open timing file
+                    log_values = db_tools.get_dict(self.redis_db, 'log_values')
+                    data_directory = log_values['data_directory']
+                    settings_suffix = log_values['settings_file_suffix']
+                    timing_suffix = log_values['timing_file_suffix']
+                    metadata_filenames = file_tools.get_metadata_filenames(
+                            self.filename,
+                            data_directory,
+                            settings_suffix,
+                            timing_suffix,
+                            )
+                    settings_filename, timing_filename = metadata_filenames
+                    settings_fid = open(settings_filename,'w')
+                    for k,v in trial_values.iteritems():
+                        settings_fid.write('{0}: {1}\n'.format(k,v))
+                    settings_fid.close()
+                    self.timing_fid = open(timing_filename,'w')
+                   
             elif command == 'stop':
-                self.done = True
-                del self.writer
-                self.writer = None
-                self.recording_message = 'stopped'
+                self.cleanup_after_recording()
                 response = True
 
             return RecordingCmdResponse(response)
                 self.frame_count += 1
                 self.progress_t = self.current_t - self.start_t
                 self.pulse_controller.update(self.progress_t)
+                self.update_timing_file()
 
                 # Check to see if we are done recording - if so stop writing frames 
                 if self.current_t >= self.start_t + self.record_t:
-                    self.done = True
-                    del self.writer
-                    self.writer = None
-                    self.pulse_controller.set_pulse_low()
-                    self.pulse_controller.turn_off()
-                    self.recording_message = 'finished'
-                    self.redis_db.set('recording_flag',0)
+                    self.cleanup_after_recording()
 
         # Publish progress message
         with self.lock:
             self.progress_msg.recording_message = self.recording_message
         self.progress_pub.publish(self.progress_msg)
 
+    def update_timing_file(self): 
+        """
+        Updates information in timing file - frame count, time and pulse
+        state.
+        """
+        if self.timing_fid is not None: 
+            self.timing_fid.write('{0} '.format(self.frame_count))
+            self.timing_fid.write('{0} '.format(self.progress_t))
+            if self.pulse_controller.state == 'high':
+                self.timing_fid.write('{0}\n'.format(1))
+            else:
+                self.timing_fid.write('{0}\n'.format(0))
+
+    def cleanup_after_recording(self):
+        """
+        Cleans up after recording is stopped - closes open files, turns off current
+        controller.
+
+        Should alwars be called with the lock.
+        """
+        self.done = True
+        del self.writer
+        self.writer = None
+        self.pulse_controller.set_pulse_low()
+        self.pulse_controller.turn_off()
+        self.recording_message = 'finished'
+        self.redis_db.set('recording_flag',0)
+        self.timing_fid.close()
+
+
+
 
 class Pulse_Controller(object):
 
         self.next_change_time = self.pulse_start_time
 
     def set_pulse_high(self):
-        print 'set_pulse_high'
         self.set_current_func(self.channel,'on',self.pulse_current)
         self.state = 'high'
 
     def set_pulse_low(self):
-        print 'set_pulse_low'
         self.set_current_func(self.channel,'on', 0)
         self.state = 'low'
 
     def turn_off(self):
-        print 'turn off'
         self.set_current_func(self.channel,'off',0)
 
     def update(self,t):
             if self.state == 'low':
                 self.set_pulse_high()
                 self.next_change_time += self.pulse_high_time
-                print 't = ', self.next_change_time
             elif self.state == 'high':
                 self.set_pulse_low()
                 self.count += 1
                 self.next_change_time = self.pulse_start_time + self.count*self.pulse_period
-                print 't = ', self.next_change_time
 
 
 def trial_time_to_secs(tt):

lia_config/launch/lia_devel.launch

     <rosparam file="$(find lia_config)/launch/camera_config.yaml"/>
   </node>
 
-  <!-- AVI writer node -->
-  <!-- <node pkg="lia_avi_writer" type="lia_avi_writer.py" name="avi_writer" args="/camera/image_raw"/> -->
-
   <!-- Current controller node -->
   <node pkg="lia_current_controller" type="current_controller_node.py" name="current_controller"/>
 
+  <!-- AVI writer node -->
+  <node pkg="lia_avi_writer" type="lia_avi_writer.py" name="avi_writer" args="/camera/image_raw"/> 
+
   <!-- FPS monitor node -->
   <node pkg="lia_fps_monitor" type="fps_node.py" name="fps_monitor" args="/camera/camera_info"/>
 

lia_web_interface/src/lia_web_interface/__init__.py

 import db_tools
+import file_tools

lia_web_interface/src/lia_web_interface/file_tools.py

 import os
 import os.path
 import time
+import db_tools
 
 def add_datetime_suffix(filename):
     """
             #avi_info_files.append(('%s.mpg'%(base,),f_size,f_time))
         return avi_info_files
 
-def delete_data_files(avi_files, data_directory):
+def delete_data_files(avi_files, log_values):
     """
     Delete avi movie files in the list as well as any associated
     files - e.g., _settings.txt and _timing.txt files.
     """
+    data_directory = log_values['data_directory']
+    settings_suffix = log_values['settings_file_suffix']
+    timing_suffix = log_values['timing_file_suffix']
     for f in avi_files:
         f_base, f_ext = os.path.splitext(f)
         if f_ext == '.avi':
                 os.remove(f_path)
             except OSError:
                 pass
+            metadata_filenames = get_metadata_filenames(
+                    f,
+                    data_directory,
+                    settings_suffix,
+                    timing_suffix,
+                    )
+            settings_filename, timing_filename = metadata_filenames
+            os.remove(settings_filename)
+            os.remove(timing_filename)
+
+def get_metadata_filenames(movie_filename,data_directory,settings_suffix,timing_suffix): 
+    """
+    Get based on the name of the avi file and the suffixes defined in the
+    log_values creates the metadata file names for settings and timing.
+    """
+    movie_filename_base, movie_ext = os.path.splitext(movie_filename)
+    settings_filename = '{0}{1}.txt'.format(movie_filename_base,settings_suffix)
+    settings_filename = os.path.join(data_directory,settings_filename)
+
+    timing_filename = '{0}{1}.txt'.format(movie_filename_base,timing_suffix)
+    timing_filename = os.path.join(data_directory,timing_filename)
+    return settings_filename, timing_filename
             
 def check_dir(dirname):
     """

lia_web_interface/src/lia_web_interface/web_interface.py

             set_values_flag = True
         if 'delete_selected_files' in flask.request.form:
             selected_avi_files = form_tools.get_selected_avi_files(flask.request.form)
-            file_tools.delete_data_files(selected_avi_files, log_values['data_directory'])
+            file_tools.delete_data_files(selected_avi_files, log_values)
             deleted_avi_files = selected_avi_files
 
     kwargs.update(log_values)