frames_io.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /** \file itasc/kdl/frames_io.cpp
  2. * \ingroup itasc
  3. */
  4. /***************************************************************************
  5. frames_io.h - description
  6. -------------------------
  7. begin : June 2006
  8. copyright : (C) 2006 Erwin Aertbelien
  9. email : firstname.lastname@mech.kuleuven.ac.be
  10. History (only major changes)( AUTHOR-Description ) :
  11. ***************************************************************************
  12. * This library is free software; you can redistribute it and/or *
  13. * modify it under the terms of the GNU Lesser General Public *
  14. * License as published by the Free Software Foundation; either *
  15. * version 2.1 of the License, or (at your option) any later version. *
  16. * *
  17. * This library is distributed in the hope that it will be useful, *
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  20. * Lesser General Public License for more details. *
  21. * *
  22. * You should have received a copy of the GNU Lesser General Public *
  23. * License along with this library; if not, write to the Free Software *
  24. * Foundation, Inc., 51 Franklin Street, *
  25. * Fifth Floor, Boston, MA 02110-1301, USA. *
  26. * *
  27. ***************************************************************************/
  28. #include "utilities/error.h"
  29. #include "utilities/error_stack.h"
  30. #include "frames.hpp"
  31. #include "frames_io.hpp"
  32. #include <stdlib.h>
  33. #include <ctype.h>
  34. #include <string.h>
  35. #include <iostream>
  36. namespace KDL {
  37. std::ostream& operator << (std::ostream& os,const Vector& v) {
  38. os << "[" << std::setw(KDL_FRAME_WIDTH) << v(0) << "," << std::setw(KDL_FRAME_WIDTH)<<v(1)
  39. << "," << std::setw(KDL_FRAME_WIDTH) << v(2) << "]";
  40. return os;
  41. }
  42. std::ostream& operator << (std::ostream& os,const Twist& v) {
  43. os << "[" << std::setw(KDL_FRAME_WIDTH) << v.vel(0)
  44. << "," << std::setw(KDL_FRAME_WIDTH) << v.vel(1)
  45. << "," << std::setw(KDL_FRAME_WIDTH) << v.vel(2)
  46. << "," << std::setw(KDL_FRAME_WIDTH) << v.rot(0)
  47. << "," << std::setw(KDL_FRAME_WIDTH) << v.rot(1)
  48. << "," << std::setw(KDL_FRAME_WIDTH) << v.rot(2)
  49. << "]";
  50. return os;
  51. }
  52. std::ostream& operator << (std::ostream& os,const Wrench& v) {
  53. os << "[" << std::setw(KDL_FRAME_WIDTH) << v.force(0)
  54. << "," << std::setw(KDL_FRAME_WIDTH) << v.force(1)
  55. << "," << std::setw(KDL_FRAME_WIDTH) << v.force(2)
  56. << "," << std::setw(KDL_FRAME_WIDTH) << v.torque(0)
  57. << "," << std::setw(KDL_FRAME_WIDTH) << v.torque(1)
  58. << "," << std::setw(KDL_FRAME_WIDTH) << v.torque(2)
  59. << "]";
  60. return os;
  61. }
  62. std::ostream& operator << (std::ostream& os,const Rotation& R) {
  63. #ifdef KDL_ROTATION_PROPERTIES_RPY
  64. double r,p,y;
  65. R.GetRPY(r,p,y);
  66. os << "[RPY]"<<endl;
  67. os << "[";
  68. os << std::setw(KDL_FRAME_WIDTH) << r << ",";
  69. os << std::setw(KDL_FRAME_WIDTH) << p << ",";
  70. os << std::setw(KDL_FRAME_WIDTH) << y << "]";
  71. #else
  72. # ifdef KDL_ROTATION_PROPERTIES_EULER
  73. double z,y,x;
  74. R.GetEulerZYX(z,y,x);
  75. os << "[EULERZYX]"<<endl;
  76. os << "[";
  77. os << std::setw(KDL_FRAME_WIDTH) << z << ",";
  78. os << std::setw(KDL_FRAME_WIDTH) << y << ",";
  79. os << std::setw(KDL_FRAME_WIDTH) << x << "]";
  80. # else
  81. os << "[";
  82. for (int i=0;i<=2;i++) {
  83. os << std::setw(KDL_FRAME_WIDTH) << R(i,0) << "," <<
  84. std::setw(KDL_FRAME_WIDTH) << R(i,1) << "," <<
  85. std::setw(KDL_FRAME_WIDTH) << R(i,2);
  86. if (i<2)
  87. os << ";"<< std::endl << " ";
  88. else
  89. os << "]";
  90. }
  91. # endif
  92. #endif
  93. return os;
  94. }
  95. std::ostream& operator << (std::ostream& os, const Frame& T)
  96. {
  97. os << "[" << T.M << std::endl<< T.p << "]";
  98. return os;
  99. }
  100. std::ostream& operator << (std::ostream& os,const Vector2& v) {
  101. os << "[" << std::setw(KDL_FRAME_WIDTH) << v(0) << "," << std::setw(KDL_FRAME_WIDTH)<<v(1)
  102. << "]";
  103. return os;
  104. }
  105. // Rotation2 gives back an angle in degrees with the << and >> operators.
  106. std::ostream& operator << (std::ostream& os,const Rotation2& R) {
  107. os << "[" << R.GetRot()*rad2deg << "]";
  108. return os;
  109. }
  110. std::ostream& operator << (std::ostream& os, const Frame2& T)
  111. {
  112. os << T.M << T.p;
  113. return os;
  114. }
  115. std::istream& operator >> (std::istream& is,Vector& v)
  116. { IOTrace("Stream input Vector (vector or ZERO)");
  117. char storage[10];
  118. EatWord(is,"[]",storage,10);
  119. if (storage[0]=='\0') {
  120. Eat(is,'[');
  121. is >> v(0);
  122. Eat(is,',');
  123. is >> v(1);
  124. Eat(is,',');
  125. is >> v(2);
  126. EatEnd(is,']');
  127. IOTracePop();
  128. return is;
  129. }
  130. if (strcmp(storage,"ZERO")==0) {
  131. v = Vector::Zero();
  132. IOTracePop();
  133. return is;
  134. }
  135. throw Error_Frame_Vector_Unexpected_id();
  136. }
  137. std::istream& operator >> (std::istream& is,Twist& v)
  138. { IOTrace("Stream input Twist");
  139. Eat(is,'[');
  140. is >> v.vel(0);
  141. Eat(is,',');
  142. is >> v.vel(1);
  143. Eat(is,',');
  144. is >> v.vel(2);
  145. Eat(is,',');
  146. is >> v.rot(0);
  147. Eat(is,',');
  148. is >> v.rot(1);
  149. Eat(is,',');
  150. is >> v.rot(2);
  151. EatEnd(is,']');
  152. IOTracePop();
  153. return is;
  154. }
  155. std::istream& operator >> (std::istream& is,Wrench& v)
  156. { IOTrace("Stream input Wrench");
  157. Eat(is,'[');
  158. is >> v.force(0);
  159. Eat(is,',');
  160. is >> v.force(1);
  161. Eat(is,',');
  162. is >> v.force(2);
  163. Eat(is,',');
  164. is >> v.torque(0);
  165. Eat(is,',');
  166. is >> v.torque(1);
  167. Eat(is,',');
  168. is >> v.torque(2);
  169. EatEnd(is,']');
  170. IOTracePop();
  171. return is;
  172. }
  173. std::istream& operator >> (std::istream& is,Rotation& r)
  174. { IOTrace("Stream input Rotation (Matrix or EULERZYX, EULERZYZ,RPY, ROT, IDENTITY)");
  175. char storage[10];
  176. EatWord(is,"[]",storage,10);
  177. if (storage[0]=='\0') {
  178. Eat(is,'[');
  179. for (int i=0;i<3;i++) {
  180. is >> r(i,0);
  181. Eat(is,',') ;
  182. is >> r(i,1);
  183. Eat(is,',');
  184. is >> r(i,2);
  185. if (i<2)
  186. Eat(is,';');
  187. else
  188. EatEnd(is,']');
  189. }
  190. IOTracePop();
  191. return is;
  192. }
  193. Vector v;
  194. if (strcmp(storage,"EULERZYX")==0) {
  195. is >> v;
  196. v=v*deg2rad;
  197. r = Rotation::EulerZYX(v(0),v(1),v(2));
  198. IOTracePop();
  199. return is;
  200. }
  201. if (strcmp(storage,"EULERZYZ")==0) {
  202. is >> v;
  203. v=v*deg2rad;
  204. r = Rotation::EulerZYZ(v(0),v(1),v(2));
  205. IOTracePop();
  206. return is;
  207. }
  208. if (strcmp(storage,"RPY")==0) {
  209. is >> v;
  210. v=v*deg2rad;
  211. r = Rotation::RPY(v(0),v(1),v(2));
  212. IOTracePop();
  213. return is;
  214. }
  215. if (strcmp(storage,"ROT")==0) {
  216. is >> v;
  217. double angle;
  218. Eat(is,'[');
  219. is >> angle;
  220. EatEnd(is,']');
  221. r = Rotation::Rot(v,angle*deg2rad);
  222. IOTracePop();
  223. return is;
  224. }
  225. if (strcmp(storage,"IDENTITY")==0) {
  226. r = Rotation::Identity();
  227. IOTracePop();
  228. return is;
  229. }
  230. throw Error_Frame_Rotation_Unexpected_id();
  231. return is;
  232. }
  233. std::istream& operator >> (std::istream& is,Frame& T)
  234. { IOTrace("Stream input Frame (Rotation,Vector) or DH[...]");
  235. char storage[10];
  236. EatWord(is,"[",storage,10);
  237. if (storage[0]=='\0') {
  238. Eat(is,'[');
  239. is >> T.M;
  240. is >> T.p;
  241. EatEnd(is,']');
  242. IOTracePop();
  243. return is;
  244. }
  245. if (strcmp(storage,"DH")==0) {
  246. double a,alpha,d,theta;
  247. Eat(is,'[');
  248. is >> a;
  249. Eat(is,',');
  250. is >> alpha;
  251. Eat(is,',');
  252. is >> d;
  253. Eat(is,',');
  254. is >> theta;
  255. EatEnd(is,']');
  256. T = Frame::DH(a,alpha*deg2rad,d,theta*deg2rad);
  257. IOTracePop();
  258. return is;
  259. }
  260. throw Error_Frame_Frame_Unexpected_id();
  261. return is;
  262. }
  263. std::istream& operator >> (std::istream& is,Vector2& v)
  264. { IOTrace("Stream input Vector2");
  265. Eat(is,'[');
  266. is >> v(0);
  267. Eat(is,',');
  268. is >> v(1);
  269. EatEnd(is,']');
  270. IOTracePop();
  271. return is;
  272. }
  273. std::istream& operator >> (std::istream& is,Rotation2& r)
  274. { IOTrace("Stream input Rotation2");
  275. Eat(is,'[');
  276. double val;
  277. is >> val;
  278. r.Rot(val*deg2rad);
  279. EatEnd(is,']');
  280. IOTracePop();
  281. return is;
  282. }
  283. std::istream& operator >> (std::istream& is,Frame2& T)
  284. { IOTrace("Stream input Frame2");
  285. is >> T.M;
  286. is >> T.p;
  287. IOTracePop();
  288. return is;
  289. }
  290. } // namespace Frame