ModifiedServo.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include "ModifiedServo.h"
11 
12 // Module specification
13 // <rtc-template block="module_spec">
14 static const char* modifiedservo_spec[] =
15  {
16  "implementation_id", "ModifiedServo",
17  "type_name", "ModifiedServo",
18  "description", "ModifiedServo component",
19  "version", "0.1",
20  "vendor", "AIST",
21  "category", "example",
22  "activity_type", "SPORADIC",
23  "kind", "DataFlowComponent",
24  "max_instance", "1",
25  "language", "C++",
26  "lang_type", "compile",
27  // Configuration variables
28  ""
29  };
30 // </rtc-template>
31 
33  // <rtc-template block="initializer">
34  : RTC::DataFlowComponentBase(manager),
35  m_tauRefIn("tauRef", m_tauRef),
36  m_qRefIn("qRef", m_qRef),
37  m_qIn("q", m_q),
38  m_torqueModeIn("torqueMode", m_torqueMode),
39  m_tauOut("tau", m_tau),
40  m_pgainsIn("pgainsSet", m_pgains),
41  m_dgainsIn("dgainsSet", m_dgains),
42  m_pgainsOut("pgainsGet", m_pgains),
43  m_dgainsOut("dgainsGet", m_dgains),
44  // </rtc-template>
45  gain_fname(""),
46  dt(0.005),
47  dof(0)
48 {
49 }
50 
52 {
53 }
54 
55 
56 RTC::ReturnCode_t ModifiedServo::onInitialize()
57 {
58  // Registration: InPort/OutPort/Service
59  // <rtc-template block="registration">
60  // Set InPort buffers
61  addInPort("tauRef", m_tauRefIn);
62  addInPort("qRef", m_qRefIn);
63  addInPort("q", m_qIn);
64  addInPort("torqueMode", m_torqueModeIn);
65  addInPort("pgainsSet", m_pgainsIn);
66  addInPort("dgainsSet", m_dgainsIn);
67 
68  // Set OutPort buffer
69  addOutPort("tau", m_tauOut);
70  addOutPort("pgainsGet", m_pgainsOut);
71  addOutPort("dgainsGet", m_dgainsOut);
72 
73  // Set service provider to Ports
74 
75  // Set service consumers to Ports
76 
77  // Set CORBA Service Ports
78 
79  // </rtc-template>
80 
81  // <rtc-template block="bind_config">
82  // Bind variables and configuration variable
83 
84  // </rtc-template>
85 
86  std::cout << m_profile.instance_name << ": onInitialize() " << std::endl;
87 
89 
90  coil::stringTo(dt, prop["dt"].c_str());
91  coil::stringTo(ref_dt, prop["ref_dt"].c_str());
92  nstep = ref_dt/dt;
93  step = nstep;
94 
96 
97  RTC::Manager & rtcManager = RTC::Manager::instance();
98  std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
99  int comPos = nameServer.find(",");
100 
101  if (comPos < 0)
102  comPos = nameServer.length();
103 
104  // In case there is more than one, retrieves only the first one
105  nameServer = nameServer.substr(0, comPos);
106 
107  RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
108 
109  if (!loadBodyFromModelLoader(m_robot, prop["model"].c_str(),
110  CosNaming::NamingContext::_duplicate(naming.getRootContext())))
111  std::cerr << "[" << m_profile.instance_name << "] failed to load model "
112  << "[" << prop["model"] << "]" << std::endl;
113 
114  return RTC::RTC_OK;
115 }
116 
117 /*
118 RTC::ReturnCode_t ModifiedServo::onFinalize()
119 {
120  return RTC::RTC_OK;
121 }
122 */
123 /*
124 RTC::ReturnCode_t ModifiedServo::onStartup(RTC::UniqueId ec_id)
125 {
126  return RTC::RTC_OK;
127 }
128 */
129 /*
130 RTC::ReturnCode_t ModifiedServo::onShutdown(RTC::UniqueId ec_id)
131 {
132  return RTC::RTC_OK;
133 }
134 */
135 
137 {
138  std::cout << m_profile.instance_name << ": on Activated" << std::endl;
139 
140  if (m_qIn.isNew()) {
141  m_qIn.read();
142  if (dof == 0) {
143  dof = m_q.data.length();
144  readGainFile();
145  }
146  }
147 
148  q_old.resize(dof);
149  qRef_old.resize(dof);
150 
151  m_tauRef.data.length(dof);
152  m_qRef.data.length(dof);
153  m_torqueMode.data.length(dof);
154 
155  m_tau.data.length(dof);
156 
157  m_pgains.data.length(dof);
158  m_dgains.data.length(dof);
159 
160  for (size_t i = 0; i < dof; i++) {
161  m_tauRef.data[i] = 0.0;
162  m_qRef.data[i] = qRef_old[i] = q_old[i] = m_q.data[i];
163  m_torqueMode.data[i] = false;
164  m_pgains.data[i] = Pgain[i];
165  m_dgains.data[i] = Dgain[i];
166  }
167 
168  return RTC::RTC_OK;
169 }
170 
172 {
173  std::cout << m_profile.instance_name << ": on Deactivated" << std::endl;
174 
175  return RTC::RTC_OK;
176 }
177 
178 RTC::ReturnCode_t ModifiedServo::onExecute(RTC::UniqueId ec_id)
179 {
180  if (m_tauRefIn.isNew())
181  m_tauRefIn.read();
182 
183  if (m_qIn.isNew())
184  m_qIn.read();
185 
186  if (m_qRefIn.isNew()) {
187  m_qRefIn.read();
188  step = nstep;
189  }
190  if(m_pgainsIn.isNew()){
191  m_pgainsIn.read();
192  }
193  if(m_dgainsIn.isNew()){
194  m_dgainsIn.read();
195  }
196 
197  if (m_torqueModeIn.isNew())
199 
200  for (size_t i = 0; i < dof; i++) {
201  Pgain[i] = m_pgains.data[i];
202  Dgain[i] = m_dgains.data[i];
203 
204  double q = m_q.data[i];
205  double qRef = step > 0 ? qRef_old[i] + (m_qRef.data[i] - qRef_old[i]) / step : qRef_old[i];
206 
207  double dq = (q - q_old[i]) / dt;
208  double dqRef = (qRef - qRef_old[i]) / dt;
209 
210  q_old[i] = q;
211  qRef_old[i] = qRef;
212 
213  double tau = m_torqueMode.data[i] ? m_tauRef.data[i] : Pgain[i] * (qRef - q) + Dgain[i] * (dqRef - dq);
214 
215  double tau_limit = m_robot->joint(i)->torqueConst * m_robot->joint(i)->climit * fabs(m_robot->joint(i)->gearRatio);
216 
217  m_tau.data[i] = std::max(std::min(tau, tau_limit), -tau_limit);
218  }
219 
220  step--;
221 
222  m_tau.tm = m_q.tm;
223  m_tauOut.write();
224  m_pgainsOut.write();
225  m_dgainsOut.write();
226 
227  return RTC::RTC_OK;
228 }
229 
230 /*
231 RTC::ReturnCode_t ModifiedServo::onAborting(RTC::UniqueId ec_id)
232 {
233  return RTC::RTC_OK;
234 }
235 */
236 /*
237 RTC::ReturnCode_t ModifiedServo::onError(RTC::UniqueId ec_id)
238 {
239  return RTC::RTC_OK;
240 }
241 */
242 /*
243 RTC::ReturnCode_t ModifiedServo::onReset(RTC::UniqueId ec_id)
244 {
245  return RTC::RTC_OK;
246 }
247 */
248 /*
249 RTC::ReturnCode_t ModifiedServo::onStateUpdate(RTC::UniqueId ec_id)
250 {
251  return RTC::RTC_OK;
252 }
253 */
254 /*
255 RTC::ReturnCode_t ModifiedServo::onRateChanged(RTC::UniqueId ec_id)
256 {
257  return RTC::RTC_OK;
258 }
259 */
260 
262 {
263  if (gain_fname == "") {
265  coil::stringTo(gain_fname, prop["pdgains_sim_file_name"].c_str());
266  }
267 
268  gain.open(gain_fname.c_str());
269 
270  if (gain.is_open()) {
271 
272  double val;
273 
274  Pgain.resize(dof);
275  Dgain.resize(dof);
276 
277  for (unsigned int i = 0; i < dof; i++) {
278 
279  if (gain >> val)
280  Pgain[i] = val;
281  else
282  std::cout << "[" << m_profile.instance_name << "] Gain file [" << gain_fname << "] is too short" << std::endl;
283 
284  if (gain >> val)
285  Dgain[i] = val;
286  else
287  std::cout << "[" << m_profile.instance_name << "] Gain file [" << gain_fname << "] is too short" << std::endl;
288  }
289 
290  gain.close();
291 
292  std::cout << "[" << m_profile.instance_name << "] Gain file [" << gain_fname << "] successfully read" << std::endl;
293  }
294  else
295  std::cout << "[" << m_profile.instance_name << "] Gain file [" << gain_fname << "] could not be opened" << std::endl;
296 }
297 
298 extern "C"
299 {
300 
302  {
304  manager->registerFactory(profile,
305  RTC::Create<ModifiedServo>,
306  RTC::Delete<ModifiedServo>);
307  }
308 
309 };
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
#define max(a, b)
TimedDoubleSeq m_dgains
InPort< TimedDoubleSeq > m_dgainsIn
bool stringTo(To &val, const char *str)
hrp::dvector qRef_old
hrp::dvector Pgain
hrp::BodyPtr m_robot
CORBA::ORB_ptr getORB()
png_uint_32 i
InPort< TimedDoubleSeq > m_qRefIn
coil::Properties & getProperties()
static Manager & instance()
bool addOutPort(const char *name, OutPortBase &outport)
std::ifstream gain
boost::shared_ptr< Body > BodyPtr
ModifiedServo component.
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
static const char * modifiedservo_spec[]
#define min(a, b)
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
coil::Properties & getConfig()
ExecutionContextHandle_t UniqueId
std::string gain_fname
hrp::dvector Dgain
virtual RTC::ReturnCode_t onInitialize()
int loadBodyFromModelLoader(::World *world, const char *name, const char *url, CosNaming::NamingContext_var cxt)
TimedDoubleSeq m_tauRef
OutPort< TimedDoubleSeq > m_tauOut
int val
TimedDoubleSeq m_tau
OutPort< TimedDoubleSeq > m_pgainsOut
TimedDoubleSeq m_q
prop
TimedBooleanSeq m_torqueMode
TimedDoubleSeq m_pgains
OutPort< TimedDoubleSeq > m_dgainsOut
TimedDoubleSeq m_qRef
naming
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
void ModifiedServoInit(RTC::Manager *manager)
virtual bool isNew()
InPort< TimedBooleanSeq > m_torqueModeIn
virtual bool write(DataType &value)
InPort< TimedDoubleSeq > m_qIn
ModifiedServo(RTC::Manager *manager)
InPort< TimedDoubleSeq > m_tauRefIn
InPort< TimedDoubleSeq > m_pgainsIn
hrp::dvector q_old
bool addInPort(const char *name, InPortBase &inport)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Sat Dec 17 2022 03:52:20