iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-Component.C
/*! @file
@author Randolph Voorhies (voorhies at usc dot edu)
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
// that it will be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
namespace robotarm
{
nrt::ParameterCategory ParamCateg("Serial Port Related Options");
// a parameter with no specification of valid values (so any value goes)
NRT_DECLARE_PARAMETER(serialport, std::string,
"Serial port device",
"/dev/ttyS0", ParamCateg);
// a parameter with a list of valid values, and also with a callback. Host class will need to implement
// onParamChange() for this param
NRT_DECLARE_PARAMETER_WITH_CALLBACK(serialbaud, int,
"Baud Rate",
115200, { 9600, 19200, 38400, 57600, 115200 }, ParamCateg);
// a parameter with a range of valid values
NRT_DECLARE_PARAMETER(rangevalue, int,
"value in [0..100]",
42, nrt::Range<int>(0, 100), ParamCateg);
// a parameter with valid values specified by a regex
NRT_DECLARE_PARAMETER(funnynumber, int,
"funny number that should be in [0..59] or special value 72",
42, boost::regex("^(([0-5]?[0-9])|72)$"), ParamCateg);
// a parameter with valid values specified by a regex, rejects online changes
NRT_DECLARE_PARAMETER(funnyoffline, int,
"funny number that should be in [0..59] or special value 72, no online changes. "
"Set the value to 72 to watch it throw while running!",
42, boost::regex("^(([0-5]?[0-9])|72)$"), ParamCateg, nrt::ParameterFlags::NoOnlineChanges);
}
class RobotArm : public nrt::Component,
public nrt::Parameter<robotarm::serialport, robotarm::serialbaud,
robotarm::rangevalue, robotarm::funnynumber,
robotarm::funnyoffline>
{
public:
RobotArm(std::string instanceID="") : nrt::Component(instanceID)
{ }
virtual ~RobotArm()
{ }
void postStart()
{
NRT_INFO(descriptor() << ":serialport = " << robotarm::serialport::get());
NRT_INFO(descriptor() << ":baudrate = " << robotarm::serialbaud::get());
}
void run()
{
NRT_INFO(descriptor() << ":funnyoffline = " << robotarm::funnyoffline::get());
if (robotarm::funnyoffline::get() == 72)
{
NRT_INFO(descriptor() << ": Trying to change a NoOnlineChanges param while running... This should throw...");
try { robotarm::funnyoffline::set(42); }
{ NRT_WARNING(descriptor() << ": Caught exception: " << e.what()); }
}
}
void onParamChange(robotarm::serialbaud const & param, int const & value)
{
NRT_INFO(descriptor() << ": Serial baud change callback triggered!");
}
};
namespace robot
{
nrt::ParameterCategory ParamCateg("Robot Related Options");
NRT_DECLARE_PARAMETER(speed, float, "Speed of the robot", 0.0F, ParamCateg);
NRT_DECLARE_PARAMETER(strength, float, "Strength of the robot", 100.0F, ParamCateg);
}
class Robot : public nrt::Component,
public nrt::Parameter<robot::speed, robot::strength>
{
public:
Robot(std::string instanceID="") : Component(instanceID)
{
addSubComponent<RobotArm>("Left");
addSubComponent<RobotArm>("Right");
}
virtual ~Robot()
{ }
void postStart()
{
NRT_INFO(descriptor() << ":speed = " << robot::speed::get());
NRT_INFO(descriptor() << ":strength = " << robot::strength::get());
}
};
int main(int argc, char const* argv[])
{
try
{
nrt::Manager mgr(argc, argv);
auto myRobot1 = mgr.addComponent<Robot>("Zhora");
auto myRobot2 = mgr.addComponent<Robot>("Pris");
mgr.launch();
mgr.prettyPrintTree(std::cout);
NRT_INFO("sleep for a bit...");
std::this_thread::sleep_for(std::chrono::seconds(1));
mgr.removeComponent(myRobot1); // note: myRobot1 has been reset() in the process...
mgr.prettyPrintTree(std::cout);
NRT_INFO("Goodbye!");
}
catch (nrt::exception::Exception const & e)
{ NRT_WARNING("Caught NRT Exception: " << e.what()); }
catch (std::exception const & e)
{ NRT_WARNING("Caught std::exception: " << e.what()); }
catch (...)
{ NRT_WARNING("Caught unknown exception; re-throwing it."); throw; }
return 0;
}