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 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 #include <ms35.h>
00056 #include <ros/ros.h>
00057 #include <boost/crc.hpp>
00058 #include <boost/cstdint.hpp>
00059 #include <boost/integer.hpp>
00060 
00061 MS35::MS35(SerialIO* serialIO)
00062 {
00063         _serialIO = serialIO;
00064         const char init_data[] = { 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd };
00065         int init_len = sizeof(init_data) / sizeof(init_data[0]);
00066         const char startup_data[] = { 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
00067         int startup_len = sizeof(startup_data) / sizeof(startup_data[0]);
00068         char init_buf[18];
00069 
00070         
00071         char tmp = 0xfd;
00072         std::string recv;
00073         int bytes_recv;
00074         do{
00075                 _serialIO->sendData(&tmp, 1);
00076                 bytes_recv = _serialIO->readData(recv, 1);
00077 
00078         }while(bytes_recv <= 0);
00079 
00080         unsigned short int crc = getChecksum(startup_data, startup_len);
00081 
00082         memcpy(&init_buf, init_data, init_len);
00083         memcpy(&init_buf[init_len], startup_data, startup_len);
00084         init_buf[init_len+startup_len] = ((char*)&crc)[1];
00085         init_buf[init_len+startup_len+1] = ((char*)&crc)[0];
00086         sendData(init_buf, 18);
00087 }
00088 
00089 MS35::~MS35()
00090 {
00091 }
00092 
00093 unsigned short int MS35::getChecksum(const char* data, size_t len)
00094 {
00095         unsigned int ret;
00096         boost::crc_16_type checksum_agent;
00097         checksum_agent.process_bytes(data, len);
00098         return checksum_agent.checksum();;
00099 }
00100 
00101 int MS35::sendData(const char* data, size_t len)
00102 {
00103         int ret = -1;
00104         int bytes_wrote = 0;
00105 
00106         bytes_wrote = _serialIO->sendData(data, len);
00107         if(bytes_wrote == -1)
00108         {
00109                 ROS_WARN("Can not write to serial port. Port closed!");
00110                 ret = -1;
00111         }
00112         else
00113         {
00114                 ROS_DEBUG("Wrote [%s] with %i bytes from %i bytes", data, bytes_wrote, len);
00115                 
00116                 
00117                 
00118                 
00119                 
00120         }
00121         return ret;
00122 }
00123 
00124 
00125 void MS35::setColor(color::rgba color)
00126 {
00127         color::rgba color_tmp = color;
00128 
00129         
00130         
00131         color.r *= color.a;
00132         color.g *= color.a;
00133         color.b *= color.a;
00134 
00135         color.r = fabs(color.r * 255);
00136         color.g = fabs(color.g * 255);
00137         color.b = fabs(color.b * 255);
00138 
00139         buffer[0] = 0x01; buffer[1] = 0x00; 
00140         buffer[2] = (int)color.r; buffer[3]=(int)color.g; buffer[4]=(int)color.b;
00141         buffer[5] = 0x00; buffer[6]=0x00;
00142 
00143         unsigned short int crc = getChecksum(buffer, 7);
00144         buffer[7] = ((char*)&crc)[1];
00145         buffer[8] = ((char*)&crc)[0];
00146         
00147 
00148         if(sendData(buffer, PACKAGE_SIZE))
00149                 m_sigColorSet(color_tmp);
00150 }