Skip to content

Commit

Permalink
Merge pull request #214 from boschresearch/feature/dbcfeedertransforms
Browse files Browse the repository at this point in the history
Transfromations and better error logging for DBC feeder
  • Loading branch information
SebastianSchildt authored Jul 13, 2021
2 parents d81ff3d + 4a59d92 commit e8eb214
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 12 deletions.
24 changes: 23 additions & 1 deletion kuksa_feeders/dbc2val/dbc2vssmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@
########################################################################

import yaml
import transforms.mapping
import transforms.math


class mapper:

def __init__(self,input):
with open(input,'r') as file:
self.mapping = yaml.full_load(file)

self.transforms={}
self.transforms['fullmapping']=transforms.mapping.mapping(discard_non_matching_items=True)
self.transforms['partialmapping']=transforms.mapping.mapping(discard_non_matching_items=False)
self.transforms['math']=transforms.math.math()



for key in self.mapping.keys():
self.mapping[key]['lastupdate']=0.0
if 'minupdatedelay' not in self.mapping[key]:
Expand All @@ -33,7 +44,18 @@ def minUpdateTimeElapsed(self,key, time):
self.mapping[key]['lastupdate']=time
return True
return False
#

# Check whether there are transforms defined to map DBC signal "signal" to
# VSS path "target". Returns the (potentially) transformed values
def transform(self,signal, target, value):
if "transform" not in self.mapping[signal]["targets"][target].keys(): #no transform defined, return as is
return value
for transform in self.mapping[signal]["targets"][target]["transform"]:
if transform in self.transforms.keys(): #found a known transform and apply
value=self.transforms[transform].transform(self.mapping[signal]["targets"][target]["transform"][transform],value)
else:
print(f"Warning: Unknown transform {transform} for {signal}->{target}")
return value

def __contains__(self,key):
return key in self.mapping.keys()
Expand Down
13 changes: 11 additions & 2 deletions kuksa_feeders/dbc2val/dbcfeeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os, sys, signal
import configparser
import queue
import json

import dbc2vssmapper
import dbcreader
Expand Down Expand Up @@ -85,9 +86,17 @@ def terminationSignalreceived(signalNumber, frame):
while running:
try:
signal, value=canQueue.get(timeout=1)
print("Update signal {} to {}".format(signal, value))
for target in mapping[signal]['targets']:
kuksa.setValue(target, value)
tv=mapping.transform(signal,target,value)
if tv is not None: #none indicates the transform decided to not set the value
print("Update VSS path {} to {} based on signal {}".format(target, tv, signal))
resp=json.loads(kuksa.setValue(target, tv))
if "error" in resp:
if "message" in resp["error"]:
print("Error setting {}: {}".format(target, resp["error"]["message"]))
else:
print("Unknown error setting {}: {}".format(target, resp))
pass
except queue.Empty:
pass

36 changes: 27 additions & 9 deletions kuksa_feeders/dbc2val/mapping.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
# Map OBD values to tree

# Mapping Speed
UIspeed_signed257:
minupdatedelay: 100
targets:
- Vehicle.OBD.Speed
Vehicle.Speed: {}
Vehicle.OBD.Speed: {}


#RearPower266:
# minupdatedelay: 100
# targets:
# - Vehicle.OBD.EngineLoad
#this is kW, do some magic to get in 0-100 int range
RearPower266:
minupdatedelay: 100
targets:
Vehicle.OBD.EngineLoad:
transform:
math: "floor(abs(x/5))"

#Change this to type string in VSS file to work with M3 DBC
#Mapping to numeriv VSS gears, and extract parking state
DIgear118:
minupdatedelay: 100
targets:
- Vehicle.Drivetrain.Transmission.Gear
Vehicle.Powertrain.Transmission.Gear:
transform:
fullmapping:
Neutral: 0
Idle: 0
Park: 0
Reverse: -1
Drive: 1
Vehicle.Chassis.ParkingBrake.IsEngaged:
transform:
fullmapping:
Neutral: "false"
Idle: "false"
Park: "true"
Reverse: "false"
Drive: "false"
1 change: 1 addition & 0 deletions kuksa_feeders/dbc2val/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ cantools
python-can
pyyaml
j1939
py_expression_eval
kuksa-viss-client
Empty file.
32 changes: 32 additions & 0 deletions kuksa_feeders/dbc2val/transforms/mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/python3

########################################################################
# Copyright (c) 2021 Robert Bosch GmbH
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
########################################################################


class mapping:

# setting discard_non_matching_items to true will return "None" if a value
# is not found in the mapping table. This can be used to only add
# a subset of possible values to VSS
# setting discard_non_matching_items to false will return values for
# which no match exists unmodified
def __init__(self, discard_non_matching_items):
self.discard_non_matching_items=discard_non_matching_items

def transform(self, spec, value):
for k,v in spec.items():
if value==k:
return v
#no match
if self.discard_non_matching_items:
return None
else:
return value
22 changes: 22 additions & 0 deletions kuksa_feeders/dbc2val/transforms/math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/python3

########################################################################
# Copyright (c) 2021 Robert Bosch GmbH
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
########################################################################

from py_expression_eval import Parser

class math:

def __init__(self):
self.parser=Parser()

def transform(self, spec, value):
return self.parser.parse(spec).evaluate({'x': value})

0 comments on commit e8eb214

Please sign in to comment.