Skip to content

Commit

Permalink
Fixed errors in configuring publisher with mixed types
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-palmer committed Apr 1, 2024
1 parent 7c67558 commit 0db4c22
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion mobile_to_maritime/launch/demo.launch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ launch:

- node:
pkg: mobile_to_maritime
exec: mobile_twist_stamped_to_maritime_twist_stamped
exec: mobile_twist_stamped_to_maritime_twist
name: convert_twist_stamped_demo
param:
- name: in_topic
Expand Down
33 changes: 17 additions & 16 deletions mobile_to_maritime/mobile_to_maritime/mobile_to_maritime.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@


class MobileToMaritime(Node, ABC):
def __init__(self, message_type: Any, node_name: str) -> None:
def __init__(
self, in_message_type: Any, node_name: str, out_message_type: Any | None = None
) -> None:
super().__init__(node_name)

self.declare_parameters("", [("in_topic", ""), ("out_topic", "")])
Expand All @@ -49,23 +51,21 @@ def __init__(self, message_type: Any, node_name: str) -> None:
)
self.declare_parameters("", [("qos_depth", 10)])

self.in_topic = (
self.get_parameter("in_topic").get_parameter_value().string_value
)
self.out_topic = (
self.get_parameter("out_topic").get_parameter_value().string_value
)
in_topic = self.get_parameter("in_topic").get_parameter_value().string_value
out_topic = self.get_parameter("out_topic").get_parameter_value().string_value

# Get the QoS profile settings
self.qos_profile = self._get_qos_profile_from_params()
qos_profile = self._get_qos_profile_from_params()

self.in_sub = self.create_subscription(
message_type, self.in_topic, self.in_callback, self.qos_profile
)
self.out_pub = self.create_publisher(
message_type, self.out_topic, self.qos_profile
in_message_type, in_topic, self.in_callback, qos_profile
)

if out_message_type is None:
out_message_type = in_message_type

self.out_pub = self.create_publisher(out_message_type, out_topic, qos_profile)

def _get_qos_profile_from_params(self) -> QoSProfile:
"""Construct a QoS profile from the parameters."""
history = QoSHistoryPolicy.get_from_short_key(
Expand Down Expand Up @@ -142,10 +142,11 @@ def in_callback(self, msg: TwistStamped) -> None:

class MobileTwistStampedToMaritimeTwist(MobileToMaritime):
def __init__(self) -> None:
super().__init__(TwistStamped, "mobile_twist_stamped_to_maritime_twist")

# Override the parent publisher to use a twist message (without the header)
self.out_pub = self.create_publisher(Twist, self.out_topic, self.qos_profile)
super().__init__(
TwistStamped,
"mobile_twist_stamped_to_maritime_twist",
out_message_type=Twist,
)

def in_callback(self, msg: TwistStamped) -> None:
maritime_twist = Twist()
Expand Down

0 comments on commit 0db4c22

Please sign in to comment.