embedded_linux_hardware.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
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 Willow Garage, Inc. nor the names of its
00018  *    contributors may be used to endorse or promote prducts derived
00019  *    from this software 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 #ifndef ROS_EMBEDDED_LINUX_HARDWARE_H_
00036 #define ROS_EMBEDDED_LINUX_HARDWARE_H_
00037 
00038 #include <iostream>
00039 
00040 #ifdef BUILD_LIBROSSERIALEMBEDDEDLINUX
00041 extern "C" int elCommInit(const char *portName, int baud);
00042 extern "C" int elCommRead(int fd);
00043 extern "C" int elCommWrite(int fd, uint8_t* data, int length);
00044 #endif
00045 
00046 #define DEFAULT_PORT "/dev/ttyAM1"
00047 
00048 class EmbeddedLinuxHardware
00049 {
00050 public:
00051   EmbeddedLinuxHardware(const char *pn, long baud = 57600)
00052   {
00053     strncpy(portName, pn, 30);
00054     baud_ = baud;
00055   }
00056 
00057   EmbeddedLinuxHardware()
00058   {
00059     const char *envPortName = getenv("ROSSERIAL_PORT");
00060     if (envPortName == NULL)
00061       strcpy(portName, DEFAULT_PORT);
00062     else
00063       strncpy(portName, envPortName, 29);
00064     portName[29] = '\0'; // in case user gave us too long a port name
00065     baud_ = 57600;
00066   }
00067 
00068   void setBaud(long baud)
00069   {
00070     this->baud_ = baud;
00071   }
00072 
00073   int getBaud()
00074   {
00075     return baud_;
00076   }
00077 
00078   void init()
00079   {
00080     fd = elCommInit(portName, baud_);
00081     if (fd < 0)
00082     {
00083       std::cout << "Exiting" << std::endl;
00084       exit(-1);
00085     }
00086     std::cout << "EmbeddedHardware.h: opened serial port successfully\n";
00087     clock_gettime(CLOCK_MONOTONIC, &start);     // record when the program started
00088   }
00089 
00090   void init(const char *pName)
00091   {
00092     fd = elCommInit(pName, baud_);
00093     if (fd < 0)
00094     {
00095       std::cout << "Exiting" << std::endl;
00096       exit(-1);
00097     }
00098     std::cout << "EmbeddedHardware.h: opened comm port successfully\n";
00099     clock_gettime(CLOCK_MONOTONIC, &start);     // record when the program started
00100   }
00101 
00102   int read()
00103   {
00104     int c = elCommRead(fd);
00105     return c;
00106   }
00107 
00108   void write(uint8_t* data, int length)
00109   {
00110     elCommWrite(fd, data, length);
00111   }
00112 
00113   unsigned long time()
00114   {
00115     long millis, seconds, nseconds;
00116 
00117     clock_gettime(CLOCK_MONOTONIC, &end);
00118 
00119     seconds  = end.tv_sec  - start.tv_sec;
00120     nseconds = end.tv_nsec - start.tv_nsec;
00121 
00122     millis = ((seconds) * 1000 + nseconds / 1000000.0) + 0.5;
00123 
00124     return millis;
00125   }
00126 
00127 protected:
00128   int fd;
00129   char portName[30];
00130   long baud_;
00131   struct timespec start, end;
00132 };
00133 
00134 #endif


rosserial_embeddedlinux
Author(s): Paul Bouchier
autogenerated on Thu Jun 6 2019 19:56:29