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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "jaus/extras/controllers/controldevice.h"
00041 #include "jaus/mobility/drivers/primitivedriver.h"
00042 #include "jaus/extras/mcu/microcontroller.h"
00043 #include "jaus/extras/mcu/setmicrocontrollerstate.h"
00044 #include <cmath>
00045 #include <tinyxml/tinyxml.h>
00046 #include <cxutils/fileio.h>
00047 #include <algorithm>
00048
00049 using namespace JAUS;
00050
00051 const std::string ControlDevice::Name = "urn:jaus:jss:jpp:extras:ControlDevice";
00052
00053
00059 ControlDevice::ControlDevice() : Management::Child(Service::ID(ControlDevice::Name),
00060 Service::ID(Management::Name))
00061 {
00062 mTakeDriveControlFlag = false;
00063 mAutoBrakingFlag = true;
00064 mSubsystemID = 0;
00065
00066 for(unsigned int i = 0; i < 11; i++)
00067 {
00068 mLimitsMapping[(WrenchEffort)i] = 100.0;
00069 }
00070 }
00071
00072
00078 ControlDevice::~ControlDevice()
00079 {
00080
00081 }
00082
00083
00093 bool ControlDevice::LoadSettings(const std::string& filename)
00094 {
00095 TiXmlDocument xml;
00096
00097 if(xml.LoadFile(filename.c_str()) == false)
00098 {
00099 return false;
00100 }
00101
00102
00103 mControlDevice.Lock();
00104 mWrenchMapping.clear();
00105 mDeadzoneMapping.clear();
00106 mInvertMapping.clear();
00107 mButtonMapping.clear();
00108 mLimitsMapping.clear();
00109 mControlDevice.Unlock();
00110
00111 TiXmlHandle doc(&xml);
00112 TiXmlNode* node;
00113 TiXmlElement* element;
00114 element = doc.FirstChild("JAUS").FirstChild("ControlDevice").FirstChild("Wrenches").FirstChild("WrenchEffort").ToElement();
00115 while(element)
00116 {
00117 if(element->Attribute("wrench") && element->Attribute("input"))
00118 {
00119 WrenchEffort wrench;
00120 int input;
00121 double deadzone = 0.0;
00122 double limit = 100.0;
00123 bool invert = false;
00124 wrench = (WrenchEffort)atoi(element->Attribute("wrench"));
00125 input = atoi(element->Attribute("input"));
00126 if(element->Attribute("deadzone"))
00127 {
00128 deadzone = atof(element->Attribute("deadzone"));
00129 }
00130 if(element->Attribute("limit"))
00131 {
00132 limit = atof(element->Attribute("limit"));
00133 }
00134 if(element->Attribute("invert"))
00135 {
00136 std::string value = element->Attribute("invert");
00137 if(value == "true" || value == "false")
00138 {
00139 if(value == "true")
00140 {
00141 invert = true;
00142 }
00143 }
00144 else
00145 {
00146 invert = atoi(value.c_str()) > 0 ? true : false;
00147 }
00148 }
00149 MapInputToWrench(input, wrench, deadzone, invert, limit);
00150 }
00151 element = element->NextSiblingElement();
00152 }
00153
00154 element = doc.FirstChild("JAUS").FirstChild("ControlDevice").FirstChild("Microcontroller").FirstChild("AnalogOut").ToElement();
00155 while(element)
00156 {
00157 if(element->Attribute("name") && element->Attribute("input"))
00158 {
00159 std::string name;
00160 int input;
00161 double limit = 100.0;
00162 bool invert = false;
00163 name = element->Attribute("name");
00164 input = atoi(element->Attribute("input"));
00165 if(element->Attribute("limit"))
00166 {
00167 limit = atof(element->Attribute("limit"));
00168 }
00169 if(element->Attribute("invert"))
00170 {
00171 std::string value = element->Attribute("invert");
00172 if(value == "true" || value == "false")
00173 {
00174 if(value == "true")
00175 {
00176 invert = true;
00177 }
00178 }
00179 else
00180 {
00181 invert = atoi(value.c_str()) > 0 ? true : false;
00182 }
00183 }
00184 MapInputToAnalogOut(input, name, invert, limit);
00185 }
00186 element = element->NextSiblingElement("AnalogOut");
00187 }
00188
00189 element = doc.FirstChild("JAUS").FirstChild("ControlDevice").FirstChild("Microcontroller").FirstChild("DigitalOut").ToElement();
00190 while(element)
00191 {
00192 if(element->Attribute("name") && element->Attribute("button"))
00193 {
00194 std::string name;
00195 int button;
00196 bool onPress = true;
00197 name = element->Attribute("name");
00198 button = atoi(element->Attribute("button"));
00199 if(element->Attribute("press"))
00200 {
00201 std::string value = element->Attribute("press");
00202 if(value == "1" || value == "true")
00203 {
00204 onPress = 1;
00205 }
00206 else
00207 {
00208 onPress = 0;
00209 }
00210 }
00211 MapButtonToDigitalOut(button, name, onPress);
00212 }
00213 element = element->NextSiblingElement("AnalogOut");
00214 }
00215
00216 node = doc.FirstChild("JAUS").FirstChild("ControlDevice").FirstChild("Buttons").FirstChild("RequestDriveControlButton").ToNode();
00217 if(node && node->FirstChild())
00218 {
00219 MapButtonToAction(atoi(node->FirstChild()->Value()), RequestDriveControl);
00220 }
00221 node = doc.FirstChild("JAUS").FirstChild("ControlDevice").FirstChild("Buttons").FirstChild("ReleaseDriveControlButton").ToNode();
00222 if(node && node->FirstChild())
00223 {
00224 MapButtonToAction(atoi(node->FirstChild()->Value()), ReleaseDriveControl);
00225 }
00226
00227 return true;
00228 }
00229
00230
00242 bool ControlDevice::LoadSettingsForVehicle(const std::string& vehicleIdentificationName,
00243 const std::string& directory)
00244 {
00245 bool result = false;
00246 if(vehicleIdentificationName == "")
00247 {
00248 return result;
00249 }
00250 std::vector<std::string> filenames;
00251 if(CxUtils::FileIO::GetFiles(filenames, "*.xml", directory, false, true) > 0)
00252 {
00253 std::string ident = vehicleIdentificationName;
00254 std::transform(ident.begin(), ident.end(), ident.begin(), tolower);
00255 for(unsigned int i = 0; i < (unsigned int)filenames.size(); i++)
00256 {
00257 std::string copy = filenames[i];
00258 std::transform(copy.begin(), copy.end(), copy.begin(), tolower);
00259 if(strstr(copy.c_str(), ident.c_str()) > 0)
00260 {
00261 result = this->LoadSettings(filenames[i]);
00262 break;
00263 }
00264 }
00265 if(result == false)
00266 {
00267 for(unsigned int i = 0; i < (unsigned int)filenames.size(); i++)
00268 {
00269 std::string copy = filenames[i];
00270 std::transform(copy.begin(), copy.end(), copy.begin(), tolower);
00271 if(strstr(copy.c_str(), "default") > 0)
00272 {
00273 result = this->LoadSettings(filenames[i]);
00274 break;
00275 }
00276 }
00277 }
00278 }
00279
00280 return result;
00281 }
00282
00283
00289 void ControlDevice::Shutdown()
00290 {
00291 TakeDriveControl(false);
00292 Mutex::ScopedLock lock(&mControlDevice);
00293 if(mPrimitiveDriverID.IsValid())
00294 {
00295 if(GetComponent()->AccessControlService()->HaveControl(mPrimitiveDriverID))
00296 {
00297 ReleaseComponentControl(mPrimitiveDriverID, true);
00298 }
00299 }
00300
00301 if(mMicrocontrollerID.IsValid())
00302 {
00303 if(GetComponent()->AccessControlService()->HaveControl(mMicrocontrollerID))
00304 {
00305 ReleaseComponentControl(mMicrocontrollerID, true);
00306 }
00307 }
00308 }
00309
00310
00318 void ControlDevice::SetSubsystemToControl(const UShort id)
00319 {
00320 Mutex::ScopedLock lock(&mControlDevice);
00321 mSubsystemID = id;
00322 if(mPrimitiveDriverID.IsValid())
00323 {
00324
00325 if(mPrimitiveDriverID.mSubsystem != mSubsystemID)
00326 {
00327 if(mTakeDriveControlFlag == true &&
00328 GetComponent()->AccessControlService()->HaveControl(mPrimitiveDriverID))
00329 {
00330 ReleaseComponentControl(mPrimitiveDriverID, true);
00331 }
00332 mPrimitiveDriverID.Clear();
00333 }
00334 }
00335 if(mMicrocontrollerID.IsValid())
00336 {
00337
00338 if(mMicrocontrollerID.mSubsystem != mSubsystemID)
00339 {
00340 if(mTakeDriveControlFlag == true &&
00341 GetComponent()->AccessControlService()->HaveControl(mMicrocontrollerID))
00342 {
00343 ReleaseComponentControl(mMicrocontrollerID, true);
00344 }
00345 mMicrocontrollerID.Clear();
00346 }
00347 }
00348
00349 if(GetComponent()->DiscoveryService()->IsEnabled() == false)
00350 {
00351 GetComponent()->DiscoveryService()->EnableService(true);
00352 }
00353 }
00354
00355
00364 void ControlDevice::TakeDriveControl(const bool enable)
00365 {
00366 Mutex::ScopedLock lock(&mControlDevice);
00367 mTakeDriveControlFlag = enable;
00368 }
00369
00370
00378 bool ControlDevice::HaveDriveControl() const
00379 {
00380 Mutex::ScopedLock lock(&mControlDevice);
00381 if(mPrimitiveDriverID.IsValid() &&
00382 GetComponent()->AccessControlService()->HaveControl(mPrimitiveDriverID))
00383 {
00384 return true;
00385 }
00386 return false;
00387 }
00388
00389
00390
00403 void ControlDevice::SignalButtonPress(const int buttonNumber)
00404 {
00405 Mutex::ScopedLock lock(&mControlDevice);
00406 std::map<int, ButtonActions>::iterator b;
00407 b = mButtonMapping.find(buttonNumber);
00408 if(b != mButtonMapping.end())
00409 {
00410 switch(b->second)
00411 {
00412 case RequestDriveControl:
00413 mTakeDriveControlFlag = true;
00414 break;
00415 case ReleaseDriveControl:
00416 mTakeDriveControlFlag = false;
00417 if(mPrimitiveDriverID.IsValid())
00418 {
00419 ReleaseComponentControl(mPrimitiveDriverID, true);
00420 }
00421 if(mMicrocontrollerID.IsValid())
00422 {
00423 ReleaseComponentControl(mMicrocontrollerID, true);
00424 }
00425 break;
00426 default:
00427 break;
00428 }
00429 }
00430
00431 std::map<int, std::string>::iterator digital;
00432 digital = mButtonDigitalMapping.begin();
00433 if(mMicrocontrollerID.IsValid() &&
00434 digital != mButtonDigitalMapping.end())
00435 {
00436 SetMicrocontrollerState command(mMicrocontrollerID, GetComponentID());
00437 (*command.GetDigitalStates())[digital->second] = mDigitalButtonState[digital->second];
00438 Send(&command);
00439 }
00440 }
00441
00442
00455 void ControlDevice::SignalButtonRelease(const int buttonNumber)
00456 {
00457 Mutex::ScopedLock lock(&mControlDevice);
00458 std::map<int, ButtonActions>::iterator b;
00459 b = mButtonMapping.find(buttonNumber);
00460 if(b != mButtonMapping.end())
00461 {
00462
00463 }
00464
00465 std::map<int, std::string>::iterator digital;
00466 digital = mButtonDigitalMapping.begin();
00467 if(mMicrocontrollerID.IsValid() &&
00468 digital != mButtonDigitalMapping.end())
00469 {
00470 SetMicrocontrollerState command(mMicrocontrollerID, GetComponentID());
00471 (*command.GetDigitalStates())[digital->second] = !mDigitalButtonState[digital->second];
00472 Send(&command);
00473 }
00474 }
00475
00476
00493 void ControlDevice::UpdateWrench(const int inputID, const double percentEffort)
00494 {
00495 Mutex::ScopedLock lock(&mControlDevice);
00496 std::map<int, WrenchEffort>::iterator w;
00497 w = mWrenchMapping.find(inputID);
00498 if(w != mWrenchMapping.end())
00499 {
00500 double value = 0.0;
00501 std::map<int, double>::iterator scale = mLimitsMapping.find(inputID);
00502 std::map<int, double>::iterator deadzone = mDeadzoneMapping.find(inputID);
00503
00504
00505 if(deadzone != mDeadzoneMapping.end())
00506 {
00507 if(fabs(percentEffort) > deadzone->second)
00508 {
00509 value = (fabs(percentEffort) - deadzone->second)*100.0/(100.0 - deadzone->second);
00510 if(percentEffort < 0)
00511 {
00512 value *= -1;
00513 }
00514 }
00515 else
00516 {
00517 value = 0.0;
00518 }
00519 }
00520
00521
00522 value = value*scale->second/100.0;
00523
00524
00525 std::map<int, bool>::iterator invert = mInvertMapping.find(inputID);
00526 if(invert != mInvertMapping.end())
00527 {
00528 if(invert->second)
00529 {
00530 value *= -1;
00531 }
00532 }
00533
00534
00535 switch(w->second)
00536 {
00537 case PropulsiveLinearEffortX:
00538 mWrenchEffort.SetPropulsiveLinearEffortX(value);
00539 if(fabs(value) < 0.001 && mAutoBrakingFlag)
00540 {
00541 mWrenchEffort.SetResistiveLinearEffortX(100.0);
00542 }
00543 else if(mAutoBrakingFlag)
00544 {
00545 mWrenchEffort.SetResistiveLinearEffortX(0.0);
00546 }
00547 break;
00548 case PropulsiveLinearEffortY:
00549 mWrenchEffort.SetPropulsiveLinearEffortY(value);
00550 if(fabs(value) < 0.001 && mAutoBrakingFlag)
00551 {
00552 mWrenchEffort.SetResistiveLinearEffortY(100.0);
00553 }
00554 else if(mAutoBrakingFlag)
00555 {
00556 mWrenchEffort.SetResistiveLinearEffortY(0.0);
00557 }
00558 break;
00559 case PropulsiveLinearEffortZ:
00560 mWrenchEffort.SetPropulsiveLinearEffortZ(value);
00561 if(fabs(value) < 0.001 && mAutoBrakingFlag)
00562 {
00563 mWrenchEffort.SetResistiveLinearEffortZ(100.0);
00564 }
00565 else if(mAutoBrakingFlag)
00566 {
00567 mWrenchEffort.SetResistiveLinearEffortZ(0.0);
00568 }
00569 break;
00570 case PropulsiveRotationalEffortX:
00571 mWrenchEffort.SetPropulsiveRotationalEffortX(value);
00572 break;
00573 case PropulsiveRotationalEffortY:
00574 mWrenchEffort.SetPropulsiveRotationalEffortY(value);
00575 break;
00576 case PropulsiveRotationalEffortZ:
00577 mWrenchEffort.SetPropulsiveRotationalEffortZ(value);
00578 break;
00579 case ResistiveLinearEffortX:
00580 mWrenchEffort.SetResistiveLinearEffortX(value);
00581 break;
00582 case ResistiveLinearEffortY:
00583 mWrenchEffort.SetResistiveLinearEffortY(value);
00584 break;
00585 case ResistiveLinearEffortZ:
00586 mWrenchEffort.SetResistiveLinearEffortZ(value);
00587 break;
00588 case ResistiveRotationalEffortX:
00589 mWrenchEffort.SetResistiveRotationalEffortX(value);
00590 break;
00591 case ResistiveRotationalEffortY:
00592 mWrenchEffort.SetResistiveRotationalEffortY(value);
00593 break;
00594 case ResistiveRotationalEffortZ:
00595 mWrenchEffort.SetResistiveRotationalEffortZ(value);
00596 break;
00597 default:
00598 break;
00599 }
00600 }
00601 }
00602
00603
00615 void ControlDevice::UpdateAnalogOut(const int inputID, const double signalValue)
00616 {
00617 Mutex::ScopedLock lock(&mControlDevice);
00618 std::map<int, std::string>::iterator a;
00619 a = mAnalogMapping.find(inputID);
00620 if(a != mAnalogMapping.end())
00621 {
00622 double value = 0.0;
00623 std::map<std::string, double>::iterator scale = mAnalogLimitsMapping.find(a->second);
00624
00625
00626 value = signalValue*scale->second/100.0;
00627
00628
00629 std::map<std::string, bool>::iterator invert = mAnalogInvertMapping.find(a->second);
00630 if(invert != mAnalogInvertMapping.end())
00631 {
00632 if(invert->second)
00633 {
00634 value *= -1;
00635 }
00636 }
00637
00638 (*mMicrocontrollerState.GetAnalogStates())[a->second] = value;
00639 }
00640 }
00641
00642
00649 void ControlDevice::SendWrenchEffort()
00650 {
00651 Mutex::ScopedLock lock(&mControlDevice);
00652 if(mPrimitiveDriverID.IsValid() && mTakeDriveControlFlag)
00653 {
00654
00655 if(!GetComponent()->AccessControlService()->HaveControl(mPrimitiveDriverID))
00656 {
00657 GetComponent()->AccessControlService()->RequestComponentControl(mPrimitiveDriverID, true);
00658 }
00659 if(mPrimitiveDriverID.IsValid() &&
00660 GetComponent()->AccessControlService()->HaveControl(mPrimitiveDriverID))
00661 {
00662 mWrenchEffort.SetSourceID(GetComponentID());
00663 mWrenchEffort.SetDestinationID(mPrimitiveDriverID);
00664 Send(&mWrenchEffort);
00665 }
00666 }
00667 }
00668
00669
00676 void ControlDevice::SendMicrocontrollerState()
00677 {
00678 Mutex::ScopedLock lock(&mControlDevice);
00679 if(mMicrocontrollerID.IsValid() && mTakeDriveControlFlag)
00680 {
00681
00682 if(!GetComponent()->AccessControlService()->HaveControl(mMicrocontrollerID))
00683 {
00684 GetComponent()->AccessControlService()->RequestComponentControl(mMicrocontrollerID, true);
00685 }
00686 if(mMicrocontrollerID.IsValid() &&
00687 GetComponent()->AccessControlService()->HaveControl(mMicrocontrollerID))
00688 {
00689 mMicrocontrollerState.SetSourceID(GetComponentID());
00690 mMicrocontrollerState.SetDestinationID(mMicrocontrollerID);
00691 Send(&mMicrocontrollerState);
00692 }
00693 }
00694 }
00695
00696
00702 void ControlDevice::ClearWrenchEffort()
00703 {
00704 Mutex::ScopedLock lock(&mControlDevice);
00705 mWrenchEffort.ClearMessage();
00706 }
00707
00708
00714 void ControlDevice::ClearMicrocontrollState()
00715 {
00716 Mutex::ScopedLock lock(&mControlDevice);
00717 mMicrocontrollerState.ClearMessage();
00718 }
00719
00720
00729 void ControlDevice::MapButtonToAction(const int buttonNumber,
00730 const ButtonActions action)
00731 {
00732 Mutex::ScopedLock lock(&mControlDevice);
00733 mButtonMapping[buttonNumber] = action;
00734 }
00735
00736
00747 void ControlDevice::MapButtonToDigitalOut(const int buttonNumber,
00748 const std::string& digital,
00749 const bool onPress)
00750 {
00751 Mutex::ScopedLock lock(&mControlDevice);
00752 mButtonDigitalMapping[buttonNumber] = digital;
00753 mDigitalButtonState[digital] = onPress;
00754 }
00755
00756
00778 void ControlDevice::MapInputToWrench(const int inputID,
00779 const WrenchEffort wrenchID,
00780 const double deadzone,
00781 const bool invert,
00782 const double limit)
00783 {
00784 Mutex::ScopedLock lock(&mControlDevice);
00785 mWrenchMapping[inputID] = wrenchID;
00786 if(fabs(deadzone) < 100)
00787 {
00788 mDeadzoneMapping[inputID] = fabs(deadzone);
00789 }
00790 mInvertMapping[inputID] = invert;
00791 if(fabs(limit) > 0 && fabs(limit) <= 100.0)
00792 {
00793 mLimitsMapping[inputID] = fabs(limit);
00794 }
00795 }
00796
00797
00809 void ControlDevice::MapInputToAnalogOut(const int inputID,
00810 const std::string& analog,
00811 const bool invert,
00812 const double limit)
00813 {
00814 Mutex::ScopedLock lock(&mControlDevice);
00815 mAnalogMapping[inputID] = analog;
00816 mAnalogInvertMapping[analog] = invert;
00817 if(fabs(limit) > 0 && fabs(limit) <= 100.0)
00818 {
00819 mAnalogLimitsMapping[analog] = fabs(limit);
00820 }
00821 }
00822
00823
00831 void ControlDevice::CheckServiceStatus(const unsigned int timeSinceLastCheckMs)
00832 {
00833
00834 Mutex::ScopedLock lock(&mControlDevice);
00835 if(mTakeDriveControlFlag && mSubsystemID != 0)
00836 {
00837 if(mPrimitiveDriverID.IsValid() == false)
00838 {
00839
00840 GetComponent()->DiscoveryService()->GetSubsystem(mSubsystemID)->HaveService(PrimitiveDriver::Name,
00841 &mPrimitiveDriverID);
00842 }
00843 if(mMicrocontrollerID.IsValid() == false)
00844 {
00845
00846 GetComponent()->DiscoveryService()->GetSubsystem(mSubsystemID)->HaveService(Microcontroller::Name,
00847 &mMicrocontrollerID);
00848 }
00849 }
00850 if(mTakeDriveControlFlag == false)
00851 {
00852 if(mPrimitiveDriverID.IsValid() && GetComponent()->AccessControlService()->HaveControl(mPrimitiveDriverID))
00853 {
00854 ReleaseComponentControl(mPrimitiveDriverID, true);
00855 }
00856 if(mMicrocontrollerID.IsValid() && GetComponent()->AccessControlService()->HaveControl(mMicrocontrollerID))
00857 {
00858 ReleaseComponentControl(mMicrocontrollerID, true);
00859 }
00860 }
00861 }
00862
00863
00877 void ControlDevice::ProcessAcquisitionOfControl(const Address& controlledComponent)
00878 {
00879 Mutex::ScopedLock lock(&mControlDevice);
00880
00881
00882 if(mTakeDriveControlFlag && controlledComponent == mPrimitiveDriverID)
00883 {
00884 GetComponent()->ManagementService()->Resume(mPrimitiveDriverID);
00885 }
00886 if(mTakeDriveControlFlag && controlledComponent == mMicrocontrollerID)
00887 {
00888 GetComponent()->ManagementService()->Resume(mMicrocontrollerID);
00889 }
00890 }
00891
00892
00899 void ControlDevice::Receive(const Message* message)
00900 {
00901
00902 }
00903
00904
00916 Message* ControlDevice::CreateMessage(const UShort messageCode) const
00917 {
00918 Message* message = NULL;
00919
00920
00921
00922
00923
00924
00925
00926 return message;
00927 }
00928
00929
00935 void ControlDevice::PrintStatus() const
00936 {
00937 Mutex::ScopedLock lock(&mControlDevice);
00938 if(mPrimitiveDriverID.IsValid() && GetComponent()->AccessControlService()->HaveControl(mPrimitiveDriverID))
00939 {
00940 std::cout << "[Control Device] - Controlling Primitive Driver [" << mPrimitiveDriverID.ToString() << "]\n";
00941
00942 }
00943 std::cout << "[Control Device] - Current Control Wrench:\n";
00944 mWrenchEffort.PrintMessageBody();
00945 }
00946
00947