-
Notifications
You must be signed in to change notification settings - Fork 275
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
Adding simple autorecover - hopefully all of my build errors are fixed #249
base: kinetic-devel
Are you sure you want to change the base?
Changes from all commits
a8badf8
4a3955b
abf64df
7829852
b0320b3
add37de
2cc23f1
43fde6d
3302da0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# CanState.msg | ||
# | ||
# Message denoting the state of the CAN Bus | ||
# | ||
# @author Joe Adkisson | ||
# @version 0.1 | ||
|
||
uint8 driver_state | ||
|
||
# enumeration values for status: | ||
uint8 CLOSED=0 | ||
uint8 OPEN=1 | ||
uint8 READY=2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
cmake_minimum_required(VERSION 2.8.3) | ||
add_compile_options(-std=c++11) | ||
project(socketcan_bridge) | ||
|
||
find_package(catkin REQUIRED | ||
|
@@ -14,6 +15,7 @@ catkin_package( | |
LIBRARIES | ||
socketcan_to_topic | ||
topic_to_socketcan | ||
socketcan_recover_ctrl | ||
CATKIN_DEPENDS | ||
can_msgs | ||
roscpp | ||
|
@@ -48,12 +50,24 @@ add_dependencies(topic_to_socketcan | |
${catkin_EXPORTED_TARGETS} | ||
) | ||
|
||
# socketcan_recover_ctrl | ||
add_library(socketcan_recover_ctrl | ||
src/socketcan_recover_ctrl.cpp | ||
) | ||
target_link_libraries(socketcan_recover_ctrl | ||
${catkin_LIBRARIES} | ||
) | ||
add_dependencies(socketcan_recover_ctrl | ||
${catkin_EXPORTED_TARGETS} | ||
) | ||
|
||
# socketcan_to_topic_node | ||
add_executable(socketcan_to_topic_node | ||
src/socketcan_to_topic_node.cpp | ||
) | ||
target_link_libraries(socketcan_to_topic_node | ||
socketcan_to_topic | ||
socketcan_recover_ctrl | ||
${catkin_LIBRARIES} | ||
) | ||
|
||
|
@@ -64,6 +78,7 @@ add_executable(topic_to_socketcan_node | |
) | ||
target_link_libraries(topic_to_socketcan_node | ||
topic_to_socketcan | ||
socketcan_recover_ctrl | ||
${catkin_LIBRARIES} | ||
) | ||
|
||
|
@@ -74,16 +89,20 @@ add_executable(socketcan_bridge_node | |
target_link_libraries(socketcan_bridge_node | ||
topic_to_socketcan | ||
socketcan_to_topic | ||
socketcan_recover_ctrl | ||
${catkin_LIBRARIES} | ||
) | ||
|
||
|
||
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. whitespace 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. Is there a |
||
install( | ||
TARGETS | ||
socketcan_bridge_node | ||
socketcan_recover_ctrl | ||
socketcan_to_topic | ||
socketcan_to_topic_node | ||
topic_to_socketcan | ||
topic_to_socketcan_node | ||
|
||
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. whitespace |
||
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} | ||
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} | ||
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} | ||
|
@@ -109,6 +128,7 @@ if(CATKIN_ENABLE_TESTING) | |
target_link_libraries(test_conversion | ||
topic_to_socketcan | ||
socketcan_to_topic | ||
socketcan_recover_ctrl | ||
${catkin_LIBRARIES} | ||
) | ||
|
||
|
@@ -131,4 +151,6 @@ if(CATKIN_ENABLE_TESTING) | |
${catkin_LIBRARIES} | ||
) | ||
|
||
|
||
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. whitespace, please keep the new line ending |
||
|
||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef SOCKETCAN_RECOVER_CTRL_H | ||
#define SOCKETCAN_RECOVER_CTRL_H | ||
|
||
#include <can_msgs/CanState.h> | ||
#include <socketcan_interface/socketcan.h> | ||
#include <ros/ros.h> | ||
|
||
namespace socketcan_bridge | ||
{ | ||
|
||
class SocketCANRecoverCtrl | ||
{ | ||
public: | ||
|
||
/** | ||
* @brief Constructor for the recovery control class | ||
* | ||
* Initializes the publisher, and sets up a timer to periodically check the bus state | ||
*/ | ||
SocketCANRecoverCtrl(ros::NodeHandle* nh, ros::NodeHandle* nh_param, boost::shared_ptr<can::DriverInterface> driver); | ||
|
||
~SocketCANRecoverCtrl() | ||
{ | ||
timer_.stop(); | ||
} | ||
|
||
private: | ||
|
||
/** | ||
* @brief Publishes the status of the bus | ||
*/ | ||
void publishStatus(const can::State & state); | ||
|
||
/** | ||
* @brief Recover the bus from an error state | ||
* | ||
* Calls the driver's recover() function | ||
*/ | ||
void recover(); | ||
|
||
/** | ||
* @brief Checks the state of the bus, if !statie.isReady() then the | ||
* recover timer is started to fire in 5secs, otherwise we stop the timer | ||
*/ | ||
void stateCallback(const can::State & s); | ||
|
||
|
||
ros::Publisher state_pub_; | ||
boost::shared_ptr<can::DriverInterface> driver_; | ||
ros::WallTimer timer_; | ||
|
||
can::StateInterface::StateListener::Ptr state_listener_; | ||
|
||
}; | ||
|
||
}; // namespace socketcan_bridge | ||
|
||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <socketcan_bridge/socketcan_recover_ctrl.h> | ||
|
||
namespace socketcan_bridge | ||
{ | ||
|
||
/** | ||
* @brief Constructor for the recovery control class | ||
* | ||
* Initializes the publisher, and sets up a timer to periodically check the bus state | ||
*/ | ||
SocketCANRecoverCtrl::SocketCANRecoverCtrl(ros::NodeHandle* nh, ros::NodeHandle* nh_param, | ||
boost::shared_ptr<can::DriverInterface> driver) : driver_(driver) | ||
{ | ||
state_pub_ = nh->advertise<can_msgs::CanState>("can_state", 1, true); | ||
|
||
timer_ = nh->createWallTimer(ros::WallDuration(5), [this](const ros::WallTimerEvent& event) {recover();}, true, false); | ||
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. please migrate the lambda into a dedicated function 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. It might be good to make the timeout configurable. |
||
|
||
state_listener_ = driver_->createStateListener( | ||
can::StateInterface::StateDelegate(this, &SocketCANRecoverCtrl::stateCallback)); | ||
|
||
} | ||
|
||
/** | ||
* @brief Checks the state of the bus, if !statie.isReady() then the | ||
* recover timer is started to fire in 5secs, otherwise we stop the timer | ||
*/ | ||
void SocketCANRecoverCtrl::stateCallback(const can::State & state) { | ||
publishStatus(state); | ||
if(!state.isReady()) | ||
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. I guess the driver should get recovered only if it is not closed. |
||
{ | ||
timer_.start(); | ||
} | ||
else | ||
{ | ||
timer_.stop(); | ||
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. Why do you need to stop a one-shot timer? 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. If status has been updated before the callback fires, we we're giving a little time for this system to "maybe" fix itself 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. void ros::WallTimer::stop () ie, if status became ready we don't want the callback to fire 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. There is no way that the state can change from bad to good without intervention (?). 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. I wasn't sure if that statement would hold true in future versions of socketcan_interface |
||
} | ||
} | ||
|
||
/** | ||
* @brief Recover the bus from an error state | ||
* | ||
* Calls the driver's recover() function | ||
*/ | ||
void SocketCANRecoverCtrl::recover() | ||
{ | ||
timer_.stop(); | ||
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. no need to stop here 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 stop is explicitly required, you will have to forgive me I don't remember why, either it made the timer hang holding a reference preventing from being able to simple Ctrl-C the app, or it was needed to call start below, ill look into my reasoning here 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. Ah yes, it might block at ctrl+c. |
||
if(driver_->recover()) { | ||
ROS_INFO("CAN driver timed out, successfully recovered"); | ||
} | ||
else | ||
{ | ||
ROS_WARN("CAN driver timed out, recovery failed"); | ||
timer_.start(); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Publishes the status of the bus | ||
*/ | ||
void SocketCANRecoverCtrl::publishStatus(const can::State & state) { | ||
can_msgs::CanState state_msg; | ||
switch(state.driver_state) { | ||
case can::State::open: | ||
state_msg.driver_state = can_msgs::CanState::OPEN; | ||
break; | ||
case can::State::closed: | ||
state_msg.driver_state = can_msgs::CanState::CLOSED; | ||
break; | ||
case can::State::ready: | ||
state_msg.driver_state = can_msgs::CanState::READY; | ||
break; | ||
default: | ||
state_msg.driver_state = can_msgs::CanState::CLOSED; | ||
break; | ||
} | ||
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. I would prefer to just copy it: 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. can::State and can_msgs::CanState are incompatible, i think |
||
|
||
state_pub_.publish(state_msg); | ||
} | ||
|
||
|
||
}; // namespace socketcan_bridge |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,7 @@ class SocketCANInterface : public AsioDriver<boost::asio::posix::stream_descript | |
ret = true; | ||
} | ||
if( internal_error & CAN_ERR_RESTARTED){ | ||
str += "ontroller restarted;"; | ||
str += "controller restarted;"; | ||
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. 👍 |
||
ret = true; | ||
} | ||
return ret; | ||
|
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.
C++11 shall not be used!
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.
Sigh. It's 2018 i.e. 2 more standards 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.
c++14 is now the default for melodc 🎉