Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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';
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);
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);
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