All Classes Namespaces Files Functions Variables Typedefs
gripper_controller.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <cmath>
3 #include <string>
4 #include <std_msgs/Float32.h>
5 #include <std_msgs/Bool.h>
6 #include <std_msgs/String.h>
7 #include <Grijper/command.h>
8 
9 using namespace std;
10 
11 void setForce(const std_msgs::Float32::ConstPtr&);
12 void gripperPhidget(const std_msgs::Float32::ConstPtr&);
13 void gripperCommand(const std_msgs::String::ConstPtr&);
14 void shutdown(const std_msgs::Bool::ConstPtr&);
15 ros::Publisher control;
16 bool close_gripper(float);
17 bool open_gripper(float);
18 bool gripper_open = true;
19 bool force_open = false;
20 float last_distance = 0;
21 float force = 0.35;
33 int main(int argc, char **argv){
34 
35  ros::init(argc, argv, "gripper_controller");
36  ros::NodeHandle n;
37  ros::Subscriber force_sub = n.subscribe("gripper_force", 1, setForce);
38  ros::Subscriber command_sub = n.subscribe("command", 1000, gripperCommand);
39  ros::Subscriber phidget_sub = n.subscribe("phidget_value", 1, gripperPhidget);
40  ros::Subscriber sd = n.subscribe("shutdown", 1, shutdown);
41  control = n.advertise<Grijper::command>("gripper_control", 1);
42  ros::spin();
43 
44  return 0;
45 }
46 
57 void gripperPhidget(const std_msgs::Float32::ConstPtr& msg){
58 
59  float distance = msg->data;
60 
61  if(abs(distance - last_distance) > 0.5){
62 
63  last_distance = distance;
64 
65  ROS_INFO("received distance: %f", distance);
66  if(close_gripper(distance) && gripper_open && !force_open){
67  ROS_INFO("Closing gripper");
68  Grijper::command msg;
69  msg.cmd = "close";
70  msg.force = force;
71  gripper_open = false;
72  control.publish(msg);
73  }
74  if(open_gripper(distance) && !gripper_open){
75  ROS_INFO("Opening gripper");
76  Grijper::command msg;
77  msg.cmd = "open";
78  msg.force = force;
79  gripper_open = true;
80  control.publish(msg);
81  }
82 
83  if(open_gripper(distance) && force_open){
84  ROS_INFO("Resetting force_open");
85  force_open = false;
86  }
87  }
88 }
89 
98 void setForce(const std_msgs::Float32::ConstPtr& msg){
99  force = msg->data;
100  ROS_INFO("Setting controller force to: %f\n", force);
101 }
102 
113 void gripperCommand(const std_msgs::String::ConstPtr& msg){
114  string cmd = msg->data;
115  Grijper::command ctl;
116  if(cmd.compare("open") == 0){
117  ROS_INFO("Opening gripper");
118  force_open = true;
119  ctl.cmd = "open";
120  ctl.force = force;
121  gripper_open = true;
122  control.publish(ctl);
123  }else if(cmd.compare("relax") == 0){
124  ROS_INFO("Relaxing gripper");
125  force_open = false;
126  ctl.cmd = "relax";
127  ctl.force = 0;
128  control.publish(ctl);
129  }else if(cmd.compare("close") == 0){
130  ROS_INFO("Closing gripper");
131  force_open = false;
132  ctl.cmd = "close";
133  ctl.force = force;
134  gripper_open = false;
135  control.publish(ctl);
136  }else{
137  ROS_INFO("Received invalid command: %s\n", cmd.c_str());
138  }
139 }
140 
149 bool close_gripper(float distance){
150  return distance < 7 && distance != 0; //TODO dit nog bewerken afhankelijk van de sensor.
151 }
152 
161 bool open_gripper(float distance){
162  return distance > 10;
163 }
164 
170 void shutdown(const std_msgs::Bool::ConstPtr& b){
171  ros::shutdown();
172 }


Grijper
Author(s): Stijn van Schooten, Wybren ten Cate, Florens Kreuk, Jan Wymenga,Joris Coenen, Roy Groot
autogenerated on Thu Oct 3 2013 22:59:42