Tetrapod Project
serial_communication.cpp
Go to the documentation of this file.
2 
4 
5 SerialCommunication::SerialCommunication(const std::string &_port) :
6  port { _port },
7  num_motors { 6 }
8 {
9  this->InitLibSerial();
10 }
11 
12 SerialCommunication::SerialCommunication(const std::string &_port, const int &_num_motors) :
13  port { _port },
14  num_motors { _num_motors }
15 {
16  this->InitLibSerial();
17 }
18 
20 {
21  serial_port.Close();
22 }
23 
24 Eigen::Matrix<Eigen::VectorXd, 3, 1> SerialCommunication::ReceiveMessage()
25 {
26  if (this->num_motors == 0)
27  {
28  ROS_ERROR("[SerialCommunication::ReceiveMessage] Trying to receive message with 0 motors!");
29 
30  std::abort();
31  }
32  else
33  {
34  while (!this->IsNewDataAvailable())
35  {
36  sleep(this->rx_timeout);
37  }
38 
39  this->serial_port.Read(this->rx_buffer, this->RX_BUFFER_SIZE, 10000);
40 
41  return this->UnpackageBuffer(this->rx_buffer.data());
42  }
43 }
44 
45 void SerialCommunication::SendMessage(const ControlMode &_control_mode, const std::vector<double> &_state)
46 {
47  if (this->num_motors == 0)
48  {
49  ROS_ERROR("[SerialCommunication::SendMessage] Trying to send message with 0 motors!");
50  }
51  else
52  {
53  if(_state.size() != this->num_motors)
54  {
55  ROS_ERROR("[SerialCommunication::SendMessage] State size does not match number of motors!");
56  std::abort();
57  }
58 
59  this->PackageBuffer(_control_mode, _state.data());
60 
61  this->serial_port.Write(tx_buffer);
62  }
63 }
64 
65 Eigen::Matrix<Eigen::VectorXd, 3, 1> SerialCommunication::UnpackageBuffer(unsigned char *_data)
66 {
67  Eigen::Matrix<Eigen::VectorXd, 3, 1> state;
68 
69  // Set state size
70  state(0).resize(this->num_motors, 1);
71  state(1).resize(this->num_motors, 1);
72  state(2).resize(this->num_motors, 1);
73 
74  // Fill data
75  for (int i = 0; i < 3; i++)
76  {
77  for (int j = 0; j < this->num_motors; j++)
78  {
79  double double_data = *((double*)_data + i*this->num_motors + j);
80 
81  state(i)(j) = double_data;
82  }
83  }
84 
85  return state;
86 }
87 
88 void SerialCommunication::PackageBuffer(const ControlMode &_control_mode, const double *_data)
89 {
90  double control_mode_data[1] = { ((double)_control_mode) };
91 
92  this->PackageBufferControlMode(control_mode_data);
93 
94  this->PackageBufferControlCommand(_data);
95 }
96 
98 {
99  for (int i = 0; i < 8; i++)
100  {
101  tx_buffer[i] = ((char *)_data)[i];
102  }
103 }
104 
106 {
107  for (int i = 8; i < this->TX_BUFFER_SIZE; i++)
108  {
109  tx_buffer[i] = ((char *)_data)[i - 8];
110  }
111 }
112 
114 {
115  if(serial_port.IsDataAvailable() == true)
116  {
117  return true;
118  }
119  else
120  {
121  return false;
122  }
123 }
124 
125 void SerialCommunication::SetPort(const std::string &_port)
126 {
127  this->port = _port;
128 }
129 
130 void SerialCommunication::SetNumberOfMotors(const int &_number_of_motors)
131 {
132  this->num_motors = _number_of_motors;
133 }
134 
136 {
137  this->serial_port.Open(this->port);
138 
139  this->serial_port.SetBaudRate(LibSerial::BaudRate::BAUD_9600);
140 
141  this->serial_port.SetCharacterSize(LibSerial::CharacterSize::CHAR_SIZE_8);
142 
143  this->serial_port.SetFlowControl(LibSerial::FlowControl::FLOW_CONTROL_NONE);
144 
145  this->serial_port.SetStopBits(LibSerial::StopBits::STOP_BITS_1);
146 
147  this->TX_BUFFER_SIZE = (1 + num_motors) * 8;
148  this->RX_BUFFER_SIZE = num_motors * 8 * 3;
149 
150  this->tx_buffer.resize(TX_BUFFER_SIZE);
151  this->rx_buffer.resize(RX_BUFFER_SIZE);
152 }
void SetNumberOfMotors(const int &_number_of_motors)
Set the number of motors for the port after creating the constructor.
std::vector< unsigned char > rx_buffer
Serial read buffer.
int RX_BUFFER_SIZE
Serial read buffer size.
void PackageBufferControlMode(const double *_data)
The PackageBufferControlMode function fills the transmit buffer with control mode data.
void PackageBufferControlCommand(const double *_data)
The PackageBufferControlCommand function fills the transmit buffer with control command data.
SerialCommunication()
Default Constructor.
void InitLibSerial()
The InitLibSerial function initializes the serial port to the correct input and sets baud rate,...
ControlMode
Control mode enumerator.
virtual ~SerialCommunication()
Destructor.
std::vector< unsigned char > tx_buffer
Serial transmit buffer.
void SendMessage(const ControlMode &_control_mode, const std::vector< double > &_state)
The SendMessage function handles the sending of joint state command messages to the motors.
bool IsNewDataAvailable()
The IsNewDataAvailable function checks if there is available data on the serial port.
std::string port
Serial Port.
LibSerial::SerialPort serial_port
LibSerial object instance.
int num_motors
Number of motors.
void PackageBuffer(const ControlMode &_control_mode, const double *_data)
The PackageBuffer function fills the transmit buffer with control mode and control command data to be...
int TX_BUFFER_SIZE
Serial transmit buffer size.
void SetPort(const std::string &_port)
Set the port for the Teensy communication after creating the constructor.
Eigen::Matrix< Eigen::VectorXd, 3, 1 > UnpackageBuffer(unsigned char *_data)
The UnpackageBuffer function unpacks the read buffer data into joint state data vectors.
static constexpr double rx_timeout
Read buffer timeout.
Eigen::Matrix< Eigen::VectorXd, 3, 1 > ReceiveMessage()
The ReceiveMessage function listens for serial messages of incoming joint state data from the motors ...