Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <rosmatlab/publisher.h>
00030 #include <rosmatlab/exception.h>
00031 #include <rosmatlab/options.h>
00032 #include <rosmatlab/conversion.h>
00033
00034 #include <introspection/message.h>
00035
00036 #include <ros/topic_manager.h>
00037
00038 namespace rosmatlab {
00039
00040 template <> const char *Object<Publisher>::class_name_ = "ros.Publisher";
00041
00042 Publisher::Publisher()
00043 : Object<Publisher>(this)
00044 {
00045 }
00046
00047 Publisher::Publisher(int nrhs, const mxArray *prhs[])
00048 : Object<Publisher>(this)
00049 {
00050 if (nrhs > 0) advertise(nrhs, prhs);
00051 }
00052
00053 Publisher::~Publisher() {
00054 shutdown();
00055 }
00056
00057 mxArray *Publisher::advertise(int nrhs, const mxArray *prhs[])
00058 {
00059 if (nrhs < 2) {
00060 throw ArgumentException("Publisher.advertise", 2);
00061 }
00062
00063 options_ = ros::AdvertiseOptions();
00064 for(int i = 0; i < nrhs; i++) {
00065 switch(i) {
00066 case 0:
00067 if (!Options::isString(prhs[i])) throw Exception("Publisher.advertise", "need a topic as 1st argument");
00068 options_.topic = Options::getString(prhs[i]);
00069 break;
00070
00071 case 1:
00072 if (!Options::isString(prhs[i])) throw Exception("Publisher.advertise", "need a datatype as 2nd argument");
00073 options_.datatype = Options::getString(prhs[i]);
00074 break;
00075
00076 case 2:
00077 if (!Options::isDoubleScalar(prhs[i])) throw Exception("Publisher.advertise", "need a queue size as 3rd argument (optional)");
00078 options_.queue_size = Options::getDoubleScalar(prhs[i]);
00079 break;
00080
00081 case 3:
00082 if (!Options::isLogicalScalar(prhs[i]) && !Options::isDoubleScalar(prhs[i])) throw Exception("Publisher.advertise", "need logical latch as 4th argument (optional)");
00083 options_.latch = Options::getLogicalScalar(prhs[i]);
00084 break;
00085
00086 default:
00087 throw ArgumentException("Publisher.advertise", "too many arguments");
00088 }
00089 }
00090
00091 introspection_ = cpp_introspection::messageByDataType(options_.datatype);
00092 if (!introspection_) throw Exception("Publisher.advertise", "unknown datatype '" + options_.datatype + "'");
00093 options_.md5sum = introspection_->getMD5Sum();
00094 options_.message_definition = introspection_->getDefinition();
00095 options_.has_header = introspection_->hasHeader();
00096
00097 *this = node_handle_.advertise(options_);
00098 return mxCreateLogicalScalar(*this);
00099 }
00100
00101 void Publisher::publish(int nrhs, const mxArray *prhs[])
00102 {
00103 if (nrhs < 1) throw ArgumentException("Publisher.publish", 1);
00104 if (!introspection_) throw Exception("Publisher.publish", "unknown message type");
00105
00106 MessagePtr message;
00107 ros::SerializedMessage m;
00108 m.type_info = &(introspection_->getTypeId());
00109 Conversion conversion(introspection_);
00110
00111 std::size_t count = conversion.numberOfInstances(prhs[0]);
00112 for(std::size_t i = 0; i < count; ++i) {
00113 message = conversion.fromMatlab(prhs[0], i);
00114 if (!message) throw Exception("Publisher.publish", "failed to parse message of type " + options_.datatype);
00115
00116
00118
00119 ros::Publisher::publish(*message);
00120 }
00121 }
00122
00123 mxArray *Publisher::getTopic() const
00124 {
00125 return mxCreateString(ros::Publisher::getTopic().c_str());
00126 }
00127
00128 mxArray *Publisher::getDataType() const
00129 {
00130 if (!introspection_) return mxCreateEmpty();
00131 return mxCreateString(introspection_->getDataType());
00132 }
00133
00134 mxArray *Publisher::getMD5Sum() const
00135 {
00136 if (!introspection_) return mxCreateEmpty();
00137 return mxCreateString(introspection_->getMD5Sum());
00138 }
00139
00140 mxArray *Publisher::getNumSubscribers() const
00141 {
00142 return mxCreateDoubleScalar(ros::Publisher::getNumSubscribers());
00143 }
00144
00145 mxArray *Publisher::isLatched() const
00146 {
00147 return mxCreateLogicalScalar(*this ? ros::Publisher::isLatched() : false);
00148 }
00149
00150 }