embedded_linux_hardware.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote prducts derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef ROS_EMBEDDED_LINUX_HARDWARE_H_
36 #define ROS_EMBEDDED_LINUX_HARDWARE_H_
37 
38 #include <iostream>
39 
40 #ifdef BUILD_LIBROSSERIALEMBEDDEDLINUX
41 extern "C" int elCommInit(char *portName, int baud);
42 extern "C" int elCommRead(int fd);
43 extern "C" elCommWrite(int fd, uint8_t* data, int length);
44 #endif
45 
46 #ifdef __linux__
47 #include <time.h>
48 #endif
49 
50 // Includes necessary to support time on OS X.
51 #ifdef __MACH__
52 #include <mach/mach.h>
53 #include <mach/mach_time.h>
54 static mach_timebase_info_data_t sTimebaseInfo;
55 #endif
56 
57 #define DEFAULT_PORT "/dev/ttyAM1"
58 
60 {
61 public:
62  EmbeddedLinuxHardware(const char *pn, long baud = 57600)
63  {
64  strncpy(portName, pn, 30);
65  baud_ = baud;
66  }
67 
69  {
70  const char *envPortName = getenv("ROSSERIAL_PORT");
71  if (envPortName == NULL)
72  strcpy(portName, DEFAULT_PORT);
73  else
74  strncpy(portName, envPortName, 29);
75  portName[29] = '\0'; // in case user gave us too long a port name
76  baud_ = 57600;
77  }
78 
79  void setBaud(long baud)
80  {
81  this->baud_ = baud;
82  }
83 
84  int getBaud()
85  {
86  return baud_;
87  }
88 
89  void init()
90  {
92  if (fd < 0)
93  {
94  std::cout << "Exiting" << std::endl;
95  exit(-1);
96  }
97  std::cout << "EmbeddedHardware.h: opened serial port successfully\n";
98 
99  initTime();
100  }
101 
102  void init(const char *pName)
103  {
104  fd = elCommInit(pName, baud_);
105  if (fd < 0)
106  {
107  std::cout << "Exiting" << std::endl;
108  exit(-1);
109  }
110  std::cout << "EmbeddedHardware.h: opened comm port successfully\n";
111 
112  initTime();
113  }
114 
115  int read()
116  {
117  int c = elCommRead(fd);
118  return c;
119  }
120 
121  void write(uint8_t* data, int length)
122  {
123  elCommWrite(fd, data, length);
124  }
125 
126 #ifdef __linux__
127  void initTime()
128  {
129  clock_gettime(CLOCK_MONOTONIC, &start);
130  }
131 
132  unsigned long time()
133  {
134  struct timespec end;
135  long seconds, nseconds;
136 
137  clock_gettime(CLOCK_MONOTONIC, &end);
138 
139  seconds = end.tv_sec - start.tv_sec;
140  nseconds = end.tv_nsec - start.tv_nsec;
141 
142  return ((seconds) * 1000 + nseconds / 1000000.0) + 0.5;
143  }
144 
145 #elif __MACH__
146  void initTime()
147  {
148  start = mach_absolute_time();
149  mach_timebase_info(&sTimebaseInfo);
150  }
151 
152  unsigned long time()
153  {
154  // See: https://developer.apple.com/library/mac/qa/qa1398/_index.html
155  uint64_t elapsed = mach_absolute_time() - start;
156  return elapsed * sTimebaseInfo.numer / (sTimebaseInfo.denom * 1000000);
157  }
158 #endif
159 
160 protected:
161  int fd;
162  char portName[30];
163  long baud_;
164 
165 #ifdef __linux__
166  struct timespec start;
167 #elif __MACH__
168  uint64_t start;
169 #endif
170 };
171 
172 #endif
void init(const char *pName)
#define DEFAULT_PORT
int elCommWrite(int fd, uint8_t *data, int len)
int elCommRead(int fd)
void write(uint8_t *data, int length)
EmbeddedLinuxHardware(const char *pn, long baud=57600)
int elCommInit(const char *portName, int baud)


rosserial_embeddedlinux
Author(s): Paul Bouchier
autogenerated on Fri Jun 7 2019 22:02:46