USB2_F_7001.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2012, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita on 01/03/2010
00036 *********************************************************************/
00037 #include <stdio.h>
00038 #include <string.h>
00039 #include <errno.h>
00040 #include <termios.h>
00041 #include <math.h>
00042 #include <poll.h>
00043 #include <signal.h>
00044 #include <fcntl.h>
00045 #include <iostream>
00046 #include <fstream>
00047 
00048 #include "USB2_F_7001.h"
00049 
00050 #include <ros/ros.h>
00051 
00052 USB2_F_7001::USB2_F_7001(std::string * serial_port_name, boost::function<void(std::string*)> f) : serial_port_()
00053 {
00054     open_ = false;
00055     
00056     try{ serial_port_.open((char*)serial_port_name->c_str(), 115200); }
00057         catch(cereal::Exception& e)
00058         {
00059                 ROS_FATAL("USB2_F_7001 - %s - Failed to open the serial port!!!", __FUNCTION__);
00060         ROS_BREAK();
00061         }
00062     
00063     receivedFrameCallback = f;
00064 }
00065 
00066 USB2_F_7001::~USB2_F_7001()
00067 {
00068     closeCANBus();
00069     
00070         serial_port_.close();
00071 }
00072 
00073 bool USB2_F_7001::openCANBus(USB2_F_7001_BitRate bit_rate)
00074 {
00075     if(open_) return false;
00076     
00077     // Set the desired bit rate
00078     if( !setCANBitRate(bit_rate) )
00079     {
00080         ROS_ERROR("USB2_F_7001 - %s - Failed to set bit rate!", __FUNCTION__);
00081         return false;
00082     }
00083     
00084     char msg[3];
00085     
00086     msg[0] = 'O';
00087     msg[1] = USB2_F_7001_OK;
00088     msg[2] = 0;
00089     
00090     // Open the CAN bus
00091     try{ serial_port_.write(msg); }
00092     catch(cereal::Exception& e)
00093     {
00094         ROS_ERROR("USB2_F_7001 - %s - Failed to send open message!", __FUNCTION__);
00095         return false;
00096     }
00097     
00098     // Reply
00099     char reply;
00100     serial_port_.read(&reply, 1);
00101     if(reply != USB2_F_7001_OK) return false;
00102     
00103     open_ = true;
00104     
00105     // Clear the buffers
00106     /*if( !clearBuffers() )
00107     {
00108         ROS_ERROR("USB2_F_7001 - %s - Failed to clear the buffers!", __FUNCTION__);
00109         return false;
00110     }*/
00111     
00112     // Start streaming data
00113     if( !serial_port_.startReadBetweenStream(boost::bind(&USB2_F_7001::newFrameCallback, this, _1), 't', USB2_F_7001_OK) )
00114         {
00115         closeCANBus();
00116         ROS_ERROR("USB2_F_7001 - %s - Failed to start streaming data!", __FUNCTION__);
00117                 return false;
00118         }
00119     
00120     return true;
00121 }
00122 
00123 bool USB2_F_7001::closeCANBus()
00124 {
00125     if(!open_) return false;
00126     
00127     serial_port_.stopStream();
00128     
00129     char msg[3];
00130     
00131     msg[0] = 'C';
00132     msg[1] = USB2_F_7001_OK;
00133     msg[2] = 0;
00134     
00135     try{ serial_port_.write(msg); }
00136     catch(cereal::Exception& e)
00137     {
00138         ROS_ERROR("USB2_F_7001 - %s - Failed to send close msg!", __FUNCTION__);
00139         return false;
00140     }
00141     
00142     // Reply
00143     char reply;
00144     serial_port_.read(&reply, 1);
00145     if(reply == USB2_F_7001_OK)
00146     {
00147         open_ = false;
00148         return true;
00149     }
00150     
00151     ROS_ERROR("USB2_F_7001 - %s - Did not get reply - %d", __FUNCTION__, (int)reply);
00152     return false;
00153 }
00154 
00155 bool USB2_F_7001::reset()
00156 {
00157     if(open_) return false;
00158     
00159     char msg[3];
00160     
00161     msg[0] = 'R';
00162     msg[1] = USB2_F_7001_OK;
00163     msg[2] = 0;
00164     
00165     try{ serial_port_.write(msg); }
00166     catch(cereal::Exception& e)
00167     {
00168         ROS_ERROR("USB2_F_7001 - %s - Failed to send reset msg!", __FUNCTION__);
00169         return false;
00170     }
00171     
00172     // Reply
00173     char reply;
00174     serial_port_.read(&reply, 1);
00175     if(reply == USB2_F_7001_OK) return true;
00176     
00177     ROS_ERROR("USB2_F_7001 - %s - Did not get reply - %d", __FUNCTION__, (int)reply);
00178     return false;
00179 }
00180 
00181 bool USB2_F_7001::setCANBitRate(USB2_F_7001_BitRate bit_rate)
00182 {
00183     if(open_) return false;
00184     
00185     char msg[4];
00186     
00187     msg[0] = 'S';
00188     msg[1] = bit_rate+48;
00189     msg[2] = USB2_F_7001_OK;
00190     msg[3] = 0;
00191     
00192     try{ serial_port_.write(msg); }
00193     catch(cereal::Exception& e)
00194     {
00195         ROS_INFO("USB2_F_7001 - %s - Unable to send bit rate!", __FUNCTION__);
00196         return false;
00197     }
00198     
00199     // Reply
00200     char reply;
00201     serial_port_.read(&reply, 1);
00202     if(reply == USB2_F_7001_OK) return true;
00203     
00204     ROS_ERROR("USB2_F_7001 - %s - Did not get reply - %d", __FUNCTION__, (int)reply);
00205     return false;
00206 }
00207 
00208 bool USB2_F_7001::clearBuffers()
00209 {
00210     char msg[3];
00211     
00212     msg[0] = 'E';
00213     msg[1] = USB2_F_7001_OK;
00214     msg[2] = 0;
00215     
00216     try{ serial_port_.write(msg); }
00217     catch(cereal::Exception& e)
00218     {
00219         ROS_INFO("USB2_F_7001 - %s - Unable to send clear buffers msg!", __FUNCTION__);
00220         return false;
00221     }
00222     
00223     // Reply
00224     char reply;
00225     serial_port_.read(&reply, 1);
00226     if(reply == USB2_F_7001_OK) return true;
00227     
00228     ROS_ERROR("USB2_F_7001 - %s - Did not get reply - %d", __FUNCTION__, (int)reply);
00229     return false;
00230 }
00231 
00232 bool USB2_F_7001::transmitStandardFrame(std::string * frame)
00233 {
00234     if(!open_) return false;
00235     
00236     char ok[2];
00237     ok[0] = USB2_F_7001_OK;
00238     ok[1] = 0;
00239     
00240     std::string msg;
00241     
00242     msg = "t";
00243     msg.append(*frame);
00244     msg.append(ok);
00245     
00246     serial_port_.pauseStream();
00247     
00248     try{ serial_port_.write(msg.c_str()); }
00249     catch(cereal::Exception& e)
00250     {
00251         return false;
00252     }
00253     
00254     // Reply
00255     char reply;
00256     serial_port_.read(&reply, 1);
00257     
00258     serial_port_.resumeStream();
00259     
00260     if(reply == 'z') return true;
00261     
00262     ROS_ERROR("USB2_F_7001 - %s - Did not get reply - %d", __FUNCTION__, (int)reply);
00263     return false;
00264 }
00265 
00266 // TODO: Implement this function!
00267 bool USB2_F_7001::transmitExtendedFrame(std::string * frame)
00268 {
00269     if(!open_) return false;
00270     
00271     return false;
00272 }
00273 
00274 // TODO: Implement this function!
00275 int USB2_F_7001::getVersion()
00276 {
00277     if(open_) return false;
00278     
00279     return 0;
00280 }
00281 
00282 // TODO: Implement this function!
00283 int USB2_F_7001::getSerialNumber()
00284 {
00285     if(open_) return false;
00286     
00287     return 0;
00288 }
00289 
00290 void USB2_F_7001::newFrameCallback(std::string * frame)
00291 {
00292     receivedFrameCallback(frame);
00293 }
00294 
00295 // EOF


can_usb_adapters
Author(s): Gonçalo Cabrita
autogenerated on Mon Jan 6 2014 11:25:18