2222#include < tf2_ros/buffer.h>
2323#include < geometry_msgs/PoseStamped.h>
2424
25+ #include < moveit_visual_tools/moveit_visual_tools.h>
26+
27+
2528static void coarseEstimateCallback (const vader_msgs::PepperArray::ConstPtr &msg);
2629static void fineEstimateCallback (const vader_msgs::PepperArray::ConstPtr &msg);
2730
31+ namespace rvt = rviz_visual_tools;
32+
2833class VADERStateMachine
2934{
3035private:
@@ -43,11 +48,11 @@ class VADERStateMachine
4348 CutterGrasp,
4449 CutterEndEffector,
4550 // -----------Finish and cleanup-----------
46- ParallelMoveStorage,
47- HomeGripper2,
48- Done,
49- Error,
50- End
51+ ParallelMoveStorage,
52+ HomeGripper2,
53+ Done,
54+ Error,
55+ End
5156 };
5257
5358 State currentState;
@@ -63,6 +68,7 @@ class VADERStateMachine
6368 bool allowCoarseEstimateUpdate = true , allowFineEstimateUpdate = true ;
6469
6570 ros::Time waitForFinePoseStartTime;
71+ moveit_visual_tools::MoveItVisualToolsPtr visual_tools = std::make_shared<moveit_visual_tools::MoveItVisualTools>(" world" ); // shared from planner server main instance
6672
6773 // Plan/Exec clients connecting to planner
6874 ros::ServiceClient planClient;
@@ -160,6 +166,39 @@ class VADERStateMachine
160166 }
161167 }
162168
169+ void visualizePepper (vader_msgs::Pepper pepper_estimate) {
170+
171+ // Visualize as a visual tools marker
172+ std_msgs::ColorRGBA pepper_color;
173+ // green
174+ pepper_color.r = 0 .0f ;
175+ pepper_color.g = 1 .0f ;
176+ pepper_color.b = 0 .0f ;
177+ pepper_color.a = 0 .8f ; // semi-transparent
178+ ROS_INFO_STREAM (" Visualizing pepper at: x=" << pepper_estimate.fruit_data .pose .position .x
179+ << " , y=" << pepper_estimate.fruit_data .pose .position .y
180+ << " , z=" << pepper_estimate.fruit_data .pose .position .z
181+ << " , quat=(" << pepper_estimate.fruit_data .pose .orientation .x << " , "
182+ << pepper_estimate.fruit_data .pose .orientation .y << " , "
183+ << pepper_estimate.fruit_data .pose .orientation .z << " , "
184+ << pepper_estimate.fruit_data .pose .orientation .w << " )" );
185+ visual_tools->publishCylinder (pepper_estimate.fruit_data .pose , pepper_color, pepper_estimate.fruit_data .shape .dimensions [0 ], pepper_estimate.fruit_data .shape .dimensions [1 ]*2 );
186+ if (pepper_estimate.peduncle_data .pose .position .x == 0 &&
187+ pepper_estimate.peduncle_data .pose .position .y == 0 &&
188+ pepper_estimate.peduncle_data .pose .position .z == 0 ){
189+ visual_tools->trigger ();
190+ return ;
191+ }
192+ std_msgs::ColorRGBA peduncle_color;
193+ // brown
194+ peduncle_color.r = 0 .6f ;
195+ peduncle_color.g = 0 .3f ;
196+ peduncle_color.b = 0 .0f ;
197+ peduncle_color.a = 0 .8f ; // semi-transparent
198+ visual_tools->publishCylinder (pepper_estimate.peduncle_data .pose , peduncle_color, pepper_estimate.peduncle_data .shape .dimensions [0 ], pepper_estimate.peduncle_data .shape .dimensions [1 ]*2 );
199+ visual_tools->trigger ();
200+ }
201+
163202 void _sendGripperCommand (int open_pct)
164203 {
165204 vader_msgs::GripperCommand gripperCommand;
@@ -280,9 +319,9 @@ class VADERStateMachine
280319 VADERStateMachine () : currentState(State::HomeGripper)
281320 {
282321 coarseEstimate = nullptr ;
283- coarseEstimateSub = n.subscribe (" /coarse_pepper_array " , 10 , coarseEstimateCallback);
322+ coarseEstimateSub = n.subscribe (" /gripper_coarse_pepper_array " , 10 , coarseEstimateCallback);
284323 fineEstimate = nullptr ;
285- fineEstimateSub = n.subscribe (" /fine_pepper_array " , 10 , fineEstimateCallback);
324+ fineEstimateSub = n.subscribe (" /gripper_fine_pepper_array " , 10 , fineEstimateCallback);
286325
287326 planClient = n.serviceClient <vader_msgs::PlanningRequest>(" vader_planning_service" );
288327
@@ -316,13 +355,19 @@ class VADERStateMachine
316355 return ;
317356 }
318357 coarseEstimates.clear ();
358+ visual_tools->deleteAllMarkers ();
319359 // ROS_INFO_STREAM("Received " << msg->peppers.size() << " coarse pepper estimates.");
320360 for (const vader_msgs::Pepper pepper_msg : msg->peppers )
321361 {
322362 vader_msgs::Pepper transformed_pepper;
323363 // ROS_INFO("Transforming pepper with frame_id: %s", msg->header.frame_id.c_str());
324364 _transformFromCameraFrameIntoRobotFrame (pepper_msg, &transformed_pepper);
365+ transformed_pepper.fruit_data .pose .orientation .x = 0 ;
366+ transformed_pepper.fruit_data .pose .orientation .y = 0 ;
367+ transformed_pepper.fruit_data .pose .orientation .z = 0 ;
368+ transformed_pepper.fruit_data .pose .orientation .w = 1 ;
325369 coarseEstimates.push_back (transformed_pepper);
370+
326371 }
327372 _prioritizeCoarseEstimatePepper (coarseEstimates);
328373
@@ -364,6 +409,7 @@ class VADERStateMachine
364409 vader_msgs::Pepper transformed_pepper;
365410 _transformFromCameraFrameIntoRobotFrame (pepper_msg, &transformed_pepper);
366411 fineEstimates.push_back (transformed_pepper);
412+ visualizePepper (transformed_pepper);
367413 }
368414 // ROS_INFO_STREAM("Transformed fine estimates count: " << fineEstimates.size());
369415
@@ -392,20 +438,21 @@ class VADERStateMachine
392438 }
393439 }
394440
395- void setFinePoseEstimateWithCoarsePoseEst (){
396- fineEstimate = new vader_msgs::Pepper (*coarseEstimate);
397- fineEstimate->header .frame_id = " world" ;
398- fineEstimate->fruit_data .pose .orientation .x = 0 ;
399- fineEstimate->fruit_data .pose .orientation .y = 0 ;
400- fineEstimate->fruit_data .pose .orientation .z = 0 ;
401- fineEstimate->fruit_data .pose .orientation .w = 1 ;
402- fineEstimate->peduncle_data .pose .orientation .x = 0 ;
403- fineEstimate->peduncle_data .pose .orientation .y = 0 ;
404- fineEstimate->peduncle_data .pose .orientation .z = 0 ;
405- fineEstimate->peduncle_data .pose .orientation .w = 1 ;
406- fineEstimate->peduncle_data .pose .position .x = fineEstimate->fruit_data .pose .position .x ;
407- fineEstimate->peduncle_data .pose .position .y = fineEstimate->fruit_data .pose .position .y ;
408- fineEstimate->peduncle_data .pose .position .z = fineEstimate->fruit_data .pose .position .z + 0.05 ; // approximate peduncle position
441+ vader_msgs::Pepper setFinePoseEstimateWithCoarsePoseEst (const vader_msgs::Pepper &coarseEstimate){
442+ vader_msgs::Pepper fakeFineEstimate = (coarseEstimate);
443+ fakeFineEstimate.header .frame_id = " world" ;
444+ fakeFineEstimate.fruit_data .pose .orientation .x = 0 ;
445+ fakeFineEstimate.fruit_data .pose .orientation .y = 0 ;
446+ fakeFineEstimate.fruit_data .pose .orientation .z = 0 ;
447+ fakeFineEstimate.fruit_data .pose .orientation .w = 1 ;
448+ fakeFineEstimate.peduncle_data .pose .orientation .x = 0 ;
449+ fakeFineEstimate.peduncle_data .pose .orientation .y = 0 ;
450+ fakeFineEstimate.peduncle_data .pose .orientation .z = 0 ;
451+ fakeFineEstimate.peduncle_data .pose .orientation .w = 1 ;
452+ fakeFineEstimate.peduncle_data .pose .position .x = fakeFineEstimate.fruit_data .pose .position .x ;
453+ fakeFineEstimate.peduncle_data .pose .position .y = fakeFineEstimate.fruit_data .pose .position .y ;
454+ fakeFineEstimate.peduncle_data .pose .position .z = fakeFineEstimate.fruit_data .pose .position .z + 0.05 ; // approximate peduncle position
455+ return fakeFineEstimate;
409456 }
410457
411458 void execute ()
@@ -489,6 +536,8 @@ class VADERStateMachine
489536 std::to_string (coarseEstimate->fruit_data .pose .orientation .y ) + " , " +
490537 std::to_string (coarseEstimate->fruit_data .pose .orientation .z ) + " , " +
491538 std::to_string (coarseEstimate->fruit_data .pose .orientation .w ) + " )" );
539+ visual_tools->deleteAllMarkers ();
540+ visualizePepper (*coarseEstimate);
492541 // currentState = State::Done;
493542 currentState = State::ParallelMovePregrasp;
494543 }
@@ -547,6 +596,8 @@ class VADERStateMachine
547596 allowFineEstimateUpdate = false ;
548597 waitForFinePoseStartTime = ros::Time (0 );
549598 currentState = State::GripperGrasp;
599+ visual_tools->deleteAllMarkers ();
600+ visualizePepper (*fineEstimate);
550601 }
551602 else
552603 {
@@ -556,7 +607,11 @@ class VADERStateMachine
556607 ros::Duration wait_duration = ros::Time::now () - waitForFinePoseStartTime;
557608 if (wait_duration.toSec () > 5.0 && allowFineEstimateUpdate){
558609 ROS_WARN (" Timeout waiting for fine pose estimate. Proceeding with coarse estimate." );
559- setFinePoseEstimateWithCoarsePoseEst ();
610+ vader_msgs::Pepper fakeFineEstimate = setFinePoseEstimateWithCoarsePoseEst (*coarseEstimate);
611+ fineEstimate = new vader_msgs::Pepper (fakeFineEstimate);
612+ allowFineEstimateUpdate = false ;
613+
614+ // *fineEstimate = setFinePoseEstimateWithCoarsePoseEst(*coarseEstimate);
560615 }
561616 }
562617 }
@@ -607,7 +662,7 @@ class VADERStateMachine
607662 if (srv.response .success )
608663 {
609664 _logWithState (" Cutter grasp successful." );
610- currentState = State::GripperEndEffector ; // First do the cutting action
665+ currentState = State::End ; // First do the cutting action
611666 }
612667 else
613668 {
0 commit comments