grabber.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (C) 2018, OMRON SENTECH. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the names of OMRON SENTECH nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *****************************************************************************/
29 
30 #include <ros/ros.h>
31 #include <boost/thread.hpp>
33 
34 #include <omronsentech_camera/GetSDKInfo.h>
35 #include <omronsentech_camera/GetLastError.h>
36 #include <omronsentech_camera/GetDeviceList.h>
37 #include <omronsentech_camera/WriteNodeFloat.h>
38 #include <omronsentech_camera/GetGenICamNodeInfo.h>
39 #include <omronsentech_camera/GetImageAcquisitionStatus.h>
40 #include <omronsentech_camera/EnableImageAcquisition.h>
41 #include <omronsentech_camera/EnableTrigger.h>
42 #include <omronsentech_camera/SendSoftTrigger.h>
43 
44 #define SERVER_NODE "stcamera_node"
45 #define MAX_GRABBED 10
46 #define TRIGGER_SELECTOR "FrameStart"
47 #define TRIGGER_SOURCE "Software"
49 
50 // Get SentechSDK and GenTL information
52 {
53  ros::ServiceClient client = n.serviceClient<omronsentech_camera::GetSDKInfo>(
54  SERVER_NODE "/get_sdk_info");
55  omronsentech_camera::GetSDKInfo srv;
56  if (client.call(srv))
57  {
58  std::cout << "SentechSDK ver.: " << srv.response.sdk_version << std::endl;
59  for (size_t i = 0; i < srv.response.gentl_info_list.size(); i++)
60  {
61  std::cout << "GenTL vendor: " << srv.response.gentl_info_list[i].vendor
62  << std::endl;
63  std::cout << "\tversion: " << srv.response.gentl_info_list[i].version
64  << std::endl;
65  std::cout << "\tproducer: " << srv.response.gentl_info_list[i]
66  .producer_version << std::endl;
67  std::cout << "\tfullpath: " << srv.response.gentl_info_list[i]
68  .full_path << std::endl;
69  std::cout << "\tTL type: " << srv.response.gentl_info_list[i]
70  .tltype << std::endl;
71  }
72  }
73 }
74 
75 // Get one connected device's namespace
77 {
79  omronsentech_camera::GetDeviceList>(SERVER_NODE "/get_device_list");
80  omronsentech_camera::GetDeviceList srv;
81  if (client.call(srv))
82  {
83  for (size_t i = 0; i < srv.response.device_list.size(); i++)
84  {
85  if (srv.response.device_list[i].connected)
86  return srv.response.device_list[i].device_namespace;
87  }
88  }
89  return "";
90 }
91 
92 // Get last error
93 int getLastError(ros::NodeHandle &n, std::string dev_ns)
94 {
95  const char NS[] = SERVER_NODE "/";
96  std::string ns = NS + dev_ns + "/get_last_error";
98  omronsentech_camera::GetLastError>(ns);
99  omronsentech_camera::GetLastError srv;
100  client.call(srv);
101  std::cout << "Last error: " << srv.response.error_code << std::endl;
102  std::cout << srv.response.description << std::endl;
103  return srv.response.error_code;
104 }
105 
106 // Set Gain
107 int setGain(ros::NodeHandle &n, std::string dev_ns, double value)
108 {
109  const char NS[] = SERVER_NODE "/";
110  std::string ns = NS + dev_ns + "/write_node_float";
112  omronsentech_camera::WriteNodeFloat>(ns);
113  omronsentech_camera::WriteNodeFloat srv;
114  srv.request.module_name = "RemoteDevice";
115  srv.request.node_name = "Gain";
116  srv.request.value = value;
117  if (client.call(srv)) return 0;
118  return getLastError(n, dev_ns);
119 }
120 
121 // Get Gain
122 int getGain(ros::NodeHandle &n, std::string dev_ns, double &value,
123  double &value_min, double &value_max)
124 {
125  const char NS[] = SERVER_NODE "/";
126  std::string ns = NS + dev_ns + "/get_genicam_node_info";
128  omronsentech_camera::GetGenICamNodeInfo>(ns);
129  omronsentech_camera::GetGenICamNodeInfo srv;
130  srv.request.module_name = "RemoteDevice";
131  srv.request.node_name = "Gain";
132  if (client.call(srv))
133  {
134  value = std::stod(srv.response.current_value);
135  value_min = std::stod(srv.response.min_value);
136  value_max = std::stod(srv.response.max_value);
137  return 0;
138  }
139  return getLastError(n, dev_ns);
140 }
141 
142 // Image callback
143 void imageCallback(const sensor_msgs::ImageConstPtr& msg)
144 {
145  if (++grabbed_images > MAX_GRABBED) return;
146  std::cout << "Grabbed " << grabbed_images << " images. WxH: "
147  << msg->width << " x " << msg->height << " Encoding: "
148  << msg->encoding << std::endl;
149 }
150 
151 // Get the status of image acquisition
152 bool isImageAcquisitionEnabled(ros::NodeHandle &n, std::string dev_ns)
153 {
154  const char NS[] = SERVER_NODE "/";
155  std::string ns = NS + dev_ns + "/get_image_acquisition_status";
157  omronsentech_camera::GetImageAcquisitionStatus>(ns);
158  omronsentech_camera::GetImageAcquisitionStatus srv;
159  if (client.call(srv))
160  {
161  return srv.response.value;
162  }
163  getLastError(n, dev_ns);
164  return false;
165 }
166 
167 // Set the status of image acquisition
168 int setImageAcquisition(ros::NodeHandle &n, std::string dev_ns, bool enabled)
169 {
170  const char NS[] = SERVER_NODE "/";
171  std::string ns = NS + dev_ns + "/enable_image_acquisition";
173  omronsentech_camera::EnableImageAcquisition>(ns);
174  omronsentech_camera::EnableImageAcquisition srv;
175  srv.request.value = enabled;
176  if (client.call(srv))
177  {
178  return 0;
179  }
180  return getLastError(n, dev_ns);
181 }
182 
183 // Set trigger
184 int setTrigger(ros::NodeHandle &n, std::string dev_ns, bool enabled)
185 {
186  const char NS[] = SERVER_NODE "/";
187  std::string ns = NS + dev_ns + "/enable_trigger";
189  omronsentech_camera::EnableTrigger>(ns);
190  omronsentech_camera::EnableTrigger srv;
191  srv.request.trigger_selector = TRIGGER_SELECTOR;
192  srv.request.trigger_source = TRIGGER_SOURCE;
193  srv.request.trigger_delayus = 0;
194  srv.request.value= enabled;
195  if (client.call(srv))
196  {
197  return 0;
198  }
199  return getLastError(n, dev_ns);
200 }
201 
202 // Send trigger
203 int sendTrigger(ros::NodeHandle &n, std::string dev_ns)
204 {
205  const char NS[] = SERVER_NODE "/";
206  std::string ns = NS + dev_ns + "/send_soft_trigger";
208  omronsentech_camera::SendSoftTrigger>(ns);
209  omronsentech_camera::SendSoftTrigger srv;
210  srv.request.trigger_selector = TRIGGER_SELECTOR;
211  if (client.call(srv))
212  {
213  return 0;
214  }
215  return getLastError(n, dev_ns);
216 }
217 
218 
219 // Main
220 int main(int argc, char **argv)
221 {
222  ros::init(argc, argv, "grabber");
223  ros::NodeHandle n;
224  boost::thread th(boost::bind(&ros::spin));
225 
226  // Display SDK info
227  displaySDKInfo(n);
228 
229  // Check connected devices
230  std::string dev_ns = "";
231  ros::Rate r(1);
232  while (1)
233  {
234  dev_ns = getConnectedDeviceNamespace(n);
235  if (!dev_ns.empty() || !ros::ok())
236  {
237  break;
238  }
239  r.sleep();
240  }
241 
242  if (!dev_ns.empty())
243  {
244  // Read gain value
245  double gain_value = 0;
246  double gain_min_value = 0;
247  double gain_max_value = 0;
248  int ret = getGain(n, dev_ns, gain_value, gain_min_value, gain_max_value);
249  if (ret == 0)
250  {
251  std::cout << "Current Gain: " << gain_value << std::endl;
252  }
253  double new_gain_value = ((int)(gain_min_value + gain_max_value)/2);
254 
255  // Change gain value
256  ret = setGain(n, dev_ns, new_gain_value);
257  if (ret == 0)
258  {
259  std::cout << "Update Gain to " << new_gain_value << " was successful"
260  << std::endl;
261  }
262 
263  // Subscribe to the connected device
264  const char NS[] = SERVER_NODE "/";
265  std::string ns = NS + dev_ns + "/image_raw";
266  ros::Subscriber image = n.subscribe(ns, 1, imageCallback);
267 
268  if (!isImageAcquisitionEnabled(n, dev_ns))
269  {
270  setImageAcquisition(n, dev_ns, true);
271  }
272  while(grabbed_images < MAX_GRABBED)
273  {
274  r.sleep();
275  }
276 
277  // Trigger mode
278  std::cout << std::endl << "Enable trigger selector: " << TRIGGER_SELECTOR <<
279  ", source: " << TRIGGER_SOURCE << std::endl;
280  try
281  {
282  setImageAcquisition(n, dev_ns, false);
283  setTrigger(n, dev_ns, true);
284  grabbed_images = 0;
285  setImageAcquisition(n, dev_ns, true);
286  for (int i = 0; i < 10; i++)
287  {
288  sendTrigger(n, dev_ns);
289  std::cout << "Sleep for 1s" << std::endl;
290  r.sleep();
291  }
292  setImageAcquisition(n, dev_ns, false);
293  setTrigger(n, dev_ns, false);
294  setImageAcquisition(n, dev_ns, true);
295  }
296  catch(...)
297  {
298  }
299 
300  // Set the original gain value
301  setGain(n, dev_ns, gain_value);
302  }
303 
304  ros::shutdown();
305  return 0;
306 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
int sendTrigger(ros::NodeHandle &n, std::string dev_ns)
Definition: grabber.cpp:203
int grabbed_images
Definition: grabber.cpp:48
std::string getConnectedDeviceNamespace(ros::NodeHandle &n)
Definition: grabber.cpp:76
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
#define TRIGGER_SOURCE
Definition: grabber.cpp:47
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
int setGain(ros::NodeHandle &n, std::string dev_ns, double value)
Definition: grabber.cpp:107
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
Definition: grabber.cpp:143
ROSCPP_DECL void spin(Spinner &spinner)
#define SERVER_NODE
Definition: grabber.cpp:44
int getGain(ros::NodeHandle &n, std::string dev_ns, double &value, double &value_min, double &value_max)
Definition: grabber.cpp:122
ROSCPP_DECL bool ok()
bool isImageAcquisitionEnabled(ros::NodeHandle &n, std::string dev_ns)
Definition: grabber.cpp:152
void displaySDKInfo(ros::NodeHandle &n)
Definition: grabber.cpp:51
bool sleep()
int setTrigger(ros::NodeHandle &n, std::string dev_ns, bool enabled)
Definition: grabber.cpp:184
#define TRIGGER_SELECTOR
Definition: grabber.cpp:46
int setImageAcquisition(ros::NodeHandle &n, std::string dev_ns, bool enabled)
Definition: grabber.cpp:168
ROSCPP_DECL void shutdown()
int getLastError(ros::NodeHandle &n, std::string dev_ns)
Definition: grabber.cpp:93
#define MAX_GRABBED
Definition: grabber.cpp:45
int main(int argc, char **argv)
Definition: grabber.cpp:220


omronsentech_camera
Author(s): OSE ROS Support
autogenerated on Tue Jul 2 2019 19:28:17