1 ////////////////////////////////////////////////////////////////////////////////////

    2 ///

    3 ///  \file tutorial_06.cpp

    4 ///  \brief This file is part of a set of tutorials for learning how to use

    5 ///        JAUS++.  This program demonstrates how to add additional services

    6 ///        to a component.  Specifically, this tutorial uses the Global

    7 ///        Pose Sensor service of the mobility library.

    8 ///

    9 ///  <br>Author(s): Daniel Barber

   10 ///  <br>Created: 3 July 2010

   11 ///  <br>Copyright (c) 2010

   12 ///  <br>Applied Cognition and Training in Immersive Virtual Environments

   13 ///  <br>(ACTIVE) Laboratory

   14 ///  <br>Institute for Simulation and Training (IST)

   15 ///  <br>University of Central Florida (UCF)

   16 ///  <br>All rights reserved.

   17 ///  <br>Email: dbarber@ist.ucf.edu

   18 ///  <br>Web:  http://active.ist.ucf.edu

   19 ///

   20 ///  Redistribution and use in source and binary forms, with or without

   21 ///  modification, are permitted provided that the following conditions are met:

   22 ///      * Redistributions of source code must retain the above copyright

   23 ///        notice, this list of conditions and the following disclaimer.

   24 ///      * Redistributions in binary form must reproduce the above copyright

   25 ///        notice, this list of conditions and the following disclaimer in the

   26 ///        documentation and/or other materials provided with the distribution.

   27 ///      * Neither the name of the ACTIVE LAB, IST, UCF, nor the

   28 ///        names of its contributors may be used to endorse or promote products

   29 ///        derived from this software without specific prior written permission.

   30 ///

   31 ///  THIS SOFTWARE IS PROVIDED BY THE ACTIVE LAB''AS IS'' AND ANY

   32 ///  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

   33 ///  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE

   34 ///  DISCLAIMED. IN NO EVENT SHALL UCF BE LIABLE FOR ANY

   35 ///  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES

   36 ///  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;

   37 ///  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND

   38 ///  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT

   39 ///  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

   40 ///  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

   41 ///

   42 ////////////////////////////////////////////////////////////////////////////////////

   43 #include <jaus/core/component.h>

   44 #include <jaus/mobility/sensors/globalposesensor.h>

   45 #include <cxutils/keyboard.h>

   46 #include <iostream>

   47 

   48 

   49 int main(int argc, char* argv[])

   50 {

   51     JAUS::Component component;

   52 

   53     // Add services to a component is done using the AddService

   54     // method.  Services can only be added before a

   55     // component is initialized using the Initialize method.

   56     // Any attempt to add after will fail.  Finally, you

   57     // cannot add multiple of the same service.

   58 

   59     // Create a Global Pose Sensor service.

   60     JAUS::GlobalPoseSensor* globalPoseSensor = new JAUS::GlobalPoseSensor();

   61     // Set the update rate of the sensor (Hz).  This

   62     // is used to determine what type of periodic

   63     // events the sensor can support.

   64     globalPoseSensor->SetSensorUpdateRate(25);

   65 

   66     // Set some global pose values.

   67     JAUS::GlobalPose globalPose;

   68     globalPose.SetLatitude(34.12345);

   69     globalPose.SetLongitude(-116.12345);

   70     globalPose.SetAltitude(100);

   71     globalPose.SetTimeStamp(JAUS::Time(true));

   72     // Set the values.

   73     globalPoseSensor->SetGlobalPose(globalPose);

   74 

   75     // Add the sensor service to the component. Remember

   76     // this must be done before initialization.  Also,

   77     // the component will delete the service for us

   78     // automatically, so we don't have to!

   79     component.AddService(globalPoseSensor);

   80 

   81     // Setup identification info.  For questions about this,

   82     // see the previous tutorial(s).

   83     JAUS::Discovery* discoveryService = NULL;

   84     discoveryService = component.DiscoveryService();

   85     discoveryService->SetSubsystemIdentification(JAUS::Subsystem::Vehicle,

   86                                                 "Robot");

   87     discoveryService->SetNodeIdentification("Primary Computer");

   88     discoveryService->SetComponentIdentification("Baseline");

   89 

   90     JAUS::Address componentID(1000, 1, 6);

   91     // Initialize!

   92     std::cout << "Initializing component...";

   93     if(component.Initialize(componentID) == false)

   94     {

   95         std::cout << "Failed to initialize component [" << componentID.ToString() << "]\n";

   96         return 0;

   97     }

   98     std::cout << "Success!\n";

   99 

  100     // Now go into your main computer loop until the

  101     // component has been told to shutdown.

  102     JAUS::Time::Stamp displayStatusTimeMs = JAUS::Time::GetUtcTimeMs();

  103     while(true)

  104     {

  105         // To "simulate" a real sensor, lets modify

  106         // the latitude like the robot is moving north.

  107         globalPose.SetLatitude(globalPose.GetLatitude() + 0.00001);

  108         globalPose.SetTimeStamp(JAUS::Time(true));

  109         // Update the sensor with the new data.  This will

  110         // automatically trigger events if someone (i.e. another

  111         // component) is subscribing to the data from our sensor.

  112         // this is because the Global Pose Sensor class inherits

  113         // from the Events service and has implemented all the

  114         // methods needed to support subscriptions.

  115         globalPoseSensor->SetGlobalPose(globalPose);

  116 

  117         JAUS::Management* managementService = NULL;

  118         managementService = component.ManagementService();

  119         if(managementService->GetStatus() == JAUS::Management::Status::Shutdown)

  120         {

  121             // Exit program.

  122             break;

  123         }

  124 

  125         if(JAUS::Time::GetUtcTimeMs() - displayStatusTimeMs > 500)

  126         {

  127             // Print out status of the service.

  128             component.GetService(JAUS::GlobalPoseSensor::Name)->PrintStatus(); std::cout << std::endl;

  129             displayStatusTimeMs = JAUS::Time::GetUtcTimeMs();

  130         }

  131 

  132         if(CxUtils::GetChar() == 27)

  133         {

  134             break;

  135         }

  136 

  137         CxUtils::SleepMs(100);

  138     }

  139 

  140     // Shutdown your component completely.  Any

  141     // services added or belonging to the component

  142     // will be deleted.

  143     component.Shutdown();

  144 

  145     return 0;

  146 }

  147 

  148 

  149 

  150 /* End of File */

  151