Advertisement
Guest User

Joulescope sample script

a guest
Nov 14th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import joulescope
  2.  
  3. # Adjust logging level of Joulescope to avoid spamming the log
  4. logging.getLogger("joulescope").setLevel(logging.WARNING)
  5. logging.getLogger("jsdrv").setLevel(logging.WARNING)
  6.  
  7.  
  8. # Configure joulescope
  9. js = joulescope.scan_require_one(config="auto")
  10. js.open()
  11. js.parameter_set("source", "on")
  12. js.parameter_set("i_range", "auto")
  13.  
  14.  
  15. def joulescope_get_data():
  16.     """Callback function to get current and voltage data from Joulescope.
  17.  
  18.    Packs it to a dictionary for easy storage as CSV.
  19.    """
  20.     try:
  21.         data = js.read(contiguous_duration=0.1)
  22.     # Atempt to re-open joulescope in case of error
  23.     except:  # pylint: disable=bare-except
  24.         js.open()
  25.         data = js.read(contiguous_duration=0.1)
  26.  
  27.     current, voltage = np.mean(data, axis=0)
  28.  
  29.     timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  30.     return {"timestamp": timestamp, "current": current, "voltage": voltage}
  31.  
  32.  
  33. # Hand of logging data to DictLogger in separate thread. Set interval to 0.9 as callback blocks for 0.1 second
  34. joulescope_logger = DictLogger(
  35.     joulescope_get_data, output_path=os.path.join(OUTPUT_FOLDER, JOULESCOPE_LOG_FILENAME), interval=0.9
  36. )
  37.  
  38. joulescope_logger.start_logging()
  39.  
  40.  
  41.  
  42. ## Do some testing for 30 hours
  43.  
  44. # Wrap up
  45. joulescope_logger.stop_logging()
  46. js.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement