Skip to content

Commit

Permalink
loads dynamic parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
sirfoga committed Jul 13, 2019
1 parent b1beaab commit 859f98a
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 148 deletions.
104 changes: 104 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"messages": [
{
"id": "288",
"filename": "AMK1_FL",
"labels": [
"FL status",
"FL actual velocity (x100 rpm)",
"FL torque curr (A)",
"FL mag curr (A)",
"FL calc torque (Nm)"
],
"bytes parser": "AMKParser",
"func": "get_as_actual_values_1"
},
{
"id": "28a",
"filename": "AMK2_FL",
"labels": [
"FL T motor (°C)",
"FL T inverter (°C)",
"FL error",
"FL T IGBT (°C)"
],
"bytes parser": "AMKParser",
"func": "get_as_actual_values_2"
},
{
"id": "287",
"filename": "AMK1_FR",
"labels": [
"FR status",
"FR actual velocity (x100 rpm)",
"FR torque curr (A)",
"FR mag curr (A)",
"FR calc torque (Nm)"
],
"bytes parser": "AMKParser",
"func": "get_as_actual_values_1"
},
{
"id": "289",
"filename": "AMK2_FR",
"labels": [
"FR T motor (°C)",
"FR T inverter (°C)",
"FR error",
"FR T IGBT (°C)"
],
"bytes parser": "AMKParser",
"func": "get_as_actual_values_2"
},
{
"id": "284",
"filename": "AMK1_RL",
"labels": [
"RL status",
"RL actual velocity (x100 rpm)",
"RL torque curr (A)",
"RL mag curr (A)",
"RL calc torque (Nm)"
],
"bytes parser": "AMKParser",
"func": "get_as_actual_values_1"
},
{
"id": "286",
"filename": "AMK2_RL",
"labels": [
"RL T motor (°C)",
"RL T inverter (°C)",
"RL error",
"RL T IGBT (°C)"
],
"bytes parser": "AMKParser",
"func": "get_as_actual_values_2"
},
{
"id": "283",
"filename": "AMK1_RR",
"labels": [
"RR status",
"RR actual velocity (x100 rpm)",
"RR torque curr (A)",
"RR mag curr (A)",
"RR calc torque (Nm)"
],
"bytes parser": "AMKParser",
"func": "get_as_actual_values_1"
},
{
"id": "285",
"filename": "AMK2_RR",
"labels": [
"RR T motor (°C)",
"RR T inverter (°C)",
"RR error",
"RR T IGBT (°C)"
],
"bytes parser": "AMKParser",
"func": "get_as_actual_values_2"
}
]
}
8 changes: 4 additions & 4 deletions peeper/analysis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def save(self, output_file):
title = "Telemetry data from " + title.replace("-", ":")
fig.suptitle(title)

self.plots["Compass"].plot(ax=ax[0, 0], title="Compass")
self.plots["RotationVector"].plot(ax=ax[0, 1], title="Rotation vector")
self.plots["Compass"]._plot(ax=ax[0, 0], title="Compass")
self.plots["RotationVector"]._plot(ax=ax[0, 1], title="Rotation vector")
self.plots["AccelerometerLinear"] \
.plot(ax=ax[1, 0], title="Accelerations")
self.plots["Gyroscope"].plot(ax=ax[1, 1], title="Gyro")
._plot(ax=ax[1, 0], title="Accelerations")
self.plots["Gyroscope"]._plot(ax=ax[1, 1], title="Gyro")

plt.savefig(
output_file,
Expand Down
18 changes: 6 additions & 12 deletions peeper/config/amk.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@ class Motors(Enum):
AMK_SETPOINTS_CAN_IDS = ['184', '185', '188', '189']

AMK_VALUES_1 = {
Motors.FL: AMK_VALUES_1_CAN_IDS[Motors.FL.value],
Motors.FR: AMK_VALUES_1_CAN_IDS[Motors.FR.value],
Motors.RL: AMK_VALUES_1_CAN_IDS[Motors.RL.value],
Motors.RR: AMK_VALUES_1_CAN_IDS[Motors.RR.value]
motor: AMK_VALUES_1_CAN_IDS[motor.value]
for motor in Motors
}
AMK_VALUES_2 = {
Motors.FL: AMK_VALUES_2_CAN_IDS[Motors.FL.value],
Motors.FR: AMK_VALUES_2_CAN_IDS[Motors.FR.value],
Motors.RL: AMK_VALUES_2_CAN_IDS[Motors.RL.value],
Motors.RR: AMK_VALUES_2_CAN_IDS[Motors.RR.value]
motor: AMK_VALUES_2_CAN_IDS[motor.value]
for motor in Motors
}
AMK_SETPOINTS = {
Motors.FL: AMK_SETPOINTS_CAN_IDS[Motors.FL.value],
Motors.FR: AMK_SETPOINTS_CAN_IDS[Motors.FR.value],
Motors.RL: AMK_SETPOINTS_CAN_IDS[Motors.RL.value],
Motors.RR: AMK_SETPOINTS_CAN_IDS[Motors.RR.value]
motor: AMK_SETPOINTS_CAN_IDS[motor.value]
for motor in Motors
}
157 changes: 28 additions & 129 deletions peeper/parsers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
""" Parses CAN log file """

import argparse
import json
import os

from hal.files.models.files import Document
from hal.files.models.system import ls_recurse, is_file
from matplotlib import pyplot as plt

from config.amk import AMK_VALUES_1, Motors, AMK_VALUES_2
from parsers.can.amk import AMKParser
from parsers.logs.yolo import YOLOLogParser
from parsers.models.explorer import LogExplorer

DEFAULT_OUTPUT_FOLDER = os.path.join(os.getcwd(), 'out')
THIS_FOLDER = os.getcwd()
DEFAULT_OUTPUT_FOLDER = os.path.join(THIS_FOLDER, 'out')
DEFAULT_CONFIG_FILE = os.path.join(THIS_FOLDER, 'config.json')


def create_args():
Expand All @@ -29,6 +29,8 @@ def create_args():
help='file to parse', required=True)
parser.add_argument('-o', dest='out',
help='output folder', default=DEFAULT_OUTPUT_FOLDER, required=False)
parser.add_argument('-c', dest='config',
help='config messages', default=DEFAULT_CONFIG_FILE, required=False)
return parser


Expand All @@ -43,10 +45,12 @@ def parse_args(parser):
args = parser.parse_args()
file = str(args.file)
out = str(args.out)
config = str(args.config)

assert os.path.exists(file)
assert os.path.exists(config)

return file, out
return file, out, config


def get_plot(messages, labels, time_index, y_indexes):
Expand All @@ -68,143 +72,38 @@ def get_plot(messages, labels, time_index, y_indexes):
pass # todo print exc


def get_explorer(file_path):
message_classes = [
{
"id": AMK_VALUES_1[Motors.FL],
"filename": "AMK1_FL",
"labels": ["FL status", "FL actual velocity (x100 rpm)", "FL torque curr (A)", "FL mag curr (A)",
"FL calc torque (Nm)"],
"bytes parser": AMKParser,
"func": AMKParser.get_as_actual_values_1
},
{
"id": AMK_VALUES_2[Motors.FL],
"filename": "AMK2_FL",
"labels": ["FL T motor (°C)", "FL T inverter (°C)", "FL error", "FL T IGBT (°C)"],
"bytes parser": AMKParser,
"func": AMKParser.get_as_actual_values_2
},
{
"id": AMK_VALUES_1[Motors.FR],
"filename": "AMK1_FR",
"labels": ["FR status", "FR actual velocity (x100 rpm)", "FR torque curr (A)", "FR mag curr (A)",
"FR calc torque (Nm)"],
"bytes parser": AMKParser,
"func": AMKParser.get_as_actual_values_1
},
{
"id": AMK_VALUES_2[Motors.FR],
"filename": "AMK2_FR",
"labels": ["FR T motor (°C)", "FR T inverter (°C)", "FR error", "FR T IGBT (°C)"],
"bytes parser": AMKParser,
"func": AMKParser.get_as_actual_values_2
},
{
"id": AMK_VALUES_1[Motors.RL],
"filename": "AMK1_RL",
"labels": ["RL status", "RL actual velocity (x100 rpm)", "RL torque curr (A)", "RL mag curr (A)",
"RL calc torque (Nm)"],
"bytes parser": AMKParser,
"func": AMKParser.get_as_actual_values_1
},
{
"id": AMK_VALUES_2[Motors.RL],
"filename": "AMK2_RL",
"labels": ["RL T motor (°C)", "RL T inverter (°C)", "RL error", "RL T IGBT (°C)"],
"bytes parser": AMKParser,
"func": AMKParser.get_as_actual_values_2
},
{
"id": AMK_VALUES_1[Motors.RR],
"filename": "AMK1_RR",
"labels": ["RR status", "RR actual velocity (x100 rpm)", "RR torque curr (A)", "RR mag curr (A)",
"RR calc torque (Nm)"],
"bytes parser": AMKParser,
"func": AMKParser.get_as_actual_values_1
},
{
"id": AMK_VALUES_2[Motors.RR],
"filename": "AMK2_RR",
"labels": ["RR T motor (°C)", "RR T inverter (°C)", "RR error", "RR T IGBT (°C)"],
"bytes parser": AMKParser,
"func": AMKParser.get_as_actual_values_2
}
] # todo read .json

