denso_variable.cpp
Go to the documentation of this file.
1 
26 
27 #define NAME_READ "_Read"
28 #define NAME_WRITE "_Write"
29 #define NAME_ID "_ID"
30 
31 namespace denso_robot_core {
32 
34  Service_Vec& service, Handle_Vec& handle,
35  const std::string& name, const int* mode,
36  int16_t vt, bool Read, bool Write, bool ID, int Duration)
37  : DensoBase(parent, service, handle, name, mode),
38  m_vt(vt), m_bRead(Read), m_bWrite(Write), m_bID(ID)
39 {
40  m_Duration = ros::Duration(Duration / 1000, (Duration % 1000) * 1000);
42 }
43 
45 {
46 
47 }
48 
50 {
51  if(*m_mode != 0) {
52  return S_FALSE;
53  }
54 
55  // Message name
56  std::string tmpName = m_parent->RosName();
57  if(tmpName != "") tmpName.append("/");
58  tmpName.append(DensoBase::RosName());
59 
60  if(m_bRead) {
61  switch(m_vt) {
62  case VT_I4:
63  m_pubValue = node.advertise<Int32>(
64  tmpName + NAME_READ, MESSAGE_QUEUE);
65  break;
66  case VT_R4:
67  m_pubValue = node.advertise<Float32>(
68  tmpName + NAME_READ, MESSAGE_QUEUE);
69  break;
70  case VT_R8:
71  m_pubValue = node.advertise<Float64>(
72  tmpName + NAME_READ, MESSAGE_QUEUE);
73  break;
74  case VT_BSTR:
75  m_pubValue = node.advertise<String>(
76  tmpName + NAME_READ, MESSAGE_QUEUE);
77  break;
78  case VT_BOOL:
79  m_pubValue = node.advertise<Bool>(
80  tmpName + NAME_READ, MESSAGE_QUEUE);
81  break;
82  case (VT_ARRAY | VT_R4):
83  m_pubValue = node.advertise<Float32MultiArray>(
84  tmpName + NAME_READ, MESSAGE_QUEUE);
85  break;
86  case (VT_ARRAY | VT_R8):
87  m_pubValue = node.advertise<Float64MultiArray>(
88  tmpName + NAME_READ, MESSAGE_QUEUE);
89  break;
90  default:
91  return E_FAIL;
92  }
93  }
94 
95  if(m_bWrite) {
96  switch(m_vt) {
97  case VT_I4:
98  m_subValue = node.subscribe<Int32>(
99  tmpName + NAME_WRITE, MESSAGE_QUEUE,
101  break;
102  case VT_R4:
103  m_subValue = node.subscribe<Float32>(
104  tmpName + NAME_WRITE, MESSAGE_QUEUE,
106  break;
107  case VT_R8:
108  m_subValue = node.subscribe<Float64>(
109  tmpName + NAME_WRITE, MESSAGE_QUEUE,
111  break;
112  case VT_BSTR:
113  m_subValue = node.subscribe<String>(
114  tmpName + NAME_WRITE, MESSAGE_QUEUE,
116  break;
117  case VT_BOOL:
118  m_subValue = node.subscribe<Bool>(
119  tmpName + NAME_WRITE, MESSAGE_QUEUE,
121  break;
122  case (VT_ARRAY | VT_R4):
123  m_subValue = node.subscribe<Float32MultiArray>(
124  tmpName + NAME_WRITE, MESSAGE_QUEUE,
126  break;
127  case (VT_ARRAY | VT_R8):
128  m_subValue = node.subscribe<Float64MultiArray>(
129  tmpName + NAME_WRITE, MESSAGE_QUEUE,
131  break;
132  default:
133  return E_FAIL;
134  }
135  }
136 
137  if(m_bID) {
138  m_subID = node.subscribe<Int32>(
139  tmpName + NAME_ID, MESSAGE_QUEUE,
141  }
142 
143  m_serving = true;
144 
145  return S_OK;
146 }
147 
149 {
150  m_mtxSrv.lock();
151  m_serving = false;
152  m_mtxSrv.unlock();
153 
156  m_subID.shutdown();
157  return S_OK;
158 }
159 
161 {
162  boost::mutex::scoped_lock lockSrv(m_mtxSrv);
163  if(!m_serving) return false;
164 
165  if(m_bRead) {
166  HRESULT hr;
167 
168  Int32 varI; Float32 varF; Float64 varD;
169  String varS; Bool varIO;
170  Float32MultiArray varFArray;
171  Float64MultiArray varDArray;
172 
173  uint32_t num;
174  float *pfltval;
175  double *pdblval;
176 
177  ros::Time pubTimeCur = ros::Time::now();
178 
179  if(pubTimeCur - m_pubTimePrev > m_Duration) {
180  VARIANT_Ptr vntRet(new VARIANT());
181  VariantInit(vntRet.get());
182 
183  hr = ExecGetValue(vntRet);
184  if(SUCCEEDED(hr)) {
185  if(vntRet->vt == m_vt) {
186  switch(m_vt){
187  case VT_I4:
188  varI.data = vntRet->lVal;
189  m_pubValue.publish(varI);
190  break;
191  case VT_R4:
192  varF.data = vntRet->fltVal;
193  m_pubValue.publish(varF);
194  break;
195  case VT_R8:
196  varD.data = vntRet->dblVal;
197  m_pubValue.publish(varD);
198  break;
199  case VT_BSTR:
200  varS.data = ConvertBSTRToString(vntRet->bstrVal);
201  m_pubValue.publish(varS);
202  break;
203  case VT_BOOL:
204  varIO.data = (vntRet->boolVal != VARIANT_FALSE) ? true : false;
205  m_pubValue.publish(varIO);
206  break;
207  case (VT_ARRAY | VT_R4):
208  num = vntRet->parray->rgsabound->cElements;
209  SafeArrayAccessData(vntRet->parray, (void**)&pfltval);
210  varFArray.data.resize(num);
211  std::copy(pfltval, &pfltval[num], varFArray.data.begin());
212  SafeArrayUnaccessData(vntRet->parray);
213  m_pubValue.publish(varFArray);
214  break;
215  case (VT_ARRAY | VT_R8):
216  num = vntRet->parray->rgsabound->cElements;
217  SafeArrayAccessData(vntRet->parray, (void**)&pdblval);
218  varDArray.data.resize(num);
219  std::copy(pdblval, &pdblval[num], varDArray.data.begin());
220  SafeArrayUnaccessData(vntRet->parray);
221  m_pubValue.publish(varDArray);
222  break;
223  }
224  }
225  }
226 
227  m_pubTimePrev = pubTimeCur;
228  }
229  }
230 
231  return true;
232 }
233 
235 {
236  VARIANT_Vec vntArgs;
237  VARIANT_Ptr vntHandle(new VARIANT());
238 
239  VariantInit(vntHandle.get());
240 
241  vntHandle->vt = VT_UI4;
242  vntHandle->ulVal = m_vecHandle[DensoBase::SRV_WATCH];
243 
244  vntArgs.push_back(*vntHandle.get());
245 
246  return m_vecService[DensoBase::SRV_WATCH]->ExecFunction(
247  ID_VARIABLE_GETVALUE, vntArgs, value);
248 }
249 
251 {
252  HRESULT hr;
253  int argc;
254  VARIANT_Vec vntArgs;
255  VARIANT_Ptr vntHandle(new VARIANT());
256  VARIANT_Ptr vntRet(new VARIANT());
257 
258  VariantInit(vntRet.get());
259 
260  vntHandle->vt = VT_UI4;
261  vntHandle->ulVal = m_vecHandle[DensoBase::SRV_WATCH];
262 
263  vntArgs.push_back(*vntHandle.get());
264 
265  vntArgs.push_back(*value.get());
266 
267  hr = m_vecService[DensoBase::SRV_WATCH]->ExecFunction(
268  ID_VARIABLE_PUTVALUE, vntArgs, vntRet);
269  if(SUCCEEDED(hr)) {
270  Update();
271  }
272 
273  return hr;
274 }
275 
277 {
278  HRESULT hr;
279  int argc;
280  VARIANT_Vec vntArgs;
281  VARIANT_Ptr vntHandle(new VARIANT());
282  VARIANT_Ptr vntValue(new VARIANT());
283  VARIANT_Ptr vntRet(new VARIANT());
284 
285  VariantInit(vntRet.get());
286 
287  vntHandle->vt = VT_UI4;
288  vntHandle->ulVal = m_vecHandle[DensoBase::SRV_WATCH];
289 
290  vntArgs.push_back(*vntHandle.get());
291 
292  vntValue->vt = VT_I4;
293  vntValue->lVal = id;
294  vntArgs.push_back(*vntValue.get());
295 
296  hr = m_vecService[DensoBase::SRV_WATCH]->ExecFunction(
297  ID_VARIABLE_PUTID, vntArgs, vntRet);
298  if(SUCCEEDED(hr)) {
299  Update();
300  }
301 
302  return hr;
303 }
304 
305 void DensoVariable::Callback_I32(const Int32::ConstPtr& msg)
306 {
307  VARIANT_Ptr vntVal(new VARIANT());
308  vntVal->vt = VT_I4;
309  vntVal->lVal = msg->data;
310 
311  ExecPutValue(vntVal);
312 }
313 
314 void DensoVariable::Callback_F32(const Float32::ConstPtr& msg)
315 {
316  VARIANT_Ptr vntVal(new VARIANT());
317  vntVal->vt = VT_R4;
318  vntVal->fltVal = msg->data;
319 
320  ExecPutValue(vntVal);
321 }
322 
323 void DensoVariable::Callback_F64(const Float64::ConstPtr& msg)
324 {
325  VARIANT_Ptr vntVal(new VARIANT());
326  vntVal->vt = VT_R8;
327  vntVal->dblVal = msg->data;
328 
329  ExecPutValue(vntVal);
330 }
331 
332 void DensoVariable::Callback_String(const String::ConstPtr& msg)
333 {
334  VARIANT_Ptr vntVal(new VARIANT());
335  vntVal->vt = VT_BSTR;
336  vntVal->bstrVal = ConvertStringToBSTR(msg->data);
337 
338  ExecPutValue(vntVal);
339 }
340 
341 void DensoVariable::Callback_Bool(const Bool::ConstPtr& msg)
342 {
343  VARIANT_Ptr vntVal(new VARIANT());
344  vntVal->vt = VT_BOOL;
345  vntVal->boolVal = (msg->data != 0) ? VARIANT_TRUE : VARIANT_FALSE;
346 
347  ExecPutValue(vntVal);
348 }
349 
350 void DensoVariable::Callback_F32Array(const Float32MultiArray::ConstPtr& msg)
351 {
352  VARIANT_Ptr vntVal(new VARIANT());
353  float *pval;
354 
355  vntVal->vt = (VT_ARRAY | VT_R4);
356  vntVal->parray = SafeArrayCreateVector(VT_R4, 0, msg->data.size());
357 
358  SafeArrayAccessData(vntVal->parray, (void**)&pval);
359  std::copy(msg->data.begin(), msg->data.end(), pval);
360  SafeArrayUnaccessData(vntVal->parray);
361 
362  ExecPutValue(vntVal);
363 }
364 
365 void DensoVariable::Callback_F64Array(const Float64MultiArray::ConstPtr& msg)
366 {
367  VARIANT_Ptr vntVal(new VARIANT());
368  double *pval;
369 
370  vntVal->vt = (VT_ARRAY | VT_R8);
371  vntVal->parray = SafeArrayCreateVector(VT_R8, 0, msg->data.size());
372 
373  SafeArrayAccessData(vntVal->parray, (void**)&pval);
374  std::copy(msg->data.begin(), msg->data.end(), pval);
375  SafeArrayUnaccessData(vntVal->parray);
376 
377  ExecPutValue(vntVal);
378 }
379 
380 void DensoVariable::Callback_ID(const Int32::ConstPtr &msg)
381 {
382  ExecPutID(msg->data);
383 }
384 
385 }
#define S_FALSE
HRESULT SafeArrayUnaccessData(SAFEARRAY *psa)
#define ID_VARIABLE_GETVALUE
short int16_t
unsigned uint32_t
SAFEARRAY * SafeArrayCreateVector(uint16_t vt, int32_t lLbound, uint32_t cElements)
void publish(const boost::shared_ptr< M > &message) const
#define VARIANT_TRUE
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
#define S_OK
HRESULT ExecPutValue(const VARIANT_Ptr &value)
void Callback_F32Array(const Float32MultiArray::ConstPtr &msg)
void Callback_ID(const Int32::ConstPtr &msg)
std::vector< VARIANT, VariantAllocator< VARIANT > > VARIANT_Vec
std::vector< uint32_t > Handle_Vec
Definition: denso_base.h:67
VT_I4
HRESULT ExecPutID(const int id)
VT_BSTR
HRESULT StartService(ros::NodeHandle &node)
std::vector< BCAPService_Ptr > Service_Vec
Definition: denso_base.h:68
void Callback_I32(const Int32::ConstPtr &msg)
void VariantInit(VARIANT *pvarg)
#define E_FAIL
int32_t HRESULT
void Callback_String(const String::ConstPtr &msg)
#define MESSAGE_QUEUE
Definition: denso_base.h:61
VT_ARRAY
VT_BOOL
static std::string ConvertBSTRToString(const BSTR bstr)
Definition: denso_base.cpp:34
VT_UI4
boost::interprocess::unique_ptr< VARIANT, variant_deleter > VARIANT_Ptr
HRESULT ExecGetValue(VARIANT_Ptr &value)
#define NAME_WRITE
HRESULT SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
static BSTR ConvertStringToBSTR(const std::string &str)
Definition: denso_base.cpp:48
#define SUCCEEDED(hr)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void Callback_F32(const Float32::ConstPtr &msg)
#define NAME_ID
#define NAME_READ
VT_R8
#define ID_VARIABLE_PUTVALUE
DensoVariable(DensoBase *parent, Service_Vec &service, Handle_Vec &handle, const std::string &name, const int *mode, int16_t vt, bool Read, bool Write, bool ID, int Duration)
#define VARIANT_FALSE
static Time now()
VT_R4
std::string RosName() const
Definition: denso_base.cpp:62
void Callback_F64Array(const Float64MultiArray::ConstPtr &msg)
void Callback_Bool(const Bool::ConstPtr &msg)
void Callback_F64(const Float64::ConstPtr &msg)
#define ID_VARIABLE_PUTID


denso_robot_core
Author(s): DENSO WAVE INCORPORATED
autogenerated on Mon Jun 10 2019 13:12:27