ocra-recipes
Doxygen documentation for the ocra-recipes repository
ClientManager.cpp
Go to the documentation of this file.
1 
9 /*
10  * This file is part of ocra-icub.
11  * Copyright (C) 2016 Institut des Systèmes Intelligents et de Robotique (ISIR)
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26 
28 
29 using namespace ocra_recipes;
30 
31 int ClientManager::CONTROLLER_CLIENT_MANAGER_COUNT = 0;
32 
33 ClientManager::ClientManager(std::shared_ptr<ControllerClient> customClient, bool suppressPerformanceWarnings)
34 {
35  // Increment the module counter and save it in module number.
36  moduleNumber = ++ClientManager::CONTROLLER_CLIENT_MANAGER_COUNT;
37 
38  client = customClient;
39  expectedClientPeriod = client->getExpectedPeriod();
40 
41  suppressWarnings = suppressPerformanceWarnings;
42 }
43 
45 {
46  rpcPort.close();
47 }
48 
50 {
51  return "ControllerClientManager_"+std::to_string(moduleNumber);
52 }
53 
54 bool ClientManager::configure(yarp::os::ResourceFinder &rf)
55 {
56  return client->configure(rf);
57 }
58 
60 {
61  client->start();
62  return runModule();
63 }
64 
66 {
67  close();
68  // if(client)
69  // client->suspend();
70  return true;
71 }
72 
74 {
75  /* Stop the control thread. */
76  if(client){
77  client->stop();
78  }
79 
80  if (!suppressWarnings) {
81  /* Print performance information */
82  printf("[PERFORMANCE INFORMATION]:\n");
83  printf("Expected period %d ms.\nReal period: %3.1f+/-%3.1f ms.\n", expectedClientPeriod, avgTime, stdDev);
84  printf("Real duration of 'run' method: %3.1f+/-%3.1f ms.\n", avgTimeUsed, stdDevUsed);
85  if(avgTime<0.5*(double)expectedClientPeriod)
86  printf("Next time you could set a lower period to improve the controller performance.\n");
87  else if(avgTime>1.3*(double)expectedClientPeriod)
88  printf("The period you set was impossible to attain. Next time you could set a higher period.\n");
89  }
90  return true;
91 }
92 
94 {
95  if (client->hasBeenReleased()) {
96  return false;
97  }
98  // Get the average time between two calls of the Rate.run() method.
99  client->getEstPeriod(avgTime, stdDev);
100 
101  // Get the average time the .run() method takes to compute the control.
102  client->getEstUsed(avgTimeUsed, stdDevUsed);
103 
104  // If the period of the control thread is too slow then print a warning.
105  if( (avgTime > 1.3*(double)expectedClientPeriod) && !suppressWarnings)
106  {
107  yLog.warning() << "CLIENT THREAD LOOP IS TOO SLOW\nReal period: "<< avgTime <<"+/-"<< stdDev <<"\nExpected period: " << expectedClientPeriod <<"\nDuration of 'run' method: "<<avgTimeUsed<<"+/-"<< stdDevUsed<<"\n";
108  }
109 
110  return customUpdateModule();
111 }
112 
114 {
115  std::cout << "Hey there this is the help for the ClientManager. Here is what the client is saying..." << std::endl;
116  client->printHelp();
117 }
118 
119 void ClientManager::callbackParser(yarp::os::Bottle& message, yarp::os::Bottle& reply)
120 {
121  if (message.size() != 0) {
122  reply.clear();
123  customCallbackParser(message, reply);
124  }
125 }
126 
127 void ClientManager::customCallbackParser(yarp::os::Bottle& message, yarp::os::Bottle& reply)
128 {
129  // Do nothing if not implemented.
130 }
132 {
133  // Do nothing if not implemented.
134  return true;
135 }
136 
137 
138 /**************************************************************************************************
139  Nested moduleCallback Class
140 **************************************************************************************************/
142 : moduleRef(newModuleRef)
143 {
144  //do nothing
145 }
146 
147 bool ClientManager::moduleCallback::read(yarp::os::ConnectionReader& connection)
148 {
149  yarp::os::Bottle input, reply;
150 
151  if (!input.read(connection)){
152  return false;
153  }
154  else{
155  moduleRef.callbackParser(input, reply);
156  yarp::os::ConnectionWriter* returnToSender = connection.getWriter();
157  if (returnToSender!=NULL) {
158  reply.write(*returnToSender);
159  }
160  return true;
161  }
162 }
163 /**************************************************************************************************
164 **************************************************************************************************/
virtual std::string getManagerName()
bool configure(yarp::os::ResourceFinder &rf)
Module class for the controller Client.
moduleCallback(ClientManager &newModuleRef)
virtual void customCallbackParser(yarp::os::Bottle &message, yarp::os::Bottle &reply)
virtual bool read(yarp::os::ConnectionReader &connection)
ClientManager(std::shared_ptr< ControllerClient > customClient, bool suppressPerformanceWarnings=false)
The controller module which launches the controller thread.
Definition: ClientManager.h:48