MKInterface.cpp
Go to the documentation of this file.
00001 /*
00002  * MKInterface.cpp
00003  *
00004  *  Created on: Dec 8, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include <telekyb_interface/MKInterface.hpp>
00009 
00010 #include <telekyb_srvs/StringOutput.h>
00011 #include <std_srvs/Empty.h>
00012 
00013 #include <telekyb_srvs/IntArrayInput.h>
00014 #include <telekyb_srvs/MKValueInputOutput.h>
00015 
00016 namespace TELEKYB_INTERFACE_NAMESPACE {
00017 
00018 MKInterface::MKInterface(int robotID_, const std::string& mainHandleNamespace)
00019         : robotID(robotID_), mainNodeHandle(mainHandleNamespace), optionController(NULL)
00020 {
00021         createOptionController();
00022 
00023         // Pub and Subs
00024         setMKValueAsyncPub = mainNodeHandle.advertise<telekyb_msgs::MKValue>(MKINTERFACE_SETMKVALUEASYNC, 100);
00025         updateMKValueAsyncPub = mainNodeHandle.advertise<telekyb_msgs::MKValue>(MKINTERFACE_UPDATEMKVALUEASYNC, 100);
00026 
00027         mkValueSub = mainNodeHandle.subscribe(MKINTERFACE_MKSINGLEVALUETOPIC, 10, &MKInterface::recvMKValue, this);
00028 
00029         // Init ValueArray
00030         for (int i = 0; i < MKDataDefines::MKDATAIDS_NUM; ++i) {
00031                 valueArray[i].id = i; // Position equals id;
00032                 valueArray[i].name = MKDataDefines::MKDATAIDS_NAMES[i];
00033         }
00034 }
00035 
00036 MKInterface::~MKInterface()
00037 {
00038         if (optionController) { delete optionController; }
00039 }
00040 
00041 OptionController* MKInterface::getOptionController() const
00042 {
00043         return optionController;
00044 }
00045 
00046 void MKInterface::createOptionController()
00047 {
00048         ros::ServiceClient client = mainNodeHandle.serviceClient<telekyb_srvs::StringOutput>(OPTION_GETOPTIONNODEHANDLE);
00049         telekyb_srvs::StringOutput soService;
00050         if (client.call(soService)) {
00051                 optionController = new OptionController(soService.response.output);
00052         } else {
00053                 ROS_ERROR_STREAM("Unable to create OptionController. Failed to call: " << client.getService());
00054                 ROS_FATAL("This is fatal!");
00055                 ros::shutdown();
00056         }
00057 }
00058 
00059 MKInterface* MKInterface::getMKInterface(int robotID_, double blockTimeOut_)
00060 {
00061         MKInterface* mkInterface = NULL;
00062 
00063         ros::NodeHandle robotIDNodeHandle( std::string(TELEKYB_BASENAME) + "/" + boost::lexical_cast<std::string>(robotID_));
00064         ros::ServiceClient client = robotIDNodeHandle.serviceClient<telekyb_srvs::StringOutput>(MKINTERFACE_GETMAINMKNODEHANDLE);
00065 
00066         telekyb_srvs::StringOutput soService;
00067 
00068         // wait for it. (starting with roslaunch)
00069         if (blockTimeOut_ > 0.0 && ! client.waitForExistence(ros::Duration(blockTimeOut_)) ) {
00070                 ROS_ERROR_STREAM("Unable to create MKInterface. Service not available.");
00071                 return NULL;
00072         }
00073 
00074         if (client.call(soService)) {
00075                 mkInterface = new MKInterface(robotID_, soService.response.output);
00076         } else {
00077                 ROS_ERROR_STREAM("Unable to create MKInterface. Failed to call: " << client.getService());
00078         }
00079 
00080         return mkInterface;
00081 }
00082 
00083 
00084 bool MKInterface::doDriftEstim()
00085 {
00086         ros::ServiceClient client = mainNodeHandle.serviceClient<std_srvs::Empty>(MKINTERFACE_DODRIFTESTIM);
00087         std_srvs::Empty service;
00088         if (! client.call(service) ) {
00089                 ROS_ERROR_STREAM("Failed to call: " << client.getService());
00090                 return false;
00091         }
00092 
00093         return true;
00094 }
00095 bool MKInterface::setActiveDataIDs(MKActiveIDs activeIDs)
00096 {
00097         ros::ServiceClient client = mainNodeHandle.serviceClient<telekyb_srvs::IntArrayInput>(MKINTERFACE_SETACTIVEDATAIDS);
00098         telekyb_srvs::IntArrayInput service;
00099 
00100         service.request.input.resize(ACTIVEDATA_SIZE);
00101         for (int i = 0; i < ACTIVEDATA_SIZE; ++i) {
00102                 service.request.input[i] = activeIDs.ids[i];
00103         }
00104 
00105         if (! client.call(service) ) {
00106                 ROS_ERROR_STREAM("Failed to call: " << client.getService());
00107                 return false;
00108         }
00109 
00110         return true;
00111 }
00112 bool MKInterface::setMKValue(MKSingleValuePacket value)
00113 {
00114         ros::ServiceClient client = mainNodeHandle.serviceClient<telekyb_srvs::MKValueInputOutput>(MKINTERFACE_SETMKVALUE);
00115         telekyb_srvs::MKValueInputOutput service;
00116 
00117         service.request.value.id = value.id;
00118         service.request.value.stamp = ros::Time::now();
00119         service.request.value.value = value.value;
00120 
00121         if (! client.call(service) ) {
00122                 ROS_ERROR_STREAM("Failed to call: " << client.getService());
00123                 return false;
00124         }
00125 
00126         return true;
00127 }
00128 
00129 bool MKInterface::updateMKValue(MKSingleValuePacket& value) // updates Value
00130 {
00131         ros::ServiceClient client = mainNodeHandle.serviceClient<telekyb_srvs::MKValueInputOutput>(MKINTERFACE_UPDATEMKVALUE);
00132         telekyb_srvs::MKValueInputOutput service;
00133 
00134         service.request.value.id = value.id;
00135         service.request.value.stamp = ros::Time::now();
00136 
00137         if (! client.call(service) ) {
00138                 ROS_ERROR_STREAM("Failed to call: " << client.getService());
00139                 return false;
00140         }
00141 
00142         // should be the same!
00143         value.id = service.response.value.id;
00144         value.value = service.response.value.value;
00145 
00146         return true;
00147 }
00148 
00149 bool MKInterface::setEmergency()
00150 {
00151         ros::ServiceClient client = mainNodeHandle.serviceClient<std_srvs::Empty>(MKINTERFACE_SETEMERGENCY);
00152         std_srvs::Empty service;
00153         if (! client.call(service) ) {
00154                 ROS_ERROR_STREAM("Failed to call: " << client.getService());
00155                 return false;
00156         }
00157 
00158         return true;
00159 }
00160 
00161 void MKInterface::setMKValueAsync(MKSingleValuePacket value)
00162 {
00163         telekyb_msgs::MKValue msg;
00164         msg.id = value.id;
00165         msg.value = value.value;
00166 
00167         setMKValueAsyncPub.publish(msg);
00168 }
00169 
00170 void MKInterface::updateMKValueAsync(MKInt id)
00171 {
00172         telekyb_msgs::MKValue msg;
00173         msg.id = id;
00174 
00175         updateMKValueAsyncPub.publish(msg);
00176 }
00177 
00178 void MKInterface::recvMKValue(const telekyb_msgs::MKValue::ConstPtr& msg)
00179 {
00180         if (msg->id >= MKDataDefines::MKDATAIDS_NUM || msg->id < 0) {
00181                 ROS_ERROR("Received Out of Bounds MKValue (%d)!. Dropping...", msg->id);
00182                 return;
00183         }
00184 
00185         boost::mutex::scoped_lock(valueArrayMutex);
00186         //valueArray[msg->id].name = msg->name;
00187         valueArray[msg->id].stamp = msg->stamp;
00188         valueArray[msg->id].value = msg->value;
00189 
00190 
00191         //ROS_INFO_STREAM("Received Msg! " << msg->name);
00192 
00193 }
00194 
00195 MKCompleteSingeValue MKInterface::getMKCompleteSingeValue(int id) const
00196 {
00197         if (id >= MKDataDefines::MKDATAIDS_NUM || id < 0) {
00198                 ROS_ERROR("getMKCompleteSingeValue with Out of Bounds ID (%d)!. Dropping...", id);
00199                 return MKCompleteSingeValue();
00200         }
00201 
00202         boost::mutex::scoped_lock(valueArrayMutex);
00203         return valueArray[id];
00204 }
00205 
00206 bool  MKInterface::updateAllMKValues()
00207 {
00208         bool retValue = true;
00209         for (int i = 0; i < MKDataDefines::MKDATAIDS_NUM; ++i) {
00210                 MKSingleValuePacket tempValue(valueArray[i].id, 0);
00211                 if ( updateMKValue(tempValue) ) { // Position equals id;
00212                         boost::mutex::scoped_lock(valueArrayMutex);
00213                         valueArray[valueArray[i].id].value = tempValue.value;
00214                         valueArray[valueArray[i].id].stamp = ros::Time::now(); // little dirty here
00215                 } else {
00216                         retValue = false;
00217                 }
00218         }
00219         return retValue;
00220 }
00221 
00222 void  MKInterface::updateAllMKValuesAsync()
00223 {
00224         for (int i = 0; i < MKDataDefines::MKDATAIDS_NUM; ++i) {
00225                 updateMKValueAsync(valueArray[i].id);
00226         }
00227 }
00228 
00229 } /* namespace telekyb_interface */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


telekyb_interface
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:12:47