PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/brown-ros-pkg-read-only/experimental/youbot_brown/youbot_imarker/src/youbot_im_test.cpp

https://github.com/dutchcheesehead/ROSMAV
C++ | 472 lines | 289 code | 102 blank | 81 comment | 10 complexity | 708938d0162068c7de630465406a9ec8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #include <ros/ros.h>
  2. #include <tf/transform_listener.h>
  3. #include <tf/transform_broadcaster.h>
  4. #include <interactive_markers/interactive_marker_server.h>
  5. #include "youbot_imarker/diffval.h"
  6. #include "youbot_with_kinect_ik.cpp"
  7. #include "trajectory_msgs/JointTrajectory.h"
  8. #include "brics_actuator/CartesianWrench.h"
  9. #include <sensor_msgs/JointState.h>
  10. #include <boost/units/io.hpp>
  11. #include <boost/units/systems/angle/degrees.hpp>
  12. #include <boost/units/conversion.hpp>
  13. #include <iostream>
  14. #include <assert.h>
  15. #include "brics_actuator/JointPositions.h"
  16. #include <boost/units/systems/si/length.hpp>
  17. #include <boost/units/systems/si/plane_angle.hpp>
  18. #include <boost/units/io.hpp>
  19. #include <boost/units/systems/angle/degrees.hpp>
  20. #include <boost/units/conversion.hpp>
  21. #include <LinearMath/btVector3.h>
  22. #include <LinearMath/btMatrix3x3.h>
  23. #include <LinearMath/btQuaternion.h>
  24. double orx1,ory1,orz1;
  25. double arm_joint4_val, arm_joint5_val;
  26. bool chit;
  27. //bool diffbr;
  28. ros::Publisher diffval_pub;
  29. ros::Publisher armPositionsPublisher;
  30. ros::Publisher gripperPositionPublisher;
  31. using namespace std;
  32. /*
  33. static void prVector3(const btVector3 &vecarg)
  34. {
  35. cout << vecarg[0] << " " << vecarg[1] << " " << vecarg[2];
  36. }
  37. */
  38. static void prQuaternion(const btQuaternion &qarg)
  39. {
  40. cout << qarg.x() << " " << qarg.y() << " " << qarg.z() << " " << qarg.w();
  41. }
  42. static void prMatrix3(const btMatrix3x3 &marg)
  43. {
  44. cout << marg[0][0] << " " << marg[0][1] << " " << marg[0][2] << "\n";
  45. cout << marg[1][0] << " " << marg[1][1] << " " << marg[1][2] << "\n";
  46. cout << marg[2][0] << " " << marg[2][1] << " " << marg[2][2] << "\n";
  47. }
  48. int control_robot(youbot_imarker::diffval dval)
  49. {
  50. // Get the robot's current end effector pose
  51. tf::TransformListener listener;
  52. tf::StampedTransform transform;
  53. try
  54. {
  55. //listener.waitForTransform("/base_link", "/gripper_palm_link", ros::Time(0),ros::Duration(1.0));
  56. //listener.lookupTransform("/base_link", "/gripper_palm_link", ros::Time(0),transform);
  57. listener.waitForTransform("/arm_link_0", "/arm_link_5", ros::Time(0),ros::Duration(1.0));
  58. listener.lookupTransform("/arm_link_0", "/arm_link_5", ros::Time(0),transform);
  59. }
  60. catch (tf::TransformException ex){
  61. ROS_ERROR("%s",ex.what());
  62. ros::Duration(1.0).sleep();
  63. }
  64. double px,py,pz;
  65. double nx,ny,nz;
  66. double qx,qy,qz,qw;
  67. px=py=pz=0.0;
  68. nx=ny=nz=0.0;
  69. qx=qy=qz=qw=0.0;
  70. px = transform.getOrigin().x();
  71. py = transform.getOrigin().y();
  72. pz = transform.getOrigin().z();
  73. printf("Gripper pose(translation):\n");
  74. printf("px: %3.15f, ", px);
  75. printf("py: %3.15f, ", py);
  76. printf("pz: %3.15f\n", pz);
  77. qx = transform.getRotation().x();
  78. qy = transform.getRotation().y();
  79. qz = transform.getRotation().z();
  80. qw = transform.getRotation().w();
  81. btQuaternion qrot(qx,qy,qz,qw);
  82. cout << "qval: " << qx << ", " << qy << ", " << qz << ", " << qw << "\n";
  83. cout << "Quaternion: ";
  84. prQuaternion(qrot);
  85. cout << "\n";
  86. btMatrix3x3 rotmat(qrot);
  87. cout << "rotmat: ";
  88. prMatrix3(rotmat);
  89. cout << "\n";
  90. printf("diffval:\n");
  91. printf("xx: %3.15f, ", dval.xx);
  92. printf("yy: %3.15f, ", dval.yy);
  93. printf("zz: %3.15f\n", dval.zz);
  94. nx = px + dval.xx;
  95. ny = py + dval.yy;
  96. nz = pz + dval.zz;
  97. printf("Sending pose:\n");
  98. printf("nx: %3.15f, ", nx);
  99. printf("ny: %3.15f, ", ny);
  100. printf("nz: %3.15f\n\n", nz);
  101. std::vector<IKSolution> vsolutions;
  102. std::vector<IKReal> vfree(2);
  103. IKReal eerot[9],eetrans[3];
  104. /*
  105. eerot[0] = atof(argv[1]); eerot[1] = atof(argv[2]); eerot[2] = atof(argv[3]); eetrans[0] = atof(argv[4]);
  106. eerot[3] = atof(argv[5]); eerot[4] = atof(argv[6]); eerot[5] = atof(argv[7]); eetrans[1] = atof(argv[8]);
  107. eerot[6] = atof(argv[9]); eerot[7] = atof(argv[10]); eerot[8] = atof(argv[11]); eetrans[2] = atof(argv[12]);
  108. for(std::size_t i = 0; i < vfree.size(); ++i)
  109. vfree[i] = atof(argv[13+i]);
  110. */
  111. eerot[0]=rotmat[0][0];
  112. eerot[1]=rotmat[0][1];
  113. eerot[2]=rotmat[0][2];
  114. eetrans[0]=nx;
  115. eerot[3]=rotmat[1][0];
  116. eerot[4]=rotmat[1][1];
  117. eerot[5]=rotmat[1][2];
  118. eetrans[1]=ny;
  119. eerot[6]=rotmat[2][0];
  120. eerot[7]=rotmat[2][1];
  121. eerot[8]=rotmat[2][2];
  122. eetrans[2]=nz;
  123. vfree[0]=3;
  124. vfree[1]=4;
  125. bool bSuccess = ik(eetrans, eerot, &vfree[0], vsolutions);
  126. if( !bSuccess ) {
  127. fprintf(stderr,"Failed to get ik solution\n");
  128. return -1;
  129. }
  130. printf("Found %d ik solutions:\n", (int)vsolutions.size());
  131. std::vector<IKReal> sol(getNumJoints());
  132. for(std::size_t i = 0; i < vsolutions.size(); ++i) {
  133. printf("sol%d (free=%d): ", (int)i, (int)vsolutions[i].GetFree().size());
  134. std::vector<IKReal> vsolfree(vsolutions[i].GetFree().size());
  135. vsolutions[i].GetSolution(&sol[0],vsolfree.size()>0?&vsolfree[0]:NULL);
  136. for( std::size_t j = 0; j < sol.size(); ++j)
  137. printf("%.15f, ", sol[j]);
  138. printf("\n");
  139. }
  140. // Need to get the robot's current configuration
  141. static const int use_sol_number=0;
  142. static const int numberOfArmJoints = 5;
  143. static const int numberOfGripperJoints = 2;
  144. brics_actuator::JointPositions command;
  145. vector <brics_actuator::JointValue> armJointPositions;
  146. vector <brics_actuator::JointValue> gripperJointPositions;
  147. armJointPositions.resize(numberOfArmJoints); //TODO:change that
  148. gripperJointPositions.resize(numberOfGripperJoints);
  149. std::vector<IKReal> sol_temp(getNumJoints());
  150. std::vector<IKReal> vsolfree_temp(vsolutions[use_sol_number].GetFree().size());
  151. vsolutions[use_sol_number].GetSolution(&sol_temp[0],vsolfree_temp.size()>0?&vsolfree_temp[0]:NULL);
  152. //for( std::size_t j = 0; j < sol.size(); ++j)
  153. //printf("%.15f, ", sol[j]);
  154. std::stringstream jointName;
  155. // ::io::base_unit_info <boost::units::si::angular_velocity>).name();
  156. int i=0;
  157. for (; i < (numberOfArmJoints-2); ++i) {
  158. jointName.str("");
  159. jointName << "arm_joint_" << (i + 1);
  160. armJointPositions[i].joint_uri = jointName.str();
  161. armJointPositions[i].value = sol_temp[i];
  162. armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
  163. cout << "Joint " << armJointPositions[i].joint_uri << " = " << armJointPositions[i].value << " " << armJointPositions[i].unit << endl;
  164. };
  165. // Fill in joint values for the free-indices
  166. // This is for arm_joint_4
  167. jointName.str("");
  168. jointName << "arm_joint_" << (i + 1);
  169. armJointPositions[i].joint_uri = jointName.str();
  170. armJointPositions[i].value = arm_joint4_val;
  171. armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
  172. cout << "Joint " << armJointPositions[i].joint_uri << " = " << armJointPositions[i].value << " " << armJointPositions[i].unit << endl;
  173. // This is for arm_joint_5
  174. i++;
  175. jointName.str("");
  176. jointName << "arm_joint_" << (i + 1);
  177. armJointPositions[i].joint_uri = jointName.str();
  178. armJointPositions[i].value = arm_joint5_val;
  179. armJointPositions[i].unit = boost::units::to_string(boost::units::si::radians);
  180. cout << "Joint " << armJointPositions[i].joint_uri << " = " << armJointPositions[i].value << " " << armJointPositions[i].unit << endl;
  181. // Gripper joints
  182. gripperJointPositions[0].joint_uri = "gripper_finger_joint_l";
  183. gripperJointPositions[0].value = 1.0;
  184. gripperJointPositions[0].unit = boost::units::to_string(boost::units::si::meter);
  185. gripperJointPositions[1].joint_uri = "gripper_finger_joint_r";
  186. gripperJointPositions[1].value = 1.0;
  187. gripperJointPositions[1].unit = boost::units::to_string(boost::units::si::meter);
  188. cout << "sending command ..." << endl;
  189. command.positions = armJointPositions;
  190. armPositionsPublisher.publish(command);
  191. command.positions = gripperJointPositions;
  192. gripperPositionPublisher.publish(command);
  193. return 1;
  194. }
  195. void processFeedback(const visualization_msgs::InteractiveMarkerFeedbackConstPtr &feedback)
  196. {
  197. /*
  198. ROS_INFO_STREAM( feedback->marker_name << " is now at "
  199. << feedback->pose.position.x << ", " << feedback->pose.position.y
  200. << ", " << feedback->pose.position.z );
  201. */
  202. youbot_imarker::diffval dval;
  203. bool pfsend_command=false;
  204. dval.xx=0.0;
  205. dval.yy=0.0;
  206. dval.zz=0.0;
  207. if(!chit)
  208. {
  209. orx1=feedback->pose.position.x;
  210. ory1=feedback->pose.position.y;
  211. orz1=feedback->pose.position.z;
  212. chit=true;
  213. }
  214. else
  215. {
  216. if (fabs(orx1-feedback->pose.position.x)>0.01)
  217. {
  218. //publish new x
  219. dval.xx = orx1-feedback->pose.position.x;
  220. pfsend_command=true;
  221. }
  222. else
  223. {
  224. dval.xx=0.0;
  225. }
  226. if (fabs(ory1-feedback->pose.position.y)>0.01)
  227. {
  228. //publish new y
  229. dval.yy = ory1-feedback->pose.position.y;
  230. pfsend_command=true;
  231. }
  232. else
  233. {
  234. dval.yy=0.0;
  235. }
  236. if (fabs(orz1-feedback->pose.position.z)>0.01)
  237. {
  238. //publish new z
  239. dval.zz = orz1-feedback->pose.position.z;
  240. pfsend_command=true;
  241. }
  242. else
  243. {
  244. dval.zz=0.0;
  245. }
  246. orx1=feedback->pose.position.x;
  247. ory1=feedback->pose.position.y;
  248. orz1=feedback->pose.position.z;
  249. }
  250. //if (!diffbr)
  251. //{
  252. if(pfsend_command)
  253. control_robot(dval);
  254. diffval_pub.publish(dval);
  255. //}
  256. //if((fabs(dval.xx)>0)||(fabs(dval.yy)>0)||(fabs(dval.zz)>0))
  257. //diffbr=true;
  258. }
  259. void joint_states_callback(const sensor_msgs::JointState& youbot_joint_state)
  260. {
  261. //vector<string> jname = youbot_joint_state.name;
  262. vector<double> jpos = youbot_joint_state.position;
  263. //vector<string>::iterator itname = jname.begin();
  264. vector<double>::iterator itpos = jpos.begin();
  265. //cout << "joint_states_callback: \n";
  266. // we need joints 12 and 13
  267. for(int i=0; i<11; i++)
  268. {
  269. //itname++;
  270. itpos++;
  271. }
  272. //cout << (*itname) << ": " << (*itpos) << " ";
  273. arm_joint4_val = (*itpos);
  274. //itname++;
  275. itpos++;
  276. arm_joint5_val = (*itpos);
  277. //cout << (*itname) << ": " << (*itpos) << "\n\n";
  278. }
  279. int main(int argc, char** argv)
  280. {
  281. ros::init(argc, argv, "simple_marker");
  282. orx1=0;
  283. ory1=0;
  284. orz1=0;
  285. arm_joint4_val=arm_joint5_val=0.0;
  286. chit=false;
  287. //diffbr=false;
  288. ros::NodeHandle n;
  289. ros::Subscriber lclzr_sub = n.subscribe("/joint_states",1,&joint_states_callback);
  290. armPositionsPublisher = n.advertise<brics_actuator::JointPositions > ("arm_1/arm_controller/position_command", 1);
  291. gripperPositionPublisher = n.advertise<brics_actuator::JointPositions > ("arm_1/gripper_controller/position_command", 1);
  292. /*
  293. tf::TransformListener listener;
  294. tf::StampedTransform transform;
  295. listener.lookupTransform("/base_footprint", "/gripper_palm_link", ros::Time(0), transform);
  296. double xee = transform.getOrigin().x();
  297. double yee = transform.getOrigin().y();
  298. double zee = transform.getOrigin().z();
  299. std::cout << "\nxee,yee,zee: " << xee << " " << yee << " " << zee << "\n";
  300. */
  301. //ros::Subscriber tf_sub = n.subscribe("/youbot_im_topic", 1, &youbot_im_callback);
  302. diffval_pub = n.advertise<youbot_imarker::diffval>("diffval",1);
  303. // create an interactive marker server on the topic namespace simple_marker
  304. interactive_markers::InteractiveMarkerServer server("simple_marker");
  305. // create an interactive marker for our server
  306. visualization_msgs::InteractiveMarker int_marker;
  307. //int_marker.header.frame_id = "/gripper_palm_link";
  308. int_marker.header.frame_id = "/arm_link_5";
  309. int_marker.name = "my_marker";
  310. int_marker.description = "Simple 3-DOF Control";
  311. // create a grey box marker
  312. visualization_msgs::Marker box_marker;
  313. box_marker.type = visualization_msgs::Marker::CUBE;
  314. /*
  315. box_marker.scale.x = 0.45;
  316. box_marker.scale.y = 0.45;
  317. box_marker.scale.z = 0.45;
  318. */
  319. box_marker.scale.x = 0.06;
  320. box_marker.scale.y = 0.06;
  321. box_marker.scale.z = 0.06;
  322. box_marker.color.r = 0.5;
  323. box_marker.color.g = 0.5;
  324. box_marker.color.b = 0.5;
  325. box_marker.color.a = 1.0;
  326. // create a non-interactive control which contains the box
  327. visualization_msgs::InteractiveMarkerControl box_control;
  328. box_control.always_visible = true;
  329. box_control.markers.push_back( box_marker );
  330. // add the control to the interactive marker
  331. int_marker.controls.push_back( box_control );
  332. // create a control which will move the box
  333. // this control does not contain any markers,
  334. // which will cause RViz to insert two arrows
  335. visualization_msgs::InteractiveMarkerControl move_control;
  336. move_control.orientation.w = 1;
  337. move_control.orientation.x = 1;
  338. move_control.orientation.y = 0;
  339. move_control.orientation.z = 0;
  340. move_control.name = "move_x";
  341. move_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
  342. int_marker.controls.push_back(move_control);
  343. move_control.orientation.w = 1;
  344. move_control.orientation.x = 0;
  345. move_control.orientation.y = 1;
  346. move_control.orientation.z = 0;
  347. move_control.name = "move_y";
  348. move_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
  349. int_marker.controls.push_back(move_control);
  350. move_control.orientation.w = 1;
  351. move_control.orientation.x = 0;
  352. move_control.orientation.y = 0;
  353. move_control.orientation.z = 1;
  354. move_control.name = "move_z";
  355. move_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
  356. int_marker.controls.push_back(move_control);
  357. //move_control.name = "move_y";
  358. //move_control.interaction_mode = visualization_msgs::InteractiveMarkerControl::MOVE_AXIS;
  359. // add the control to the interactive marker
  360. //int_marker.controls.push_back(move_control);
  361. // add the interactive marker to our collection &
  362. // tell the server to call processFeedback() when feedback arrives for it
  363. server.insert(int_marker, &processFeedback);
  364. // 'commit' changes and send to all clients
  365. server.applyChanges();
  366. // start the ROS main loop
  367. ros::spin();
  368. }
  369. // %Tag(fullSource)%