Tetrapod Project
SerialCommunication Class Reference

A class for serial communication. More...

#include <serial_communication.h>

Public Types

enum  ControlMode { position = 1 , velocity = 2 , torque = 3 }
 Control mode enumerator. More...
 

Public Member Functions

 SerialCommunication ()
 Default Constructor. More...
 
 SerialCommunication (const std::string &_port)
 Custom Constructor, defaults number of motors to 6. More...
 
 SerialCommunication (const std::string &_port, const int &_num_motors)
 Custom Constructor. More...
 
virtual ~SerialCommunication ()
 Destructor. More...
 
Eigen::Matrix< Eigen::VectorXd, 3, 1 > ReceiveMessage ()
 The ReceiveMessage function listens for serial messages of incoming joint state data from the motors and assign corresponding values to a joint state vector of vectors. More...
 
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. More...
 
bool IsNewDataAvailable ()
 The IsNewDataAvailable function checks if there is available data on the serial port. More...
 
void SetPort (const std::string &_port)
 Set the port for the Teensy communication after creating the constructor. More...
 
void SetNumberOfMotors (const int &_number_of_motors)
 Set the number of motors for the port after creating the constructor. More...
 
void InitLibSerial ()
 The InitLibSerial function initializes the serial port to the correct input and sets baud rate, character size, flow control, and stop bits. It also sets buffer sizes for transmit (TX) and receive (RX). More...
 

Private Member Functions

Eigen::Matrix< Eigen::VectorXd, 3, 1 > UnpackageBuffer (unsigned char *_data)
 The UnpackageBuffer function unpacks the read buffer data into joint state data vectors. More...
 
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 transmitted on the serial port. More...
 
void PackageBufferControlMode (const double *_data)
 The PackageBufferControlMode function fills the transmit buffer with control mode data. More...
 
void PackageBufferControlCommand (const double *_data)
 The PackageBufferControlCommand function fills the transmit buffer with control command data. More...
 

Private Attributes

int num_motors
 Number of motors. More...
 
std::string port
 Serial Port. More...
 
LibSerial::SerialPort serial_port
 LibSerial object instance. More...
 
int TX_BUFFER_SIZE
 Serial transmit buffer size. More...
 
int RX_BUFFER_SIZE
 Serial read buffer size. More...
 
std::vector< unsigned char > tx_buffer
 Serial transmit buffer. More...
 
std::vector< unsigned char > rx_buffer
 Serial read buffer. More...
 

Static Private Attributes

static constexpr double rx_timeout = 0.00001
 Read buffer timeout. More...
 

Detailed Description

A class for serial communication.

Definition at line 40 of file serial_communication.h.

Member Enumeration Documentation

◆ ControlMode

Control mode enumerator.

Enumerator
position 
velocity 
torque 

Definition at line 46 of file serial_communication.h.

Constructor & Destructor Documentation

◆ SerialCommunication() [1/3]

SerialCommunication::SerialCommunication ( )

Default Constructor.

Definition at line 3 of file serial_communication.cpp.

◆ SerialCommunication() [2/3]

SerialCommunication::SerialCommunication ( const std::string &  _port)

Custom Constructor, defaults number of motors to 6.

Parameters
[in]_portSerial port name for teensy communication.

Definition at line 5 of file serial_communication.cpp.

◆ SerialCommunication() [3/3]

SerialCommunication::SerialCommunication ( const std::string &  _port,
const int &  _num_motors 
)

Custom Constructor.

Parameters
[in]_portSerial port name for teensy communication.
[in]_num_motorsNumber of motors in use.

Definition at line 12 of file serial_communication.cpp.

◆ ~SerialCommunication()

SerialCommunication::~SerialCommunication ( )
virtual

Destructor.

Definition at line 19 of file serial_communication.cpp.

Member Function Documentation

◆ ReceiveMessage()

Eigen::Matrix< Eigen::VectorXd, 3, 1 > SerialCommunication::ReceiveMessage ( )

The ReceiveMessage function listens for serial messages of incoming joint state data from the motors and assign corresponding values to a joint state vector of vectors.

Returns
Returns joint positions, velocities, and torques in the form of a vector containing the 3 respective vectors.

Definition at line 24 of file serial_communication.cpp.

