All Classes Namespaces Files Functions Variables Typedefs
phidget_reader.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <math.h>
3 #include <std_msgs/Float32.h>
4 #include <std_msgs/String.h>
5 #include <std_msgs/Bool.h>
6 #include <string>
7 #include <phidget_ik/phidget_ik.h>
8 #include <Grijper/command.h>
9 
10 using namespace std;
11 
12 void shiftBuffer(int);
13 int mean();
14 void printbuffer();
15 void shutdown(const std_msgs::Bool::ConstPtr&);
16 float sensorToDistance(int);
17 
18 const int sensor_id = 4;
19 const int freq = 250;
20 const int buffer_size = 25;
32 int main(int argc, char **argv){
33 
34  ros::init(argc, argv, "phidget_reader");
35  ros::NodeHandle n;
36  ros::Publisher publisher = n.advertise<std_msgs::Float32>("phidget_value", 1000);
37  ros::Subscriber sd = n.subscribe("shutdown", 1, shutdown);
38  ros::Rate loop_rate(freq);
39  PhidgetIK phidget_ik;
40 
41  phidget_ik.init(-1);
42  phidget_ik.waitForAttachment(1000);
43 
44  if (phidget_ik.getLastError()){
45  std::cerr << "Error initializing PhidgetInterfaceKit: " << phidget_ik.getErrorDescription(phidget_ik.getLastError()) << std::endl;
46  return 1;
47  }
48 
49  for(int i = 0; i < buffer_size; i++)
50  {
51  buffer[i] = 1;
52  }
53 
54  while(ros::ok()){
55  int data = phidget_ik.getSensorValue(sensor_id);
56  std_msgs::Float32 sensor_val;
57  shiftBuffer(data);
58  data = mean();
59 
60  if(data > 700){
61  printf("Ignoring sensor value of %i\n", data);
62  }else{
63  float distance;
64  if(data < 100){
65  distance = 0;
66  } else {
67  distance = sensorToDistance(data);
68  }
69  sensor_val.data = distance;
70  printf("Reading sensor %i, value: %i, distance:%f\n",sensor_id, data, distance);
71  //printbuffer();
72 
73  publisher.publish(sensor_val);
74  }
75  ros::spinOnce();
76  loop_rate.sleep();
77  }
78 
79  return 0;
80 }
81 
89 void shiftBuffer(int data){
90  for(int i = 0; i < buffer_size - 1; i++){
91  buffer[i] = buffer[i+1];
92  }
93  buffer[buffer_size - 1] = data;
94 }
95 
102 int mean(){
103  int size = 0;
104  for(int i = 0; i < buffer_size; i
105  ++){
106  size = size + buffer[i];
107  }
108  return(ceil(size/buffer_size));
109 }
110 
116 void printbuffer(){
117  cout << " - [";
118  for(int i = 0; i < buffer_size; i++){
119  printf("%i,", buffer[i]);
120  }
121  cout << "]\n";
122 }
123 
132 float sensorToDistance(int sensorValue){
133 
134  return 2076.0f / (sensorValue - 11.0f);
135 
136 }
137 
143 void shutdown(const std_msgs::Bool::ConstPtr& b){
144  ros::shutdown();
145 }


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