Skip to content

Commit 743f5dc

Browse files
committed
Add new package nxt_rviz_plugin for ROS Groovy
1 parent 56a05d2 commit 743f5dc

15 files changed

Lines changed: 1046 additions & 0 deletions

nxt/nxt_rviz_plugin/CMakeLists.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
cmake_minimum_required(VERSION 2.4.6)
2+
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
3+
set(ROS_BUILD_TYPE RelWithDebInfo)
4+
rosbuild_init()
5+
6+
find_package(Qt4 COMPONENTS QtCore QtGui REQUIRED)
7+
include(${QT_USE_FILE})
8+
9+
# Use Qt signals and slots to avoid defining "emit", "slots", etc.
10+
# because they can conflict with Boost signals, so define QT_NO_KEYWORDS here.
11+
add_definitions(-DQT_NO_KEYWORDS)
12+
13+
# Specify which header files need to be run through "moc",
14+
# Qt's meta-object compiler.
15+
qt4_wrap_cpp(MOC_FILES
16+
src/nxt_color_display.h
17+
src/nxt_ultrasonic_display.h
18+
)
19+
20+
set(SOURCE_FILES
21+
src/nxt_color_display.cpp
22+
src/nxt_color_visual.cpp
23+
src/nxt_ultrasonic_display.cpp
24+
src/nxt_ultrasonic_visual.cpp
25+
${MOC_FILES}
26+
)
27+
28+
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
29+
30+
# An rviz plugin is just a shared library, so here we declare the
31+
# library to be called PROJECT_NAME
32+
rosbuild_add_library(${PROJECT_NAME} ${SOURCE_FILES})
33+
34+
target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES})
35+

nxt/nxt_rviz_plugin/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include $(shell rospack find mk)/cmake.mk
744 Bytes
Loading
573 Bytes
Loading

