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  // </rtc-template>
41  gain_fname(""),
42  dt(0.005),
43  dof(0)
44 {
45 }
46 
48 {
49 }
50 
51 
52 RTC::ReturnCode_t ModifiedServo::onInitialize()
53 {
54  // Registration: InPort/OutPort/Service
55  // <rtc-template block="registration">
56  // Set InPort buffers
57  addInPort("tauRef", m_tauRefIn);
58  addInPort("qRef", m_qRefIn);
59  addInPort("q", m_qIn);
60  addInPort("torqueMode", m_torqueModeIn);
61 
62  // Set OutPort buffer
63  addOutPort("tau", m_tauOut);
64 
65  // Set service provider to Ports
66 
67  // Set service consumers to Ports
68 
69  // Set CORBA Service Ports
70 
71  // </rtc-template>
72 
73  // <rtc-template block="bind_config">
74  // Bind variables and configuration variable
75 
76  // </rtc-template>
77 
78  std::cout << m_profile.instance_name << ": onInitialize() " << std::endl;
79 
81 
82  coil::stringTo(dt, prop["dt"].c_str());
83  coil::stringTo(ref_dt, prop["ref_dt"].c_str());
84  nstep = ref_dt/dt;
85  step = nstep;
86 
88 
89  RTC::Manager & rtcManager = RTC::Manager::instance();
90  std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
91  int comPos = nameServer.find(",");
92 
93  if (comPos < 0)
94  comPos = nameServer.length();
95 
96  // In case there is more than one, retrieves only the first one
97  nameServer = nameServer.substr(0, comPos);
98 
99  RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
100 
101  if (!loadBodyFromModelLoader(m_robot, prop["model"].c_str(),
102  CosNaming::NamingContext::_duplicate(naming.getRootContext())))
103  std::cerr << "[" << m_profile.instance_name << "] failed to load model "
104  << "[" << prop["model"] << "]" << std::endl;
105 
106  return RTC::RTC_OK;
107 }
108 
109 /*
110 RTC::ReturnCode_t ModifiedServo::onFinalize()
111 {
112  return RTC::RTC_OK;
113 }
114 */
115 /*
116 RTC::ReturnCode_t ModifiedServo::onStartup(RTC::UniqueId ec_id)
117 {
118  return RTC::RTC_OK;
119 }
120 */
121 /*
122 RTC::ReturnCode_t ModifiedServo::onShutdown(RTC::UniqueId ec_id)
123 {
124  return RTC::RTC_OK;
125 }
126 */
127 
129 {
130  std::cout << m_profile.instance_name << ": on Activated" << std::endl;
131 
132  if (m_qIn.isNew()) {
133  m_qIn.read();
134  if (dof == 0) {
135  dof = m_q.data.length();
136  readGainFile();
137  }
138  }
139 
140  q_old.resize(dof);
141  qRef_old.resize(dof);
142 
143  m_tauRef.data.length(dof);
144  m_qRef.data.length(dof);
145  m_torqueMode.data.length(dof);
146 
147  m_tau.data.length(dof);
148 
149  for (size_t i = 0; i < dof; i++) {
150  m_tauRef.data[i] = 0.0;
151  m_qRef.data[i] = qRef_old[i] = q_old[i] = m_q.data[i];
152  m_torqueMode.data[i] = false;
153  }
154 
155  return RTC::RTC_OK;
156 }
157 
159 {
160  std::cout << m_profile.instance_name << ": on Deactivated" << std::endl;
161 
162  return RTC::RTC_OK;
163 }
164 
165 RTC::ReturnCode_t ModifiedServo::onExecute(RTC::UniqueId ec_id)
166 {
167  if (m_tauRefIn.isNew())
168  m_tauRefIn.read();
169 
170  if (m_qIn.isNew())
171  m_qIn.read();
172 
173  if (m_qRefIn.isNew()) {
174  m_qRefIn.read();
175  step = nstep;
176  }
177 
178  if (m_torqueModeIn.isNew())
180 
181  for (size_t i = 0; i < dof; i++) {
182 
183  double q = m_q.data[i];
184  double qRef = step > 0 ? qRef_old[i] + (m_qRef.data[i] - qRef_old[i]) / step : qRef_old[i];
185 
186  double dq = (q - q_old[i]) / dt;
187  double dqRef = (qRef - qRef_old[i]) / dt;
188 
189  q_old[i] = q;
190  qRef_old[i] = qRef;
191 
192  double tau = m_torqueMode.data[i] ? m_tauRef.data[i] : Pgain[i] * (qRef - q) + Dgain[i] * (dqRef - dq);
193 
194  double tau_limit = m_robot->joint(i)->torqueConst * m_robot->joint(i)->climit * fabs(m_robot->joint(i)->gearRatio);
195 
196  m_tau.data[i] = std::max(std::min(tau, tau_limit), -tau_limit);
197  }
198 
199  step--;
200 
201  m_tau.tm = m_q.tm;
202  m_tauOut.write();
203 
204  return RTC::RTC_OK;
205 }
206 
207 /*
208 RTC::ReturnCode_t ModifiedServo::onAborting(RTC::UniqueId ec_id)
209 {
210  return RTC::RTC_OK;
211 }
212 */
213 /*
214 RTC::ReturnCode_t ModifiedServo::onError(RTC::UniqueId ec_id)
215 {
216  return RTC::RTC_OK;
217 }
218 */
219 /*
220 RTC::ReturnCode_t ModifiedServo::onReset(RTC::UniqueId ec_id)
221 {
222  return RTC::RTC_OK;
223 }
224 */
225 /*
226 RTC::ReturnCode_t ModifiedServo::onStateUpdate(RTC::UniqueId ec_id)
227 {
228  return RTC::RTC_OK;
229 }
230 */
231 /*
232 RTC::ReturnCode_t ModifiedServo::onRateChanged(RTC::UniqueId ec_id)
233 {
234  return RTC::RTC_OK;
235 }
236 */
237 
239 {
240  if (gain_fname == "") {
242  coil::stringTo(gain_fname, prop["pdgains_sim_file_name"].c_str());
243  }
244 
245  gain.open(gain_fname.c_str());
246 
247  if (gain.is_open()) {
248 
249  double val;
250 
251  Pgain.resize(dof);
252  Dgain.resize(dof);
253 
254  for (unsigned int i = 0; i < dof; i++) {
255 
256  if (gain >> val)
257  Pgain[i] = val;
258  else
259  std::cout << "[" << m_profile.instance_name << "] Gain file [" << gain_fname << "] is too short" << std::endl;
260 
261  if (gain >> val)
262  Dgain[i] = val;
263  else
264  std::cout << "[" << m_profile.instance_name << "] Gain file [" << gain_fname << "] is too short" << std::endl;
265  }
266 
267  gain.close();
268 
269  std::cout << "[" << m_profile.instance_name << "] Gain file [" << gain_fname << "] successfully read" << std::endl;
270  }
271  else
272  std::cout << "[" << m_profile.instance_name << "] Gain file [" << gain_fname << "] could not be opened" << std::endl;
273 }
274 
275 extern "C"
276 {
277 
279  {
281  manager->registerFactory(profile,
282  RTC::Create<ModifiedServo>,
283  RTC::Delete<ModifiedServo>);
284  }
285 
286 };
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
#define max(a, b)
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
TimedDoubleSeq m_q
prop
TimedBooleanSeq m_torqueMode
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
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 Thu May 6 2021 02:41:50