Skip to content

Commit 92b4c64

Browse files
committed
topic_publisher and translator executed in main()
1 parent 0b1c3cf commit 92b4c64

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

mocha_core/mocha_core/topic_publisher.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def create_topic_publisher_node(robot_name, robot_configs_path=None, topic_confi
253253
robot_configs_path = robot_configs_path if robot_configs_path else None
254254
topic_configs_path = topic_configs_path if topic_configs_path else None
255255

256+
pub = None
256257
try:
257258
# Create topic publisher node and publisher instance
258259
_, pub = create_topic_publisher_node(this_robot, robot_configs_path, topic_configs_path, node)
@@ -263,6 +264,51 @@ def create_topic_publisher_node(robot_name, robot_configs_path=None, topic_confi
263264
pass
264265
finally:
265266
# Cleanup
266-
pub.shutdown()
267+
if pub is not None:
268+
pub.shutdown()
267269
node.destroy_node()
268270
rclpy.shutdown()
271+
272+
def main():
273+
rclpy.init(args=None)
274+
node = rclpy.create_node("topic_publisher")
275+
logger = node.get_logger()
276+
277+
# Declare parameters
278+
node.declare_parameter("robot_name", "")
279+
node.declare_parameter("robot_configs", "")
280+
node.declare_parameter("topic_configs", "")
281+
282+
# Get robot from the parameters
283+
this_robot = node.get_parameter("robot_name").get_parameter_value().string_value
284+
if not this_robot:
285+
logger.error("robot_name parameter is required")
286+
rclpy.shutdown()
287+
sys.exit(1)
288+
289+
# Get config file paths from parameters
290+
robot_configs_path = node.get_parameter("robot_configs").get_parameter_value().string_value
291+
topic_configs_path = node.get_parameter("topic_configs").get_parameter_value().string_value
292+
293+
# Use empty strings as None for the function
294+
robot_configs_path = robot_configs_path if robot_configs_path else None
295+
topic_configs_path = topic_configs_path if topic_configs_path else None
296+
297+
pub = None
298+
try:
299+
# Create topic publisher node and publisher instance
300+
_, pub = create_topic_publisher_node(this_robot, robot_configs_path, topic_configs_path, node)
301+
302+
# Run the publisher
303+
pub.run()
304+
except KeyboardInterrupt:
305+
pass
306+
finally:
307+
# Cleanup
308+
if pub is not None:
309+
pub.shutdown()
310+
node.destroy_node()
311+
rclpy.shutdown()
312+
313+
if __name__ == "__main__":
314+
main()

mocha_core/mocha_core/translator.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,43 @@ def create_translator_node(robot_name, robot_configs_path=None, topic_configs_pa
197197
finally:
198198
node.destroy_node()
199199
rclpy.shutdown()
200+
201+
def main():
202+
rclpy.init(args=None)
203+
node = rclpy.create_node('translator')
204+
205+
# Declare parameters
206+
node.declare_parameter('robot_name', '')
207+
node.declare_parameter('robot_configs', '')
208+
node.declare_parameter('topic_configs', '')
209+
210+
logger = node.get_logger()
211+
212+
# Get robot name from parameters
213+
this_robot = node.get_parameter('robot_name').get_parameter_value().string_value
214+
if not this_robot:
215+
logger.error('robot_name parameter not set')
216+
sys.exit(1)
217+
218+
# Get config file paths from parameters
219+
robot_configs_path = node.get_parameter("robot_configs").get_parameter_value().string_value
220+
topic_configs_path = node.get_parameter("topic_configs").get_parameter_value().string_value
221+
222+
# Use empty strings as None for the function
223+
robot_configs_path = robot_configs_path if robot_configs_path else None
224+
topic_configs_path = topic_configs_path if topic_configs_path else None
225+
226+
try:
227+
# Create translator node and translators
228+
create_translator_node(this_robot, robot_configs_path, topic_configs_path, node)
229+
230+
# Spin the node
231+
rclpy.spin(node)
232+
except KeyboardInterrupt:
233+
pass
234+
finally:
235+
node.destroy_node()
236+
rclpy.shutdown()
237+
238+
if __name__ == "__main__":
239+
main()

0 commit comments

Comments
 (0)