mcx-datalogger
3 minute read
Motorcortex is capable of providing huge amounts of data. It can be very convenient using python tools to process this data and create plots for example.
Installing Motorcortex-Python-Tools
Motorcortex-python-tools can be downloaded and installed via pip and on the latest motorcortex RTOS images it is already pre-installed.
pip install motorcortex-python-tools
The files are stored on motorcortex-python-tools. Here you can also find examples of how to use the datalogger and the dataplotter.
mcx-datalogger
mcx-datalogger.py is a command-line tool for logging data from a MOTORCORTEX Server to a CSV file. It offers flexible options for specifying parameters, output files, connection details, and advanced logging features.
It is installed on the motorcortex RTOS images by default. To use it on your local machine, you need to install the motorcortex-python-tools package as described above.
Usage
mcx-datalogger.py [-h] -p PARAMETERFILE [-f FILE] [-F FOLDER]
[-c COMMENT [COMMENT ...]] [-u URL] [-s CERTIFICATE]
[-d DIVIDER] [--trigger TRIGGER]
[--triggerinterval TRIGGERINTERVAL]
[--triggervalue TRIGGERVALUE]
[--triggerop {==,<,>,<=,>=,!=}] [-C] [--noparamdump]
Description
Log data from a MOTORCORTEX Server to a CSV file.
Arguments
-h, --help
Show help message and exit.-p PARAMETERFILE, --parameterfile PARAMETERFILE
Required. JSON file with a list of parameters to log. Format:[ {"path": "root/signal1"}, {"path": "root/signal2"} ]-f FILE, --file FILE
Output filename. Defaults to a name based on current date and time.-F FOLDER, --folder FOLDER
Output folder for files.-c COMMENT [COMMENT ...], --comment COMMENT [COMMENT ...]
Comment(s) to append to filename.-u URL, --url URL
URL to connect to. Default:wss://192.168.2.100:5568:5567-s CERTIFICATE, --certificate CERTIFICATE
Certificate for secure connection. Default:mcx.cert.crt-d DIVIDER, --divider DIVIDER
Frequency divider for downsampling. Sends every N-th sample. Default:10--trigger TRIGGER
Path to signal monitored for triggering logging.--triggerinterval TRIGGERINTERVAL
Trigger interval in seconds. Default:0.500--triggervalue TRIGGERVALUE
Value to compare trigger signal against.--triggerop {==,<,>,<=,>=,!=}
Operator for trigger comparison.-C, --compress
Compress traces using LZMA (creates.xzfiles).--noparamdump
Do not dump parameters to file for each trace.
Example
mcx-datalogger.py -p params.json -f output.csv -u wss://192.168.2.100 -d 5 --trigger root/signal1 --triggervalue 100 --triggerop '>'
Using motorcortex-python-tools in your own Python code
To use the datalogger in your own python scripts you first need to import the module
# import the DataLogger module
from motorcortex_tools import DataLogger
# import numpy
import numpy as np
# import matplotlib (optional)
import matplotlib.pyplot as plt
# import time module (required to use sleep())
import time
Then create a DataLogger object. In the example below the DataLogger object is connected to the controller on ip address ‘192.168.2.100’ and subscribes to ‘path/to/param’ with frequency divider of 10.
# Create a DataLogger object and set the options
logger = DataLogger('192.168.2.100', ['path/to/param'], divider=10)
# Start the logger
logger.start()
While the logger is running you can interact with the system by setting some parameters or waiting until you see some response or some time has elapsed.
# do something, like setting a parameter to True
req.setParameter('path/to/another/param', True).get()
# wait a bit
time.sleep(10)
# Stop the logger
logger.stop()
# Close the connection to the server
logger.close()
Now the logger object contains the time traces of the recorded parameters. These traces can be accessed and converted into NumPy Arrays for instance. The time traces are stored in a Dictionary where each trace can be accessed by its path. The time of a trace is stored under the “t” key, the value of the parameter in the “y” key.
# get the time as a vector
t = np.array(logger.traces['path/to/param']["t"]).transpose()
# get the first column of the corresponding values of the parameter
y = np.array(logger.traces['path/to/param']["y"][0]).transpose()
The NumPy arrays can then be processed by NumPy or Matplotlib or PyLab tools. Here is an example of plotting the trace in Matplotlib.
plt.plot(t,y)
plt.show()