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(char *portName, int baud);
00042 extern "C" int elCommRead(int fd);
00043 extern "C" elCommWrite(int fd, uint8_t* data, int length);
00044 #endif
00045
00046 #define DEFAULT_PORT "/dev/ttyAM1"
00047
00048 class EmbeddedLinuxHardware {
00049 public:
00050 EmbeddedLinuxHardware(char *pn, long baud= 57600){
00051 strncpy(portName, pn, 30);
00052 baud_ = baud;
00053 }
00054 EmbeddedLinuxHardware()
00055 {
00056 char *envPortName = getenv("ROSSERIAL_PORT");
00057 if (envPortName == NULL)
00058 strcpy(portName, DEFAULT_PORT);
00059 else
00060 strncpy(portName, envPortName, 29);
00061 portName[29] = '\0';
00062 baud_ = 57600;
00063 }
00064
00065 void setBaud(long baud){
00066 this->baud_= baud;
00067 }
00068
00069 int getBaud(){return baud_;}
00070
00071 void init(){
00072 fd = elCommInit(portName, baud_);
00073 if (fd < 0) {
00074 std::cout << "Exiting" << std::endl;
00075 exit(-1);
00076 }
00077 std::cout << "EmbeddedHardware.h: opened serial port successfully\n";
00078 clock_gettime(CLOCK_MONOTONIC, &start);
00079 }
00080
00081 void init(char *pName){
00082 fd = elCommInit(pName, baud_);
00083 if (fd < 0) {
00084 std::cout << "Exiting" << std::endl;
00085 exit(-1);
00086 }
00087 std::cout << "EmbeddedHardware.h: opened comm port successfully\n";
00088 clock_gettime(CLOCK_MONOTONIC, &start);
00089 }
00090
00091 int read()
00092 {
00093 int c = elCommRead(fd);
00094
00095 if (c > 0) {
00096 }
00097 return c;
00098 }
00099
00100 void write(uint8_t* data, int length)
00101 {
00102 elCommWrite(fd, data, length);
00103
00104
00105
00106
00107 }
00108
00109 unsigned long time()
00110 {
00111 long millis, seconds, nseconds;
00112
00113 clock_gettime(CLOCK_MONOTONIC, &end);
00114
00115 seconds = end.tv_sec - start.tv_sec;
00116 nseconds = end.tv_nsec - start.tv_nsec;
00117
00118 millis = ((seconds) * 1000 + nseconds/1000000.0) + 0.5;
00119
00120 return millis;
00121 }
00122
00123 protected:
00124 int fd;
00125 char portName[30];
00126 long baud_;
00127 struct timespec start, end;
00128 };
00129
00130 #endif