◆ SendMessage()

void SerialCommunication::SendMessage ( const ControlMode _control_mode,
const std::vector< double > &  _state 
)

The SendMessage function handles the sending of joint state command messages to the motors.

Parameters
[in]_control_modeDesired motor control mode.
[in]_stateDesired motor commands.

Definition at line 45 of file serial_communication.cpp.

◆ UnpackageBuffer()

Eigen::Matrix< Eigen::VectorXd, 3, 1 > SerialCommunication::UnpackageBuffer ( unsigned char *  _data)
private

The UnpackageBuffer function unpacks the read buffer data into joint state data vectors.

Parameters
[in]_dataRead buffer data.
Returns
Returns joint positions, velocities, and torques in the form of a vector containing the 3 respective vectors.

Definition at line 65 of file serial_communication.cpp.

◆ PackageBuffer()

void SerialCommunication::PackageBuffer ( const ControlMode _control_mode,
const double *  _data 
)
private

The PackageBuffer function fills the transmit buffer with control mode and control command data to be transmitted on the serial port.

Parameters
[in]_control_modeMotor control mode.
[in]_dataMotor command data.

Definition at line 88 of file serial_communication.cpp.

◆ PackageBufferControlMode()

void SerialCommunication::PackageBufferControlMode ( const double *  _data)
private

The PackageBufferControlMode function fills the transmit buffer with control mode data.

Parameters
[in]_dataControl mode data.

Definition at line 97 of file serial_communication.cpp.

◆ PackageBufferControlCommand()

void SerialCommunication::PackageBufferControlCommand ( const double *  _data)
private

The PackageBufferControlCommand function fills the transmit buffer with control command data.

Parameters
[in]_dataControl command data.

Definition at line 105 of file serial_communication.cpp.

◆ IsNewDataAvailable()

bool SerialCommunication::IsNewDataAvailable ( )

The IsNewDataAvailable function checks if there is available data on the serial port.

Returns
Returns true if data is available, false if not.

Definition at line 113 of file serial_communication.cpp.

◆ SetPort()

void SerialCommunication::SetPort ( const std::string &  _port)

Set the port for the Teensy communication after creating the constructor.

Parameters
[in]_portSerial port name for the Teensy communication

Definition at line 125 of file serial_communication.cpp.

◆ SetNumberOfMotors()

void SerialCommunication::SetNumberOfMotors ( const int &  _number_of_motors)

Set the number of motors for the port after creating the constructor.

Parameters
[in]_number_of_motorsNumber of motors to use

Definition at line 130 of file serial_communication.cpp.

◆ InitLibSerial()

void SerialCommunication::InitLibSerial ( )

The InitLibSerial function initializes the serial port to the correct input and sets baud rate, character size, flow control, and stop bits. It also sets buffer sizes for transmit (TX) and receive (RX).

Definition at line 135 of file serial_communication.cpp.

Member Data Documentation

◆ rx_timeout

constexpr double SerialCommunication::rx_timeout = 0.00001
staticconstexprprivate

Read buffer timeout.

Definition at line 43 of file serial_communication.h.

◆ num_motors

int SerialCommunication::num_motors
private

Number of motors.

Definition at line 121 of file serial_communication.h.

◆ port

std::string SerialCommunication::port
private

Serial Port.

Definition at line 124 of file serial_communication.h.

◆ serial_port

LibSerial::SerialPort SerialCommunication::serial_port
private

LibSerial object instance.

Definition at line 127 of file serial_communication.h.

◆ TX_BUFFER_SIZE

int SerialCommunication::TX_BUFFER_SIZE
private

Serial transmit buffer size.

Definition at line 130 of file serial_communication.h.

◆ RX_BUFFER_SIZE

int SerialCommunication::RX_BUFFER_SIZE
private

Serial read buffer size.

Definition at line 133 of file serial_communication.h.

◆ tx_buffer

std::vector<unsigned char> SerialCommunication::tx_buffer
private

Serial transmit buffer.

Definition at line 136 of file serial_communication.h.

◆ rx_buffer

std::vector<unsigned char> SerialCommunication::rx_buffer
private

Serial read buffer.

Definition at line 139 of file serial_communication.h.


The documentation for this class was generated from the following files: