ms35.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #include <ms35.h>
00019 #include <ros/ros.h>
00020 #include <boost/crc.hpp>
00021 #include <boost/cstdint.hpp>
00022 #include <boost/integer.hpp>
00023 
00024 MS35::MS35(SerialIO* serialIO)
00025 {
00026   _serialIO = serialIO;
00027 }
00028 
00029 MS35::~MS35()
00030 {
00031 }
00032 
00033 bool MS35::init()
00034 {
00035   bool ret = false;
00036   const char init_data[] = { 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd };
00037   int init_len = sizeof(init_data) / sizeof(init_data[0]);
00038   const char startup_data[] = { 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
00039   int startup_len = sizeof(startup_data) / sizeof(startup_data[0]);
00040   char init_buf[18];
00041 
00042   //write data until controller is ready to receive valid package
00043   char tmp = 0xfd;
00044   std::string recv;
00045   int bytes_recv;
00046   do{
00047     _serialIO->sendData(&tmp, 1);
00048     bytes_recv = _serialIO->readData(recv, 1);
00049 
00050   }while(bytes_recv <= 0);
00051 
00052   unsigned short int crc = getChecksum(startup_data, startup_len);
00053 
00054   memcpy(&init_buf, init_data, init_len);
00055   memcpy(&init_buf[init_len], startup_data, startup_len);
00056   init_buf[init_len+startup_len] = ((char*)&crc)[1];
00057   init_buf[init_len+startup_len+1] = ((char*)&crc)[0];
00058   if(sendData(init_buf, 18) == 18)
00059     ret = true;
00060   return ret;
00061 }
00062 
00063 unsigned short int MS35::getChecksum(const char* data, size_t len)
00064 {
00065   unsigned int ret;
00066   boost::crc_16_type checksum_agent;
00067   checksum_agent.process_bytes(data, len);
00068   return checksum_agent.checksum();
00069 }
00070 
00071 int MS35::sendData(const char* data, size_t len)
00072 {
00073   int bytes_wrote = 0;
00074 
00075   bytes_wrote = _serialIO->sendData(data, len);
00076 
00077   if(bytes_wrote == -1)
00078     ROS_WARN("Can not write to serial port. Port closed!");
00079   else
00080     ROS_DEBUG("Wrote [%s] with %i bytes from %lu bytes", data, bytes_wrote, len);
00081 
00082   return bytes_wrote;
00083 }
00084 
00085 void MS35::setColorMulti(std::vector<color::rgba> &colors)
00086 {
00087 
00088 }
00089 
00090 void MS35::setColor(color::rgba color)
00091 {
00092   color::rgba color_tmp = color;
00093 
00094   //calculate rgb spektrum for spezific alpha value, because
00095   //led board is not supporting alpha values
00096   color.r *= color.a;
00097   color.g *= color.a;
00098   color.b *= color.a;
00099 
00100   //shift values into 8 bit rgb space
00101   color.r = fabs(color.r * 255);
00102   color.g = fabs(color.g * 255);
00103   color.b = fabs(color.b * 255);
00104 
00105   buffer[0] = 0x01; buffer[1] = 0x00;
00106   buffer[2] = (int)color.r; buffer[3]=(int)color.g; buffer[4]=(int)color.b;
00107   buffer[5] = 0x00; buffer[6]=0x00;
00108 
00109   unsigned short int crc = getChecksum(buffer, 7);
00110   buffer[7] = ((char*)&crc)[1];
00111   buffer[8] = ((char*)&crc)[0];
00112   
00113   if(sendData(buffer, PACKAGE_SIZE) == PACKAGE_SIZE)
00114     m_sigColorSet(color_tmp);
00115   else
00116     ROS_ERROR("Could not write to serial port");
00117 }


cob_light
Author(s): Benjamin Maidel
autogenerated on Sat Jun 8 2019 21:02:07