iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-Component.C
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies (voorhies at usc dot edu)
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #include <nrt/Core/Model/Manager.H>
36 #include <nrt/Core/Typing/Range.H>
37 #include <nrt/Core/Debugging/Log.H>
38 
39 namespace robotarm
40 {
41  nrt::ParameterCategory ParamCateg("Serial Port Related Options");
42 
43  // a parameter with no specification of valid values (so any value goes)
44  NRT_DECLARE_PARAMETER(serialport, std::string,
45  "Serial port device",
46  "/dev/ttyS0", ParamCateg);
47 
48  // a parameter with a list of valid values, and also with a callback. Host class will need to implement
49  // onParamChange() for this param
50  NRT_DECLARE_PARAMETER_WITH_CALLBACK(serialbaud, int,
51  "Baud Rate",
52  115200, { 9600, 19200, 38400, 57600, 115200 }, ParamCateg);
53 
54  // a parameter with a range of valid values
55  NRT_DECLARE_PARAMETER(rangevalue, int,
56  "value in [0..100]",
57  42, nrt::Range<int>(0, 100), ParamCateg);
58 
59  // a parameter with valid values specified by a regex
60  NRT_DECLARE_PARAMETER(funnynumber, int,
61  "funny number that should be in [0..59] or special value 72",
62  42, boost::regex("^(([0-5]?[0-9])|72)$"), ParamCateg);
63 
64  // a parameter with valid values specified by a regex, rejects online changes
65  NRT_DECLARE_PARAMETER(funnyoffline, int,
66  "funny number that should be in [0..59] or special value 72, no online changes. "
67  "Set the value to 72 to watch it throw while running!",
68  42, boost::regex("^(([0-5]?[0-9])|72)$"), ParamCateg, nrt::ParameterFlags::NoOnlineChanges);
69 }
70 
71 class RobotArm : public nrt::Component,
72  public nrt::Parameter<robotarm::serialport, robotarm::serialbaud,
73  robotarm::rangevalue, robotarm::funnynumber,
74  robotarm::funnyoffline>
75 {
76  public:
77  RobotArm(std::string instanceID="") : nrt::Component(instanceID)
78  { }
79 
80  virtual ~RobotArm()
81  { }
82 
83  void postStart()
84  {
85  NRT_INFO(descriptor() << ":serialport = " << robotarm::serialport::get());
86  NRT_INFO(descriptor() << ":baudrate = " << robotarm::serialbaud::get());
87  }
88 
89  void run()
90  {
91  NRT_INFO(descriptor() << ":funnyoffline = " << robotarm::funnyoffline::get());
92 
93  if (robotarm::funnyoffline::get() == 72)
94  {
95  NRT_INFO(descriptor() << ": Trying to change a NoOnlineChanges param while running... This should throw...");
96 
97  try { robotarm::funnyoffline::set(42); }
98  catch (nrt::exception::ParameterException const & e)
99  { NRT_WARNING(descriptor() << ": Caught exception: " << e.what()); }
100  }
101  }
102 
103  void onParamChange(robotarm::serialbaud const & param, int const & value)
104  {
105  NRT_INFO(descriptor() << ": Serial baud change callback triggered!");
106  }
107 };
108 
109 
110 
111 namespace robot
112 {
113  nrt::ParameterCategory ParamCateg("Robot Related Options");
114  NRT_DECLARE_PARAMETER(speed, float, "Speed of the robot", 0.0F, ParamCateg);
115  NRT_DECLARE_PARAMETER(strength, float, "Strength of the robot", 100.0F, ParamCateg);
116 }
117 
118 
119 class Robot : public nrt::Component,
120  public nrt::Parameter<robot::speed, robot::strength>
121 {
122  public:
123  Robot(std::string instanceID="") : Component(instanceID)
124  {
125  addSubComponent<RobotArm>("Left");
126  addSubComponent<RobotArm>("Right");
127  }
128 
129  virtual ~Robot()
130  { }
131 
132  void postStart()
133  {
134  NRT_INFO(descriptor() << ":speed = " << robot::speed::get());
135  NRT_INFO(descriptor() << ":strength = " << robot::strength::get());
136  }
137 };
138 
139 
140 int main(int argc, char const* argv[])
141 {
142  try
143  {
144  nrt::Manager mgr(argc, argv);
145 
146  auto myRobot1 = mgr.addComponent<Robot>("Zhora");
147  auto myRobot2 = mgr.addComponent<Robot>("Pris");
148 
149  mgr.launch();
150 
151  mgr.prettyPrintTree(std::cout);
152  NRT_INFO("sleep for a bit...");
153  std::this_thread::sleep_for(std::chrono::seconds(1));
154 
155  mgr.removeComponent(myRobot1); // note: myRobot1 has been reset() in the process...
156 
157  mgr.prettyPrintTree(std::cout);
158 
159  NRT_INFO("Goodbye!");
160  }
161  catch (nrt::exception::Exception const & e)
162  { NRT_WARNING("Caught NRT Exception: " << e.what()); }
163  catch (std::exception const & e)
164  { NRT_WARNING("Caught std::exception: " << e.what()); }
165  catch (...)
166  { NRT_WARNING("Caught unknown exception; re-throwing it."); throw; }
167 
168  return 0;
169 }