This article works in the following environment.
item | value |
---|---|
CPU | Core i5-8250U |
Ubuntu | 16.04 |
ROS | Kinetic |
python | 2.7.12 |
mongodb | 2.6.10 |
For installation, refer to ROS Course 02 Installation. Also, the program in this article has been uploaded to github. See ROS Course 11 git repository.
In ROS, information is basically stored in rosbag, but this is a debug log-like thing that humans look back at later, and database-like data to be reused at runtime is saved by each node or a file is written by itself. There is no choice but to read. What comes out here is DataBase, and ROS publishes a package called mongodb that connects to DataBase.
mongodb has a structure of database-> collection-> document
.
mongodb_store installation
sudo apt install ros-kinetic-mongodb-store
Decide the directory where you want to put the database files at startup. Create an empty directory. If you specify a directory that does not exist, an error will occur.
mongodb_Run store
mkdir /tmp/test_db
roslaunch mongodb_store mongodb_store.launch db_path:=/tmp/test_db port:=27017
web_lecture/scripts/mongo_pose_write.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
from mongodb_store.message_store import MessageStoreProxy
from geometry_msgs.msg import Pose, Point, Quaternion
if __name__ == '__main__':
rospy.init_node("mongo_pose_write")
msg_store = MessageStoreProxy(database="srs", collection="pose1")
p = Pose(Point(0, 1, 2), Quaternion(0, 0, 0, 1))
try:
p_id = msg_store.insert_named("named_pose", p)
p_id = msg_store.insert(p)
except rospy.ServiceException, e:
print "Service call failed: %s"%e
MessageStoreProxy (database =" srs ", collection =" pose1 ")
. Specify the databas name and collection name here.msg_store.insert_named ("named_pose ", p)
.Write node execution
rosrun web_lecture mongo_pose_write.py
web_lecture/scripts/mongo_pose_read.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy
import mongodb_store_msgs.srv as dc_srv
import mongodb_store.util as dc_util
from mongodb_store.message_store import MessageStoreProxy
from geometry_msgs.msg import Pose, Point, Quaternion
import StringIO
if __name__ == '__main__':
rospy.init_node("mongo_pose_write")
msg_store = MessageStoreProxy(database="srs", collection="pose1")
try:
for item in msg_store.query(Pose._type):
print item[0].position
except rospy.ServiceException, e:
print "Service call failed: %s"%e
msg_store.query (Pose._type)
to extract pose type data from the database. You can also enter a detailed query.Run read node
rosrun web_lecture mongo_pose_read.py
Execution result
x: 0.0
y: 1.0
z: 2.0
x: 0.0
y: 1.0
z: 2.0
You can also read the database directly instead of the ROS interface.
Invoking the command mongo
will connect you to the mongo console.
show dbs
.show collections
.db. {collection name} .find ()
can see the whole data in that collection.If mongodb is already running behind the scenes, port27017 is suppressed, so roslaunch will result in an error.
Let's see if service is started with systemctl status mongodb.service
. If it is up, stop it with sudo systemctl disable mongodb.service && sudo systemctl stop mongodb.service
.
Link to ROS course table of contents
Recommended Posts