ms35.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <ms35.h>
19 #include <ros/ros.h>
20 #include <boost/crc.hpp>
21 #include <boost/cstdint.hpp>
22 #include <boost/integer.hpp>
23 
24 MS35::MS35(SerialIO* serialIO)
25 {
26  _serialIO = serialIO;
27 }
28 
30 {
31 }
32 
33 bool MS35::init()
34 {
35  bool ret = false;
36  const char init_data[] = { 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd };
37  int init_len = sizeof(init_data) / sizeof(init_data[0]);
38  const char startup_data[] = { 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
39  int startup_len = sizeof(startup_data) / sizeof(startup_data[0]);
40  char init_buf[18];
41 
42  //write data until controller is ready to receive valid package
43  char tmp = 0xfd;
44  std::string recv;
45  int bytes_recv;
46  do{
47  _serialIO->sendData(&tmp, 1);
48  bytes_recv = _serialIO->readData(recv, 1);
49 
50  }while(bytes_recv <= 0);
51 
52  unsigned short int crc = getChecksum(startup_data, startup_len);
53 
54  memcpy(&init_buf, init_data, init_len);
55  memcpy(&init_buf[init_len], startup_data, startup_len);
56  init_buf[init_len+startup_len] = ((char*)&crc)[1];
57  init_buf[init_len+startup_len+1] = ((char*)&crc)[0];
58  if(sendData(init_buf, 18) == 18)
59  ret = true;
60  return ret;
61 }
62 
63 unsigned short int MS35::getChecksum(const char* data, size_t len)
64 {
65  unsigned int ret;
66  boost::crc_16_type checksum_agent;
67  checksum_agent.process_bytes(data, len);
68  return checksum_agent.checksum();
69 }
70 
71 int MS35::sendData(const char* data, size_t len)
72 {
73  int bytes_wrote = 0;
74 
75  bytes_wrote = _serialIO->sendData(data, len);
76 
77  if(bytes_wrote == -1)
78  ROS_WARN("Can not write to serial port. Port closed!");
79  else
80  ROS_DEBUG("Wrote [%s] with %i bytes from %lu bytes", data, bytes_wrote, len);
81 
82  return bytes_wrote;
83 }
84 
85 void MS35::setColorMulti(std::vector<color::rgba> &colors)
86 {
87 
88 }
89 
91 {
92  color::rgba color_tmp = color;
93 
94  //calculate rgb spektrum for spezific alpha value, because
95  //led board is not supporting alpha values
96  color.r *= color.a;
97  color.g *= color.a;
98  color.b *= color.a;
99 
100  //shift values into 8 bit rgb space
101  color.r = fabs(color.r * 255);
102  color.g = fabs(color.g * 255);
103  color.b = fabs(color.b * 255);
104 
105  buffer[0] = 0x01; buffer[1] = 0x00;
106  buffer[2] = (int)color.r; buffer[3]=(int)color.g; buffer[4]=(int)color.b;
107  buffer[5] = 0x00; buffer[6]=0x00;
108 
109  unsigned short int crc = getChecksum(buffer, 7);
110  buffer[7] = ((char*)&crc)[1];
111  buffer[8] = ((char*)&crc)[0];
112 
114  m_sigColorSet(color_tmp);
115  else
116  ROS_ERROR("Could not write to serial port");
117 }
SerialIO * _serialIO
Definition: ms35.h:38
void setColor(color::rgba color)
Definition: ms35.cpp:90
char buffer[PACKAGE_SIZE]
Definition: ms35.h:41
#define ROS_WARN(...)
unsigned short int getChecksum(const char *data, size_t len)
Definition: ms35.cpp:63
static const int PACKAGE_SIZE
Definition: ms35.h:40
MS35(SerialIO *serialIO)
Definition: ms35.cpp:24
void setColorMulti(std::vector< color::rgba > &colors)
Definition: ms35.cpp:85
bool init()
Definition: ms35.cpp:33
int sendData(std::string value)
Definition: serialIO.cpp:63
boost::signals2::signal< void(color::rgba color)> m_sigColorSet
Definition: iColorO.h:45
virtual ~MS35()
Definition: ms35.cpp:29
int sendData(const char *data, size_t len)
Definition: ms35.cpp:71
int readData(std::string &value, size_t nBytes)
Definition: serialIO.cpp:83
#define ROS_ERROR(...)
#define ROS_DEBUG(...)


cob_light
Author(s): Benjamin Maidel
autogenerated on Wed Apr 7 2021 02:11:39