00001 #include "ros/ros.h"
00002 #include "std_msgs/Bool.h"
00003 #include "cob_tray_sensors/CheckOccupied.h"
00004
00005 #include <libphidgets/phidget21.h>
00006 #include <sstream>
00007
00008
00009 int wert[100];
00010 int i;
00011
00012 bool bOccupied_;
00013
00014 void display_generic_properties(CPhidgetHandle phid)
00015 {
00016 int sernum, version;
00017 const char *deviceptr, *label;
00018 CPhidget_getDeviceType(phid, &deviceptr);
00019 CPhidget_getSerialNumber(phid, &sernum);
00020 CPhidget_getDeviceVersion(phid, &version);
00021 CPhidget_getDeviceLabel(phid, &label);
00022
00023 ROS_INFO("%s", deviceptr);
00024 ROS_INFO("Version: %8d SerialNumber: %10d", version, sernum);
00025 ROS_INFO("Label: %s", label);
00026 return;
00027 }
00028
00029 int IFK_AttachHandler(CPhidgetHandle IFK, void *userptr)
00030 {
00031
00032
00033 return 0;
00034 }
00035
00036
00037 int IFK_SensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
00038 {
00039
00040
00041 if (i <10)
00042 {
00043 CPhidgetInterfaceKit_getSensorValue(IFK,0, &wert[0+(10*i)]);
00044
00045 CPhidgetInterfaceKit_getSensorValue(IFK,1, &wert[1+(10*i)]);
00046
00047 CPhidgetInterfaceKit_getSensorValue(IFK,2, &wert[2+(10*i)]);
00048
00049 CPhidgetInterfaceKit_getSensorValue(IFK,3, &wert[3+(10*i)]);
00050
00051 }
00052
00053 else
00054 {
00055
00056
00057
00058
00059
00060
00061
00062 wert[94] = ((wert[0] + wert[10]+wert[20]+wert[30]+wert[40]+wert[50]+wert[60]+wert[70]+wert[80]+wert[90]) / 10);
00063 wert[95] = ((wert[1] + wert[11]+wert[21]+wert[31]+wert[41]+wert[51]+wert[61]+wert[71]+wert[81]+wert[91]) / 10);
00064 wert[96] = ((wert[2] + wert[12]+wert[22]+wert[32]+wert[42]+wert[52]+wert[62]+wert[72]+wert[82]+wert[92]) / 10);
00065 wert[97] = ((wert[3] + wert[13]+wert[23]+wert[33]+wert[43]+wert[53]+wert[63]+wert[73]+wert[83]+wert[93]) / 10);
00066
00067 if ((wert[94] < 120) && (wert[95] <300) && (wert[96] < 110) && (wert[97] < 120))
00068 {
00069 ROS_DEBUG("Sensor0123 manually are %d %d %d %d ; ", wert[94],wert[95],wert[96],wert[97]);
00070 ROS_DEBUG("tablet empty");
00071 bOccupied_ = false;
00072 }
00073 else
00074 {
00075 ROS_DEBUG("Sensor0123 manually are %d %d %d %d ;", wert[94],wert[95],wert[96],wert[97]);
00076 ROS_DEBUG("tablet NOT empty!");
00077 bOccupied_ = true;
00078 }
00079
00080 for( i = 0; i < 100; i++)
00081 wert[i]=0;
00082 i =0;
00083 }
00084 i++;
00085
00086 return 0;
00087 }
00088
00089
00090 bool check_occupied(cob_tray_sensors::CheckOccupied::Request &req,
00091 cob_tray_sensors::CheckOccupied::Response &res )
00092 {
00093 res.occupied.data = bOccupied_;
00094 return true;
00095 }
00096
00097
00098 int main(int argc, char **argv)
00099 {
00100 ros::init(argc, argv, "cob_tray_sensors");
00101 ros::NodeHandle n;
00102 ros::Publisher occupied_pub = n.advertise<std_msgs::Bool>("/tray/occupied", 0);
00103 ros::ServiceServer service = n.advertiseService("/tray/check_occupied", check_occupied);
00104
00105 ros::Rate loop_rate(10);
00106
00107 bOccupied_ = false;
00108
00109
00110 int numInputs, numOutputs, numSensors;
00111 int err;
00112
00113 CPhidgetInterfaceKitHandle IFK = 0;
00114 CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
00115 CPhidgetInterfaceKit_create(&IFK);
00116 CPhidgetInterfaceKit_set_OnSensorChange_Handler(IFK, IFK_SensorChangeHandler, NULL);
00117 CPhidget_set_OnAttach_Handler((CPhidgetHandle)IFK, IFK_AttachHandler, NULL);
00118
00119 CPhidget_open((CPhidgetHandle)IFK, -1);
00120
00121
00122 if((err = CPhidget_waitForAttachment((CPhidgetHandle)IFK, 0)) != EPHIDGET_OK )
00123 {
00124 const char *errStr;
00125 CPhidget_getErrorDescription(err, &errStr);
00126 ROS_ERROR("Error waiting for attachment: (%d): %s",err,errStr);
00127 goto exit;
00128 }
00129
00130 display_generic_properties((CPhidgetHandle)IFK);
00131 CPhidgetInterfaceKit_getOutputCount((CPhidgetInterfaceKitHandle)IFK, &numOutputs);
00132 CPhidgetInterfaceKit_getInputCount((CPhidgetInterfaceKitHandle)IFK, &numInputs);
00133 CPhidgetInterfaceKit_getSensorCount((CPhidgetInterfaceKitHandle)IFK, &numSensors);
00134 CPhidgetInterfaceKit_setOutputState((CPhidgetInterfaceKitHandle)IFK, 0, 1);
00135
00136 ROS_INFO("Sensors:%d Inputs:%d Outputs:%d", numSensors, numInputs, numOutputs);
00137
00138 while (ros::ok())
00139 {
00140 std_msgs::Bool occupied;
00141 occupied.data = bOccupied_;
00142 occupied_pub.publish(occupied);
00143 ros::spinOnce();
00144 loop_rate.sleep();
00145 }
00146
00147 exit:
00148 CPhidget_close((CPhidgetHandle)IFK);
00149 CPhidget_delete((CPhidgetHandle)IFK);
00150
00151
00152 return 0;
00153 }