Skip to content

Commit

Permalink
Merge pull request #33 from adambinch/sentor_devel
Browse files Browse the repository at this point in the history
Custom Lambda Expressions.
  • Loading branch information
adambinch authored Nov 20, 2020
2 parents 81a6c86 + 6f53add commit 4caef7d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
7 changes: 5 additions & 2 deletions config/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
repeat_exec: True
tags: ["expression 1 is not published"]
signal_lambdas:
- expression: "lambda msg : msg.data == True"
# - expression: "lambda msg : msg.data == True"
- expression: CustomLambda
file: CustomLambdaExample
package: sentor
timeout: 0.1
safety_critical: True
safety_critical: False
default_notifications: True
when_published: False
process_indices: [0,1]
Expand Down
10 changes: 10 additions & 0 deletions src/sentor/CustomLambdaExample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
"""
Created on Fri Nov 20 11:35:22 2020
@author: Adam Binch ([email protected])
"""
#########################################################################################################
def CustomLambda(msg):
return msg.data == True
#########################################################################################################
13 changes: 9 additions & 4 deletions src/sentor/ROSTopicFilter.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#!/usr/bin/env python
"""
@author: Francesco Del Duchetto ([email protected])
@author: Adam Binch ([email protected])
"""
#####################################################################################
from __future__ import division
import rospy, math, numpy
# imported the package `math` so that it can be used in the lambda expressions
# imported the packages math and numpy so that they can be used in the lambda expressions

class ROSTopicFilter(object):

def __init__(self, topic_name, lambda_fn_str, config):
self.topic_name = topic_name
self.lambda_fn_str = lambda_fn_str
self.config = config

self.lambda_fn = None
try:
self.lambda_fn = eval(self.lambda_fn_str)
if config["file"] is not None and config["package"] is not None:
exec("from {}.{} import {} as lambda_fn".format(config["package"], config["file"], self.lambda_fn_str))
self.lambda_fn = lambda_fn
else:
self.lambda_fn = eval(self.lambda_fn_str)
except Exception as e:
rospy.logerr("Error evaluating lambda function %s : %s" % (self.lambda_fn_str, e))

Expand Down Expand Up @@ -73,4 +78,4 @@ def register_satisfied_cb(self, func):
def register_unsatisfied_cb(self, func):

self.unsat_callbacks.append(func)
#####################################################################################
#####################################################################################
6 changes: 6 additions & 0 deletions src/sentor/TopicMonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ def process_lambda_config(self, signal_lambda):

lambda_config = {}
lambda_config["expr"] = ""
lambda_config["file"] = None
lambda_config["package"] = None
lambda_config["timeout"] = self.timeout
lambda_config["safety_critical"] = False
lambda_config["default_notifications"] = self.default_notifications
Expand All @@ -220,6 +222,10 @@ def process_lambda_config(self, signal_lambda):

if "expression" in signal_lambda:
lambda_config["expr"] = signal_lambda["expression"]
if "file" in signal_lambda:
lambda_config["file"] = signal_lambda["file"]
if "package" in signal_lambda:
lambda_config["package"] = signal_lambda["package"]
if "timeout" in signal_lambda:
lambda_config["timeout"] = signal_lambda["timeout"]
if "safety_critical" in signal_lambda:
Expand Down

0 comments on commit 4caef7d

Please sign in to comment.