00001 /*!@file Devices/HMR3300.C class for interfacing with a Honeywell 3300 compass */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Nitin Dhavale <dhavale@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/HMR3300.C $ 00035 // $Id: HMR3300.C 7880 2007-02-09 02:34:07Z itti $ 00036 // 00037 00038 #include "Devices/HMR3300.H" 00039 #include <sstream> 00040 #include <string> 00041 00042 void *Compass_run(void *c); 00043 00044 // ###################################################################### 00045 void *Compass_run(void *c) 00046 { 00047 HMR3300 *d = (HMR3300 *)c; 00048 d->run(); 00049 return NULL; 00050 } 00051 00052 // ###################################################################### 00053 HMR3300Listener::~HMR3300Listener() 00054 { } 00055 00056 // ###################################################################### 00057 HMR3300::HMR3300(OptionManager& mgr, const std::string& descrName, 00058 const std::string& tagName, const char *dev) : 00059 ModelComponent(mgr, descrName, tagName), 00060 itsSerial(new Serial(mgr, descrName+" Serial Port", tagName+"SerialPort")), 00061 itsKeepgoing(true), itsListener(), itsHeading(), itsPitch(), itsRoll() 00062 { 00063 itsSerial->configure(dev, 19200, "8N1", false, false, 0); 00064 addSubComponent(itsSerial); 00065 pthread_mutex_init(&itsLock, NULL); 00066 } 00067 00068 // ###################################################################### 00069 void HMR3300::setListener(rutz::shared_ptr<HMR3300Listener>& listener) 00070 { itsListener = listener; } 00071 00072 // ###################################################################### 00073 void HMR3300::start2() 00074 { pthread_create(&itsRunner, NULL, &Compass_run, (void *)this); } 00075 00076 // ###################################################################### 00077 void HMR3300::stop1() 00078 { 00079 itsKeepgoing = false; 00080 usleep(300000); // make sure thread has exited 00081 } 00082 00083 // ###################################################################### 00084 HMR3300::~HMR3300() 00085 { 00086 pthread_mutex_destroy(&itsLock); 00087 } 00088 00089 // ###################################################################### 00090 void HMR3300::run() 00091 { 00092 unsigned char c = 255; 00093 while(itsKeepgoing) 00094 { 00095 // skip to next LF: 00096 while(c != '\n') c = itsSerial->read(); 00097 00098 // get data until next LF: 00099 std::string str; 00100 while( (c = itsSerial->read() ) != '\n') 00101 if (isdigit(c) || c == '.' || c == '-') str += c; 00102 else str += ' '; 00103 // convert to doubles: 00104 std::stringstream strs(str); 00105 double hh, pp, rr; strs >> hh >> rr >> pp; 00106 00107 // update our internal data: 00108 pthread_mutex_lock(&itsLock); 00109 itsHeading = hh; itsPitch = pp; itsRoll = rr; 00110 pthread_mutex_unlock(&itsLock); 00111 00112 // if we have a listener, let it know: 00113 if (itsListener.is_valid()) itsListener->newData(hh, pp, rr); 00114 } 00115 00116 pthread_exit(0); 00117 } 00118 00119 // ###################################################################### 00120 void HMR3300::get(Angle& heading, Angle& pitch, Angle& roll) 00121 { 00122 pthread_mutex_lock(&itsLock); 00123 heading = itsHeading; pitch = itsPitch; roll = itsRoll; 00124 pthread_mutex_unlock(&itsLock); 00125 } 00126 00127 // ###################################################################### 00128 Angle HMR3300::getHeading() 00129 { 00130 pthread_mutex_lock(&itsLock); 00131 const Angle ret = itsHeading; 00132 pthread_mutex_unlock(&itsLock); 00133 return ret; 00134 } 00135 00136 // ###################################################################### 00137 Angle HMR3300::getPitch() 00138 { 00139 pthread_mutex_lock(&itsLock); 00140 const Angle ret = itsPitch; 00141 pthread_mutex_unlock(&itsLock); 00142 return ret; 00143 } 00144 00145 // ###################################################################### 00146 Angle HMR3300::getRoll() 00147 { 00148 pthread_mutex_lock(&itsLock); 00149 const Angle ret = itsRoll; 00150 pthread_mutex_unlock(&itsLock); 00151 return ret; 00152 } 00153 00154 // ###################################################################### 00155 /* So things look consistent in everyone's emacs... */ 00156 /* Local Variables: */ 00157 /* indent-tabs-mode: nil */ 00158 /* End: */