Skip to content

Commit

Permalink
mavlink_interface: rename serial link related function to include 'se…
Browse files Browse the repository at this point in the history
…rial' (#670)
  • Loading branch information
BazookaJoe1900 authored Dec 13, 2020
1 parent 19981d6 commit bccb487
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
19 changes: 12 additions & 7 deletions include/mavlink_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <vector>
#include <regex>
#include <thread>
Expand Down Expand Up @@ -119,12 +122,13 @@ class MavlinkInterface {
void acceptConnections();

// Serial interface
void do_read();
void parse_buffer(const boost::system::error_code& err, std::size_t bytes_t);
inline bool is_open(){
void open_serial();
void do_serial_read();
void parse_serial_buffer(const boost::system::error_code& err, std::size_t bytes_t);
inline bool is_serial_open(){
return serial_dev_.is_open();
}
void do_write(bool check_tx_state);
void do_serial_write(bool check_tx_state);

static const unsigned n_out_max = 16;

Expand Down Expand Up @@ -164,8 +168,6 @@ class MavlinkInterface {
int mavlink_udp_port_{kDefaultMavlinkUdpPort}; // MAVLink refers to the PX4 simulator interface here
int mavlink_tcp_port_{kDefaultMavlinkTcpPort}; // MAVLink refers to the PX4 simulator interface here

boost::asio::io_service io_service_{};
boost::asio::serial_port serial_dev_;

int simulator_socket_fd_{0};
int simulator_tcp_client_fd_{0};
Expand All @@ -176,9 +178,12 @@ class MavlinkInterface {
bool enable_lockstep_{false};

// Serial interface
boost::asio::io_service io_service_{};
boost::asio::serial_port serial_dev_;
bool serial_enabled_{false};

mavlink_status_t m_status_{};
mavlink_message_t m_buffer_{};
bool serial_enabled_{false};
std::thread io_thread_;
std::string device_{kDefaultDevice};

Expand Down
24 changes: 12 additions & 12 deletions src/mavlink_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ void MavlinkInterface::Load()

if (serial_enabled_) {
// Set up serial interface
io_service_.post(std::bind(&MavlinkInterface::do_read, this));
io_service_.post(std::bind(&MavlinkInterface::do_serial_read, this));

// run io_service for async io
io_thread_ = std::thread([this] () {
io_service_.run();
});
open();
open_serial();

} else {
memset((char *)&remote_simulator_addr_, 0, sizeof(remote_simulator_addr_));
Expand Down Expand Up @@ -392,7 +392,7 @@ void MavlinkInterface::send_mavlink_message(const mavlink_message_t *message)

if (serial_enabled_) {

if (!is_open()) {
if (!is_serial_open()) {
std::cerr << "Serial port closed! \n";
return;
}
Expand All @@ -405,7 +405,7 @@ void MavlinkInterface::send_mavlink_message(const mavlink_message_t *message)
}
tx_q_.emplace_back(message);
}
io_service_.post(std::bind(&MavlinkInterface::do_write, this, true));
io_service_.post(std::bind(&MavlinkInterface::do_serial_write, this, true));

} else {
uint8_t buffer[MAVLINK_MAX_PACKET_LEN];
Expand Down Expand Up @@ -449,7 +449,7 @@ void MavlinkInterface::send_mavlink_message(const mavlink_message_t *message)
}
}

void MavlinkInterface::open() {
void MavlinkInterface::open_serial() {
try{
serial_dev_.open(device_);
serial_dev_.set_option(boost::asio::serial_port_base::baud_rate(baudrate_));
Expand All @@ -471,7 +471,7 @@ void MavlinkInterface::close()
::close(sdk_socket_fd_);

std::lock_guard<std::recursive_mutex> lock(mutex_);
if (!is_open())
if (!is_serial_open())
return;

io_service_.stop();
Expand All @@ -491,7 +491,7 @@ void MavlinkInterface::close()
}
}

void MavlinkInterface::do_write(bool check_tx_state){
void MavlinkInterface::do_serial_write(bool check_tx_state){
if (check_tx_state && tx_in_progress_)
return;

Expand Down Expand Up @@ -524,24 +524,24 @@ void MavlinkInterface::do_write(bool check_tx_state){
}

if (!tx_q_.empty()) {
do_write(false);
do_serial_write(false);
}
else {
tx_in_progress_ = false;
}
});
}

void MavlinkInterface::do_read(void)
void MavlinkInterface::do_serial_read(void)
{
serial_dev_.async_read_some(boost::asio::buffer(rx_buf_), boost::bind(
&MavlinkInterface::parse_buffer, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred
&MavlinkInterface::parse_serial_buffer, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred
)
);
}

// Based on MAVConnInterface::parse_buffer in MAVROS
void MavlinkInterface::parse_buffer(const boost::system::error_code& err, std::size_t bytes_t){
void MavlinkInterface::parse_serial_buffer(const boost::system::error_code& err, std::size_t bytes_t){
mavlink_status_t status;
mavlink_message_t message;
uint8_t *buf = this->rx_buf_.data();
Expand Down Expand Up @@ -571,7 +571,7 @@ void MavlinkInterface::parse_buffer(const boost::system::error_code& err, std::s
handle_message(&message);
}
}
do_read();
do_serial_read();
}

void MavlinkInterface::onSigInt() {
Expand Down

0 comments on commit bccb487

Please sign in to comment.