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 #include "jaus/extras/mcu/microcontroller.h"
00041 #include "jaus/extras/mcu/querymicrocontrollerstate.h"
00042 #include "jaus/extras/mcu/reportmicrocontrollerstate.h"
00043 #include "jaus/extras/mcu/setmicrocontrollerstate.h"
00044
00045 using namespace JAUS;
00046
00047 const std::string Microcontroller::Name = "urn:jaus:jss:jpp:extras:Microcontroller";
00048
00049
00055 Microcontroller::Microcontroller() : Management::Child(Service::ID(Microcontroller::Name),
00056 Service::ID(Management::Name))
00057 {
00058 }
00059
00060
00066 Microcontroller::~Microcontroller()
00067 {
00068 }
00069
00070
00083 void Microcontroller::SetDigitalInput(const std::string& name, const bool value)
00084 {
00085 bool signalEvent = false;
00086 if(name.empty())
00087 {
00088 std::cout << "JAUS::Microcontroller::SetDigitalInput - Empty String Argument Error\n";
00089 return;
00090 }
00091 Mutex::ScopedLock lock(&mMcuMutex);
00092 DigitalStates::iterator pin = mDigitalStates.find(name);
00093 if(pin == mDigitalStates.end() || pin->second != value)
00094 {
00095 signalEvent = true;
00096 }
00097 mDigitalStates[name] = value;
00098 if(signalEvent)
00099 {
00100 SignalEvent(true, name);
00101 }
00102 }
00103
00104
00117 void Microcontroller::SetAnalogInput(const std::string& name, const double value)
00118 {
00119 if(name.empty())
00120 {
00121 std::cout << "JAUS::Microcontroller::SetAnalogInput - Empty String Argument Error\n";
00122 return;
00123 }
00124 if(value < -100.0 || value > 100.0)
00125 {
00126 std::cout << "JAUS::Microcontroller::SetAnalogInput - Input Value Out of Bounds\n";
00127 return;
00128 }
00129
00130 bool signalEvent = false;
00131 Mutex::ScopedLock lock(&mMcuMutex);
00132 AnalogStates::iterator pin = mAnalogStates.find(name);
00133 if(pin == mAnalogStates.end() || pin->second != value)
00134 {
00135 signalEvent = true;
00136 }
00137 mAnalogStates[name] = value;
00138 if(signalEvent)
00139 {
00140 SignalEvent(false, name);
00141 }
00142 }
00143
00144
00155 bool Microcontroller::GetDigitalState(const std::string& name) const
00156 {
00157 Mutex::ScopedLock lock(&mMcuMutex);
00158 DigitalStates::const_iterator digital = mDigitalStates.find(name);
00159 if(digital != mDigitalStates.end())
00160 {
00161 return digital->second;
00162 }
00163 return false;
00164 }
00165
00166
00177 double Microcontroller::GetAnalogState(const std::string& name) const
00178 {
00179 Mutex::ScopedLock lock(&mMcuMutex);
00180 AnalogStates::const_iterator analog = mAnalogStates.find(name);
00181 if(analog != mAnalogStates.end())
00182 {
00183 return analog->second;
00184 }
00185 return 0.0;
00186 }
00187
00188
00198 bool Microcontroller::GenerateEvent(const Events::Subscription& info) const
00199 {
00200 if(info.mpQueryMessage->GetMessageCode() == QUERY_MICROCONTROLLER_STATE)
00201 {
00202 const QueryMicrocontrollerState* query = dynamic_cast<const QueryMicrocontrollerState*>(info.mpQueryMessage);
00203 Mutex::ScopedLock lock(&mMcuMutex);
00204
00205 DigitalStates::const_iterator digital;
00206 AnalogStates::const_iterator analog;
00207
00208 ReportMicrocontrollerState report;
00209
00210 if(query->GetDigitalStates()->size() == 0)
00211 {
00212 *report.GetDigitalStates() = mDigitalStates;
00213 }
00214 else
00215 {
00216 std::set<std::string>::const_iterator name;
00217 for(name = query->GetDigitalStates()->begin();
00218 name != query->GetDigitalStates()->end();
00219 name++)
00220 {
00221 digital = mDigitalStates.find(*name);
00222 if(digital != mDigitalStates.end())
00223 {
00224 (*report.GetAnalogStates())[*name] = digital->second;
00225 }
00226 }
00227 }
00228
00229 if(query->GetAnalogStates()->size() == 0)
00230 {
00231 *report.GetAnalogStates() = mAnalogStates;
00232 }
00233 else
00234 {
00235 std::set<std::string>::const_iterator name;
00236 for(name = query->GetAnalogStates()->begin();
00237 name != query->GetAnalogStates()->end();
00238 name++)
00239 {
00240 analog = mAnalogStates.find(*name);
00241 if(analog!= mAnalogStates.end())
00242 {
00243 (*report.GetAnalogStates())[*name] = analog->second;
00244 }
00245 }
00246 }
00247
00248 SendEvent(info, &report);
00249 return true;
00250 }
00251
00252 return false;
00253 }
00254
00255
00271 bool Microcontroller::IsEventSupported(const Events::Type type,
00272 const double requestedPeriodicRate,
00273 const Message* queryMessage,
00274 double& confirmedPeriodicRate,
00275 std::string& errorMessage) const
00276 {
00277 if(queryMessage->GetMessageCode() == QUERY_MICROCONTROLLER_STATE)
00278 {
00279 const QueryMicrocontrollerState* query = dynamic_cast<const QueryMicrocontrollerState*>(queryMessage);
00280 Mutex::ScopedLock lock(&mMcuMutex);
00281
00282 DigitalStates::const_iterator digital;
00283 AnalogStates::const_iterator analog;
00284
00285 ReportMicrocontrollerState report;
00286
00287 if(query->GetDigitalStates()->size() != 0)
00288 {
00289 std::set<std::string>::const_iterator name;
00290 for(name = query->GetDigitalStates()->begin();
00291 name != query->GetDigitalStates()->end();
00292 name++)
00293 {
00294 digital = mDigitalStates.find(*name);
00295 if(digital == mDigitalStates.end())
00296 {
00297 errorMessage = "Digital Device Not Supported."; return false;
00298 }
00299 }
00300 }
00301
00302 if(query->GetAnalogStates()->size() != 0)
00303 {
00304 std::set<std::string>::const_iterator name;
00305 for(name = query->GetAnalogStates()->begin();
00306 name != query->GetAnalogStates()->end();
00307 name++)
00308 {
00309 analog = mAnalogStates.find(*name);
00310 if(analog == mAnalogStates.end())
00311 {
00312 errorMessage = "Analog Device Not Supported."; return false;
00313 }
00314 }
00315 }
00316
00317 confirmedPeriodicRate = requestedPeriodicRate;
00318
00319 return true;
00320 }
00321
00322 return false;
00323 }
00324
00325
00337 Message* Microcontroller::CreateMessage(const UShort messageCode) const
00338 {
00339 Message* message;
00340 switch(messageCode)
00341 {
00342 case QUERY_MICROCONTROLLER_STATE:
00343 message = new QueryMicrocontrollerState();
00344 break;
00345 case REPORT_MICROCONTROLLER_STATE:
00346 message = new ReportMicrocontrollerState();
00347 break;
00348 case SET_MICROCONTROLLER_STATE:
00349 message = new SetMicrocontrollerState();
00350 break;
00351 default:
00352 message = NULL;
00353 break;
00354 }
00355 return message;
00356 }
00357
00358
00367 void Microcontroller::Receive(const Message* message)
00368 {
00369 switch(message->GetMessageCode())
00370 {
00371 case QUERY_MICROCONTROLLER_STATE:
00372 {
00373 const QueryMicrocontrollerState* query = dynamic_cast<const QueryMicrocontrollerState*>(message);
00374 if(query)
00375 {
00376 Mutex::ScopedLock lock(&mMcuMutex);
00377
00378 DigitalStates::const_iterator digital;
00379 AnalogStates::const_iterator analog;
00380
00381 ReportMicrocontrollerState report(message->GetSourceID(), GetComponentID());
00382
00383 if(query->GetDigitalStates()->size() == 0)
00384 {
00385 *report.GetDigitalStates() = mDigitalStates;
00386 }
00387 else
00388 {
00389 std::set<std::string>::const_iterator name;
00390 for(name = query->GetDigitalStates()->begin();
00391 name != query->GetDigitalStates()->end();
00392 name++)
00393 {
00394 digital = mDigitalStates.find(*name);
00395 if(digital != mDigitalStates.end())
00396 {
00397 (*report.GetAnalogStates())[*name] = digital->second;
00398 }
00399 }
00400 }
00401
00402 if(query->GetAnalogStates()->size() == 0)
00403 {
00404 *report.GetAnalogStates() = mAnalogStates;
00405 }
00406 else
00407 {
00408 std::set<std::string>::const_iterator name;
00409 for(name = query->GetAnalogStates()->begin();
00410 name != query->GetAnalogStates()->end();
00411 name++)
00412 {
00413 analog = mAnalogStates.find(*name);
00414 if(analog!= mAnalogStates.end())
00415 {
00416 (*report.GetAnalogStates())[*name] = analog->second;
00417 }
00418 }
00419 }
00420 Send(&report);
00421 }
00422 }
00423 break;
00424 case SET_MICROCONTROLLER_STATE:
00425 {
00426 const SetMicrocontrollerState* command = dynamic_cast<const SetMicrocontrollerState*>(message);
00427 if(command)
00428 {
00429 DigitalStates::const_iterator digital;
00430 AnalogStates::const_iterator analog;
00431
00432 for(digital = command->GetDigitalStates()->begin();
00433 digital != command->GetDigitalStates()->end();
00434 digital++)
00435 {
00436 SetDigitalOut(digital->first,
00437 digital->second);
00438 Mutex::ScopedLock lock(&mMcuMutex);
00439 mDigitalStates[digital->first] = digital->second;
00440 SignalEvent(true,
00441 digital->first);
00442 }
00443
00444 for(analog = command->GetAnalogStates()->begin();
00445 analog != command->GetAnalogStates()->end();
00446 analog++)
00447 {
00448 SetAnalogOut(analog->first,
00449 analog->second);
00450 Mutex::ScopedLock lock(&mMcuMutex);
00451 mAnalogStates[analog->first] = analog->second;
00452 SignalEvent(false,
00453 analog->first);
00454 }
00455 }
00456 }
00457 break;
00458 case REPORT_MICROCONTROLLER_STATE:
00459 {
00460 const ReportMicrocontrollerState* report = dynamic_cast<const ReportMicrocontrollerState*>(message);
00461 if(report && report->GetSourceID() == GetSynchronizeID())
00462 {
00463 Mutex::ScopedLock lock(&mMcuMutex);
00464 mDigitalStates = *report->GetDigitalStates();
00465 mAnalogStates = *report->GetAnalogStates();
00466 Events::Child::SignalEvent(REPORT_MICROCONTROLLER_STATE);
00467 }
00468 }
00469 break;
00470 default:
00471 break;
00472 }
00473 }
00474
00475
00481 void Microcontroller::PrintStatus() const
00482 {
00483
00484 if(GetSynchronizeID().IsValid())
00485 {
00486 std::cout << "[" << GetServiceID().ToString() << "] - Synchronized to [" << GetSynchronizeID().ToString() << "]:\n";
00487 }
00488 else
00489 {
00490 std::cout << "[" << GetServiceID().ToString() << "] - Current Pin States:\n";
00491 }
00492 mMcuMutex.Lock();
00493 DigitalStates digitalStates = mDigitalStates;
00494 AnalogStates analogStates = mAnalogStates;
00495 mMcuMutex.Unlock();
00496
00497 DigitalStates::const_iterator digital;
00498 for(digital = digitalStates.begin();
00499 digital != digitalStates.end();
00500 digital++)
00501 {
00502 std::cout << "Digital - " << digital->first << " - " << (int)digital->second << std::endl;
00503 }
00504 AnalogStates::const_iterator analog;
00505 for(analog = analogStates.begin();
00506 analog != analogStates.end();
00507 analog++)
00508 {
00509 std::cout << "Analog - " << analog->first << " - " << analog->second << std::endl;
00510 }
00511 }
00512
00513
00523 void Microcontroller::CheckServiceSynchronization(const unsigned int timeSinceLastCheckMs)
00524 {
00525 Address syncID = GetSynchronizeID();
00526 if(syncID.IsValid())
00527 {
00528
00529 if(EventsService()->HaveSubscription(REPORT_MICROCONTROLLER_STATE, syncID) == false)
00530 {
00531 QueryMicrocontrollerState query;
00532 EventsService()->RequestEveryChangeEvent(syncID, &query);
00533 }
00534 }
00535 }
00536
00537
00538
00547 void Microcontroller::SignalEvent(const bool digital,
00548 const std::string& name)
00549 {
00550
00551
00552
00553
00554 Events::Subscription::List myEvents = GetComponent()->EventsService()->GetProducedEvents(REPORT_IMAGE);
00555 Events::Subscription::List::iterator e;
00556 for(e = myEvents.begin();
00557 e != myEvents.end();
00558 e++)
00559 {
00560 const QueryMicrocontrollerState* query = dynamic_cast<const QueryMicrocontrollerState*>(e->mpQueryMessage);
00561 if(query)
00562 {
00563 if(digital)
00564 {
00565 if(query->GetDigitalStates()->size() == 0 || query->GetDigitalStates()->find(name) != query->GetDigitalStates()->end())
00566 {
00567 Events::Child::SignalEvent((*e));
00568 }
00569 }
00570 else
00571 {
00572 if(query->GetAnalogStates()->size() == 0 || query->GetAnalogStates()->find(name) != query->GetAnalogStates()->end())
00573 {
00574 Events::Child::SignalEvent((*e));
00575 }
00576 }
00577 }
00578 }
00579 }
00580
00581