-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Smoother serial #124
Draft
BillThePlatypus
wants to merge
6
commits into
main
Choose a base branch
from
smoother_serial
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Smoother serial #124
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0e40259
Allow option to wait for serial connection
BillThePlatypus 9f56548
Show error message on disconnect instead of crashing
BillThePlatypus 7ee04f4
Restructure code to support better disconnect handling
BillThePlatypus af61c68
Handle ctrl-c while looking for connection better
BillThePlatypus e8b1541
Option to reconnect on lost connection. Not working fully
BillThePlatypus 63d3e37
Clean up reconnection after lost connection
BillThePlatypus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,8 +65,9 @@ rosflightIO::rosflightIO() | |
calibrate_rc_srv_ = nh_.advertiseService("calibrate_rc_trim", &rosflightIO::calibrateRCTrimSrvCallback, this); | ||
reboot_srv_ = nh_.advertiseService("reboot", &rosflightIO::rebootSrvCallback, this); | ||
reboot_bootloader_srv_ = nh_.advertiseService("reboot_to_bootloader", &rosflightIO::rebootToBootloaderSrvCallback, this); | ||
|
||
ros::NodeHandle nh_private("~"); | ||
do_reconnect_ = nh_private.param<bool>("do_reconnect", false); | ||
|
||
if (nh_private.param<bool>("udp", false)) | ||
{ | ||
|
@@ -84,25 +85,67 @@ rosflightIO::rosflightIO() | |
std::string port = nh_private.param<std::string>("port", "/dev/ttyACM0"); | ||
int baud_rate = nh_private.param<int>("baud_rate", 921600); | ||
|
||
wait_for_serial_ = nh_private.param<bool>("wait_for_serial", false); | ||
serial_retry_delay_s_ = nh_private.param<float>("serial_check_period", 1.0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems long. (I assume that this is designed for hardfault recovery.) How small can we make this? |
||
ROS_INFO("Connecting to serial port \"%s\", at %d baud", port.c_str(), baud_rate); | ||
|
||
mavlink_comm_ = new mavrosflight::MavlinkSerial(port, baud_rate); | ||
} | ||
|
||
try | ||
{ | ||
mavlink_comm_->open(); //! \todo move this into the MavROSflight constructor | ||
mavrosflight_ = new mavrosflight::MavROSflight(*mavlink_comm_); | ||
} | ||
catch (mavrosflight::SerialException e) | ||
if(!attempt_connect(wait_for_serial_, serial_retry_delay_s_)) | ||
{ | ||
ROS_FATAL("%s", e.what()); | ||
ros::shutdown(); | ||
return; | ||
} | ||
|
||
mavrosflight_->comm.register_mavlink_listener(this); | ||
mavrosflight_->param.register_param_listener(this); | ||
|
||
// Set up a few other random things | ||
frame_id_ = nh_private.param<std::string>("frame_id", "world"); | ||
|
||
prev_status_.armed = false; | ||
prev_status_.failsafe = false; | ||
prev_status_.rc_override = false; | ||
prev_status_.offboard = false; | ||
prev_status_.control_mode = OFFBOARD_CONTROL_MODE_ENUM_END; | ||
prev_status_.error_code = ROSFLIGHT_ERROR_NONE; | ||
|
||
finish_setup(); | ||
} | ||
|
||
bool rosflightIO::attempt_connect(bool do_retry, float retry_delay) | ||
{ | ||
bool connected = false; | ||
while(!connected && ros::ok()) | ||
{ | ||
try | ||
{ | ||
mavlink_comm_->open(); //! \todo move this into the MavROSflight constructor | ||
mavrosflight_ = new mavrosflight::MavROSflight(*mavlink_comm_); | ||
connected = true; | ||
} | ||
catch (mavrosflight::SerialException e) | ||
{ | ||
connected = false; | ||
if(do_retry) | ||
{ | ||
ROS_WARN("%s", e.what()); | ||
ROS_WARN("Retrying connection in %f s", retry_delay); | ||
ros::Duration(retry_delay).sleep(); | ||
} | ||
else | ||
{ | ||
ROS_FATAL("%s", e.what()); | ||
return false; | ||
} | ||
} | ||
} | ||
return ros::ok(); | ||
} | ||
|
||
void rosflightIO::finish_setup() | ||
{ | ||
// request the param list | ||
mavrosflight_->param.request_params(); | ||
param_timer_ = nh_.createTimer(ros::Duration(PARAMETER_PERIOD), &rosflightIO::paramTimerCallback, this); | ||
|
@@ -116,18 +159,35 @@ rosflightIO::rosflightIO() | |
unsaved_msg.data = false; | ||
unsaved_params_pub_.publish(unsaved_msg); | ||
|
||
// Set up a few other random things | ||
frame_id_ = nh_private.param<std::string>("frame_id", "world"); | ||
|
||
prev_status_.armed = false; | ||
prev_status_.failsafe = false; | ||
prev_status_.rc_override = false; | ||
prev_status_.offboard = false; | ||
prev_status_.control_mode = OFFBOARD_CONTROL_MODE_ENUM_END; | ||
prev_status_.error_code = ROSFLIGHT_ERROR_NONE; | ||
|
||
//Start the heartbeat | ||
heartbeat_timer_ = nh_.createTimer(ros::Duration(HEARTBEAT_PERIOD), &rosflightIO::heartbeatTimerCallback, this); | ||
|
||
} | ||
|
||
void rosflightIO::handle_disconnect() | ||
{ | ||
if(do_reconnect_) | ||
{ | ||
ROS_ERROR("Connection to firmware lost. Attempting to reconnect"); | ||
cleanup_connection(); | ||
wait_for_serial_ = true; | ||
attempt_connect(do_reconnect_, serial_retry_delay_s_); | ||
wait_for_serial_ = false; | ||
ROS_INFO("Connection regained"); | ||
finish_setup(); | ||
} | ||
else | ||
{ | ||
ROS_FATAL("Connection to firmware lost. Shutting down."); | ||
ros::shutdown(); | ||
} | ||
} | ||
|
||
void rosflightIO::cleanup_connection() | ||
{ | ||
param_timer_.stop(); | ||
version_timer_.stop(); | ||
heartbeat_timer_.stop(); | ||
} | ||
|
||
rosflightIO::~rosflightIO() | ||
|
@@ -209,6 +269,11 @@ void rosflightIO::handle_mavlink_message(const mavlink_message_t &msg) | |
} | ||
} | ||
|
||
void rosflightIO::on_mavlink_disconnect() | ||
{ | ||
handle_disconnect(); | ||
} | ||
|
||
void rosflightIO::on_new_param_received(std::string name, double value) | ||
{ | ||
ROS_DEBUG("Got parameter %s with value %g", name.c_str(), value); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you just remove this line?