add ROS middleware examples

This commit is contained in:
Brian Delhaisse
2019-10-17 09:33:28 +02:00
parent 833c377c74
commit 68629ad9c2
4 changed files with 198 additions and 0 deletions
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#!/usr/bin/env python
"""Test the publisher using Gazebo with ROS control and Pybullet
@@ -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)
+28
View File
@@ -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)
@@ -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)