serial_interface.cpp
Go to the documentation of this file.
00001 #include "quad_can_driver/serial_interface.h"
00002 
00003 int dev_;
00004 speed_t serialport_baud_;
00005 unsigned char tx_buffer[150];
00006 
00007 speed_t bitrate (int Bitrate){
00008         
00009     switch (Bitrate){
00010       case 9600:
00011         return B9600;
00012       case 19200:
00013         return B19200;
00014       case 38400:
00015         return B38400;
00016       case 57600:
00017         return B57600;
00018       case 115200:
00019         return B115200;
00020       case 230400:
00021         return B230400;
00022       default:                 // invalid bitrate
00023         return B0;
00024     }
00025 }
00026 
00027 void InitSerialInterface (std::string serialport_name_, uint32_t serialport_speed_){
00028         struct termios tio;
00029       serialport_baud_ = bitrate (serialport_speed_);
00030       ROS_INFO ("Initializing serial port...");
00031 
00032       dev_ = open(serialport_name_.c_str (),O_RDWR | O_NOCTTY | O_NDELAY);
00033       ROS_ASSERT_MSG (dev_ != -1, "Failed to open serial port: %s %s", serialport_name_.c_str (), strerror (errno));
00034 
00035       ROS_ASSERT_MSG (tcgetattr (dev_, &tio) == 0, "Unknown Error: %s", strerror (errno));
00036 
00037       cfsetispeed (&tio, serialport_baud_);
00038       cfsetospeed (&tio, serialport_baud_);
00039 
00040       tio.c_iflag = 0;
00041       tio.c_iflag &= ~(BRKINT | ICRNL | IMAXBEL);
00042       tio.c_iflag |= IGNBRK;
00043 
00044       tio.c_oflag = 0;
00045       tio.c_oflag &= ~(OPOST | ONLCR);
00046 
00047       tio.c_cflag = (tio.c_cflag & ~CSIZE) | CS8;
00048       tio.c_cflag &= ~(PARENB | CRTSCTS | CSTOPB);
00049 
00050       tio.c_lflag = 0;
00051       tio.c_lflag |= NOFLSH;
00052       tio.c_lflag &= ~(ISIG | IEXTEN | ICANON | ECHO | ECHOE);
00053 
00054       ROS_ASSERT_MSG (tcsetattr (dev_, TCSADRAIN, &tio) == 0, "Unknown Error: %s", strerror (errno));
00055 
00056       tio.c_cc[VMIN] = 0;
00057       tio.c_cc[VTIME] = 0;
00058 
00059       tcflush (dev_, TCIOFLUSH);
00060 
00061       ROS_ASSERT_MSG (dev_ != -1, "Could not open serial port %s", serialport_name_.c_str ());
00062       ROS_INFO ("Successfully connected to %s, Baudrate %d\n", serialport_name_.c_str (), serialport_speed_);
00063  }
00064 
00065 void CloseSerialInterface (void){
00066     ROS_INFO ("Destroying CAN Interface");
00067     close (dev_);
00068 }
00069 
00070 void AddCRC_CAN(unsigned int frame_length){ //length of #,adr,cmd,data
00071         
00072         unsigned short int tmpCRC = 0;
00073         unsigned int i;
00074 
00075         for (i=0; i < frame_length;i++)
00076         {
00077                 tmpCRC += tx_buffer[i+3];
00078         }
00079 
00080         tx_buffer[16] = 0x00FF & tmpCRC;
00081         tx_buffer[17] = tmpCRC >> 8;
00082 }
00083   
00084 int sendStringToCom (unsigned char *output, int len){
00085           
00086     int i;
00087     i = write (dev_, output, len);
00088         return i;
00089 }
00090 
00091 void SendOutData(unsigned char ID, str_ExternControl cmd, unsigned char N){
00092 
00093         int j=0;
00094 
00095         tx_buffer[0] = 0x55;               // Start-Byte
00096         tx_buffer[1] = 0x41;
00097         tx_buffer[2] = 0x56;
00098         tx_buffer[3] = ID;                // Adress
00099         tx_buffer[4] = 0X00;               
00100         tx_buffer[5] = 0X00;
00101         tx_buffer[6] = 0x00;
00102         tx_buffer[7] = N;
00103         tx_buffer[8] = cmd.Throttle;        // Commands
00104         tx_buffer[9] = (unsigned char) cmd.Yaw;
00105         tx_buffer[10] = (unsigned char) cmd.Pitch;
00106         tx_buffer[11] = (unsigned char) cmd.Roll;
00107         tx_buffer[12] = (unsigned char) cmd.Aux1;
00108         tx_buffer[13] = (unsigned char) cmd.Aux2;
00109         tx_buffer[14] = (unsigned char) cmd.Aux3;
00110         tx_buffer[15] = (unsigned char) cmd.Aux4;
00111         
00112         AddCRC_CAN(13);
00113 
00114         j=sendStringToCom(tx_buffer, 16+2);     //whole frame length is pt+3
00115                                                                                 //#,adr,cmd,data ; crc1,crc2,\r
00116                                                                                 
00117         if (j<0) ROS_WARN("Failed to write to COM");
00118 }
00119 


quad_can_driver
Author(s): Henrique
autogenerated on Mon Jan 6 2014 11:48:02