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
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "jaus/core/component.h"
00042 #include "jaus/mobility/drivers/localwaypointdriver.h"
00043
00044 using namespace JAUS;
00045
00046 const std::string LocalWaypointDriver::Name = "urn:jaus:jss:mobility:LocalWaypointDriver";
00047
00048
00054 LocalWaypointDriver::LocalWaypointDriver() : Management::Child(Service::ID(LocalWaypointDriver::Name),
00055 Service::ID(Management::Name))
00056 {
00057 mWaypointAchievedFlag = false;
00058 mpLocalPoseSensor = NULL;
00059 mpVelocityStateSensor = NULL;
00060 mLocalWaypointTime;
00061 }
00062
00063
00069 LocalWaypointDriver::~LocalWaypointDriver()
00070 {
00071 }
00072
00073
00079 bool LocalWaypointDriver::SetLocalWaypoint(const JAUS::SetLocalWaypoint *command)
00080 {
00081 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00082 mLocalWaypoint = *command;
00083 mLocalWaypoint.SetSourceID(GetComponentID());
00084 mWaypointAchievedFlag = false;
00085 return true;
00086 }
00087
00088
00096 bool LocalWaypointDriver::SetDesiredTravelSpeed(const double speed)
00097 {
00098 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00099 mTravelSpeedTime.SetCurrentTime();
00100 mDesiredTravelSpeed.SetSourceID(GetComponentID());
00101 mDesiredTravelSpeed.SetSpeed(speed);
00102 return true;
00103 }
00104
00105
00113 bool LocalWaypointDriver::SetDriverToControl(const Address& driver)
00114 {
00115
00116 if(driver == GetComponentID())
00117 {
00118 return false;
00119 }
00120
00121 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00122 mControlledDriverID = driver;
00123 return true;
00124 }
00125
00126
00134 SetLocalWaypoint LocalWaypointDriver::GetLocalWaypoint() const
00135 {
00136 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00137 return mLocalWaypoint;
00138 }
00139
00140
00148 LocalPose LocalWaypointDriver::GetLocalPose() const
00149 {
00150 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00151 return mLocalPose;
00152 }
00153
00154
00163 VelocityState LocalWaypointDriver::GetVelocityState() const
00164 {
00165 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00166 return mVelocityState;
00167 }
00168
00169
00177 SetTravelSpeed LocalWaypointDriver::GetDesiredTravelSpeed() const
00178 {
00179 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00180 return mDesiredTravelSpeed;
00181 }
00182
00183
00190 Time LocalWaypointDriver::GetLocalWaypointTime() const
00191 {
00192 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00193 return mLocalWaypointTime;
00194 }
00195
00196
00203 Time LocalWaypointDriver::GetTravelSpeedTime() const
00204 {
00205 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00206 return mTravelSpeedTime;
00207 }
00208
00209
00215 Address LocalWaypointDriver::GetControlledDriverID() const
00216 {
00217 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00218 return mControlledDriverID;
00219 }
00220
00221
00231 bool LocalWaypointDriver::GenerateEvent(const Events::Subscription& info) const
00232 {
00233 if(info.mpQueryMessage->GetMessageCode() == QUERY_LOCAL_WAYPOINT)
00234 {
00235 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00236 const QueryLocalWaypoint* query = dynamic_cast<const QueryLocalWaypoint*>(info.mpQueryMessage);
00237
00238 if(query == NULL)
00239 {
00240 return false;
00241 }
00242
00243 ReportLocalWaypoint report;
00244 CreateReportFromQuery(query, report);
00245 SendEvent(info, &report);
00246
00247 return true;
00248 }
00249 else if(info.mpQueryMessage->GetMessageCode() == QUERY_TRAVEL_SPEED)
00250 {
00251 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00252 const QueryTravelSpeed* query = dynamic_cast<const QueryTravelSpeed*>(info.mpQueryMessage);
00253
00254 if(query == NULL)
00255 {
00256 return false;
00257 }
00258
00259 ReportTravelSpeed report;
00260 CreateReportFromQuery(query, report);
00261 SendEvent(info, &report);
00262
00263 return true;
00264 }
00265
00266 return false;
00267 }
00268
00269
00285 bool LocalWaypointDriver::IsEventSupported(const Events::Type type,
00286 const double requestedPeriodicRate,
00287 const Message* queryMessage,
00288 double& confirmedPeriodicRate,
00289 std::string& errorMessage) const
00290 {
00291
00292 if(queryMessage->GetMessageCode() == QUERY_LOCAL_WAYPOINT)
00293 {
00294 confirmedPeriodicRate = requestedPeriodicRate;
00295 return true;
00296 }
00297
00298 else if(queryMessage->GetMessageCode() == QUERY_TRAVEL_SPEED)
00299 {
00300 confirmedPeriodicRate = requestedPeriodicRate;
00301 return true;
00302 }
00303 return false;
00304 }
00305
00306
00318 void LocalWaypointDriver::Receive(const Message *message)
00319 {
00320 switch(message->GetMessageCode())
00321 {
00322 case QUERY_LOCAL_WAYPOINT:
00323 {
00324 const JAUS::QueryLocalWaypoint* query = dynamic_cast<const JAUS::QueryLocalWaypoint*>(message);
00325 if(query)
00326 {
00327 ReportLocalWaypoint report;
00328 CreateReportFromQuery(query, report);
00329 Send(&report);
00330 }
00331 }
00332 break;
00333 case QUERY_TRAVEL_SPEED:
00334 {
00335 const JAUS::QueryTravelSpeed* query = dynamic_cast<const JAUS::QueryTravelSpeed*>(message);
00336 if(query)
00337 {
00338 ReportTravelSpeed report;
00339 CreateReportFromQuery(query, report);
00340 Send(&report);
00341 }
00342 }
00343 break;
00344 case SET_LOCAL_WAYPOINT:
00345 {
00346 const JAUS::SetLocalWaypoint* command = dynamic_cast<const JAUS::SetLocalWaypoint*>(message);
00347 if(command)
00348 {
00349 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00350 if(SetLocalWaypoint(command))
00351 {
00352 mLocalWaypoint = *command;
00353 mLocalWaypointTime.SetCurrentTime();
00354 SignalEvent(REPORT_LOCAL_WAYPOINT);
00355 }
00356 }
00357 }
00358 break;
00359 case SET_TRAVEL_SPEED:
00360 {
00361 const JAUS::SetTravelSpeed* command = dynamic_cast<const JAUS::SetTravelSpeed*>(message);
00362 if(command)
00363 {
00364 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00365 if(SetDesiredTravelSpeed(command))
00366 {
00367 mDesiredTravelSpeed = *command;
00368 mTravelSpeedTime.SetCurrentTime();
00369 SignalEvent(REPORT_TRAVEL_SPEED);
00370 }
00371 }
00372 }
00373 break;
00374 default:
00375 break;
00376 }
00377 }
00378
00379
00391 Message* LocalWaypointDriver::CreateMessage(const UShort messageCode) const
00392 {
00393 Message* message = NULL;
00394 switch(messageCode)
00395 {
00396 case QUERY_LOCAL_WAYPOINT:
00397 message = new JAUS::QueryLocalWaypoint();
00398 break;
00399 case QUERY_TRAVEL_SPEED:
00400 message = new JAUS::QueryTravelSpeed();
00401 break;
00402 case REPORT_LOCAL_WAYPOINT:
00403 message = new JAUS::ReportLocalWaypoint();
00404 break;
00405 case REPORT_TRAVEL_SPEED:
00406 message = new JAUS::ReportTravelSpeed();
00407 break;
00408 case SET_LOCAL_WAYPOINT:
00409 message = new JAUS::SetLocalWaypoint();
00410 break;
00411 case SET_TRAVEL_SPEED:
00412 message = new JAUS::SetTravelSpeed();
00413 break;
00414 default:
00415 message = NULL;
00416 break;
00417 };
00418 return message;
00419 }
00420
00421
00427 void LocalWaypointDriver::PrintStatus() const
00428 {
00429 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00430 if(mLocalWaypoint.GetSourceID().IsValid())
00431 {
00432 std::cout << "[" << GetServiceID().ToString() << "] - Driving to Waypoint:\n";
00433 mLocalWaypoint.PrintMessageBody();
00434 if(mDesiredTravelSpeed.GetSourceID().IsValid())
00435 {
00436 mDesiredTravelSpeed.PrintMessageBody();
00437 }
00438 }
00439 else
00440 {
00441 std::cout << "[" << GetServiceID().ToString() << "] - Idle\n";
00442 }
00443 }
00444
00445
00452 bool LocalWaypointDriver::Resume()
00453 {
00454 return (CheckDriver() && CheckSensors());
00455 }
00456
00457
00464 bool LocalWaypointDriver::Reset()
00465 {
00466 bool success = ReleaseResources();
00467
00468 mWaypointAchievedFlag = false;
00469 mControlledDriverID.Clear();
00470 mpLocalPoseSensor = NULL;
00471 mpVelocityStateSensor = NULL;
00472 mLocalWaypointTime.Clear();
00473 mLocalWaypoint.ClearMessage();
00474 mDesiredTravelSpeed.ClearMessage();
00475
00476 return success;
00477 }
00478
00479
00486 bool LocalWaypointDriver::Standby()
00487 {
00488 return ReleaseResources();
00489 }
00490
00491
00499 bool LocalWaypointDriver::SetEmergency()
00500 {
00501 bool success = true;
00502 Message* command = NULL;
00503 command = GenerateIdleDriveCommand(GetStatus());
00504 if(command)
00505 {
00506 success &= Send(command);
00507 }
00508 success &= ReleaseResources();
00509
00510 return success;
00511 }
00512
00513
00520 bool LocalWaypointDriver::ClearEmergency()
00521 {
00522 return Resume();
00523 }
00524
00525
00532 bool LocalWaypointDriver::ReleaseControl()
00533 {
00534 return true;
00535 }
00536
00537
00544 bool LocalWaypointDriver::ReleaseResources()
00545 {
00546 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00547
00548 if(GetComponent()->AccessControlService()->HaveControl(mControlledDriverID))
00549 {
00550 GetComponent()->AccessControlService()->ReleaseComponentControl(mControlledDriverID);
00551 }
00552
00553 return true;
00554 }
00555
00556
00570 void LocalWaypointDriver::CheckServiceStatus(const unsigned int timeSinceLastCheckMs)
00571 {
00572 Byte status = GetComponent()->ManagementService()->GetStatus();
00573 switch(status)
00574 {
00575 case Management::Status::Ready:
00576 {
00577 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00578
00579 if(CheckDriver())
00580 {
00581 Message *command = NULL;
00582
00583 if(CheckSensors() &&
00584 mLocalWaypoint.GetSourceID().IsValid())
00585 {
00586
00587 if(IsWaypointAchieved(mLocalPose, mLocalWaypoint))
00588 {
00589 if(!mWaypointAchievedFlag)
00590 {
00591 mWaypointAchievedFlag = true;
00592 WaypointAchieved(mLocalWaypoint);
00593
00594
00595 mLocalWaypoint.ClearMessage();
00596 }
00597
00598 command = GenerateIdleDriveCommand(status);
00599 }
00600 else
00601 {
00602 command = GenerateDriveCommand(status);
00603 }
00604 }
00605 else
00606 {
00607 command = GenerateIdleDriveCommand(status);
00608 }
00609
00610 if(command)
00611 {
00612 command->SetDestinationID(mControlledDriverID);
00613 command->SetSourceID(this->GetComponentID());
00614 Send(command);
00615 delete command;
00616 }
00617 }
00618 }
00619 break;
00620 case Management::Status::Standby:
00621
00622 break;
00623 default:
00624 break;
00625 };
00626
00627 WaypointDriverUpdateEvent(timeSinceLastCheckMs);
00628 }
00629
00630
00639 void LocalWaypointDriver::CreateReportFromQuery(const QueryLocalWaypoint* query,
00640 ReportLocalWaypoint& report) const
00641 {
00642 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00643 report.ClearMessage();
00644 report.SetDestinationID(query->GetSourceID());
00645 report.SetSourceID(GetComponentID());
00646 UInt pv1 = query->GetPresenceVector();
00647 UInt pv2 = mLocalWaypoint.GetPresenceVector();
00648
00649 report.SetX(mLocalWaypoint.GetX());
00650 report.SetY(mLocalWaypoint.GetY());
00651 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Z)) > 0) { report.SetZ(mLocalWaypoint.GetZ()); }
00652 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Roll)) > 0) { report.SetRoll(mLocalWaypoint.GetRoll()); }
00653 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Pitch)) > 0) { report.SetPitch(mLocalWaypoint.GetPitch()); }
00654 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Yaw)) > 0) { report.SetYaw(mLocalWaypoint.GetYaw()); }
00655 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::WaypointTolerance)) > 0) { report.SetWaypointTolerance(mLocalWaypoint.GetWaypointTolerance()); }
00656 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::PathTolerance)) > 0) { report.SetPathTolerance(mLocalWaypoint.GetPathTolerance()); }
00657 }
00658
00659
00668 void LocalWaypointDriver::CreateReportFromQuery(const QueryTravelSpeed* query,
00669 ReportTravelSpeed& report) const
00670 {
00671 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00672 report.ClearMessage();
00673 report.SetDestinationID(query->GetSourceID());
00674 report.SetSourceID(GetComponentID());
00675 report.SetSpeed(mDesiredTravelSpeed.GetSpeed());
00676 }
00677
00678
00686 bool LocalWaypointDriver::CheckDriver()
00687 {
00688 bool success = false;
00689 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00690
00691
00692 if(GetComponent()->AccessControlService()->HaveControl(mControlledDriverID))
00693 {
00694 success = true;
00695 }
00696 else
00697 {
00698 if(GetComponent()->AccessControlService()->RequestComponentControl(mControlledDriverID, false))
00699 {
00700 success = GetComponent()->ManagementService()->Resume(mControlledDriverID);
00701 }
00702 }
00703
00704 return success;
00705 }
00706
00707
00717 bool LocalWaypointDriver::CheckSensors()
00718 {
00719 bool success = true;
00720 Mutex::ScopedLock lock(&mLocalWaypointDriverMutex);
00721
00722 if(mpLocalPoseSensor == NULL)
00723 {
00724 mpLocalPoseSensor = dynamic_cast<LocalPoseSensor*>(GetComponent()->GetService(LocalPoseSensor::Name));
00725 }
00726
00727 if(mpLocalPoseSensor)
00728 {
00729 mLocalPose = mpLocalPoseSensor->GetLocalPose();
00730 if(mLocalPose.AreFieldsPresent(ReportLocalPose::PresenceVector::X |
00731 ReportLocalPose::PresenceVector::Y ) == false)
00732 {
00733 success = false;
00734 }
00735 }
00736 else
00737 {
00738 success = false;
00739 }
00740
00741 if(mpVelocityStateSensor == NULL)
00742 {
00743 mpVelocityStateSensor = dynamic_cast<VelocityStateSensor*>(GetComponent()->GetService(VelocityStateSensor::Name));
00744 }
00745
00746 if(mpVelocityStateSensor)
00747 {
00748 mVelocityState = mpVelocityStateSensor->GetVelocityState();
00749 }
00750 else
00751 {
00752 success = false;
00753 }
00754
00755 return success;
00756 }
00757
00758
00759