2019-05-24 11:13:28 +00:00
|
|
|
#! /usr/bin/python
|
|
|
|
|
|
|
|
from scipy.signal import savgol_coeffs
|
|
|
|
import numpy as np
|
|
|
|
import sys
|
2019-06-02 16:02:15 +00:00
|
|
|
import threading
|
2019-06-06 19:05:10 +00:00
|
|
|
import functools
|
2019-05-24 11:13:28 +00:00
|
|
|
|
|
|
|
import rospy
|
|
|
|
|
2019-06-02 16:02:15 +00:00
|
|
|
from std_msgs.msg import Float64, Empty
|
2019-05-24 11:13:28 +00:00
|
|
|
|
|
|
|
import dynamic_reconfigure.server
|
|
|
|
from detect_targets.cfg import ProportionalNodeConfig, IntegralNodeConfig
|
2019-06-05 06:54:37 +00:00
|
|
|
from detect_targets.cfg import DerivativeNodeConfig, InputNodeConfig
|
|
|
|
from detect_targets.cfg import SaturateNodeConfig, RateNodeConfig
|
2019-05-24 11:13:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ControlNode(object):
|
2019-06-05 06:54:37 +00:00
|
|
|
def __init__(self, refresh_time=0.05, config_class=None):
|
2019-05-24 11:13:28 +00:00
|
|
|
self.last_time = rospy.get_rostime()
|
|
|
|
self.refresh_time = rospy.Duration.from_sec(refresh_time)
|
|
|
|
self.output = 0.0
|
|
|
|
|
|
|
|
self.output_topic = rospy.Publisher('output', Float64, queue_size=1)
|
|
|
|
|
|
|
|
self.input_topics = {
|
2019-06-02 16:02:15 +00:00
|
|
|
"reset": rospy.Subscriber("reset", Empty, self.on_reset),
|
2019-05-24 11:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if config_class is not None:
|
|
|
|
self.server = dynamic_reconfigure.server.Server(config_class, self.on_reconf)
|
|
|
|
|
2019-06-06 19:05:10 +00:00
|
|
|
def on_reset(self, value=None):
|
|
|
|
self.output = 0.0
|
2019-05-24 11:13:28 +00:00
|
|
|
|
|
|
|
def on_reconf(self, config, level):
|
|
|
|
self.refresh_time = rospy.Duration.from_sec(config["refresh_time"])
|
2019-06-06 19:05:10 +00:00
|
|
|
self.on_reset()
|
2019-05-24 11:13:28 +00:00
|
|
|
return config
|
|
|
|
|
|
|
|
def on_compute(self, value):
|
|
|
|
self.output_topic.publish(data=self.output)
|
|
|
|
self.last_time = rospy.get_rostime()
|
|
|
|
|
|
|
|
def check_time(self, delta_time=0.0):
|
|
|
|
t = rospy.get_rostime()
|
|
|
|
if delta_time == 0.0:
|
|
|
|
delta_time = t - self.last_time
|
|
|
|
|
|
|
|
return delta_time >= self.refresh_time, delta_time.to_sec()
|
|
|
|
|
2019-06-02 16:02:15 +00:00
|
|
|
def on_mainloop(self):
|
|
|
|
rospy.spin()
|
|
|
|
|
2019-05-24 11:13:28 +00:00
|
|
|
class InputControlNode(ControlNode):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(InputControlNode, self).__init__(*args, **kwargs)
|
|
|
|
self.input_topics["input"] = rospy.Subscriber("input", Float64, self.on_compute)
|
|
|
|
|
2019-06-02 16:02:15 +00:00
|
|
|
class RateNode(InputControlNode):
|
2019-06-05 06:54:37 +00:00
|
|
|
def __init__(self, refresh_time=0.05):
|
2019-06-02 16:02:15 +00:00
|
|
|
self.lock = threading.Lock()
|
2019-06-05 06:54:37 +00:00
|
|
|
super(RateNode, self).__init__(refresh_time, RateNodeConfig)
|
|
|
|
self.rate = rospy.Rate(1.0/self.refresh_time.to_sec())
|
2019-06-02 16:02:15 +00:00
|
|
|
|
|
|
|
def on_reconf(self, config, level):
|
2019-06-05 06:54:37 +00:00
|
|
|
c = super(RateNode, self).on_reconf(config, level)
|
|
|
|
self.rate = rospy.Rate(self.refresh_time.to_sec())
|
2019-06-02 16:02:15 +00:00
|
|
|
return c
|
|
|
|
|
|
|
|
def on_compute(self, value):
|
|
|
|
self.lock.acquire()
|
2019-06-05 06:54:37 +00:00
|
|
|
self.output = value.data
|
2019-06-02 16:02:15 +00:00
|
|
|
self.lock.release()
|
|
|
|
|
|
|
|
def on_mainloop(self):
|
|
|
|
while not rospy.is_shutdown():
|
|
|
|
self.lock.acquire()
|
2019-06-05 06:54:37 +00:00
|
|
|
self.output_topic.publish(self.output)
|
2019-06-02 16:02:15 +00:00
|
|
|
self.lock.release()
|
|
|
|
self.rate.sleep()
|
|
|
|
|
2019-05-24 11:13:28 +00:00
|
|
|
class ProportionalNode(InputControlNode):
|
2019-06-05 06:54:37 +00:00
|
|
|
def __init__(self, k=1.0, refresh_time=0.05):
|
2019-05-24 11:13:28 +00:00
|
|
|
super(ProportionalNode, self).__init__(refresh_time, ProportionalNodeConfig)
|
|
|
|
self.k = k
|
|
|
|
|
|
|
|
def on_reconf(self, config, level):
|
|
|
|
self.k = config["k"]
|
|
|
|
return super(ProportionalNode, self).on_reconf(config, level)
|
|
|
|
|
|
|
|
def on_compute(self, value):
|
|
|
|
time_ok,_ = self.check_time()
|
|
|
|
if time_ok:
|
|
|
|
self.output = self.k * value.data
|
|
|
|
super(ProportionalNode, self).on_compute(value)
|
|
|
|
|
|
|
|
class SaturateNode(InputControlNode):
|
2019-06-05 06:54:37 +00:00
|
|
|
def __init__(self, min=None, max=None, refresh_time=0.05, config=SaturateNodeConfig):
|
2019-05-24 11:13:28 +00:00
|
|
|
super(SaturateNode, self).__init__(refresh_time, config)
|
|
|
|
self.min = min
|
|
|
|
self.max = max
|
|
|
|
|
|
|
|
def on_reconf(self, config, level):
|
|
|
|
self.min = config["min"] if config["use_min"] else None
|
|
|
|
self.max = config["max"] if config["use_max"] else None
|
|
|
|
return super(SaturateNode, self).on_reconf(config, level)
|
|
|
|
|
|
|
|
def on_compute(self, value):
|
|
|
|
time_ok,_ = self.check_time()
|
|
|
|
if time_ok:
|
|
|
|
self.output = min(self.max or value.data, value.data)
|
|
|
|
self.output = max(self.min or self.output, self.output)
|
|
|
|
super(SaturateNode, self).on_compute(value)
|
|
|
|
|
|
|
|
class IntegralNode(SaturateNode):
|
2019-06-06 19:05:10 +00:00
|
|
|
def __init__(self, k=0.0, min=None, max=None, refresh_time=0.05):
|
2019-05-24 11:13:28 +00:00
|
|
|
super(IntegralNode, self).__init__(min, max, refresh_time, IntegralNodeConfig)
|
|
|
|
self.k = k
|
|
|
|
|
|
|
|
def on_reconf(self, config, level):
|
|
|
|
self.k = config["k"]
|
|
|
|
return super(IntegralNode, self).on_reconf(config, level)
|
|
|
|
|
|
|
|
def on_compute(self, value):
|
|
|
|
time_ok, delta = self.check_time()
|
|
|
|
if time_ok:
|
2019-06-06 19:05:10 +00:00
|
|
|
old_output = self.output
|
|
|
|
self.output += self.k * value.data * delta
|
|
|
|
return super(IntegralNode, self).on_compute(Float64(self.output))
|
2019-05-24 11:13:28 +00:00
|
|
|
|
|
|
|
class DerivativeNode(InputControlNode):
|
2019-06-05 06:54:37 +00:00
|
|
|
def __init__(self, k=0.05, size=9, polyorder=3, deriv=1, refresh_time=0.05):
|
2019-05-24 11:13:28 +00:00
|
|
|
super(DerivativeNode, self).__init__(refresh_time, DerivativeNodeConfig)
|
|
|
|
self.k = k
|
|
|
|
self.filter = savgol_coeffs(size, polyorder, deriv, delta=refresh_time, use='dot')
|
2019-06-02 16:02:15 +00:00
|
|
|
self.last_points = np.zeros(size)
|
2019-05-24 11:13:28 +00:00
|
|
|
|
|
|
|
def on_reconf(self, config, level):
|
|
|
|
self.k = config["k"]
|
|
|
|
try:
|
|
|
|
self.filter = savgol_coeffs(
|
|
|
|
config["size"],
|
|
|
|
config["poly_order"],
|
|
|
|
config["deriv"],
|
|
|
|
delta=config["refresh_time"],
|
|
|
|
use='dot'
|
|
|
|
)
|
|
|
|
except ValueError as e:
|
|
|
|
rospy.logerr(e)
|
|
|
|
self.last_points = np.zeros(config["size"])
|
|
|
|
return super(DerivativeNode, self).on_reconf(config, level)
|
|
|
|
|
|
|
|
def on_compute(self, value):
|
|
|
|
time_ok, delta = self.check_time()
|
|
|
|
if time_ok:
|
2019-06-02 16:02:15 +00:00
|
|
|
self.last_points = np.concatenate([self.last_points[1:], [value.data]])
|
2019-06-06 19:05:10 +00:00
|
|
|
self.output = self.k * self.last_points.dot(self.filter)
|
2019-05-24 11:13:28 +00:00
|
|
|
return super(DerivativeNode, self).on_compute(value)
|
|
|
|
|
2019-06-06 19:05:10 +00:00
|
|
|
def on_reset(self, value=None):
|
|
|
|
self.last_points = np.zeros(len(self.filter))
|
|
|
|
return super(DerivativeNode, self).on_reset(value)
|
2019-05-24 11:13:28 +00:00
|
|
|
|
|
|
|
class DifferenciateNode(InputControlNode):
|
|
|
|
def __init__(self):
|
|
|
|
self.measure = 0.0
|
|
|
|
self.input = 0.0
|
|
|
|
super(DifferenciateNode, self).__init__()
|
|
|
|
self.input_topics["measure"] = rospy.Subscriber("measure", Float64, self.on_compute_measure)
|
|
|
|
|
|
|
|
def on_compute(self, value):
|
|
|
|
self.input = value.data
|
|
|
|
self.output = self.input - self.measure
|
|
|
|
super(DifferenciateNode, self).on_compute(value)
|
|
|
|
|
|
|
|
def on_compute_measure(self, value):
|
|
|
|
self.measure = value.data
|
|
|
|
self.output = self.input - self.measure
|
|
|
|
super(DifferenciateNode, self).on_compute(value)
|
|
|
|
|
|
|
|
class InputNode(InputControlNode):
|
|
|
|
def __init__(self):
|
2019-06-05 06:54:37 +00:00
|
|
|
super(InputNode, self).__init__(refresh_time=0.05, config_class=InputNodeConfig)
|
2019-05-24 11:13:28 +00:00
|
|
|
|
|
|
|
def on_reconf(self, config, level):
|
|
|
|
self.output = config["value"]
|
2019-06-06 19:05:10 +00:00
|
|
|
super(InputNode, self).on_compute(Float64(config["value"]))
|
2019-05-24 11:13:28 +00:00
|
|
|
return config
|
|
|
|
|
|
|
|
def on_compute(self, value):
|
|
|
|
self.output = value.data
|
|
|
|
super(InputNode, self).on_compute(value)
|
|
|
|
|
|
|
|
class SumNode(ControlNode):
|
|
|
|
def __init__(self, nb_topics):
|
2019-06-05 06:54:37 +00:00
|
|
|
super(SumNode, self).__init__(refresh_time=0.05)
|
2019-05-24 11:13:28 +00:00
|
|
|
self.nb_topics = int(nb_topics)
|
|
|
|
self.inputs = dict()
|
|
|
|
for i in range(self.nb_topics):
|
|
|
|
topic_name = 'input_'+str(i)
|
2019-06-06 19:05:10 +00:00
|
|
|
self.input_topics[topic_name] = rospy.Subscriber(topic_name, Float64, functools.partial(self.update_value, topic_name))
|
|
|
|
|
|
|
|
def update_value(self, key, value):
|
|
|
|
self.inputs.update({key:value.data})
|
|
|
|
self.on_compute(value)
|
2019-05-24 11:13:28 +00:00
|
|
|
|
|
|
|
def on_compute(self, value):
|
|
|
|
self.output = sum(self.inputs.values())
|
|
|
|
super(SumNode, self).on_compute(value)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
nodes = {
|
|
|
|
'sum' : (SumNode, True),
|
|
|
|
'differenciate' : (DifferenciateNode, False),
|
|
|
|
'input' : (InputNode, False),
|
|
|
|
'saturate' : (SaturateNode, False),
|
|
|
|
'derivative' : (DerivativeNode, False),
|
|
|
|
'integral' : (IntegralNode, False),
|
|
|
|
'proportional' : (ProportionalNode, False),
|
2019-06-02 16:02:15 +00:00
|
|
|
'rate': (RateNode, False),
|
2019-05-24 11:13:28 +00:00
|
|
|
}
|
|
|
|
rospy.init_node('control_node', anonymous=True)
|
2019-06-06 19:05:10 +00:00
|
|
|
rospy.loginfo("Running node " + sys.argv[1])
|
2019-05-24 11:13:28 +00:00
|
|
|
try:
|
|
|
|
node_class, need_param = nodes[sys.argv[1]]
|
|
|
|
except KeyError as e:
|
|
|
|
raise ValueError(sys.argv[1] + " is not a valid node name.")
|
|
|
|
|
|
|
|
if need_param:
|
|
|
|
param = sys.argv[2]
|
|
|
|
node = node_class(param)
|
|
|
|
else:
|
|
|
|
node = node_class()
|
|
|
|
|
2019-06-05 06:54:37 +00:00
|
|
|
node.on_mainloop()
|