42 std::lock_guard<std::recursive_mutex> l(m_mutex);
48 std::lock_guard<std::recursive_mutex> guard(m_mutex);
52 double tmpDT = m_now - m_prev;
54 if (tmpDT>m_deltaTMax)
56 if (tmpDT<m_deltaTMin)
75 getEnvelope(newStamp);
78 if (m_lastStamp.isValid() ==
false)
80 m_lastStamp = newStamp;
91 for (
int i = 0; i < bsize; i++)
108 m_transforms.push_back(
t);
116 m_lastStamp = newStamp;
121 std::lock_guard<std::recursive_mutex> guard(m_mutex);
135 std::lock_guard<std::recursive_mutex> guard(m_mutex);
143 std::lock_guard<std::recursive_mutex> guard(m_mutex);
145 min=m_deltaTMin*1000;
146 max=m_deltaTMax*1000;
160 std::lock_guard<std::recursive_mutex> l(m_mutex);
161 m_transforms.clear();
173 if (!this->open(local_streaming_name))
175 yCError(FRAMETRANSFORMCLIENT,
"open(): Could not open port %s, check network", local_streaming_name.c_str());
188 std::lock_guard<std::recursive_mutex> l(m_mutex);
189 return m_transforms.size();
194 std::lock_guard<std::recursive_mutex> l(m_mutex);
195 return m_transforms[idx];
201 std::lock_guard<std::mutex> lock (m_rpc_mutex);
204 bool ok = in.
read(connection);
205 if (!ok)
return false;
208 if (request ==
"help")
211 out.
addString(
"'get_transform <src> <dst>: print the transform from <src> to <dst>");
212 out.
addString(
"'list_frames: print all the available reference frames");
213 out.
addString(
"'list_ports: print all the opened ports for transform broadcasting");
214 out.
addString(
"'publish_transform <src> <dst> <portname> <format>: opens a port to publish transform from src to dst");
215 out.
addString(
"'unpublish_transform <portname>: closes a previously opened port to publish a transform");
216 out.
addString(
"'unpublish_all <portname>: closes a all previously opened ports to publish a transform");
220 else if (request ==
"is_connected")
222 if (isConnectedWithServer())
231 else if (request ==
"reconnect")
233 if (reconnectWithServer())
242 else if (request ==
"list_frames")
244 std::vector<std::string> v;
245 this->getAllFrameIds(v);
247 out.
addString(
"List of available reference frames:");
252 std::string str = std::to_string(count) +
"- " + vec;
256 else if (request ==
"get_transform")
262 this->getTransform(src, dst, m);
263 out.
addString(
"Transform from " + src +
" to " + dst +
" is: ");
266 else if (request ==
"list_ports")
269 if (m_array_of_ports.size()==0)
271 out.
addString(
"No ports are currently active");
273 for (
auto& m_array_of_port : m_array_of_ports)
277 std::string s = m_array_of_port->port.getName() +
" "+ m_array_of_port->transform_src +
" -> " + m_array_of_port->transform_dst;
282 else if (request ==
"publish_transform")
288 std::string format =
"matrix";
291 if (port_name[0]==
'/') port_name.erase(port_name.begin());
292 std::string full_port_name = m_local_name +
"/" + port_name;
294 for (
auto& m_array_of_port : m_array_of_ports)
296 if (m_array_of_port && m_array_of_port->port.getName() == full_port_name)
302 if (this->frameExists(src)==
false)
304 out.
addString(
"Requested src frame " + src +
" does not exists.");
305 yCWarning(FRAMETRANSFORMCLIENT,
"Requested src frame %s does not exists.", src.c_str());
307 if (this->frameExists(dst)==
false)
309 out.
addString(
"Requested dst frame " + dst +
" does not exists.");
310 yCWarning(FRAMETRANSFORMCLIENT,
"Requested fst frame %s does not exists.", dst.c_str());
316 b->transform_dst = dst;
318 bool pret = b->port.open(full_port_name);
321 out.
addString(
"Operation complete. Port " + full_port_name +
" opened.");
322 m_array_of_ports.push_back(b);
323 if (m_array_of_ports.size()==1) this->start();
328 out.
addString(
"Operation failed. Unable to open port " + full_port_name +
".");
333 out.
addString(
"unable to perform operation");
336 else if (request ==
"unpublish_all")
338 for (
auto& m_array_of_port : m_array_of_ports)
340 m_array_of_port->port.close();
341 delete m_array_of_port;
342 m_array_of_port=
nullptr;
344 m_array_of_ports.clear();
345 if (m_array_of_ports.size()==0) this->askToStop();
348 else if (request ==
"unpublish_transform")
352 if (port_name[0]==
'/') port_name.erase(port_name.begin());
353 std::string full_port_name = m_local_name +
"/" + port_name;
354 for (
auto it = m_array_of_ports.begin(); it != m_array_of_ports.end(); it++)
356 if ((*it)->port.getName() == port_name)
361 m_array_of_ports.erase(it);
368 out.
addString(
"Port " + full_port_name +
" has been closed.");
372 out.
addString(
"Port " + full_port_name +
" was not found.");
374 if (m_array_of_ports.size()==0) this->askToStop();
378 yCError(FRAMETRANSFORMCLIENT,
"Invalid vocab received in FrameTransformClient");
385 if (returnToSender !=
nullptr)
387 out.
write(*returnToSender);
391 yCError(FRAMETRANSFORMCLIENT) <<
"Invalid return to sender";
398 m_local_name.clear();
399 m_remote_name.clear();
403 m_streaming_connection_type =
"udp";
405 if (m_local_name ==
"")
407 yCError(FRAMETRANSFORMCLIENT,
"open(): Invalid local name");
410 if (m_remote_name ==
"")
412 yCError(FRAMETRANSFORMCLIENT,
"open(): Invalid remote name");
416 if (config.
check(
"period"))
418 m_period = config.
find(
"period").
asInt32() / 1000.0;
423 yCWarning(FRAMETRANSFORMCLIENT,
"Using default period of %f s" , m_period);
426 m_local_rpcServer = m_local_name +
"/rpc:o";
427 m_local_rpcUser = m_local_name +
"/rpc:i";
428 m_remote_rpc = m_remote_name +
"/rpc";
429 m_remote_streaming_name = m_remote_name +
"/transforms:o";
430 m_local_streaming_name = m_local_name +
"/transforms:i";
432 if (!m_rpc_InterfaceToUser.open(m_local_rpcUser))
434 yCError(FRAMETRANSFORMCLIENT,
"open(): Could not open rpc port %s, check network", m_local_rpcUser.c_str());
438 if (!m_rpc_InterfaceToServer.open(m_local_rpcServer))
440 yCError(FRAMETRANSFORMCLIENT,
"open(): Could not open rpc port %s, check network", m_local_rpcServer.c_str());
445 bool ok = Network::connect(m_remote_streaming_name.c_str(), m_local_streaming_name.c_str(), m_streaming_connection_type.c_str());
448 yCError(FRAMETRANSFORMCLIENT,
"open(): Could not connect to %s", m_remote_streaming_name.c_str());
452 ok = Network::connect(m_local_rpcServer, m_remote_rpc);
455 yCError(FRAMETRANSFORMCLIENT,
"open(): Could not connect to %s", m_remote_rpc.c_str());
460 m_rpc_InterfaceToUser.setReader(*
this);
466 m_rpc_InterfaceToServer.close();
467 m_rpc_InterfaceToUser.close();
468 if (m_transform_storage !=
nullptr)
470 delete m_transform_storage;
471 m_transform_storage =
nullptr;
478 for (
size_t i = 0; i < m_transform_storage->size(); i++)
480 all_frames += (*m_transform_storage)[i].toString() +
" ";
485 FrameTransformClient::ConnectionType FrameTransformClient::getConnectionType(
const std::string &target_frame,
const std::string &source_frame, std::string* commonAncestor =
nullptr)
487 if (target_frame == source_frame) {
return IDENTITY;}
491 std::vector<std::string> tar2root_vec;
492 std::vector<std::string> src2root_vec;
493 std::string ancestor, child;
494 child = target_frame;
495 std::lock_guard<std::recursive_mutex> l(tfVec.
m_mutex);
496 while(getParent(child, ancestor))
498 if(ancestor == source_frame)
503 tar2root_vec.push_back(ancestor);
506 child = source_frame;
507 while(getParent(child, ancestor))
509 if(ancestor == target_frame)
514 src2root_vec.push_back(ancestor);
518 for(i = 0; i < tar2root_vec.size(); i++)
522 for(j = 0; j < src2root_vec.size(); j++)
524 if(a == src2root_vec[j])
540 return getConnectionType(target_frame, source_frame) != DISCONNECTED;
549 bool ret = m_rpc_InterfaceToServer.write(b, resp);
554 yCError(FRAMETRANSFORMCLIENT) <<
"clear(): Received error from server";
560 yCError(FRAMETRANSFORMCLIENT) <<
"clear(): Error on writing on rpc port";
564 m_transform_storage->clear();
570 for (
size_t i = 0; i < m_transform_storage->size(); i++)
572 if (((*m_transform_storage)[i].src_frame_id) == frame_id) {
return true; }
573 if (((*m_transform_storage)[i].dst_frame_id) == frame_id) {
return true; }
580 for (
size_t i = 0; i < m_transform_storage->size(); i++)
583 for (
const auto&
id : ids)
585 if (((*m_transform_storage)[i].src_frame_id) ==
id) { found =
true;
break; }
587 if (found ==
false) ids.push_back((*m_transform_storage)[i].src_frame_id);
590 for (
size_t i = 0; i < m_transform_storage->size(); i++)
593 for (
const auto&
id : ids)
595 if (((*m_transform_storage)[i].dst_frame_id) ==
id) { found =
true;
break; }
597 if (found ==
false) ids.push_back((*m_transform_storage)[i].dst_frame_id);
605 for (
size_t i = 0; i < m_transform_storage->size(); i++)
607 std::string par((*m_transform_storage)[i].dst_frame_id);
608 if (((*m_transform_storage)[i].dst_frame_id == frame_id))
611 parent_frame_id = (*m_transform_storage)[i].src_frame_id;
618 bool FrameTransformClient::canExplicitTransform(
const std::string& target_frame_id,
const std::string& source_frame_id)
const
621 size_t i, tfVec_size;
622 std::lock_guard<std::recursive_mutex> l(tfVec.
m_mutex);
624 tfVec_size = tfVec.
size();
625 for (i = 0; i < tfVec_size; i++)
627 if (tfVec[i].dst_frame_id == target_frame_id && tfVec[i].src_frame_id == source_frame_id)
635 bool FrameTransformClient::getChainedTransform(
const std::string& target_frame_id,
const std::string& source_frame_id,
yarp::sig::Matrix& transform)
const
638 size_t i, tfVec_size;
639 std::lock_guard<std::recursive_mutex> l(tfVec.
m_mutex);
641 tfVec_size = tfVec.
size();
642 for (i = 0; i < tfVec_size; i++)
644 if (tfVec[i].dst_frame_id == target_frame_id)
646 if (tfVec[i].src_frame_id == source_frame_id)
648 transform = tfVec[i].toMatrix();
654 if (getChainedTransform(tfVec[i].src_frame_id, source_frame_id, m))
656 transform = m * tfVec[i].toMatrix();
668 std::string ancestor;
669 ct = getConnectionType(target_frame_id, source_frame_id, &ancestor);
672 return getChainedTransform(target_frame_id, source_frame_id, transform);
674 else if (ct == INVERSE)
677 getChainedTransform(source_frame_id, target_frame_id, m);
681 else if(ct == UNDIRECT)
684 getChainedTransform(source_frame_id, ancestor, root2src);
685 getChainedTransform(target_frame_id, ancestor, root2tar);
689 else if (ct == IDENTITY)
696 yCError(FRAMETRANSFORMCLIENT) <<
"getTransform(): Frames " << source_frame_id <<
" and " << target_frame_id <<
" are not connected";
702 if(target_frame_id == source_frame_id)
704 yCError(FRAMETRANSFORMCLIENT) <<
"setTransform(): Invalid transform detected.\n" \
705 "\t Source frame and target frame are both equal to " << source_frame_id;
709 if (!canExplicitTransform(target_frame_id, source_frame_id) && canTransform(target_frame_id, source_frame_id))
711 yCError(FRAMETRANSFORMCLIENT) <<
"setTransform(): Such transform already exist by chaining transforms";
719 if (!
tf.fromMatrix(transform))
721 yCError(FRAMETRANSFORMCLIENT) <<
"setTransform(): Wrong matrix format, it has to be 4 by 4";
737 bool ret = m_rpc_InterfaceToServer.write(b, resp);
742 yCError(FRAMETRANSFORMCLIENT) <<
"setTransform(): Received error from server on creating frame between " + source_frame_id +
" and " + target_frame_id;
748 yCError(FRAMETRANSFORMCLIENT) <<
"setTransform(): Error on writing on rpc port";
756 if(target_frame_id == source_frame_id)
758 yCError(FRAMETRANSFORMCLIENT) <<
"setTransformStatic(): Invalid transform detected.\n" \
759 "\t Source frame and target frame are both equal to " << source_frame_id;
763 if (canTransform(target_frame_id, source_frame_id))
765 yCError(FRAMETRANSFORMCLIENT) <<
"setTransform(): Such static transform already exist, directly or by chaining transforms";
773 if (!
tf.fromMatrix(transform))
775 yCError(FRAMETRANSFORMCLIENT) <<
"setTransform(): Wrong matrix format, it has to be 4 by 4";
791 bool ret = m_rpc_InterfaceToServer.write(b, resp);
796 yCError(FRAMETRANSFORMCLIENT) <<
"setTransform(): Received error from server on creating frame between " + source_frame_id +
" and " + target_frame_id;
802 yCError(FRAMETRANSFORMCLIENT) <<
"setTransform(): Error on writing on rpc port";
816 bool ret = m_rpc_InterfaceToServer.write(b, resp);
821 yCError(FRAMETRANSFORMCLIENT) <<
"Received error from server on deleting frame between "+source_frame_id+
" and "+target_frame_id;
827 yCError(FRAMETRANSFORMCLIENT) <<
"deleteFrame(): Error on writing on rpc port";
835 if (input_point.
size() != 3)
837 yCError(FRAMETRANSFORMCLIENT) <<
"Only 3 dimensional vector allowed.";
841 if (!getTransform(target_frame_id, source_frame_id, m))
843 yCError(FRAMETRANSFORMCLIENT) <<
"No transform found between source '" << target_frame_id <<
"' and target '" << source_frame_id <<
"'";
848 transformed_point = m * in;
855 if (input_pose.
size() != 6)
857 yCError(FRAMETRANSFORMCLIENT) <<
"Only 6 dimensional vector (3 axes + roll pith and yaw) allowed.";
860 if (transformed_pose.
size() != 6)
862 yCWarning(FRAMETRANSFORMCLIENT,
"transformPose(): Performance warning: size transformed_pose should be 6, resizing.");
863 transformed_pose.
resize(6, 0.0);
866 if (!getTransform(target_frame_id, source_frame_id, m))
868 yCError(FRAMETRANSFORMCLIENT) <<
"No transform found between source '" << target_frame_id <<
"' and target '" << source_frame_id <<
"'";
872 t.transFromVec(input_pose[0], input_pose[1], input_pose[2]);
873 t.rotFromRPY(input_pose[3], input_pose[4], input_pose[5]);
874 t.fromMatrix(m *
t.toMatrix());
875 transformed_pose[0] =
t.translation.tX;
876 transformed_pose[1] =
t.translation.tY;
877 transformed_pose[2] =
t.translation.tZ;
881 transformed_pose[3] = rot[0];
882 transformed_pose[4] = rot[1];
883 transformed_pose[5] = rot[2];
890 if (!getTransform(target_frame_id, source_frame_id, m))
892 yCError(FRAMETRANSFORMCLIENT) <<
"No transform found between source '" << target_frame_id <<
"' and target '" << source_frame_id <<
"'";
896 t.rotation=input_quaternion;
905 while (!canTransform(target_frame_id, source_frame_id))
909 yCError(FRAMETRANSFORMCLIENT) <<
"waitForTransform(): timeout expired";
918 m_transform_storage(nullptr),
927 yCTrace(FRAMETRANSFORMCLIENT,
"Thread started");
933 yCTrace(FRAMETRANSFORMCLIENT,
"Thread stopped");
948 std::string src = m_array_of_port->transform_src;
949 std::string dst = m_array_of_port->transform_dst;
952 if (m_array_of_port->format ==
"matrix")
954 m_array_of_port->port.write(m);
958 yCError(FRAMETRANSFORMCLIENT) <<
"Unknown format requested: " << m_array_of_port->format;
987 yCError(FRAMETRANSFORMCLIENT,
"reconnectWithServer(): Could not connect to %s",
m_remote_rpc.c_str());