trigger.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013 Boris Gromov, BioRobotics Lab at Korea Tech
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the author nor other contributors may be
18  * used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #include <ros/ros.h>
36 #include "trigger.h"
37 
45 // initializing constants
46 const std::string Trigger::trigger_mode_names_[DC1394_TRIGGER_MODE_NUM] = {"mode_0", "mode_1", "mode_2", "mode_3", "mode_4",
47  "mode_5", "mode_14", "mode_15", };
48 const std::string Trigger::trigger_source_names_[DC1394_TRIGGER_SOURCE_NUM] = {"source_0", "source_1", "source_2",
49  "source_3", "source_software", };
50 const std::string Trigger::trigger_polarity_names_[DC1394_TRIGGER_ACTIVE_NUM] = {"active_low", "active_high", };
51 
52 bool Trigger::findTriggerMode(std::string str)
53 {
54  if (str == "mode_0")
55  triggerMode_ = DC1394_TRIGGER_MODE_0;
56  else if(str == "mode_1")
57  triggerMode_ = DC1394_TRIGGER_MODE_1;
58  else if(str == "mode_2")
59  triggerMode_ = DC1394_TRIGGER_MODE_2;
60  else if(str == "mode_3")
61  triggerMode_ = DC1394_TRIGGER_MODE_3;
62  else if(str == "mode_4")
63  triggerMode_ = DC1394_TRIGGER_MODE_4;
64  else if(str == "mode_5")
65  triggerMode_ = DC1394_TRIGGER_MODE_5;
66  else if(str == "mode_14")
67  triggerMode_ = DC1394_TRIGGER_MODE_14;
68  else if(str == "mode_15")
69  triggerMode_ = DC1394_TRIGGER_MODE_15;
70  else
71  {
72  triggerMode_ = (dc1394trigger_mode_t) DC1394_TRIGGER_MODE_NUM;
73  return false;
74  }
75 
76  return true;
77 }
78 
79 bool Trigger::findTriggerSource(std::string str)
80 {
81  if (str == "source_0")
82  triggerSource_ = DC1394_TRIGGER_SOURCE_0;
83  else if(str == "source_1")
84  triggerSource_ = DC1394_TRIGGER_SOURCE_1;
85  else if(str == "source_2")
86  triggerSource_ = DC1394_TRIGGER_SOURCE_2;
87  else if(str == "source_3")
88  triggerSource_ = DC1394_TRIGGER_SOURCE_3;
89  else if(str == "source_software")
90  triggerSource_ = DC1394_TRIGGER_SOURCE_SOFTWARE;
91  else
92  {
93  triggerSource_ = (dc1394trigger_source_t) DC1394_TRIGGER_SOURCE_NUM;
94  return false;
95  }
96 
97  return true;
98 }
99 
100 bool Trigger::findTriggerPolarity(std::string str)
101 {
102  if(str == "active_low")
103  triggerPolarity_ = DC1394_TRIGGER_ACTIVE_LOW;
104  else if(str == "active_high")
105  triggerPolarity_ = DC1394_TRIGGER_ACTIVE_HIGH;
106  else
107  {
108  triggerPolarity_ = (dc1394trigger_polarity_t) DC1394_TRIGGER_ACTIVE_NUM;
109  return false;
110  }
111 
112  return true;
113 }
114 
115 bool Trigger::checkTriggerSource(dc1394trigger_source_t source)
116 {
117  for(size_t i = 0; i < triggerSources_.num; i++)
118  if(source == triggerSources_.sources[i]) return true;
119 
120  return false;
121 }
122 
128 bool Trigger::enumSources(dc1394camera_t *camera, dc1394trigger_sources_t &sources)
129 {
130  dc1394error_t err = dc1394_external_trigger_get_supported_sources(camera, &sources);
131  if (err != DC1394_SUCCESS)
132  {
133  ROS_FATAL("enumTriggerSources() failed: %d", err);
134  return false; // failure
135  }
136 
137  std::ostringstream ss;
138  if(sources.num != 0)
139  {
140  for(size_t i = 0; i < sources.num - 1; i++)
141  ss << triggerSourceName(sources.sources[i]) << ", ";
142  ss << triggerSourceName(sources.sources[sources.num - 1]);
143  }
144  else
145  {
146  ss << "none";
147  }
148 
149  ROS_DEBUG_STREAM("Trigger sources: " << ss.str());
150  return true;
151 }
152 
159 dc1394trigger_polarity_t Trigger::getPolarity(dc1394camera_t *camera)
160 {
161  dc1394trigger_polarity_t current_polarity;
162 
163  dc1394bool_t has_polarity;
164  dc1394error_t err = dc1394_external_trigger_has_polarity(camera, &has_polarity);
165  if (err != DC1394_SUCCESS)
166  {
167  ROS_FATAL("getPolarity() failed: %d", err);
168  return (dc1394trigger_polarity_t) DC1394_TRIGGER_ACTIVE_NUM; // failure
169  }
170 
171  if (has_polarity == DC1394_TRUE)
172  {
173  err = dc1394_external_trigger_get_polarity(camera, &current_polarity);
174  if (err != DC1394_SUCCESS)
175  {
176  ROS_FATAL("getPolarity() failed: %d", err);
177  return (dc1394trigger_polarity_t) DC1394_TRIGGER_ACTIVE_NUM; // failure
178  }
179  }
180  else
181  {
182  ROS_ERROR("Polarity is not supported");
183  return (dc1394trigger_polarity_t) DC1394_TRIGGER_ACTIVE_NUM; // failure
184  }
185 
186  return current_polarity; // success
187 }
188 
196 bool Trigger::setPolarity(dc1394camera_t *camera, dc1394trigger_polarity_t &polarity)
197 {
198  dc1394trigger_polarity_t current_polarity = getPolarity(camera);
199 
200  dc1394bool_t has_polarity;
201  dc1394error_t err = dc1394_external_trigger_has_polarity(camera, &has_polarity);
202  if (err != DC1394_SUCCESS)
203  {
204  ROS_FATAL("setPolarity() failed: %d", err);
205  return false; // failure
206  }
207 
208  if (has_polarity == DC1394_TRUE)
209  {
210  // if config not changed, then do nothing
211  if(current_polarity == polarity) return true;
212 
213  err = dc1394_external_trigger_set_polarity(camera, polarity);
214  if (err != DC1394_SUCCESS)
215  {
216  polarity = current_polarity;
217  ROS_FATAL("setPolarity() failed: %d", err);
218  return false; // failure
219  }
220  ROS_DEBUG("setPolarity(): %s", triggerPolarityName(polarity).c_str());
221  }
222  else
223  {
224  ROS_FATAL("Polarity is not supported");
225  return false; // failure
226  }
227 
228  return true; // success
229 }
230 
236 dc1394switch_t Trigger::getExternalTriggerPowerState(dc1394camera_t *camera)
237 {
238  dc1394switch_t state;
239 
240  dc1394error_t err = dc1394_external_trigger_get_power(camera, &state);
241  if (err != DC1394_SUCCESS)
242  {
243  ROS_FATAL("getExternalTriggerPowerState() failed: %d", err);
244  return (dc1394switch_t)-1; // failure
245  }
247  return state;
248 }
249 
257 bool Trigger::setExternalTriggerPowerState(dc1394camera_t *camera, dc1394switch_t &state)
258 {
259  dc1394switch_t current_state = getExternalTriggerPowerState(camera);
260 
261  // if config not changed, then do nothing
262  if(current_state == state) return true;
263 
264  dc1394error_t err = dc1394_external_trigger_set_power(camera, state);
265  if (err != DC1394_SUCCESS)
266  {
267  state = current_state;
268  ROS_FATAL("setExternalTriggerPowerState() failed: %d", err);
269  return false; // failure
270  }
272  ROS_DEBUG("setExternalTriggerPowerState(): %s", (state == DC1394_ON ? "ON" : "OFF"));
273  return true; // success
274 }
275 
281 dc1394switch_t Trigger::getSoftwareTriggerPowerState(dc1394camera_t *camera)
282 {
283  dc1394switch_t state;
284 
285  dc1394error_t err = dc1394_software_trigger_get_power(camera, &state);
286  if (err != DC1394_SUCCESS)
287  {
288  ROS_FATAL("getSoftwareTriggerPowerState() failed: %d", err);
289  return (dc1394switch_t)-1; // failure
290  }
291  return state;
292 }
293 
301 bool Trigger::setSoftwareTriggerPowerState(dc1394camera_t *camera, dc1394switch_t &state)
302 {
303  dc1394switch_t current_state = getSoftwareTriggerPowerState(camera);
304 
305  // if config not changed, then do nothing
306  if(current_state == state) return true;
307 
308  dc1394error_t err = dc1394_software_trigger_set_power(camera, state);
309  if (err != DC1394_SUCCESS)
310  {
311  state = current_state;
312  ROS_FATAL("setSoftwareTriggerPowerState() failed: %d", err);
313  return false; // failure
314  }
315  ROS_DEBUG("setSoftwareTriggerPowerState(): %s", (state == DC1394_ON ? "ON" : "OFF"));
316  return true; // success
317 }
318 
325 dc1394trigger_mode_t Trigger::getMode(dc1394camera_t *camera)
326 {
327  dc1394trigger_mode_t mode;
328 
329  dc1394error_t err = dc1394_external_trigger_get_mode(camera, &mode);
330  if (err != DC1394_SUCCESS)
331  {
332  ROS_FATAL("getTriggerMode() failed: %d", err);
333  return (dc1394trigger_mode_t) DC1394_TRIGGER_MODE_NUM; // failure
334  }
335 
336  return mode;
337 }
338 
346 bool Trigger::setMode(dc1394camera_t *camera, dc1394trigger_mode_t &mode)
347 {
348  dc1394trigger_mode_t current_mode = getMode(camera);
349 
350  // if config not changed, then do nothing
351  if(current_mode == mode) return true;
352 
353  dc1394error_t err = dc1394_external_trigger_set_mode(camera, mode);
354  if (err != DC1394_SUCCESS)
355  {
356  mode = current_mode;
357  ROS_FATAL("setTriggerMode() failed: %d", err);
358  return false; // failure
359  }
360  ROS_DEBUG("setMode(): %s", triggerModeName(mode).c_str());
361  return true; // success
362 }
363 
370 dc1394trigger_source_t Trigger::getSource(dc1394camera_t *camera)
371 {
372  dc1394trigger_source_t source;
373 
374  dc1394error_t err = dc1394_external_trigger_get_source(camera, &source);
375  if (err != DC1394_SUCCESS)
376  {
377  ROS_FATAL("getTriggerSource() failed: %d", err);
378  return (dc1394trigger_source_t) DC1394_TRIGGER_SOURCE_NUM; // failure
379  }
380 
381  return source;
382 }
383 
391 bool Trigger::setSource(dc1394camera_t *camera, dc1394trigger_source_t &source)
392 {
393  dc1394trigger_source_t current_source = getSource(camera);
394 
395  // if config not changed, then do nothing
396  if(current_source == source) return true;
397 
398  dc1394error_t err = dc1394_external_trigger_set_source(camera, source);
399 
400  if (err != DC1394_SUCCESS)
401  {
402  source = current_source;
403  ROS_FATAL("setTriggerSource() failed: %d", err);
404  return false; // failure
405  }
406  ROS_DEBUG("setSource(): %s", triggerSourceName(source).c_str());
407 
408  return true; // success
409 }
410 
417 bool Trigger::reconfigure(Config *newconfig)
418 {
419  bool is_ok = true;
420 
422  // set triggering modes
424  dc1394switch_t on_off = (dc1394switch_t) newconfig->external_trigger;
426  {
427  newconfig->external_trigger = on_off;
428  ROS_ERROR("Failed to set external trigger power");
429  is_ok = false;
430  }
431 
432  on_off = (dc1394switch_t) newconfig->software_trigger;
434  {
435  newconfig->software_trigger = on_off;
436  ROS_ERROR("Failed to set software trigger power");
437  is_ok = false;
438  }
439 
440  if (findTriggerMode(newconfig->trigger_mode))
441  {
443  {
444  // Possible if driver compiled against different version of libdc1394
445  ROS_ASSERT(triggerMode_ <= DC1394_TRIGGER_MODE_MAX);
446  newconfig->trigger_mode = Trigger::trigger_mode_names_[triggerMode_ - DC1394_TRIGGER_MODE_MIN];
447  ROS_ERROR("Failed to set trigger mode");
448  is_ok = false;
449  }
450  }
451  else
452  {
453  ROS_ERROR_STREAM("Unknown trigger mode: " << newconfig->trigger_mode);
454  is_ok = false;
455  }
456 
457  if (triggerSources_.num != 0)
458  {
459  if (findTriggerSource(newconfig->trigger_source) && checkTriggerSource(triggerSource_))
460  {
462  {
463  // Possible if driver compiled against different version of libdc1394
464  ROS_ASSERT(triggerSource_ <= DC1394_TRIGGER_SOURCE_MAX);
465  newconfig->trigger_source = Trigger::trigger_source_names_[triggerSource_ - DC1394_TRIGGER_SOURCE_MIN];
466  ROS_ERROR("Failed to set trigger source");
467  is_ok = false;
468  }
469  }
470  else
471  {
472  ROS_ERROR_STREAM("Unknown trigger source: " << newconfig->trigger_source);
473  is_ok = false;
474  }
475  }
476  else
477  {
478  ROS_DEBUG("No triggering sources available");
479  }
480 
481  if (findTriggerPolarity(newconfig->trigger_polarity))
482  {
484  {
485  // Possible if driver compiled against different version of libdc1394
486  ROS_ASSERT(triggerPolarity_ <= DC1394_TRIGGER_ACTIVE_MAX);
487  newconfig->trigger_polarity = Trigger::trigger_polarity_names_[triggerPolarity_ - DC1394_TRIGGER_ACTIVE_MIN];
488  ROS_ERROR("Failed to set trigger polarity");
489  is_ok = false;
490  }
491  }
492  else
493  {
494  ROS_ERROR_STREAM("Unknown trigger polarity: " << newconfig->trigger_polarity);
495  is_ok = false;
496  }
497 
498  return is_ok;
499 }
500 
508 bool Trigger::initialize(Config *newconfig)
509 {
510  ROS_INFO("[%016lx] has trigger support", camera_->guid);
511 
512  // Enumerate trigger sources
514  {
515  ROS_ERROR("Failed to enumerate trigger sources");
516  return false;
517  }
518 
519  // Update externalTriggerPowerState_ variable with current value
521 
522  // configure trigger features
523  return reconfigure(newconfig);
524 }
bool enumSources(dc1394camera_t *camera, dc1394trigger_sources_t &sources)
Definition: trigger.cpp:128
dc1394trigger_polarity_t triggerPolarity_
Definition: trigger.h:70
#define ROS_FATAL(...)
dc1394trigger_source_t triggerSource_
Definition: trigger.h:68
static const std::string trigger_source_names_[DC1394_TRIGGER_SOURCE_NUM]
driver parameter names, corresponding to DC1394 trigger sources
Definition: trigger.h:61
bool findTriggerSource(std::string str)
Definition: trigger.cpp:79
bool findTriggerPolarity(std::string str)
Definition: trigger.cpp:100
dc1394switch_t externalTriggerPowerState_
Definition: trigger.h:72
const std::string triggerPolarityName(dc1394trigger_polarity_t polarity)
Definition: trigger.h:119
static const std::string trigger_polarity_names_[DC1394_TRIGGER_ACTIVE_NUM]
driver parameter names, corresponding to DC1394 trigger sources
Definition: trigger.h:63
dc1394trigger_sources_t triggerSources_
Definition: trigger.h:69
bool setPolarity(dc1394camera_t *camera, dc1394trigger_polarity_t &polarity)
Definition: trigger.cpp:196
const std::string triggerModeName(dc1394trigger_mode_t mode)
Definition: trigger.h:93
dc1394switch_t getSoftwareTriggerPowerState(dc1394camera_t *camera)
Definition: trigger.cpp:281
const std::string triggerSourceName(dc1394trigger_source_t source)
Definition: trigger.h:106
dc1394trigger_polarity_t getPolarity(dc1394camera_t *camera)
Definition: trigger.cpp:159
dc1394switch_t getExternalTriggerPowerState(dc1394camera_t *camera)
Definition: trigger.cpp:236
bool setMode(dc1394camera_t *camera, dc1394trigger_mode_t &mode)
Definition: trigger.cpp:346
#define ROS_INFO(...)
bool reconfigure(Config *newconfig)
Definition: trigger.cpp:417
dc1394trigger_mode_t triggerMode_
Definition: trigger.h:67
static const std::string trigger_mode_names_[DC1394_TRIGGER_MODE_NUM]
driver parameter names, corresponding to DC1394 trigger modes
Definition: trigger.h:59
libdc1394 triggering modes interface
bool initialize(Config *newconfig)
Definition: trigger.cpp:508
#define ROS_DEBUG_STREAM(args)
bool setExternalTriggerPowerState(dc1394camera_t *camera, dc1394switch_t &state)
Definition: trigger.cpp:257
bool checkTriggerSource(dc1394trigger_source_t source)
Definition: trigger.cpp:115
bool findTriggerMode(std::string str)
Definition: trigger.cpp:52
bool setSoftwareTriggerPowerState(dc1394camera_t *camera, dc1394switch_t &state)
Definition: trigger.cpp:301
camera1394::Camera1394Config Config
Definition: driver1394.h:53
#define ROS_ERROR_STREAM(args)
#define ROS_ASSERT(cond)
bool setSource(dc1394camera_t *camera, dc1394trigger_source_t &source)
Definition: trigger.cpp:391
#define ROS_ERROR(...)
dc1394trigger_mode_t getMode(dc1394camera_t *camera)
Definition: trigger.cpp:325
dc1394camera_t * camera_
Definition: trigger.h:65
dc1394trigger_source_t getSource(dc1394camera_t *camera)
Definition: trigger.cpp:370
#define ROS_DEBUG(...)


camera1394
Author(s): Jack O'Quin, Ken Tossell, Patrick Beeson, Nate Koenig, Andrew Howard, Damien Douxchamps, Dan Dennedy
autogenerated on Mon Jun 10 2019 12:52:31