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/mobility/drivers/localwaypointlistdriver.h" 00042 #include "jaus/core/component.h" 00043 #include <iostream> 00044 #include <iomanip> 00045 00046 using namespace JAUS; 00047 00048 const std::string LocalWaypointListDriver::Name = "urn:jaus:jss:mobility:LocalWaypointListDriver"; 00049 00055 LocalWaypointListDriver::LocalWaypointListDriver() : ListDriver(Service::ID(LocalWaypointListDriver::Name), 00056 Service::ID(ListManager::Name)) 00057 { 00058 00059 } 00060 00061 00067 LocalWaypointListDriver::~LocalWaypointListDriver() 00068 { 00069 } 00070 00071 00082 void LocalWaypointListDriver::ExecuteList(const double speedMetersPerSecond) 00083 { 00084 Mutex::ScopedLock lock(&mListDriverMutex); 00085 if(speedMetersPerSecond > 0.0) 00086 { 00087 mSpeedMetersPerSecond = speedMetersPerSecond; 00088 } 00089 } 00090 00091 00102 bool LocalWaypointListDriver::IsElementSupported(const JAUS::Message* message) const 00103 { 00104 const JAUS::SetLocalWaypoint* command = dynamic_cast<const JAUS::SetLocalWaypoint*>(message); 00105 return command != NULL ? true : false; 00106 } 00107 00108 00120 void LocalWaypointListDriver::Receive(const Message* message) 00121 { 00122 switch(message->GetMessageCode()) 00123 { 00124 case QUERY_LOCAL_WAYPOINT: 00125 { 00126 Mutex::ScopedLock lock(&mListDriverMutex); 00127 const QueryLocalWaypoint* query = dynamic_cast<const QueryLocalWaypoint*>(message); 00128 00129 if(query == NULL) 00130 { 00131 return; 00132 } 00133 00134 ReportLocalWaypoint report(message->GetSourceID(), GetComponentID()); 00135 Element current = GetActiveListElement(); 00136 if(current.mpElement) 00137 { 00138 SetLocalWaypoint* wp = dynamic_cast<SetLocalWaypoint*>(current.mpElement); 00139 if(wp) 00140 { 00141 UInt pv1 = query->GetPresenceVector(); 00142 UInt pv2 = wp->GetPresenceVector(); 00143 00144 report.SetX(wp->GetX()); 00145 report.SetY(wp->GetY()); 00146 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Z)) > 0) { report.SetZ(wp->GetZ()); } 00147 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Roll)) > 0) { report.SetRoll(wp->GetRoll()); } 00148 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Pitch)) > 0) { report.SetPitch(wp->GetPitch()); } 00149 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Yaw)) > 0) { report.SetYaw(wp->GetYaw()); } 00150 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::WaypointTolerance)) > 0) { report.SetWaypointTolerance(wp->GetWaypointTolerance()); } 00151 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::PathTolerance)) > 0) { report.SetPathTolerance(wp->GetPathTolerance()); } 00152 } 00153 } 00154 Send(&report); 00155 } 00156 break; 00157 default: 00158 ListDriver::Receive(message); 00159 break; 00160 } 00161 } 00162 00163 00175 Message* LocalWaypointListDriver::CreateMessage(const UShort messageCode) const 00176 { 00177 switch(messageCode) 00178 { 00179 case SET_LOCAL_WAYPOINT: 00180 return new JAUS::SetLocalWaypoint(); 00181 break; 00182 case QUERY_LOCAL_WAYPOINT: 00183 return new QueryLocalWaypoint(); 00184 break; 00185 case REPORT_LOCAL_WAYPOINT: 00186 return new ReportLocalWaypoint(); 00187 break; 00188 default: 00189 return ListDriver::CreateMessage(messageCode); 00190 break; 00191 } 00192 return NULL; 00193 } 00194 00195 00196 00212 bool LocalWaypointListDriver::IsEventSupported(const Events::Type type, 00213 const double requestedPeriodicRate, 00214 const Message* queryMessage, 00215 double& confirmedPeriodicRate, 00216 std::string& errorMessage) const 00217 { 00218 // Support any type of event for QUERY_LOCAL_WAYPOINT data. 00219 if(queryMessage->GetMessageCode() == QUERY_LOCAL_WAYPOINT) 00220 { 00221 confirmedPeriodicRate = requestedPeriodicRate; 00222 return true; 00223 } 00224 // Support any type of event for QUERY_TRAVEL_SPEED data. 00225 else if(queryMessage->GetMessageCode() == QUERY_TRAVEL_SPEED) 00226 { 00227 confirmedPeriodicRate = requestedPeriodicRate; 00228 return true; 00229 } 00230 return false; 00231 } 00232 00233 00243 bool LocalWaypointListDriver::GenerateEvent(const Events::Subscription& info) const 00244 { 00245 if(info.mpQueryMessage->GetMessageCode() == QUERY_LOCAL_WAYPOINT) 00246 { 00247 const QueryLocalWaypoint* query = dynamic_cast<const QueryLocalWaypoint*>(info.mpQueryMessage); 00248 00249 if(query == NULL) 00250 { 00251 return false; 00252 } 00253 00254 ReportLocalWaypoint report; 00255 Element current = GetActiveListElement(); 00256 if(current.mpElement) 00257 { 00258 SetLocalWaypoint* wp = dynamic_cast<SetLocalWaypoint*>(current.mpElement); 00259 if(wp) 00260 { 00261 UInt pv1 = query->GetPresenceVector(); 00262 UInt pv2 = wp->GetPresenceVector(); 00263 00264 report.SetX(wp->GetX()); 00265 report.SetY(wp->GetY()); 00266 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Z)) > 0) { report.SetZ(wp->GetZ()); } 00267 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Roll)) > 0) { report.SetRoll(wp->GetRoll()); } 00268 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Pitch)) > 0) { report.SetPitch(wp->GetPitch()); } 00269 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::Yaw)) > 0) { report.SetYaw(wp->GetYaw()); } 00270 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::WaypointTolerance)) > 0) { report.SetWaypointTolerance(wp->GetWaypointTolerance()); } 00271 if( (pv2 & (pv1 & LocalWaypoint::PresenceVector::PathTolerance)) > 0) { report.SetPathTolerance(wp->GetPathTolerance()); } 00272 } 00273 } 00274 SendEvent(info, &report); 00275 return true; 00276 } 00277 else if(info.mpQueryMessage->GetMessageCode() == QUERY_TRAVEL_SPEED) 00278 { 00279 Mutex::ScopedLock lock(&mListDriverMutex); 00280 const QueryTravelSpeed* query = dynamic_cast<const QueryTravelSpeed*>(info.mpQueryMessage); 00281 if(query == NULL) 00282 { 00283 return false; 00284 } 00285 ReportTravelSpeed report; 00286 report.SetSpeed(mSpeedMetersPerSecond); 00287 SendEvent(info, &report); 00288 return true; 00289 } 00290 00291 return false; 00292 } 00293 00294 00300 JAUS::SetLocalWaypoint LocalWaypointListDriver::GetCurrentWaypoint() const 00301 { 00302 Element current = GetActiveListElement(); 00303 if(current.mpElement) 00304 { 00305 JAUS::SetLocalWaypoint* cmd = dynamic_cast<JAUS::SetLocalWaypoint*>(current.mpElement); 00306 if(cmd) 00307 { 00308 return *cmd; 00309 } 00310 } 00311 return JAUS::SetLocalWaypoint(); 00312 } 00313 00314 00320 std::vector<JAUS::SetLocalWaypoint> LocalWaypointListDriver::GetWaypointList() const 00321 { 00322 // Get local waypoint list. 00323 JAUS::Element::Map elementList = GetElementList(); 00324 // Convert to SetLocalWaypointCommands 00325 JAUS::Element::Map::iterator listElement; 00326 std::vector<JAUS::SetLocalWaypoint> commandList; 00327 for(listElement = elementList.begin(); 00328 listElement != elementList.end(); 00329 listElement++) 00330 { 00331 if(listElement->second.mpElement->GetMessageCode() == JAUS::SET_LOCAL_WAYPOINT) 00332 { 00333 commandList.push_back(*( (JAUS::SetLocalWaypoint *)(listElement->second.mpElement)) ); 00334 } 00335 } 00336 return commandList; 00337 } 00338 00339 00345 void LocalWaypointListDriver::PrintStatus() const 00346 { 00347 std::vector<JAUS::SetLocalWaypoint> waypoints = GetWaypointList(); 00348 if(IsExecuting() == true) 00349 { 00350 std::cout << "[" << GetServiceID().ToString() << "] - Driving to Waypoint [" << GetActiveListElementID() << "]:\n"; 00351 std::cout << "There are " << waypoints.size() << " Waypoints in the List.\n"; 00352 std::cout << "Execution Speed (m/s): " << std::fixed << std::setprecision(2) << mSpeedMetersPerSecond << std::endl; 00353 } 00354 else 00355 { 00356 std::cout << "[" << GetServiceID().ToString() << "] - Idle\n"; 00357 std::cout << "There are " << waypoints.size() << " Waypoints in the List.\n"; 00358 } 00359 } 00360 00361 00362 /* End of File */