nxt/nxt_rviz_plugin/manifest.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<package>
2+
<description brief="nxt_rviz_plugin">
3+
Plugins for visualizing data coming from the Lego NXT Brick in Rviz.
4+
Suitable for ROS Groovy and newer.
5+
</description>
6+
<author>David Butterworth</author>
7+
<license>BSD</license>
8+
<review status="unreviewed" notes=""/>
9+
<url>http://ros.org</url>
10+
11+
<depend package="roscpp"/>
12+
<depend package="rviz"/>
13+
<depend package="tf"/>
14+
<depend package="geometry_msgs"/>
15+
<depend package="nxt_msgs"/>
16+
17+
<export>
18+
<rosdoc config="${prefix}/rosdoc.yaml"/>
19+
<rviz plugin="${prefix}/plugin_description.xml"/>
20+
</export>
21+
22+
</package>
23+
24+
25+
26+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<library path="lib/libnxt_rviz_plugin">
2+
3+
<class name="nxt_rviz_plugin/NXTColor"
4+
type="nxt_rviz_plugin::NXTColorDisplay"
5+
base_class_type="rviz::Display">
6+
<description>
7+
Displays data from a nxt_msgs::Color message as a cylinder of color.
8+
</description>
9+
</class>
10+
11+
<class name="nxt_rviz_plugin/NXTUltrasonic"
12+
type="nxt_rviz_plugin::NXTUltrasonicDisplay"
13+
base_class_type="rviz::Display">
14+
<description>
15+
Displays data from a nxt_msgs::Range message as a cone.
16+
</description>
17+
</class>
18+
19+
</library>
20+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
3+
#
4+
# Script to publish dummy msgs
5+
# to test the NXT Rviz plugins.
6+
#
7+
8+
import roslib; roslib.load_manifest('nxt_rviz_plugin')
9+
from nxt_msgs.msg import Color
10+
from nxt_msgs.msg import Range
11+
import rospy
12+
import tf
13+
14+
color_topic = '/my_color_sensor'
15+
color_publisher = rospy.Publisher(color_topic, Color)
16+
color_values = list()
17+
color_values.append([0.0,0.0,0.0]) # black
18+
color_values.append([0.0,0.0,1.0]) # blue
19+
color_values.append([0.0,1.0,0.0]) # green
20+
color_values.append([1.0,1.0,0.0]) # yellow
21+
color_values.append([1.0,0.0,0.0]) # red
22+
color_values.append([1.0,1.0,1.0]) # white
23+
color_count = 0
24+
25+
range_topic = '/my_range_sensor'
26+
range_publisher = rospy.Publisher(range_topic, Range)
27+
range_values = list()
28+
range_values.append(0.05)
29+
range_values.append(0.07)
30+
range_values.append(0.09)
31+
range_values.append(0.11)
32+
range_values.append(0.13)
33+
range_count = 0
34+
35+
rospy.init_node('send_test_msgs')
36+
37+
print "Publishing test Color messages to topic ", color_topic
38+
print "Publishing test Range messages to topic ", range_topic
39+
40+
br = tf.TransformBroadcaster()
41+
42+
rate = rospy.Rate(1)
43+
while not rospy.is_shutdown():
44+
45+
color_msg = Color()
46+
color_msg.header.frame_id = "/color_sensor_frame"
47+
color_msg.header.stamp = rospy.Time.now()
48+
color_msg.r = color_values[color_count][0]
49+
color_msg.g = color_values[color_count][1]
50+
color_msg.b = color_values[color_count][2]
51+
color_publisher.publish(color_msg)
52+
color_count = color_count + 1
53+
if color_count > (len(color_values)-1):
54+
color_count = 0
55+
56+
range_msg = Range()
57+
range_msg.header.frame_id = "/range_sensor_frame"
58+
range_msg.header.stamp = rospy.Time.now()
59+
range_msg.range = range_values[range_count]
60+
range_msg.spread_angle = 0.2
61+
range_publisher.publish(range_msg)
62+
range_count = range_count + 1
63+
if range_count > (len(range_values)-1):
64+
range_count = 0
65+
66+
br.sendTransform((0, 0, 0),
67+
tf.transformations.quaternion_from_euler(0, 0, 0),
68+
rospy.Time.now(),
69+
"color_sensor_frame",
70+
"map")
71+
72+
br.sendTransform((0, 0, 0),
73+
tf.transformations.quaternion_from_euler(0, 0, 0),
74+
rospy.Time.now(),
75+
"range_sensor_frame",
76+
"map")
77+
78+
rate.sleep()
79+
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
* Copyright (c) 2016, David Butterworth
3+
* All rights reserved.
4+
*
5+
* An Rviz plugin to display data from the
6+
* LEGO Mindstorms NXT 2.0 color sensor.
7+
*
8+
* Based on nxt_rviz_plugin & rviz_plugin_tutorials by Willow Garage.
9+
*
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions are met:
13+
*
14+
* * Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
* * Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in the
18+
* documentation and/or other materials provided with the distribution.
19+
* * Neither the name of the Willow Garage, Inc. nor the names of its
20+
* contributors may be used to endorse or promote products derived from
21+
* this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33+
* POSSIBILITY OF SUCH DAMAGE.
34+
*/
35+
36+
#include <OGRE/OgreSceneNode.h>
37+
#include <OGRE/OgreSceneManager.h>
38+
39+
#include <tf/transform_listener.h>
40+
41+
#include <rviz/visualization_manager.h>
42+
#include <rviz/properties/float_property.h>
43+
#include <rviz/properties/int_property.h>
44+
#include <rviz/frame_manager.h>
45+
46+
#include "nxt_color_visual.h"
47+
#include "nxt_color_display.h"
48+
49+
namespace nxt_rviz_plugin
50+
{
51+
NXTColorDisplay::NXTColorDisplay()
52+
{
53+
// Properties for this Display that the user
54+
// can change from within Rviz:
55+
cylinder_length_property_ = new rviz::FloatProperty("Length", 0.003,
56+
"Length of the cylinder visual.",
57+
this, SLOT(updateCylinderLength()));
58+
alpha_property_ = new rviz::FloatProperty("Alpha", 0.5,
59+
"0 is fully transparent, 1.0 is fully opaque.",
60+
this, SLOT(updateAlpha()));
61+
history_length_property_ = new rviz::IntProperty("History Length", 1,
62+
"Number of prior measurements to display.",
63+
this, SLOT(updateHistoryLength()));
64+
history_length_property_->setMin(1);
65+
history_length_property_->setMax(100000);
66+
}
67+
68+
void NXTColorDisplay::onInitialize()
69+
{
70+
MFDClass::onInitialize();
71+
updateHistoryLength();
72+
}
73+
74+
NXTColorDisplay::~NXTColorDisplay()
75+
{
76+
}
77+
78+
// Clear this display back to its initial state
79+
void NXTColorDisplay::reset()
80+
{
81+
MFDClass::reset();
82+
visuals_.clear();
83+
}
84+
85+
// Qt slot: set the current color and alpha values for each visual.
86+
void NXTColorDisplay::updateAlpha()
87+
{
88+
float alpha = alpha_property_->getFloat();
89+
90+
for(size_t i=0; i < visuals_.size(); i++)
91+
{
92+
visuals_[i]->setColor(color_.r, color_.g, color_.b, alpha);
93+
}
94+
}
95+
96+
// Qt slot: set the number of past visuals to show
97+
void NXTColorDisplay::updateHistoryLength()
98+
{
99+
visuals_.rset_capacity(history_length_property_->getInt());
100+
}
101+
102+
// Qt slot: set the length of the cylinder visual
103+
void NXTColorDisplay::updateCylinderLength()
104+
{
105+
float length = cylinder_length_property_->getFloat();
106+
107+
for(size_t i=0; i < visuals_.size(); i++)
108+
{
109+
visuals_[i]->setScale(length);
110+
}
111+
}
112+
113+
// Callback to handle an incoming ROS message
114+
void NXTColorDisplay::processMessage(const nxt_msgs::Color::ConstPtr& msg)
115+
{
116+
float length = cylinder_length_property_->getFloat();
117+
118+
// Get the transform from the fixed frame to the frame
119+
// in the header of this msg
120+
Ogre::Quaternion orientation;
121+
Ogre::Vector3 position;
122+
if( !context_->getFrameManager()->getTransform(msg->header.frame_id,
123+
msg->header.stamp,
124+
position,
125+
orientation))
126+
{
127+
ROS_DEBUG("Error transforming from frame '%s' to frame '%s'",
128+
msg->header.frame_id.c_str(), qPrintable(fixed_frame_));
129+
return;
130+
}
131+
132+
// We are keeping a circular buffer of visual pointers.
133+
// Get the next ptr or make a new one.
134+
boost::shared_ptr<NXTColorVisual> visual;
135+
if(visuals_.full())
136+
{
137+
visual = visuals_.front();
138+
}
139+
else
140+
{
141+
visual.reset(new NXTColorVisual(context_->getSceneManager(), scene_node_));
142+
}
143+
144+
// Update the pose of the Ogre::SceneNode
145+
visual->setFramePosition(position);
146+
visual->setFrameOrientation(orientation);
147+
148+
// Set the pose and size of the cylinder visual
149+
visual->setScale(length);
150+
151+
// Store the RGB color value from the msg
152+
// and set the color of the cylinder visual
153+
float alpha = alpha_property_->getFloat();
154+
color_.r = msg->r;
155+
color_.g = msg->g;
156+
color_.b = msg->b;
157+
visual->setColor(color_.r, color_.g, color_.b, alpha);
158+
159+
// Add visual to end of the circular buffer
160+
visuals_.push_back(visual);
161+
}
162+
163+
} // end namespace nxt_rviz_plugin
164+
165+
// Tell pluginlib about this class
166+
#include <pluginlib/class_list_macros.h>
167+
PLUGINLIB_EXPORT_CLASS(nxt_rviz_plugin::NXTColorDisplay,rviz::Display)
168+

0 commit comments

Comments
 (0)