Tetrapod Project
serial_communication.h
Go to the documentation of this file.
1 /*******************************************************************/
2 /* AUTHOR: Paal Arthur S. Thorseth & Adrian B. Ghansah */
3 /* ORGN: Dept of Eng Cybernetics, NTNU Trondheim */
4 /* FILE: serial_communication.h */
5 /* DATE: Jun 25, 2021 */
6 /* */
7 /* Copyright (C) 2021 Paal Arthur S. Thorseth, */
8 /* Adrian B. Ghansah */
9 /* */
10 /* This program is free software: you can redistribute it */
11 /* and/or modify it under the terms of the GNU General */
12 /* Public License as published by the Free Software Foundation, */
13 /* either version 3 of the License, or (at your option) any */
14 /* later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty */
18 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
19 /* See the GNU General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU General Public */
22 /* License along with this program. If not, see */
23 /* <https://www.gnu.org/licenses/>. */
24 /* */
25 /*******************************************************************/
26 
27 #pragma once
28 
29 // ROS
30 #include "ros/ros.h"
31 
32 // Eigen
33 #include <Eigen/Core>
34 
35 // LibSerial libraries
36 #include <libserial/SerialPort.h>
37 #include <libserial/SerialStream.h>
38 
41 {
43  private: static constexpr double rx_timeout = 0.00001;
44 
46  public: enum ControlMode { position = 1, velocity = 2, torque = 3};
47 
49  public: SerialCommunication();
50 
53  public: SerialCommunication(const std::string &_port);
54 
58  public: SerialCommunication(const std::string &_port, const int &_num_motors);
59 
61  public: virtual ~SerialCommunication();
62 
69  public: Eigen::Matrix<Eigen::VectorXd, 3, 1> ReceiveMessage();
70 
75  public: void SendMessage(const ControlMode &_control_mode, const std::vector<double> &_state);
76 
82  private: Eigen::Matrix<Eigen::VectorXd, 3, 1> UnpackageBuffer(unsigned char *_data);
83 
89  private: void PackageBuffer(const ControlMode &_control_mode, const double *_data);
90 
94  private: void PackageBufferControlMode(const double *_data);
95 
99  private: void PackageBufferControlCommand(const double *_data);
100 
104  public: bool IsNewDataAvailable();
105 
108  public: void SetPort(const std::string &_port);
109 
112  public: void SetNumberOfMotors(const int &_number_of_motors);
113 
118  public: void InitLibSerial();
119 
121  private: int num_motors;
122 
124  private: std::string port;
125 
127  private: LibSerial::SerialPort serial_port;
128 
130  private: int TX_BUFFER_SIZE;
131 
133  private: int RX_BUFFER_SIZE;
134 
136  private: std::vector<unsigned char> tx_buffer;
137 
139  private: std::vector<unsigned char> rx_buffer;
140 
141 };
A class for serial communication.
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 ...