embedded_linux_hardware.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef ROS_EMBEDDED_LINUX_HARDWARE_H_
19 #define ROS_EMBEDDED_LINUX_HARDWARE_H_
20 
21 #include <iostream>
22 
23 #ifdef BUILD_LIBROSSERIALEMBEDDEDLINUX
24 extern "C" int elCommInit(char *portName, int baud);
25 extern "C" int elCommRead(int fd);
26 extern "C" elCommWrite(int fd, uint8_t* data, int length);
27 #endif
28 
29 #define DEFAULT_PORT "/dev/ttyAM1"
30 
32 {
33 public:
34  EmbeddedLinuxHardware(const char *pn, long baud = 57600)
35  {
36  strncpy(portName, pn, 30);
37  baud_ = baud;
38  }
39 
41  {
42  const char *envPortName = getenv("ROSSERIAL_PORT");
43  if (envPortName == NULL)
44  strcpy(portName, DEFAULT_PORT);
45  else
46  strncpy(portName, envPortName, 29);
47  portName[29] = '\0'; // in case user gave us too long a port name
48  baud_ = 57600;
49  }
50 
51  void setBaud(long baud)
52  {
53  this->baud_ = baud;
54  }
55 
56  int getBaud()
57  {
58  return baud_;
59  }
60 
61  void init()
62  {
64  if (fd < 0)
65  {
66  std::cout << "Exiting" << std::endl;
67  exit(-1);
68  }
69  std::cout << "EmbeddedHardware.h: opened serial port successfully\n";
70  clock_gettime(CLOCK_MONOTONIC, &start); // record when the program started
71  }
72 
73  void init(const char *pName)
74  {
75  fd = elCommInit(pName, baud_);
76  if (fd < 0)
77  {
78  std::cout << "Exiting" << std::endl;
79  exit(-1);
80  }
81  std::cout << "EmbeddedHardware.h: opened comm port successfully\n";
82  clock_gettime(CLOCK_MONOTONIC, &start); // record when the program started
83  }
84 
85  int read()
86  {
87  int c = elCommRead(fd);
88  return c;
89  }
90 
91  void write(uint8_t* data, int length)
92  {
93  elCommWrite(fd, data, length);
94  }
95 
96  unsigned long time()
97  {
98  long millis, seconds, nseconds;
99 
100  clock_gettime(CLOCK_MONOTONIC, &end);
101 
102  seconds = end.tv_sec - start.tv_sec;
103  nseconds = end.tv_nsec - start.tv_nsec;
104 
105  millis = ((seconds) * 1000 + nseconds / 1000000.0) + 0.5;
106 
107  return millis;
108  }
109 
110 protected:
111  int fd;
112  char portName[30];
113  long baud_;
114  struct timespec start, end;
115 };
116 
117 #endif
struct timespec start end
void init(const char *pName)
#define DEFAULT_PORT
int elCommWrite(int fd, uint8_t *data, int len)
start
Definition: hall.py:25
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)
baud


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57