SamplePD_HG.cpp
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*-
2 /*
3  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
4  * All rights reserved. This program is made available under the terms of the
5  * Eclipse Public License v1.0 which accompanies this distribution, and is
6  * available at http://www.eclipse.org/legal/epl-v10.html
7  * Contributors:
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  * General Robotix Inc.
10  */
19 #include "SamplePD_HG.h"
20 
21 #include <iostream>
22 
23 #define PD_DOF (17)
24 #define HG_DOF (12)
25 #define TIMESTEP 0.002
26 
27 #define WAIST_P 26
28 #define WAIST_R 27
29 #define CHEST 28
30 #define LARM_SHOULDER_P 19
31 #define LARM_SHOULDER_R 20
32 #define LARM_SHOULDER_Y 21
33 #define LARM_ELBOW 22
34 #define LARM_WRIST_Y 23
35 #define LARM_WRIST_P 24
36 #define LARM_WRIST_R 25
37 #define RARM_SHOULDER_P 6
38 #define RARM_SHOULDER_R 7
39 #define RARM_SHOULDER_Y 8
40 #define RARM_ELBOW 9
41 #define RARM_WRIST_Y 10
42 #define RARM_WRIST_P 11
43 #define RARM_WRIST_R 12
44 #define LLEG_HIP_R 13
45 #define LLEG_HIP_P 14
46 #define LLEG_HIP_Y 15
47 #define LLEG_KNEE 16
48 #define LLEG_ANKLE_P 17
49 #define LLEG_ANKLE_R 18
50 #define RLEG_HIP_R 0
51 #define RLEG_HIP_P 1
52 #define RLEG_HIP_Y 2
53 #define RLEG_KNEE 3
54 #define RLEG_ANKLE_P 4
55 #define RLEG_ANKLE_R 5
56 
57 #define ANGLE_FILE "etc/angle.dat"
58 #define VEL_FILE "etc/vel.dat"
59 #define ACC_FILE "etc/acc.dat"
60 
61 #define GAIN_FILE "etc/PDgain.dat"
62 
63 namespace {
64  const bool CONTROLLER_BRIDGE_DEBUG = false;
65 }
66 
67 
68 // Module specification
69 // <rtc-template block="module_spec">
70 static const char* SamplePD_HG_spec[] =
71  {
72  "implementation_id", "SamplePD_HG",
73  "type_name", "SamplePD_HG",
74  "description", "Sample PD component",
75  "version", "0.1",
76  "vendor", "AIST",
77  "category", "Generic",
78  "activity_type", "DataFlowComponent",
79  "max_instance", "10",
80  "language", "C++",
81  "lang_type", "compile",
82  // Configuration variables
83 
84  ""
85  };
86 // </rtc-template>
87 
88 SamplePD_HG::SamplePD_HG(RTC::Manager* manager)
89  : RTC::DataFlowComponentBase(manager),
90  // <rtc-template block="initializer">
91  m_angle_inIn("angle_in", m_angle_in),
92  m_torqueOut("torque", m_torque),
93  m_angle_outOut("angle_out", m_angle_out),
94  m_velOut("vel", m_vel),
95  m_accOut("acc", m_acc),
96 
97  // </rtc-template>
98  dummy(0),
99  qold(DOF)
100 {
101  if( CONTROLLER_BRIDGE_DEBUG )
102  {
103  std::cout << "SamplePD_HG::SamplePD_HG" << std::endl;
104  }
105 
106  // Registration: InPort/OutPort/Service
107  // <rtc-template block="registration">
108 
109  // Set service provider to Ports
110 
111  // Set service consumers to Ports
112 
113  // Set CORBA Service Ports
114 
115  // </rtc-template>
116 }
117 
119 {
120  closeFiles();
121  delete [] Pgain;
122  delete [] Dgain;
123 }
124 
125 
126 RTC::ReturnCode_t SamplePD_HG::onInitialize()
127 {
128  // <rtc-template block="bind_config">
129  // Bind variables and configuration variable
130  if( CONTROLLER_BRIDGE_DEBUG )
131  {
132  std::cout << "onInitialize" << std::endl;
133  }
134 
135  // Set InPort buffers
136  addInPort("angle_in", m_angle_inIn);
137 
138  // Set OutPort buffer
139  addOutPort("torque", m_torqueOut);
140  addOutPort("angle_out", m_angle_outOut);
141  addOutPort("vel", m_velOut);
142  addOutPort("acc", m_accOut);
143 
144  Pgain = new double[DOF];
145  Dgain = new double[DOF];
146 
147  gain.open(GAIN_FILE);
148  if (gain.is_open()){
149  for (int i=0; i<DOF; i++){
150  gain >> Pgain[i];
151  gain >> Dgain[i];
152  }
153  gain.close();
154  }else{
155  std::cerr << GAIN_FILE << " not opened" << std::endl;
156  }
157  // </rtc-template>
158 
159  m_angle_in.data.length(DOF);
160  m_angle_out.data.length(HG_DOF);
161  m_vel.data.length(HG_DOF);
162  m_acc.data.length(HG_DOF);
163  m_torque.data.length(PD_DOF);
164 
165  return RTC::RTC_OK;
166 }
167 
168 
169 
170 /*
171 RTC::ReturnCode_t SamplePD_HG::onFinalize()
172 {
173  return RTC::RTC_OK;
174 }
175 */
176 
177 /*
178 RTC::ReturnCode_t SamplePD_HG::onStartup(RTC::UniqueId ec_id)
179 {
180  return RTC::RTC_OK;
181 }
182 */
183 
184 /*
185 RTC::ReturnCode_t SamplePD_HG::onShutdown(RTC::UniqueId ec_id)
186 {
187  return RTC::RTC_OK;
188 }
189 */
190 
191 RTC::ReturnCode_t SamplePD_HG::onActivated(RTC::UniqueId ec_id)
192 {
193  std::cout << "on Activated" << std::endl;
194  openFiles();
195 
196  if(m_angle_inIn.isNew()){
197  m_angle_inIn.read();
198  }
199 
200  for(int i=0; i < DOF; ++i){
201  qold[i] = m_angle_in.data[i];
202  q_ref[i] = dq_ref[i] = ddq_ref[i] = 0.0;
203  }
204 
205  return RTC::RTC_OK;
206 }
207 
208 
209 RTC::ReturnCode_t SamplePD_HG::onDeactivated(RTC::UniqueId ec_id)
210 {
211  std::cout << "on Deactivated" << std::endl;
212  closeFiles();
213  return RTC::RTC_OK;
214 }
215 
216 
217 
218 RTC::ReturnCode_t SamplePD_HG::onExecute(RTC::UniqueId ec_id)
219 {
220  if( CONTROLLER_BRIDGE_DEBUG )
221  {
222  std::cout << "onExecute" << std::endl;
223  std::string localStr;
224  std::cin >> localStr;
225  }
226 
227  if(m_angle_inIn.isNew()){
228  m_angle_inIn.read();
229  }
230 
231  if(!angle.eof()){
232  angle >> q_ref[0]; vel >> dq_ref[0]; acc >> ddq_ref[0];// skip time
233  for (int i=0; i<DOF; i++){
234  angle >> q_ref[i];
235  vel >> dq_ref[i];
236  acc >> ddq_ref[i];
237  }
238  }
239 
240  double tor_ref[DOF];
241  for(int i=0; i<DOF; i++){
242  double q = m_angle_in.data[i];
243  double dq = (q - qold[i]) / TIMESTEP;
244  qold[i] = q;
245 
246  tor_ref[i] = -(q - q_ref[i]) * Pgain[i] - (dq - dq_ref[i]) * Dgain[i];
247  }
248 
249  m_torque.data[0] = tor_ref[WAIST_P];
250  m_torque.data[1] = tor_ref[WAIST_R];
251  m_torque.data[2] = tor_ref[CHEST];
252  m_torque.data[3] = tor_ref[LARM_SHOULDER_P];
253  m_torque.data[4] = tor_ref[LARM_SHOULDER_R];
254  m_torque.data[5] = tor_ref[LARM_SHOULDER_Y];
255  m_torque.data[6] = tor_ref[LARM_ELBOW];
256  m_torque.data[7] = tor_ref[LARM_WRIST_Y];
257  m_torque.data[8] = tor_ref[LARM_WRIST_P];
258  m_torque.data[9] = tor_ref[LARM_WRIST_R];
259  m_torque.data[10] = tor_ref[RARM_SHOULDER_P];
260  m_torque.data[11] = tor_ref[RARM_SHOULDER_R];
261  m_torque.data[12] = tor_ref[RARM_SHOULDER_Y];
262  m_torque.data[13] = tor_ref[RARM_ELBOW];
263  m_torque.data[14] = tor_ref[RARM_WRIST_Y];
264  m_torque.data[15] = tor_ref[RARM_WRIST_P];
265  m_torque.data[16] = tor_ref[RARM_WRIST_R];
266 
267  m_angle_out.data[0] = q_ref[LLEG_HIP_R];
268  m_angle_out.data[1] = q_ref[LLEG_HIP_P];
269  m_angle_out.data[2] = q_ref[LLEG_HIP_Y];
270  m_angle_out.data[3] = q_ref[LLEG_KNEE];
271  m_angle_out.data[4] = q_ref[LLEG_ANKLE_P];
272  m_angle_out.data[5] = q_ref[LLEG_ANKLE_R];
273  m_angle_out.data[6] = q_ref[RLEG_HIP_R];
274  m_angle_out.data[7] = q_ref[RLEG_HIP_P];
275  m_angle_out.data[8] = q_ref[RLEG_HIP_Y];
276  m_angle_out.data[9] = q_ref[RLEG_KNEE];
277  m_angle_out.data[10] = q_ref[RLEG_ANKLE_P];
278  m_angle_out.data[11] = q_ref[RLEG_ANKLE_R];
279  m_vel.data[0] = dq_ref[LLEG_HIP_R];
280  m_vel.data[1] = dq_ref[LLEG_HIP_P];
281  m_vel.data[2] = dq_ref[LLEG_HIP_Y];
282  m_vel.data[3] = dq_ref[LLEG_KNEE];
283  m_vel.data[4] = dq_ref[LLEG_ANKLE_P];
284  m_vel.data[5] = dq_ref[LLEG_ANKLE_R];
285  m_vel.data[6] = dq_ref[RLEG_HIP_R];
286  m_vel.data[7] = dq_ref[RLEG_HIP_P];
287  m_vel.data[8] = dq_ref[RLEG_HIP_Y];
288  m_vel.data[9] = dq_ref[RLEG_KNEE];
289  m_vel.data[10] = dq_ref[RLEG_ANKLE_P];
290  m_vel.data[11] = dq_ref[RLEG_ANKLE_R];
291  m_acc.data[0] = ddq_ref[LLEG_HIP_R];
292  m_acc.data[1] = ddq_ref[LLEG_HIP_P];
293  m_acc.data[2] = ddq_ref[LLEG_HIP_Y];
294  m_acc.data[3] = ddq_ref[LLEG_KNEE];
295  m_acc.data[4] = ddq_ref[LLEG_ANKLE_P];
296  m_acc.data[5] = ddq_ref[LLEG_ANKLE_R];
297  m_acc.data[6] = ddq_ref[RLEG_HIP_R];
298  m_acc.data[7] = ddq_ref[RLEG_HIP_P];
299  m_acc.data[8] = ddq_ref[RLEG_HIP_Y];
300  m_acc.data[9] = ddq_ref[RLEG_KNEE];
301  m_acc.data[10] = ddq_ref[RLEG_ANKLE_P];
302  m_acc.data[11] = ddq_ref[RLEG_ANKLE_R];
303 
304  m_torqueOut.write();
305  m_angle_outOut.write();
306  m_velOut.write();
307  m_accOut.write();
308 
309  return RTC::RTC_OK;
310 }
311 
312 
313 /*
314  RTC::ReturnCode_t SamplePD_HG::onAborting(RTC::UniqueId ec_id)
315  {
316  return RTC::RTC_OK;
317  }
318 */
319 
320 /*
321  RTC::ReturnCode_t SamplePD_HG::onError(RTC::UniqueId ec_id)
322  {
323  return RTC::RTC_OK;
324  }
325 */
326 
327 /*
328  RTC::ReturnCode_t SamplePD_HG::onReset(RTC::UniqueId ec_id)
329  {
330  return RTC::RTC_OK;
331  }
332 */
333 
334 /*
335  RTC::ReturnCode_t SamplePD_HG::onStateUpdate(RTC::UniqueId ec_id)
336  {
337  return RTC::RTC_OK;
338  }
339 */
340 
341 /*
342  RTC::ReturnCode_t SamplePD_HG::onRateChanged(RTC::UniqueId ec_id)
343  {
344  return RTC::RTC_OK;
345  }
346 */
347 
349 {
350  angle.open(ANGLE_FILE);
351  if (!angle.is_open()){
352  std::cerr << ANGLE_FILE << " not opened" << std::endl;
353  }
354 
355  vel.open(VEL_FILE);
356  if (!vel.is_open()){
357  std::cerr << VEL_FILE << " not opened" << std::endl;
358  }
359 
360  acc.open(ACC_FILE);
361  if (!acc.is_open())
362  {
363  std::cerr << ACC_FILE << " not opend" << std::endl;
364  }
365 }
366 
368 {
369  if( angle.is_open() ){
370  angle.close();
371  angle.clear();
372  }
373  if( vel.is_open() ){
374  vel.close();
375  vel.clear();
376  }
377  if( acc.is_open() ){
378  acc.close();
379  acc.clear();
380  }
381 }
382 
383 
384 extern "C"
385 {
386 
387  DLL_EXPORT void SamplePD_HGInit(RTC::Manager* manager)
388  {
389  coil::Properties profile(SamplePD_HG_spec);
390  manager->registerFactory(profile,
391  RTC::Create<SamplePD_HG>,
392  RTC::Delete<SamplePD_HG>);
393  }
394 
395 };
396 
LARM_WRIST_P
#define LARM_WRIST_P
Definition: SamplePD_HG.cpp:35
RLEG_KNEE
#define RLEG_KNEE
Definition: SamplePD_HG.cpp:53
SamplePD_HG::m_torqueOut
OutPort< TimedDoubleSeq > m_torqueOut
Definition: SamplePD_HG.h:124
SamplePD_HG::acc
std::ifstream acc
Definition: SamplePD_HG.h:145
i
png_uint_32 i
Definition: png.h:2732
SamplePD_HG::q_ref
double q_ref[DOF]
Definition: SamplePD_HG.h:149
RLEG_ANKLE_R
#define RLEG_ANKLE_R
Definition: SamplePD_HG.cpp:55
SamplePD_HG::m_angle_in
TimedDoubleSeq m_angle_in
Definition: SamplePD_HG.h:110
LARM_ELBOW
#define LARM_ELBOW
Definition: SamplePD_HG.cpp:33
SamplePD_HG::SamplePD_HG
SamplePD_HG(RTC::Manager *manager)
Definition: SamplePD_HG.cpp:88
RARM_SHOULDER_Y
#define RARM_SHOULDER_Y
Definition: SamplePD_HG.cpp:39
SamplePD_HG::~SamplePD_HG
~SamplePD_HG()
Definition: SamplePD_HG.cpp:118
SamplePD_HG::m_angle_outOut
OutPort< TimedDoubleSeq > m_angle_outOut
Definition: SamplePD_HG.h:118
SamplePD_HG::m_vel
TimedDoubleSeq m_vel
Definition: SamplePD_HG.h:119
SamplePD_HG::closeFiles
void closeFiles()
Definition: SamplePD_HG.cpp:367
SamplePD_HG::gain
std::ifstream gain
Definition: SamplePD_HG.h:145
RLEG_HIP_Y
#define RLEG_HIP_Y
Definition: SamplePD_HG.cpp:52
DOF
#define DOF
Definition: PA10Controller.h:22
SamplePD_HG::m_velOut
OutPort< TimedDoubleSeq > m_velOut
Definition: SamplePD_HG.h:120
SamplePD_HG_spec
static const char * SamplePD_HG_spec[]
Definition: SamplePD_HG.cpp:70
RLEG_ANKLE_P
#define RLEG_ANKLE_P
Definition: SamplePD_HG.cpp:54
SamplePD_HG::onDeactivated
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
Definition: SamplePD_HG.cpp:209
SamplePD_HG::vel
std::ifstream vel
Definition: SamplePD_HG.h:145
profile
png_infop png_charpp int png_charpp profile
Definition: png.h:2380
SamplePD_HG::ddq_ref
double ddq_ref[DOF]
Definition: SamplePD_HG.h:149
TIMESTEP
#define TIMESTEP
Definition: SamplePD_HG.cpp:25
SamplePD_HG::dq_ref
double dq_ref[DOF]
Definition: SamplePD_HG.h:149
RLEG_HIP_R
#define RLEG_HIP_R
Definition: SamplePD_HG.cpp:50
CHEST
#define CHEST
Definition: SamplePD_HG.cpp:29
SamplePD_HGInit
DLL_EXPORT void SamplePD_HGInit(RTC::Manager *manager)
Definition: SamplePD_HG.cpp:387
SamplePD_HG::m_torque
TimedDoubleSeq m_torque
Definition: SamplePD_HG.h:123
LARM_WRIST_Y
#define LARM_WRIST_Y
Definition: SamplePD_HG.cpp:34
SamplePD_HG::openFiles
void openFiles()
Definition: SamplePD_HG.cpp:348
SamplePD_HG::angle
std::ifstream angle
Definition: SamplePD_HG.h:145
LARM_SHOULDER_P
#define LARM_SHOULDER_P
Definition: SamplePD_HG.cpp:30
SamplePD_HG::onInitialize
virtual RTC::ReturnCode_t onInitialize()
Definition: SamplePD_HG.cpp:126
SamplePD_HG.h
Sample PD component.
DLL_EXPORT
#define DLL_EXPORT
Definition: bush_customizer.cpp:20
LLEG_HIP_P
#define LLEG_HIP_P
Definition: SamplePD_HG.cpp:45
LARM_WRIST_R
#define LARM_WRIST_R
Definition: SamplePD_HG.cpp:36
LLEG_KNEE
#define LLEG_KNEE
Definition: SamplePD_HG.cpp:47
RARM_WRIST_P
#define RARM_WRIST_P
Definition: SamplePD_HG.cpp:42
GAIN_FILE
#define GAIN_FILE
Definition: SamplePD_HG.cpp:61
LARM_SHOULDER_R
#define LARM_SHOULDER_R
Definition: SamplePD_HG.cpp:31
RARM_SHOULDER_R
#define RARM_SHOULDER_R
Definition: SamplePD_HG.cpp:38
RARM_WRIST_Y
#define RARM_WRIST_Y
Definition: SamplePD_HG.cpp:41
SamplePD_HG::onActivated
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
Definition: SamplePD_HG.cpp:191
SamplePD_HG::Pgain
double * Pgain
Definition: SamplePD_HG.h:146
RARM_ELBOW
#define RARM_ELBOW
Definition: SamplePD_HG.cpp:40
SamplePD_HG::Dgain
double * Dgain
Definition: SamplePD_HG.h:147
LARM_SHOULDER_Y
#define LARM_SHOULDER_Y
Definition: SamplePD_HG.cpp:32
RARM_SHOULDER_P
#define RARM_SHOULDER_P
Definition: SamplePD_HG.cpp:37
ACC_FILE
#define ACC_FILE
Definition: SamplePD_HG.cpp:59
SamplePD_HG::onExecute
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
Definition: SamplePD_HG.cpp:218
LLEG_HIP_R
#define LLEG_HIP_R
Definition: SamplePD_HG.cpp:44
SamplePD_HG::m_acc
TimedDoubleSeq m_acc
Definition: SamplePD_HG.h:121
SamplePD_HG::m_angle_inIn
InPort< TimedDoubleSeq > m_angle_inIn
Definition: SamplePD_HG.h:111
RARM_WRIST_R
#define RARM_WRIST_R
Definition: SamplePD_HG.cpp:43
WAIST_P
#define WAIST_P
Definition: SamplePD_HG.cpp:27
RTC
Definition: SimulationExecutionContext.cpp:7
SamplePD_HG::m_angle_out
TimedDoubleSeq m_angle_out
Definition: SamplePD_HG.h:117
PD_DOF
#define PD_DOF
Definition: SamplePD_HG.cpp:23
LLEG_ANKLE_P
#define LLEG_ANKLE_P
Definition: SamplePD_HG.cpp:48
LLEG_HIP_Y
#define LLEG_HIP_Y
Definition: SamplePD_HG.cpp:46
HG_DOF
#define HG_DOF
Definition: SamplePD_HG.cpp:24
SamplePD_HG::qold
std::vector< double > qold
Definition: SamplePD_HG.h:148
VEL_FILE
#define VEL_FILE
Definition: SamplePD_HG.cpp:58
ANGLE_FILE
#define ANGLE_FILE
Definition: SamplePD_HG.cpp:57
SamplePD_HG::m_accOut
OutPort< TimedDoubleSeq > m_accOut
Definition: SamplePD_HG.h:122
RLEG_HIP_P
#define RLEG_HIP_P
Definition: SamplePD_HG.cpp:51
LLEG_ANKLE_R
#define LLEG_ANKLE_R
Definition: SamplePD_HG.cpp:49
WAIST_R
#define WAIST_R
Definition: SamplePD_HG.cpp:28


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Wed Sep 7 2022 02:51:04