-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented mock data node #453
base: main
Are you sure you want to change the base?
Changes from 6 commits
5724251
4fe4d90
acb6535
ff972fb
548fbc4
7ef5580
c89766e
73d5f81
4932335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import random | ||
import sys | ||
|
||
import rclpy | ||
from custom_interfaces.msg import DesiredHeading, HelperHeading, SailCmd | ||
from rclpy.node import Node | ||
|
||
import boat_simulator.common.constants as Constants | ||
|
||
# based on following: | ||
# https://docs.ros.org | ||
# /en/humble/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Py-Publisher-And-Subscriber.html | ||
# the purpose of this node is to publish to all topics that there is | ||
# subscribers in physics engine node so send goal code can work. | ||
|
||
|
||
class MockDataNode(Node): | ||
|
||
def __init__(self): | ||
super().__init__("mock_data") | ||
self.__declare_ros_parameters() | ||
|
||
self.desired_heading_pub = self.create_publisher( | ||
msg_type=DesiredHeading, | ||
topic=Constants.PHYSICS_ENGINE_SUBSCRIPTIONS.DESIRED_HEADING, | ||
qos_profile=self.qos_depth, | ||
) | ||
self.sail_trim_tab_angle_pub = self.create_publisher( | ||
msg_type=SailCmd, | ||
topic=Constants.PHYSICS_ENGINE_SUBSCRIPTIONS.SAIL_TRIM_TAB_ANGLE, | ||
qos_profile=self.qos_depth, | ||
) | ||
|
||
timer_period = self.pub_period # seconds | ||
self.timer = self.create_timer(timer_period, self.timer_callback) | ||
|
||
def __declare_ros_parameters(self): | ||
"""Declares ROS parameters from the global configuration file that will be used in this | ||
node. This node will monitor for any changes to these parameters during execution and will | ||
update itself accordingly. | ||
""" | ||
self.get_logger().debug("Declaring ROS parameters...") | ||
self.declare_parameters( | ||
namespace="", | ||
parameters=[ | ||
("pub_period_sec", rclpy.Parameter.Type.DOUBLE), | ||
("mock_sail_trim_tab", rclpy.Parameter.Type.BOOL), | ||
("mock_desired_heading", rclpy.Parameter.Type.BOOL), | ||
("qos_depth", rclpy.Parameter.Type.INTEGER), | ||
], | ||
) | ||
|
||
all_parameters = self._parameters | ||
for name, parameter in all_parameters.items(): | ||
value_str = str(parameter.value) | ||
self.get_logger().debug(f"Got parameter {name} with value {value_str}") | ||
|
||
def timer_callback(self): | ||
if self.mock_desired_heading: | ||
self.publish_mock_desired_heading() | ||
if self.mock_sail_trim_tab: | ||
self.publish_mock_sail_trim_tab_angle() | ||
|
||
def publish_mock_desired_heading(self): | ||
"""Publishes mock wind sensor data.""" | ||
heading = random.uniform(-179.99, 180.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please make the interval configurable through |
||
|
||
helper_heading = HelperHeading() | ||
helper_heading.heading = heading | ||
|
||
msg = DesiredHeading() | ||
msg.heading = helper_heading | ||
|
||
self.desired_heading_pub.publish(msg) | ||
self.get_logger().info( | ||
f"Publishing to {self.desired_heading_pub.topic} " | ||
+ f"a mock desired heading of {heading} degrees" | ||
) | ||
Comment on lines
+75
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for |
||
|
||
def publish_mock_sail_trim_tab_angle(self): | ||
"""Publishes mock wind sensor data.""" | ||
trim_tab_angle_degrees = random.uniform(-40, 40) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for the interval here |
||
|
||
msg = SailCmd() | ||
msg.trim_tab_angle_degrees = trim_tab_angle_degrees | ||
|
||
self.sail_trim_tab_angle_pub.publish(msg) | ||
self.get_logger().info( | ||
f"Publishing to {self.sail_trim_tab_angle_pub.topic} " | ||
+ f"a mock trim tab angle of {trim_tab_angle_degrees} degrees" | ||
) | ||
Comment on lines
+88
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
|
||
@property | ||
def pub_period(self) -> float: | ||
return self.get_parameter("pub_period_sec").get_parameter_value().double_value | ||
|
||
@property | ||
def mock_desired_heading(self) -> bool: | ||
return self.get_parameter("mock_desired_heading").get_parameter_value().bool_value | ||
|
||
@property | ||
def mock_sail_trim_tab(self) -> bool: | ||
return self.get_parameter("mock_sail_trim_tab").get_parameter_value().bool_value | ||
|
||
@property | ||
def qos_depth(self) -> int: | ||
return self.get_parameter("qos_depth").get_parameter_value().integer_value | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args=args) | ||
|
||
if is_mock_data_enabled(): | ||
try: | ||
node = MockDataNode() | ||
rclpy.spin(node) | ||
finally: | ||
rclpy.shutdown() | ||
|
||
|
||
def is_mock_data_enabled() -> bool: | ||
try: | ||
is_mock_data_enabled_index = sys.argv.index(Constants.MOCK_DATA_CLI_ARG_NAME) + 1 | ||
is_mock_data_enabled = sys.argv[is_mock_data_enabled_index] == "true" | ||
except ValueError: | ||
is_mock_data_enabled = False | ||
return is_mock_data_enabled | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,3 +76,10 @@ data_collection_node: | |
bag: false | ||
json: true | ||
write_period_sec: 0.5 | ||
|
||
mock_data_node: | ||
Comment on lines
78
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove space here to group with the rest of the boat simulator parameters |
||
ros__parameters: | ||
qos_depth: 10 | ||
mock_desired_heading: True | ||
mock_sail_trim_tab: True | ||
pub_period_sec: 5.0 | ||
alberto-escobar marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this to a docstring for the
MockDataNode
class