data_logger.py 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. import numpy as np
  2. import logging
  3. from rblidar import RBLidar # Assuming rblidar.py is in the same directory
  4. class DataLogger:
  5. def __init__(self):
  6. self.data_frames = [] # List to store the frames of data
  7. logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s')
  8. def frame_callback(self, point_data_array):
  9. # Append the received frame (NumPy array) to the list
  10. self.data_frames.append(point_data_array)
  11. logging.info(f"Logged frame with {len(point_data_array)} points.")
  12. def save_data(self, filename='logged_data.npy'):
  13. # Concatenate all frames into a single NumPy array and save it
  14. if self.data_frames:
  15. all_data = np.concatenate(self.data_frames)
  16. np.save(filename, all_data)
  17. logging.info(f"Data saved to {filename}.")
  18. else:
  19. logging.info("No data to save.")
  20. if __name__ == "__main__":
  21. data_logger = DataLogger()
  22. lidar = RBLidar("192.168.8.1", 2368, frame_callback=data_logger.frame_callback)
  23. try:
  24. while True:
  25. pass # Keep the program running to receive data
  26. except KeyboardInterrupt:
  27. logging.info("Stopping and saving data...")
  28. data_logger.save_data() # Save data on exit