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
00042 #include "jaus/core/component.h"
00043 #include "jaus/mobility/drivers/globalwaypointdriver.h"
00044
00045 using namespace JAUS;
00046
00047 const std::string GlobalWaypointDriver::Name = "urn:jaus:jss:mobility:GlobalWaypointDriver";
00048
00049
00055 GlobalWaypointDriver::GlobalWaypointDriver() : Management::Child(Service::ID(GlobalWaypointDriver::Name),
00056 Service::ID(Management::Name))
00057 {
00058 mWaypointAchievedFlag = false;
00059 mpGlobalPoseSensor = NULL;
00060 mpVelocityStateSensor = NULL;
00061 mGlobalWaypointTime;
00062 }
00063
00064
00070 GlobalWaypointDriver::~GlobalWaypointDriver()
00071 {
00072 }
00073
00074
00080 bool GlobalWaypointDriver::SetGlobalWaypoint(const JAUS::SetGlobalWaypoint *command)
00081 {
00082 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00083 mGlobalWaypoint = *command;
00084 mGlobalWaypoint.SetSourceID(GetComponentID());
00085 mWaypointAchievedFlag = false;
00086 return true;
00087 }
00088
00089
00097 bool GlobalWaypointDriver::SetDesiredTravelSpeed(const double speed)
00098 {
00099 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00100 mTravelSpeedTime.SetCurrentTime();
00101 mDesiredTravelSpeed.SetSourceID(GetComponentID());
00102 mDesiredTravelSpeed.SetSpeed(speed);
00103 return true;
00104 }
00105
00106
00114 bool GlobalWaypointDriver::SetDriverToControl(const Address& driver)
00115 {
00116
00117 if(driver == GetComponentID())
00118 {
00119 return false;
00120 }
00121
00122 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00123 mControlledDriverID = driver;
00124 return true;
00125 }
00126
00127
00135 SetGlobalWaypoint GlobalWaypointDriver::GetGlobalWaypoint() const
00136 {
00137 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00138 return mGlobalWaypoint;
00139 }
00140
00141
00149 GlobalPose GlobalWaypointDriver::GetGlobalPose() const
00150 {
00151 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00152 return mGlobalPose;
00153 }
00154
00155
00164 VelocityState GlobalWaypointDriver::GetVelocityState() const
00165 {
00166 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00167 return mVelocityState;
00168 }
00169
00170
00178 SetTravelSpeed GlobalWaypointDriver::GetDesiredTravelSpeed() const
00179 {
00180 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00181 return mDesiredTravelSpeed;
00182 }
00183
00184
00191 Time GlobalWaypointDriver::GetGlobalWaypointTime() const
00192 {
00193 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00194 return mGlobalWaypointTime;
00195 }
00196
00197
00204 Time GlobalWaypointDriver::GetTravelSpeedTime() const
00205 {
00206 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00207 return mTravelSpeedTime;
00208 }
00209
00210
00216 Address GlobalWaypointDriver::GetControlledDriverID() const
00217 {
00218 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00219 return mControlledDriverID;
00220 }
00221
00222
00232 bool GlobalWaypointDriver::GenerateEvent(const Events::Subscription& info) const
00233 {
00234 if(info.mpQueryMessage->GetMessageCode() == QUERY_GLOBAL_WAYPOINT)
00235 {
00236 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00237 const QueryGlobalWaypoint* query = dynamic_cast<const QueryGlobalWaypoint*>(info.mpQueryMessage);
00238
00239 if(query == NULL)
00240 {
00241 return false;
00242 }
00243
00244 ReportGlobalWaypoint report;
00245 CreateReportFromQuery(query, report);
00246 SendEvent(info, &report);
00247
00248 return true;
00249 }
00250 else if(info.mpQueryMessage->GetMessageCode() == QUERY_TRAVEL_SPEED)
00251 {
00252 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00253 const QueryTravelSpeed* query = dynamic_cast<const QueryTravelSpeed*>(info.mpQueryMessage);
00254
00255 if(query == NULL)
00256 {
00257 return false;
00258 }
00259
00260 ReportTravelSpeed report;
00261 CreateReportFromQuery(query, report);
00262 SendEvent(info, &report);
00263
00264 return true;
00265 }
00266
00267 return false;
00268 }
00269
00270
00286 bool GlobalWaypointDriver::IsEventSupported(const Events::Type type,
00287 const double requestedPeriodicRate,
00288 const Message* queryMessage,
00289 double& confirmedPeriodicRate,
00290 std::string& errorMessage) const
00291 {
00292
00293 if(queryMessage->GetMessageCode() == QUERY_GLOBAL_WAYPOINT)
00294 {
00295 confirmedPeriodicRate = requestedPeriodicRate;
00296 return true;
00297 }
00298
00299 else if(queryMessage->GetMessageCode() == QUERY_TRAVEL_SPEED)
00300 {
00301 confirmedPeriodicRate = requestedPeriodicRate;
00302 return true;
00303 }
00304 return false;
00305 }
00306
00307
00319 void GlobalWaypointDriver::Receive(const Message *message)
00320 {
00321 switch(message->GetMessageCode())
00322 {
00323 case QUERY_GLOBAL_WAYPOINT:
00324 {
00325 const JAUS::QueryGlobalWaypoint* query = dynamic_cast<const JAUS::QueryGlobalWaypoint*>(message);
00326 if(query)
00327 {
00328 ReportGlobalWaypoint report;
00329 CreateReportFromQuery(query, report);
00330 Send(&report);
00331 }
00332 }
00333 break;
00334 case QUERY_TRAVEL_SPEED:
00335 {
00336 const JAUS::QueryTravelSpeed* query = dynamic_cast<const JAUS::QueryTravelSpeed*>(message);
00337 if(query)
00338 {
00339 ReportTravelSpeed report;
00340 CreateReportFromQuery(query, report);
00341 Send(&report);
00342 }
00343 }
00344 break;
00345 case SET_GLOBAL_WAYPOINT:
00346 {
00347 const JAUS::SetGlobalWaypoint* command = dynamic_cast<const JAUS::SetGlobalWaypoint*>(message);
00348 if(command)
00349 {
00350 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00351 if(SetGlobalWaypoint(command))
00352 {
00353 mGlobalWaypoint = *command;
00354 mGlobalWaypointTime.SetCurrentTime();
00355 SignalEvent(REPORT_GLOBAL_WAYPOINT);
00356 }
00357 }
00358 }
00359 break;
00360 case SET_TRAVEL_SPEED:
00361 {
00362 const JAUS::SetTravelSpeed* command = dynamic_cast<const JAUS::SetTravelSpeed*>(message);
00363 if(command)
00364 {
00365 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00366 if(SetDesiredTravelSpeed(command))
00367 {
00368 mDesiredTravelSpeed = *command;
00369 mTravelSpeedTime.SetCurrentTime();
00370 SignalEvent(REPORT_TRAVEL_SPEED);
00371 }
00372 }
00373 }
00374 break;
00375 default:
00376 break;
00377 }
00378 }
00379
00380
00392 Message* GlobalWaypointDriver::CreateMessage(const UShort messageCode) const
00393 {
00394 Message* message = NULL;
00395 switch(messageCode)
00396 {
00397 case QUERY_GLOBAL_WAYPOINT:
00398 message = new JAUS::QueryGlobalWaypoint();
00399 break;
00400 case QUERY_TRAVEL_SPEED:
00401 message = new JAUS::QueryTravelSpeed();
00402 break;
00403 case REPORT_GLOBAL_WAYPOINT:
00404 message = new JAUS::ReportGlobalWaypoint();
00405 break;
00406 case REPORT_TRAVEL_SPEED:
00407 message = new JAUS::ReportTravelSpeed();
00408 break;
00409 case SET_GLOBAL_WAYPOINT:
00410 message = new JAUS::SetGlobalWaypoint();
00411 break;
00412 case SET_TRAVEL_SPEED:
00413 message = new JAUS::SetTravelSpeed();
00414 break;
00415 default:
00416 message = NULL;
00417 break;
00418 };
00419 return message;
00420 }
00421
00422
00428 void GlobalWaypointDriver::PrintStatus() const
00429 {
00430
00431
00432 if(mGlobalWaypoint.GetSourceID().IsValid())
00433 {
00434 std::cout << "[" << GetServiceID().ToString() << "] - Driving to Waypoint:\n";
00435
00436 mGlobalWaypointDriverMutex.Lock();
00437 JAUS::SetGlobalWaypoint wp = mGlobalWaypoint;
00438 JAUS::SetTravelSpeed speed = mDesiredTravelSpeed;
00439 mGlobalWaypointDriverMutex.Unlock();
00440
00441 wp.PrintMessageBody();
00442 if(speed.GetSourceID().IsValid())
00443 {
00444 speed.PrintMessageBody();
00445 }
00446 }
00447 else
00448 {
00449 std::cout << "[" << GetServiceID().ToString() << "] - Idle\n";
00450 }
00451
00452 }
00453
00454
00461 bool GlobalWaypointDriver::Resume()
00462 {
00463 return (CheckDriver() && CheckSensors());
00464 }
00465
00466
00473 bool GlobalWaypointDriver::Reset()
00474 {
00475 bool success = ReleaseResources();
00476
00477 mWaypointAchievedFlag = false;
00478 mControlledDriverID.Clear();
00479 mpGlobalPoseSensor = NULL;
00480 mpVelocityStateSensor = NULL;
00481 mGlobalWaypointTime.Clear();
00482 mGlobalWaypoint.ClearMessage();
00483 mDesiredTravelSpeed.ClearMessage();
00484
00485 return success;
00486 }
00487
00488
00495 bool GlobalWaypointDriver::Standby()
00496 {
00497 return ReleaseResources();
00498 }
00499
00500
00508 bool GlobalWaypointDriver::SetEmergency()
00509 {
00510 bool success = true;
00511 Message* command = NULL;
00512 command = GenerateIdleDriveCommand(GetStatus());
00513 if(command)
00514 {
00515 success &= Send(command);
00516 }
00517 success &= ReleaseResources();
00518
00519 return success;
00520 }
00521
00522
00529 bool GlobalWaypointDriver::ClearEmergency()
00530 {
00531 return Resume();
00532 }
00533
00534
00541 bool GlobalWaypointDriver::ReleaseControl()
00542 {
00543 return true;
00544 }
00545
00546
00553 bool GlobalWaypointDriver::ReleaseResources()
00554 {
00555 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00556
00557 if(GetComponent()->AccessControlService()->HaveControl(mControlledDriverID))
00558 {
00559 GetComponent()->AccessControlService()->ReleaseComponentControl(mControlledDriverID);
00560 }
00561
00562 return true;
00563 }
00564
00565
00579 void GlobalWaypointDriver::CheckServiceStatus(const unsigned int timeSinceLastCheckMs)
00580 {
00581 Byte status = GetComponent()->ManagementService()->GetStatus();
00582 switch(status)
00583 {
00584 case Management::Status::Ready:
00585 {
00586 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00587
00588 if(CheckDriver())
00589 {
00590 Message *command = NULL;
00591
00592 if(CheckSensors() &&
00593 mGlobalWaypoint.GetSourceID().IsValid())
00594 {
00595
00596 if(IsWaypointAchieved(mGlobalPose, mGlobalWaypoint))
00597 {
00598 if(!mWaypointAchievedFlag)
00599 {
00600 mWaypointAchievedFlag = true;
00601 WaypointAchieved(mGlobalWaypoint);
00602
00603
00604 mGlobalWaypoint.ClearMessage();
00605 }
00606
00607 command = GenerateIdleDriveCommand(status);
00608 }
00609 else
00610 {
00611 command = GenerateDriveCommand(status);
00612 }
00613 }
00614 else
00615 {
00616 command = GenerateIdleDriveCommand(status);
00617 }
00618
00619 if(command)
00620 {
00621 command->SetDestinationID(mControlledDriverID);
00622 command->SetSourceID(this->GetComponentID());
00623 Send(command);
00624 delete command;
00625 }
00626 }
00627 }
00628 break;
00629 case Management::Status::Standby:
00630
00631 break;
00632 default:
00633 break;
00634 };
00635
00636 WaypointDriverUpdateEvent(timeSinceLastCheckMs);
00637 }
00638
00639
00648 void GlobalWaypointDriver::CreateReportFromQuery(const QueryGlobalWaypoint* query,
00649 ReportGlobalWaypoint& report) const
00650 {
00651 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00652 report.ClearMessage();
00653 report.SetDestinationID(query->GetSourceID());
00654 report.SetSourceID(GetComponentID());
00655 UInt pv1 = query->GetPresenceVector();
00656 UInt pv2 = mGlobalWaypoint.GetPresenceVector();
00657
00658 report.SetLatitude(mGlobalWaypoint.GetLatitude());
00659 report.SetLongitude(mGlobalWaypoint.GetLongitude());
00660 if( (pv2 & (pv1 & ReportGlobalWaypoint::PresenceVector::Altitude)) > 0) { report.SetAltitude(mGlobalWaypoint.GetAltitude()); }
00661 if( (pv2 & (pv1 & ReportGlobalWaypoint::PresenceVector::Roll)) > 0) { report.SetRoll(mGlobalWaypoint.GetRoll()); }
00662 if( (pv2 & (pv1 & ReportGlobalWaypoint::PresenceVector::Pitch)) > 0) { report.SetPitch(mGlobalWaypoint.GetPitch()); }
00663 if( (pv2 & (pv1 & ReportGlobalWaypoint::PresenceVector::Yaw)) > 0) { report.SetYaw(mGlobalWaypoint.GetYaw()); }
00664 if( (pv2 & (pv1 & ReportGlobalWaypoint::PresenceVector::WaypointTolerance)) > 0) { report.SetWaypointTolerance(mGlobalWaypoint.GetWaypointTolerance()); }
00665 if( (pv2 & (pv1 & ReportGlobalWaypoint::PresenceVector::PathTolerance)) > 0) { report.SetPathTolerance(mGlobalWaypoint.GetPathTolerance()); }
00666 }
00667
00668
00677 void GlobalWaypointDriver::CreateReportFromQuery(const QueryTravelSpeed* query,
00678 ReportTravelSpeed& report) const
00679 {
00680 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00681 report.ClearMessage();
00682 report.SetDestinationID(query->GetSourceID());
00683 report.SetSourceID(GetComponentID());
00684 report.SetSpeed(mDesiredTravelSpeed.GetSpeed());
00685 }
00686
00687
00695 bool GlobalWaypointDriver::CheckDriver()
00696 {
00697 bool success = false;
00698 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00699
00700
00701 if(GetComponent()->AccessControlService()->HaveControl(mControlledDriverID))
00702 {
00703 success = true;
00704 }
00705 else
00706 {
00707 if(GetComponent()->AccessControlService()->RequestComponentControl(mControlledDriverID, false))
00708 {
00709 success = GetComponent()->ManagementService()->Resume(mControlledDriverID);
00710 }
00711 }
00712
00713 return success;
00714 }
00715
00716
00726 bool GlobalWaypointDriver::CheckSensors()
00727 {
00728 bool success = true;
00729 Mutex::ScopedLock lock(&mGlobalWaypointDriverMutex);
00730
00731 if(mpGlobalPoseSensor == NULL)
00732 {
00733 mpGlobalPoseSensor = dynamic_cast<GlobalPoseSensor*>(GetComponent()->GetService(GlobalPoseSensor::Name));
00734 }
00735
00736 if(mpGlobalPoseSensor)
00737 {
00738 mGlobalPose = mpGlobalPoseSensor->GetGlobalPose();
00739 if(mGlobalPose.AreFieldsPresent(ReportGlobalPose::PresenceVector::Latitude |
00740 ReportGlobalPose::PresenceVector::Longitude ) == false)
00741 {
00742 success = false;
00743 }
00744 }
00745 else
00746 {
00747 success = false;
00748 }
00749
00750 if(mpVelocityStateSensor == NULL)
00751 {
00752 mpVelocityStateSensor = dynamic_cast<VelocityStateSensor*>(GetComponent()->GetService(VelocityStateSensor::Name));
00753 }
00754
00755 if(mpVelocityStateSensor)
00756 {
00757 mVelocityState = mpVelocityStateSensor->GetVelocityState();
00758 }
00759 else
00760 {
00761 success = false;
00762 }
00763
00764 return success;
00765 }
00766
00767
00768