mirror of
https://github.com/wassname/pyrobolearn.git
synced 2026-09-11 12:31:07 +08:00
update ROS/BulletROS + add simulator examples
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
## Simulator examples
|
||||
|
||||
In this folder, you will have simple examples on how to use different simulators by loading a simple world (with a
|
||||
floor and gravity enabled), and a simple robot.
|
||||
|
||||
The various files can be called by `python <simulator>.py`. Note that they are mostly identical, and only the line
|
||||
|
||||
```python
|
||||
sim = Simulator(args) # Bullet, BulletROS, ROS, etc
|
||||
```
|
||||
|
||||
need to be changed. The basic idea is that your code should work without depending on which simulator you use. Note
|
||||
that currently, the `Bullet` simulator is the only fully functional API, while the others are partially implemented
|
||||
or still need to be implemented.
|
||||
|
||||
Here are the few examples that you can find in this folder:
|
||||
1. `bullet.py`: simple example where we use the `Bullet` simulator, load a basic world (with a floor and gravity
|
||||
enabled) and the RRBot robot in it.
|
||||
2. `bullet_ros_publisher.py`: example where we use the `BulletROS(publish=True)` simulator which publishes the
|
||||
joint position values that were returned by the Bullet simulator on the corresponding ROS topic.
|
||||
3. `bullet_ros_subscriber.py`: example where we use the `BulletROS(subscribe=True)` simulator which gets the joint
|
||||
values from the ROS topics and change them in the simulator. This works with the `bullet_ros_publisher.py` code
|
||||
presented above. By moving the robot with your mouse in the publisher version, you will see the robot in this
|
||||
subscriber version moves in accordance with. This can be useful if you have access to the real platform as well.
|
||||
|
||||
Later, a `ROS`/`ROS_RBDL` "simulator" (without passing by a real simulator like `Bullet`) will allow you to make
|
||||
your code works on a real platform using ROS without changing any other lines of code. This is one of the big
|
||||
TODOs but is not currently my priority.
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python
|
||||
"""Example on how to use the Bullet simulator in pyrobolearn.
|
||||
|
||||
Simple example where we use the bullet simulator, load a basic world (with a floor and gravity enabled) and the RRBot
|
||||
robot in it.
|
||||
|
||||
You can move inside the world using your mouse and keyboard:
|
||||
- `ctrl + left click`: rotate the camera
|
||||
- `scroll wheel` or `ctrl + right click`: zoom in/out
|
||||
- `ctrl + middle click`: move the camera
|
||||
- `left click` on an object: if the object has a mass and a collision shape, you can interact with it with the mouse
|
||||
- `w`: wireframe (see collision shapes)
|
||||
- `g`: show/hide menu
|
||||
- `esc`: quit the simulator
|
||||
"""
|
||||
|
||||
from itertools import count
|
||||
import pyrobolearn as prl
|
||||
|
||||
|
||||
# create simulator
|
||||
sim = prl.simulators.Bullet(render=True)
|
||||
|
||||
# create basic world (i.e. with a floor and gravity enabled)
|
||||
world = prl.worlds.BasicWorld(sim)
|
||||
|
||||
# load rrbot
|
||||
robot = prl.robots.RRBot(sim)
|
||||
|
||||
|
||||
# run simulator
|
||||
for t in count():
|
||||
# perform a step in the simulator and sleep for `sim.dt` (which is 1./240 in this case)
|
||||
sim.step(sim.dt)
|
||||
@@ -0,0 +1,66 @@
|
||||
#!/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 simulator (ros core will automatically be launched if it has not already been launched)
|
||||
sim = prl.simulators.BulletROS(subscribe=False, publish=True, teleoperate=True)
|
||||
|
||||
# load world
|
||||
world = prl.worlds.BasicWorld(sim)
|
||||
|
||||
# load rrbot
|
||||
robot = prl.robots.RRBot(sim)
|
||||
|
||||
|
||||
# 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)
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/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)
|
||||
sim = prl.simulators.BulletROS(subscribe=True, publish=False)
|
||||
|
||||
# load world
|
||||
world = prl.worlds.BasicWorld(sim)
|
||||
|
||||
# load rrbot
|
||||
robot = prl.robots.RRBot(sim)
|
||||
|
||||
|
||||
# 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)
|
||||
Reference in New Issue
Block a user