explorer = LogExplorer(file_path, YOLOLogParser, message_classes)
return explorer, message_classes


def plot(explorer, messages_list, labels_list):
for i in range(1, 8, 2):
plt.subplot(2, 2, int(i / 2) + 1) # select subplot

# labels = [labels_list[i][1]]
# y_indexes = [10, 11, 12, 13]

labels = [labels_list[i][0], labels_list[i][1], labels_list[i][3]]
y_indexes = [9, 10, 12]

explorer.plot(messages_list[i], labels, time_index=8, y_indexes=y_indexes)

# show plots
plt.show()


def save(explorer, messages_list, labels_list, files_list, output_folder):
if output_folder:
print('Saving to {}'.format(output_folder))
explorer.save_many_to_csv(
messages_list,
labels_list,
files_list,
output_folder
)


def print_log(explorer, messages_list, labels_list):
explorer.pretty_print(messages_list, labels_list)


def get_lists(explorer, message_classes):
messages_list = [
explorer.get_messages(
message_class['bytes parser'],
message_class['func'],
msg_id=message_class['id']
)
for message_class in message_classes
] # parse
labels_list = [
message_class['labels']
for message_class in message_classes
]
files_list = [
message_class['filename']
for message_class in message_classes
]
return messages_list, labels_list, files_list
def get_explorer(file_path, config_file):
with open(config_file, 'r') as json_data:
data = json.load(json_data)
message_classes = data["messages"]
for i, message_class in enumerate(message_classes):
byte_parser = globals()[message_class["bytes parser"]]
func = getattr(byte_parser, message_classes[i]["func"])

message_classes[i]["bytes parser"] = byte_parser
message_classes[i]["func"] = func

explorer = LogExplorer(file_path, YOLOLogParser, message_classes)
return explorer, message_classes


def main():
folder_path, out_folder = parse_args(create_args())
folder_path, out_folder, config = parse_args(create_args())
file_extension = ".csv"
files = sorted([
file
for file in ls_recurse(folder_path)
if is_file(file) and Document(file).extension == file_extension
])
]) # finds files

for file_path in files:
output_folder_name = os.path.basename(file_path).replace(file_extension, '')
output_folder = os.path.join(out_folder, output_folder_name)

explorer, message_classes = get_explorer(file_path)
messages_list, labels_list, files_list = get_lists(explorer, message_classes)
plot(explorer, messages_list, labels_list)
save(explorer, messages_list, labels_list, files_list, output_folder)
# print_log(explorer, messages_list, labels_list)
explorer, message_classes = get_explorer(file_path, config)
messages_list, labels_list, files_list = explorer.get_lists(message_classes)
explorer.plot(messages_list, labels_list)
explorer.save(messages_list, labels_list, files_list, output_folder)


if __name__ == '__main__':
Expand Down

0 comments on commit 859f98a

Please sign in to comment.