|
| 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