21 #ifndef __HAPTIX_SIM_UTILS_API_HAPTIX_H__
22 #define __HAPTIX_SIM_UTILS_API_HAPTIX_H__
27 #include <ignition/transport.hh>
29 #include "msg/hxCollideMode.pb.h"
30 #include "msg/hxColor.pb.h"
31 #include "msg/hxContactPoint_V.pb.h"
32 #include "msg/hxEmpty.pb.h"
33 #include "msg/hxJoint.pb.h"
34 #include "msg/hxLink.pb.h"
35 #include "msg/hxModel.pb.h"
36 #include "msg/hxQuaternion.pb.h"
37 #include "msg/hxSimInfo.pb.h"
38 #include "msg/hxTime.pb.h"
39 #include "msg/hxTransform.pb.h"
40 #include "msg/hxVector3.pb.h"
47 static std::mutex lastResultLock;
56 static ignition::transport::Node *getHxNode()
69 template <
typename T,
typename T2>
bool hxs_convertScalar(T _in, T2 *_out)
73 std::cerr <<
"hxs_convertScalar() error: No [data] in msg" << std::endl;
79 std::cerr <<
"hxs_convertScalar() error: NULL output" << std::endl;
93 static bool hxs_convertVector3(
const haptix::comm::msgs::hxVector3 _in,
98 std::cerr <<
"hxs_convertVector3() error: NULL output" << std::endl;
103 memset(_out, 0,
sizeof(hxsVector3));
118 static bool hxs_convertVector3(
const hxsVector3 *_in,
119 haptix::comm::msgs::hxVector3 *_out)
123 std::cerr <<
"hxs_convertVector3() error: NULL input" << std::endl;
129 std::cerr <<
"hxs_convertVector3() error: NULL output" << std::endl;
149 static bool hxs_convertQuaternion(
const haptix::comm::msgs::hxQuaternion _in,
154 std::cerr <<
"hxs_convertQuaternion() error: NULL output" << std::endl;
159 memset(_out, 0,
sizeof(hxsQuaternion));
175 static bool hxs_convertQuaternion(
const hxsQuaternion *_in,
176 haptix::comm::msgs::hxQuaternion *_out)
180 std::cerr <<
"hxs_convertQuaternion() error: NULL input" << std::endl;
186 std::cerr <<
"hxs_convertQuaternion() error: NULL output" << std::endl;
207 static bool hxs_convertColor(
const haptix::comm::msgs::hxColor _in,
212 std::cerr <<
"hxs_convertColor() error: NULL output" << std::endl;
217 memset(_out, 0,
sizeof(hxsColor));
222 _out->alpha = _in.alpha();
233 static bool hxs_convertCollisionMode(
234 const haptix::comm::msgs::hxCollideMode _in, hxsCollideMode *_out)
238 std::cerr <<
"hxs_convertCollisionMode() error: NULL output" << std::endl;
243 memset(_out, 0,
sizeof(hxsCollideMode));
247 case haptix::comm::msgs::hxCollideMode::hxsNOCOLLIDE:
248 *_out = hxsNOCOLLIDE;
250 case haptix::comm::msgs::hxCollideMode::hxsDETECTIONONLY:
251 *_out = hxsDETECTIONONLY;
253 case haptix::comm::msgs::hxCollideMode::hxsCOLLIDE:
257 std::cerr <<
"hxs_convertCollisionMode() error: Unkown collision mode ["
258 << _in.mode() <<
"]" << std::endl;
271 static bool hxs_convertTransform(
const haptix::comm::msgs::hxTransform _in,
276 std::cerr <<
"hxs_convertTransform() error: NULL output" << std::endl;
281 memset(_out, 0,
sizeof(hxsTransform));
284 result &= hxs_convertVector3(_in.pos(), &_out->pos);
285 result &= hxs_convertQuaternion(_in.orient(), &_out->orient);
296 static bool hxs_convertTransform(
const hxsTransform *_in,
297 haptix::comm::msgs::hxTransform *_out)
301 std::cerr <<
"hxs_convertTransform() error: NULL input" << std::endl;
307 std::cerr <<
"hxs_convertTransform() error: NULL output" << std::endl;
315 result &= hxs_convertVector3(&(_in->pos), _out->mutable_pos());
316 result &= hxs_convertQuaternion(&(_in->orient), _out->mutable_orient());
327 static bool hxs_convertJoint(
const haptix::comm::msgs::hxJoint _in,
332 std::cerr <<
"hxs_convertJoint() error: NULL output" << std::endl;
337 memset(_out, 0,
sizeof(hxsJoint));
341 std::cerr <<
"hxs_convertJoint() error: The name of the joint ["
342 << _in.name() <<
"] exceeds the maximum size allowed ("
347 strncpy(_out->name, _in.name().c_str(), strlen(_in.name().c_str()));
348 _out->name[strlen(_in.name().c_str())] =
'\0';
349 _out->pos = _in.pos();
350 _out->vel = _in.vel();
351 _out->torque_motor = _in.torque_motor();
354 result &= hxs_convertVector3(_in.wrench_reactive().force(),
355 &_out->wrench_reactive.force);
357 result &= hxs_convertVector3(_in.wrench_reactive().torque(),
358 &_out->wrench_reactive.torque);
369 static bool hxs_convertWrench(
const hxsWrench *_in,
370 haptix::comm::msgs::hxWrench *_out)
374 std::cerr <<
"hxs_convertWrench() error: NULL input" << std::endl;
380 std::cerr <<
"hxs_convertWrench() error: NULL output" << std::endl;
385 result &= hxs_convertVector3(&_in->force, _out->mutable_force());
386 result &= hxs_convertVector3(&_in->torque, _out->mutable_torque());
396 static bool hxs_convertLink(
const haptix::comm::msgs::hxLink _in, hxsLink *_out)
400 std::cerr <<
"hxs_convertLink() error: NULL output" << std::endl;
405 memset(_out, 0,
sizeof(hxsLink));
409 std::cerr <<
"hxs_convertLink() error: The name of the link ["
410 << _in.name() <<
"] exceeds the maximum size allowed ("
415 strncpy(_out->name, _in.name().c_str(), strlen(_in.name().c_str()));
416 _out->name[strlen(_in.name().c_str())] =
'\0';
419 result &= hxs_convertTransform(_in.transform(), &_out->transform);
420 result &= hxs_convertVector3(_in.lin_vel(), &_out->lin_vel);
421 result &= hxs_convertVector3(_in.ang_vel(), &_out->ang_vel);
422 result &= hxs_convertVector3(_in.lin_acc(), &_out->lin_acc);
423 result &= hxs_convertVector3(_in.ang_acc(), &_out->ang_acc);
434 static bool hxs_convertModel(
const haptix::comm::msgs::hxModel _in,
439 std::cerr <<
"hxs_convertModel() error: The name of the model ["
440 << _in.name() <<
"] exceeds the maximum size allowed ("
447 std::cerr <<
"hxs_convertModel() error: NULL output" << std::endl;
452 memset(_out, 0,
sizeof(hxsModel));
454 strncpy(_out->name, _in.name().c_str(), strlen(_in.name().c_str()));
455 _out->name[strlen(_in.name().c_str())] =
'\0';
458 result &= hxs_convertTransform(_in.transform(), &(_out->transform));
459 _out->link_count = _in.links_size();
460 _out->joint_count = _in.joints_size();
463 for (
int i = 0; i < _out->link_count; ++i)
464 result &= hxs_convertLink(_in.links(i), &_out->links[i]);
467 for (
int i = 0; i < _out->joint_count; ++i)
468 result &= hxs_convertJoint(_in.joints(i), &_out->joints[i]);
470 _out->gravity_mode = _in.gravity_mode();
481 static bool hxs_convertContactPoints(
482 const haptix::comm::msgs::hxContactPoint_V _in, hxsContactPoints *_out)
486 std::cerr <<
"hxs_convertContactPoints() error: NULL output" << std::endl;
491 memset(_out, 0,
sizeof(hxsContactPoints));
493 _out->contact_count = _in.contacts_size();
497 for (
int i = 0; i < _out->contact_count; ++i)
501 std::cerr <<
"hxs_convertContactPoints() error: The name of the link1 ["
502 << _in.contacts(i).link1() <<
"] exceeds the maximum size "
509 std::cerr <<
"hxs_convertContactPoints() error: The name of the link2 ["
510 << _in.contacts(i).link2() <<
"] exceeds the maximum size "
515 strncpy(_out->contacts[i].link1, _in.contacts(i).link1().c_str(),
516 strlen(_in.contacts(i).link1().c_str()));
517 _out->contacts[i].link1[strlen(_in.contacts(i).link1().c_str())] =
'\0';
519 strncpy(_out->contacts[i].link2, _in.contacts(i).link2().c_str(),
520 strlen(_in.contacts(i).link2().c_str()));
521 _out->contacts[i].link2[strlen(_in.contacts(i).link2().c_str())] =
'\0';
523 result &= hxs_convertVector3(_in.contacts(i).point(),
524 &_out->contacts[i].point);
525 result &= hxs_convertVector3(_in.contacts(i).normal(),
526 &_out->contacts[i].normal);
527 _out->contacts[i].distance = _in.contacts(i).distance();
528 result &= hxs_convertVector3(_in.contacts(i).wrench().force(),
529 &_out->contacts[i].wrench.force);
530 result &= hxs_convertVector3(_in.contacts(i).wrench().torque(),
531 &_out->contacts[i].wrench.torque);
543 static bool hxs_convertSimInfo(
const haptix::comm::msgs::hxSimInfo _in,
548 std::cerr <<
"hxs_convertSimInfo() error: NULL output" << std::endl;
553 memset(_out, 0,
sizeof(hxsSimInfo));
555 _out->model_count = _in.models_size();
560 for (
int i = 0; i < _out->model_count; ++i)
561 result &= hxs_convertModel(_in.models(i), &_out->models[i]);
564 result &= hxs_convertTransform(_in.camera_transform(),
565 &_out->camera_transform);
581 template<
typename REQ,
typename REP,
typename T>
583 const std::string &_funcName,
587 bool (*_f)(
const REP _rep, T _dst))
590 ignition::transport::Node *hxNode = getHxNode();
593 bool executed = hxNode->Request(_service, _req,
kTimeout, _rep, result);
607 std::lock_guard<std::mutex> lock(lastResultLock);
608 lastResult = _funcName +
"() Service call failed.";
614 std::lock_guard<std::mutex> lock(lastResultLock);
615 lastResult = _funcName +
"() Service call timed out.";
628 template<
typename REQ,
typename REP>
630 const std::string &_funcName,
635 ignition::transport::Node *hxNode = getHxNode();
638 bool executed = hxNode->Request(_service, _req,
kTimeout, _rep, result);
646 std::lock_guard<std::mutex> lock(lastResultLock);
647 lastResult = _funcName +
"() Service call failed.";
653 std::lock_guard<std::mutex> lock(lastResultLock);
654 lastResult = _funcName +
"() Service call timed out.";
unsigned int kTimeout
Timeout used for the service requests (ms.).
Definition: haptix_sim_utils.h:43
hxResult hxs_call(const std::string &_service, const std::string &_funcName, const REQ _req, REP _rep, T _dst, bool(*_f)(const REP _rep, T _dst))
Function that requests a given service and converts the protobuf response to a C-struct.
Definition: haptix_sim_utils.h:582
success
Definition: haptix.h:63
#define hxsMAXNAMESIZE
Maximum number of characters allowed per name.
Definition: haptix_sim.h:46
failure
Definition: haptix.h:65
static ignition::transport::Node * haptixSimUtilsNode
ignition transport node.
Definition: haptix_sim_utils.h:50
hxResult
API return codes.
Definition: haptix.h:60
Structures and functions for the primary HAPTIX API.
static std::string lastResult
Error string, to be retrieved by hx_last_result()
Definition: haptix_sim_utils.h:46