From 68629ad9c287c4dce8fe84c98938369279d6900c Mon Sep 17 00:00:00 2001 From: Brian Delhaisse Date: Thu, 17 Oct 2019 09:33:28 +0200 Subject: [PATCH] add ROS middleware examples --- .../middlewares/bullet_ros_control_gazebo.py | 1 + examples/middlewares/bullet_ros_publisher.py | 99 +++++++++++++++++++ examples/middlewares/bullet_ros_rqt.py | 28 ++++++ examples/middlewares/bullet_ros_subscriber.py | 70 +++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 examples/middlewares/bullet_ros_publisher.py create mode 100644 examples/middlewares/bullet_ros_rqt.py create mode 100644 examples/middlewares/bullet_ros_subscriber.py diff --git a/examples/middlewares/bullet_ros_control_gazebo.py b/examples/middlewares/bullet_ros_control_gazebo.py index 4785202..aa7a1c4 100644 --- a/examples/middlewares/bullet_ros_control_gazebo.py +++ b/examples/middlewares/bullet_ros_control_gazebo.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- #!/usr/bin/env python """Test the publisher using Gazebo with ROS control and Pybullet diff --git a/examples/middlewares/bullet_ros_publisher.py b/examples/middlewares/bullet_ros_publisher.py new file mode 100644 index 0000000..a7984c0 --- /dev/null +++ b/examples/middlewares/bullet_ros_publisher.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +"""Example on how to use the Bullet-ROS simulator (the publisher version) in PRL. + +The publisher version publish the various data on the corresponding topics every time the joints are set. If the +:attr:`teleoperate` is set to True, it will also publish every time we call a `get_*()` method. + +Before running this file, run in the terminal: +```bash +$ roscore # note that roscore is launched automatically by this file if has not already been launched +$ rostopic list # to show the list of published topics +``` + +The last command should print the following: +```bash +/rosout +/rosout_agg +``` + +Now, run this file and check the published topics again: +```bash +$ rostopic list +``` + +This time, you should get as well: +```bash +/rosout +/rosout_agg +/rrbot/joint_states +``` + +You can print the output of the topic with: +```bash +$ rostopic echo /rrbot/joint_states +``` + +Try to move the robot with the mouse, and see that the published joint position values changed as well. + +You can run this code in parallel with `bullet_ros_subscriber.py`, which implements the subscriber version, that is, +it listens to the topics and set them in the bullet simulator. So by moving the robot in this simulator, you should +see that it also moves in the other simulator. + +Note: this code also works with other robots. +""" + +from itertools import count +import pyrobolearn as prl + + +# create middleware and simulator (roscore will automatically be launched if it has not already been launched) +ros = prl.middlewares.ROS(publish=True, teleoperate=True) +sim = prl.simulators.Bullet(middleware=ros) + +# load world +world = prl.worlds.BasicWorld(sim) + +# load robot +robot = world.load_robot('rrbot') + +# run simulation +for t in count(): + # get the joint positions from the Bullet simulator (because :attr:`teleoperate` has been set to True, + # it will publish these read positions on the corresponding topic) + q = robot.get_joint_positions() + + # perform a step in the simulator (and sleep for `sim.dt`) + world.step(sim.dt) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/middlewares/bullet_ros_rqt.py b/examples/middlewares/bullet_ros_rqt.py new file mode 100644 index 0000000..0b28170 --- /dev/null +++ b/examples/middlewares/bullet_ros_rqt.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +"""Test the publisher using PyBullet, ROS, and RQT. +""" + +import pyrobolearn as prl + +# create middleware and simulator +ros = prl.middlewares.ROS(publish=True, teleoperate=True) +sim = prl.simulators.Bullet(middleware=ros) + +# launch RQT +ros.launch_gui() + +# load world +world = prl.worlds.BasicWorld(sim) + +# load robot +robot = world.load_robot('rrbot') + +# run simulation +for t in prl.count(): + # get the joint positions from the Bullet simulator (because :attr:`teleoperate` has been set to True, + # it will publish these read positions on the corresponding topic) + q = robot.get_joint_positions() + + # perform a step in the simulator (and sleep for `sim.dt`) + world.step(sim.dt) diff --git a/examples/middlewares/bullet_ros_subscriber.py b/examples/middlewares/bullet_ros_subscriber.py new file mode 100644 index 0000000..ec66f43 --- /dev/null +++ b/examples/middlewares/bullet_ros_subscriber.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +#!/usr/bin/env python +"""Example on how to use the Bullet-ROS simulator (the subscriber version) in PRL. + +The subscriber version subscribe to various topics related to the loaded robot. + +This code works in parallel with the `bullet_ros_publisher.py`, which implements the publisher version, that is, +it publish to the topics the various data. By moving the robot in the publisher version of the simulator, you should +see that the robot in this simulator should move in accordance. + +Note: this code also works with other robots. +""" + +from itertools import count +import pyrobolearn as prl + + +# create simulator (ros core will automatically be launched if it has not already been launched) +ros = prl.middlewares.ROS(subscribe=True, teleoperate=True) +sim = prl.simulators.Bullet(middleware=ros) + +# load world +world = prl.worlds.BasicWorld(sim) + +# load robot +robot = world.load_robot('rrbot') + +# run simulation +for t in count(): + # get the joint positions from the ROS subscribers (if possible) + robot.get_joint_positions() + + # perform a step in the world, and sleep for `sim.dt` + world.step(sim.dt) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +