iCub-main
MotorTemperaturePublisher.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 iCub Facility - Istituto Italiano di Tecnologia
3  * Author: Jacopo Losi
4  * email: jacopo.losi@iit.it
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * http://www.robotcub.org/icub/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16 */
17 
19 
20 #include <yarp/os/Network.h>
21 #include <yarp/os/Log.h>
22 #include <yarp/os/LogStream.h>
23 #include <yarp/os/Stamp.h>
24 
25 using namespace std;
26 using namespace yarp::os;
27 
28 
29 bool MotorTemperaturePublisher::configure(yarp::os::ResourceFinder& rf)
30 {
31  // Read configuration file
32  Bottle &conf_group = rf.findGroup("GENERAL");
33  Bottle* jointsBottle = nullptr;
34  if (conf_group.isNull())
35  {
36  yWarning() << "Missing GENERAL group! The module uses the default values";
37  }
38  else
39  {
40  if(conf_group.check("portprefix")) { m_portPrefix = conf_group.find("portprefix").asString(); }
41  if(conf_group.check("period")) { _updatePeriod = conf_group.find("period").asFloat64(); }
42  if(conf_group.check("robotname")) { _robotName = conf_group.find("robotname").asString(); }
43  if (conf_group.check("listofjoints"))
44  {
45  jointsBottle = conf_group.find("listofjoints").asList();
46  _nEnabledMotors = jointsBottle->size();
47  for(int i=0; i < _nEnabledMotors; i++) _listOfJoints.push_back(jointsBottle->get(i).asInt32());
48  }
49 
50  }
51 
52  // Create remote motion control device
53  Property options;
54  options.put("device", "remote_controlboard");
55  options.put("remote", "/"+ _robotName + m_portPrefix);
56  options.put("local", m_portPrefix + "/mc");
57 
58 
59  yDebug() << "++++ config:\n"
60  << "\t portprefix: " << m_portPrefix << "\n"
61  << "\t period: " << _updatePeriod << "\n"
62  << "\t robotname: " << _robotName << "\n"
63  << "\t listofjoints: " << jointsBottle->toString() << "\n";
64 
65  _motionControlDevice.open(options);
66 
67  if (!_motionControlDevice.isValid())
68  {
69  yError() << "Unable to open device driver. Aborting...";
70  return false;
71  }
72 
73  if (!_motionControlDevice.view(_imot) || _imot==nullptr)
74  {
75  yError() << "Unable to open motor raw interface. Aborting...";
76  return false;
77  }
78 
79  if (!_imot->getNumberOfMotors(&_nmotors))
80  {
81  yError() << "Unable to retrieve the number of motors";
82  return false;
83  }
84  else
85  {
86  yDebug() << "Working with" << _nmotors << "motors";
87  yDebug() << "Enabling" << _nEnabledMotors << "motors of the subpart";
88  }
89 
90  // Allocate memory for pointer
91  if (!alloc(_nEnabledMotors))
92  {
93  yError() << "Error allocating memory for pointers. Aborting...";
94  return false;
95  }
96 
97 
98  // open the communication port towards motor controller module
99  if(!_outputPort.open(m_portPrefix +"/motor_temperatures:o"))
100  {
101  yError() << "Error opening output port for motor control";
102  return false;
103  }
104 
105  for (uint8_t i = 0; i < _listOfJoints.size(); i++)
106  {
107  if (!_imot->getTemperatureLimit(i, &_motorTemperatureLimits[i]))
108  {
109  yError() << "Unable to get motor temperature Limits. Aborting...";
110  return false;
111  }
112  else
113  {
114  yDebug() << "Limit for motor#" << i << "value:" << _motorTemperatureLimits[i];
115  }
116 
117  }
118 
119  return true;
120 }
121 
123 {
124  // Closing port explicitely
125  yInfo() << "Calling close functionality\n";
126 
127  // Deallocating memory for pointers
128  if (!dealloc())
129  {
130  yError() << "Error deallocating memory for pointer. Failing...";
131  return false;
132  }
133 
134  return true;
135 }
136 
138 {
139  return _updatePeriod;
140 }
141 
143 {
144 
145  for (int i = 0; i < _listOfJoints.size(); i++)
146  {
147  _motorTemperatures[i]= 0;
148  int jointNib = (int)_listOfJoints[i];
149  if (!_imot->getTemperature(jointNib, &_motorTemperatures[jointNib]))
150  {
151  yError() << "Unable to get motor " << jointNib << " temperature.\n";
152  }
153  }
154 
155  sendData2OutputPort(_motorTemperatures);
156 
157  return true;
158 }
159 
160 
162 {;}
163 
165 {;}
166 
167 // Private methods
168 bool MotorTemperaturePublisher::sendData2OutputPort(double * temperatures)
169 {
170  static yarp::os::Stamp stamp;
171 
172  stamp.update();
173 
174  Bottle &b = _outputPort.prepare();
175  _outputPort.setEnvelope(stamp);
176 
177  b.clear();
178 
179  b.addFloat64(stamp.getTime());
180  for (size_t i = 0; i < _nEnabledMotors; i++)
181  {
182  b.addFloat64(temperatures[i]);
183  uint8_t allarm=0;
184  if(temperatures[i] >= _motorTemperatureLimits[i])
185  allarm=1;
186  b.addInt8(allarm);
187  }
188  _outputPort.write();
189 
190 
191  return true;
192 }
193 
194 bool MotorTemperaturePublisher::alloc(int nm)
195 {
196  _motorTemperatures = allocAndCheck<double>(nm);
197  _motorTemperatureLimits = allocAndCheck<double>(nm);
198 
199  return true;
200 
201 }
202 
203 bool MotorTemperaturePublisher::dealloc()
204 {
205  checkAndDestroy(_motorTemperatures);
206  checkAndDestroy(_motorTemperatureLimits);
207 
208  return true;
209 }
bool configure(yarp::os::ResourceFinder &rf)