bag.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #include <rosmatlab/rosbag/bag.h>
00030 #include <rosmatlab/conversion.h>
00031 #include <rosmatlab/connection_header.h>
00032 #include <rosmatlab/log.h>
00033 
00034 #include <rosmatlab/rosbag/view.h>
00035 
00036 #include <introspection/message.h>
00037 
00038 namespace rosmatlab {
00039 namespace rosbag {
00040 
00041 template <> const char *Object<Bag>::class_name_ = "rosbag.Bag";
00042 
00043 Bag::Bag()
00044   : Object<Bag>(this)
00045 {
00046 }
00047 
00048 Bag::Bag(int nrhs, const mxArray *prhs[])
00049   : Object<Bag>(this)
00050 {
00051   if (nrhs > 0) open(nrhs, prhs);
00052 }
00053 
00054 Bag::~Bag() {
00055   close();
00056 }
00057 
00058 void Bag::open(int nrhs, const mxArray *prhs[])
00059 {
00060   if (nrhs < 1) throw ArgumentException("Bag.open", 1);
00061   std::string filename = Options::getString(prhs[0]);
00062   uint32_t mode = ::rosbag::bagmode::Read;
00063 
00064   if (nrhs >= 2) {
00065     mode = Options::getIntegerScalar(prhs[1]);
00066     if (mode > 7) throw Exception("Bag.open", "Invalid value for mode");
00067   }
00068 
00069   try {
00070     ::rosbag::Bag::open(filename, mode);
00071   } catch(std::runtime_error &e) {
00072     throw Exception(e);
00073   }
00074 }
00075 
00076 void Bag::close()
00077 {
00078   ::rosbag::Bag::close();
00079 }
00080 
00081 void Bag::data(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
00082 {
00083   View view(*this, nrhs, prhs);
00084   view.data(nlhs, plhs, 0, 0);
00085 
00086   // if only one topic has been given as a single parameter, return data without the outer struct
00087   if (nrhs % 2 != 0 && mxIsStruct(plhs[0])) {
00088     mxArray *temp = 0;
00089     if (mxGetNumberOfFields(plhs[0]) == 1) {
00090       temp = mxDuplicateArray(mxGetFieldByNumber(plhs[0], 0, 0));
00091     } else {
00092       temp = mxCreateStructMatrix(0, 0, 0, 0);
00093     }
00094     mxDestroyArray(plhs[0]);
00095     plhs[0] = temp;
00096   }
00097 }
00098 
00099 mxArray *Bag::getFileName() const
00100 {
00101   return mxCreateString(::rosbag::Bag::getFileName().c_str());
00102 }
00103 
00104 mxArray *Bag::getMode() const
00105 {
00106   mxArray *result = mxCreateNumericMatrix(1, 1, mxUINT8_CLASS, mxREAL);
00107   *static_cast<uint8_T *>(mxGetData(result)) = static_cast<uint8_T>(::rosbag::Bag::getMode());
00108   return result;
00109 }
00110 
00111 mxArray *Bag::getMajorVersion() const
00112 {
00113   mxArray *result = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL);
00114   *static_cast<uint32_T *>(mxGetData(result)) = static_cast<uint32_T>(::rosbag::Bag::getMajorVersion());
00115   return result;
00116 }
00117 
00118 mxArray *Bag::getMinorVersion() const
00119 {
00120   mxArray *result = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL);
00121   *static_cast<uint32_T *>(mxGetData(result)) = static_cast<uint32_T>(::rosbag::Bag::getMinorVersion());
00122   return result;
00123 }
00124 
00125 mxArray *Bag::getSize() const
00126 {
00127   mxArray *result = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
00128   *static_cast<uint64_T *>(mxGetData(result)) = static_cast<uint64_T>(::rosbag::Bag::getSize());
00129   return result;
00130 }
00131 
00132 void Bag::setCompression(int nrhs, const mxArray *prhs[])
00133 {
00134   if (nrhs < 1) throw ArgumentException("Bag.setCompression", 1);
00135   int value = Options::getIntegerScalar(prhs[0]);
00136   if (value < 0 || value > ::rosbag::compression::BZ2) throw Exception("Bag.setCompression", "Invalid value for property Compression");
00137   ::rosbag::compression::CompressionType compression = static_cast< ::rosbag::compression::CompressionType >(Options::getIntegerScalar(prhs[0]));
00138   ::rosbag::Bag::setCompression(compression);
00139 }
00140 
00141 mxArray *Bag::getCompression() const
00142 {
00143   mxArray *result = mxCreateNumericMatrix(1, 1, mxUINT8_CLASS, mxREAL);
00144   *static_cast<uint8_T *>(mxGetData(result)) = static_cast<uint8_T>(::rosbag::Bag::getCompression());
00145   return result;
00146 }
00147 
00148 void Bag::setChunkThreshold(int nrhs, const mxArray *prhs[])
00149 {
00150   if (nrhs < 1) throw ArgumentException("Bag.setChunkThreshold", 1);
00151   uint32_t chunkThreshold = static_cast<uint32_t>(Options::getIntegerScalar(prhs[0]));
00152   ::rosbag::Bag::setChunkThreshold(chunkThreshold);
00153 }
00154 
00155 mxArray *Bag::getChunkThreshold() const
00156 {
00157   mxArray *result = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL);
00158   *static_cast<uint32_T *>(mxGetData(result)) = static_cast<uint32_T>(::rosbag::Bag::getChunkThreshold());
00159   return result;
00160 }
00161 
00162 void Bag::write(int nrhs, const mxArray *prhs[])
00163 {
00164   if (nrhs < 3) throw ArgumentException("Bag.write", 3);
00165 
00166   // parse arguments
00167   std::string topic = Options::getString(prhs[0]);
00168   std::string datatype = Options::getString(prhs[1]);
00169   const mxArray *data = prhs[2];
00170   ConnectionHeader connection_header;
00171   ros::Time timestamp;
00172   for(int i = 3; i < nrhs; ++i) {
00173     if (mxIsStruct(prhs[i])) {
00174       if (!connection_header.fromMatlab(prhs[i])) throw Exception("Bag.write", "failed to parse connection header");
00175       continue;
00176     }
00177 
00178     if (Options::isDoubleScalar(prhs[i])) {
00179       timestamp = ros::Time(Options::getDoubleScalar(prhs[i]));
00180       continue;
00181     }
00182   }
00183 
00184   // set timestamp to current time if no timestamp was given
00185   if (timestamp.isZero()) timestamp = ros::Time::now();
00186 
00187   // introspect message
00188   MessagePtr introspection = cpp_introspection::messageByDataType(datatype);
00189   if (!introspection) throw Exception("Bag.write", "unknown datatype '" + datatype + "'");
00190   Conversion conversion(introspection);
00191 
00192   // ... and finally write data to the bag
00193   std::size_t count = conversion.numberOfInstances(data);
00194   for(std::size_t i = 0; i < count; ++i) {
00195     MessagePtr message = Conversion(introspection).fromMatlab(data, i);
00196     if (!message) throw Exception("Bag.write", "failed to parse message of type " + datatype);
00197 
00198     ::rosbag::Bag::write(topic, timestamp, *message, connection_header);
00199   }
00200 }
00201 
00202 }
00203 }


rosmatlab_rosbag
Author(s): Johannes Meyer
autogenerated on Fri Jul 25 2014 06:48:22