trigger_node.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2012 Jack O'Quin
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the author nor other contributors may be
00018 *     used to endorse or promote products derived from this software
00019 *     without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 #include <signal.h>
00036 #include <dc1394/dc1394.h>
00037 #include <ros/ros.h>
00038 
00050 void sigsegv_handler(int sig)
00051 {
00052   signal(SIGSEGV, SIG_DFL);
00053   fprintf(stderr, "Segmentation fault, stopping camera driver.\n");
00054   ROS_FATAL("Segmentation fault, stopping camera.");
00055   ros::shutdown();                      // stop the main loop
00056 }
00057 
00058 class TriggerNode
00059 {
00060 public:
00061 
00062   TriggerNode():
00063     camera_(NULL)
00064   {}
00065 
00073   bool setup(void)
00074   {
00075     dc1394_t *dev = dc1394_new();
00076     if (dev == NULL)
00077       {
00078         ROS_FATAL("Failed to initialize dc1394_context.");
00079         return false;
00080       }
00081 
00082     // list all 1394 cameras attached to this computer
00083     dc1394camera_list_t *cameras;
00084     int err = dc1394_camera_enumerate(dev, &cameras);
00085     if (err != DC1394_SUCCESS)
00086       {
00087         ROS_FATAL("Could not get list of cameras");
00088         return false;
00089       }
00090     if (cameras->num == 0)
00091       {
00092         ROS_FATAL("No cameras found");
00093         return false;
00094       }
00095 
00096     // attach to first camera found
00097     ROS_INFO_STREAM("Connecting to first camera, GUID: "
00098                     << std::setw(16) << std::setfill('0') << std::hex
00099                     << cameras->ids[0].guid);
00100     camera_ = dc1394_camera_new(dev, cameras->ids[0].guid);
00101     if (!camera_)
00102       {
00103         ROS_FATAL("Failed to initialize camera.");
00104         return false;
00105       }
00106 
00107     dc1394_camera_free_list(cameras);
00108     return true;
00109   }
00110 
00112   bool spin(void)
00113   {
00114     bool retval = true;
00115     ros::Rate hz(2.0);
00116     while (node_.ok())
00117       {
00118         ros::spinOnce();
00119         if (!trigger())
00120           {
00121             retval = false;
00122             break;
00123           }
00124         hz.sleep();
00125       }
00126     shutdown();
00127     return retval;
00128   }
00129 
00130 private:
00131 
00133   void shutdown(void)
00134   {
00135     dc1394_camera_free(camera_);
00136     camera_ = NULL;
00137   }
00138 
00142   bool trigger(void)
00143   {
00144     dc1394error_t err = dc1394_software_trigger_set_power(camera_, DC1394_ON);
00145     bool retval = (err == DC1394_SUCCESS);
00146     if (!retval)
00147       ROS_FATAL("camera does not provide software trigger");
00148     return retval;
00149   }
00150 
00151   ros::NodeHandle node_;
00152   dc1394camera_t *camera_;
00153 };
00154 
00156 int main(int argc, char **argv)
00157 {
00158   ros::init(argc, argv, "camera1394_trigger_node");
00159   signal(SIGSEGV, &sigsegv_handler);
00160 
00161   TriggerNode trig;
00162 
00163   if (!trig.setup())                    // device connection failed?
00164     return 1;
00165 
00166   if (!trig.spin())                     // device does not support trigger?
00167     return 2;
00168 
00169   return 0;
00170 }


camera1394
Author(s): Jack O'Quin, Ken Tossell, Patrick Beeson, Nate Koenig, Andrew Howard, Damien Douxchamps, Dan Dennedy
autogenerated on Thu Jun 6 2019 19:34:17