embedded_linux_hardware.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #ifndef ROS_EMBEDDED_LINUX_HARDWARE_H_
00019 #define ROS_EMBEDDED_LINUX_HARDWARE_H_
00020 
00021 #include <iostream>
00022 
00023 #ifdef BUILD_LIBROSSERIALEMBEDDEDLINUX
00024 extern "C" int elCommInit(char *portName, int baud);
00025 extern "C" int elCommRead(int fd);
00026 extern "C" elCommWrite(int fd, uint8_t* data, int length);
00027 #endif
00028 
00029 #define DEFAULT_PORT "/dev/ttyAM1"
00030 
00031 class EmbeddedLinuxHardware
00032 {
00033 public:
00034   EmbeddedLinuxHardware(const char *pn, long baud = 57600)
00035   {
00036     strncpy(portName, pn, 30);
00037     baud_ = baud;
00038   }
00039 
00040   EmbeddedLinuxHardware()
00041   {
00042     const char *envPortName = getenv("ROSSERIAL_PORT");
00043     if (envPortName == NULL)
00044       strcpy(portName, DEFAULT_PORT);
00045     else
00046       strncpy(portName, envPortName, 29);
00047     portName[29] = '\0'; // in case user gave us too long a port name
00048     baud_ = 57600;
00049   }
00050 
00051   void setBaud(long baud)
00052   {
00053     this->baud_ = baud;
00054   }
00055 
00056   int getBaud()
00057   {
00058     return baud_;
00059   }
00060 
00061   void init()
00062   {
00063     fd = elCommInit(portName, baud_);
00064     if (fd < 0)
00065     {
00066       std::cout << "Exiting" << std::endl;
00067       exit(-1);
00068     }
00069     std::cout << "EmbeddedHardware.h: opened serial port successfully\n";
00070     clock_gettime(CLOCK_MONOTONIC, &start);     // record when the program started
00071   }
00072 
00073   void init(const char *pName)
00074   {
00075     fd = elCommInit(pName, baud_);
00076     if (fd < 0)
00077     {
00078       std::cout << "Exiting" << std::endl;
00079       exit(-1);
00080     }
00081     std::cout << "EmbeddedHardware.h: opened comm port successfully\n";
00082     clock_gettime(CLOCK_MONOTONIC, &start);     // record when the program started
00083   }
00084 
00085   int read()
00086   {
00087     int c = elCommRead(fd);
00088     return c;
00089   }
00090 
00091   void write(uint8_t* data, int length)
00092   {
00093     elCommWrite(fd, data, length);
00094   }
00095 
00096   unsigned long time()
00097   {
00098     long millis, seconds, nseconds;
00099 
00100     clock_gettime(CLOCK_MONOTONIC, &end);
00101 
00102     seconds  = end.tv_sec  - start.tv_sec;
00103     nseconds = end.tv_nsec - start.tv_nsec;
00104 
00105     millis = ((seconds) * 1000 + nseconds / 1000000.0) + 0.5;
00106 
00107     return millis;
00108   }
00109 
00110 protected:
00111   int fd;
00112   char portName[30];
00113   long baud_;
00114   struct timespec start, end;
00115 };
00116 
00117 #endif


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57