Skip to content
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

fix: Use variable to catch fast NMT state transitions #436

Open
wants to merge 1 commit into
base: melodic-devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions canopen_master/include/canopen_master/canopen.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class Node : public Layer{
void switchState(const uint8_t &s);

State state_;
State wait_for_state_;
SDOClient sdo_;
PDOMapper pdo_;

Expand Down
9 changes: 6 additions & 3 deletions canopen_master/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ struct NMTcommand{
#pragma pack(pop) /* pop previous alignment from stack */

Node::Node(const can::CommInterfaceSharedPtr interface, const ObjectDictSharedPtr dict, uint8_t node_id, const SyncCounterSharedPtr sync)
: Layer("Node 301"), node_id_(node_id), interface_(interface), sync_(sync) , state_(Unknown), sdo_(interface, dict, node_id), pdo_(interface){
: Layer("Node 301"), node_id_(node_id), interface_(interface), sync_(sync) , state_(Unknown), sdo_(interface, dict, node_id), pdo_(interface),
wait_for_state_(Unknown){
try{
getStorage()->entry(heartbeat_, 0x1017);
}
Expand Down Expand Up @@ -108,6 +109,7 @@ void Node::switchState(const uint8_t &s){
}
if(changed){
state_ = (State) s;
if(s == wait_for_state_) wait_for_state_ = Unknown;
state_dispatcher_.dispatch(state_);
cond.notify_one();
}
Expand All @@ -122,14 +124,15 @@ void Node::handleNMT(const can::Frame & msg){
template<typename T> int Node::wait_for(const State &s, const T &timeout){
boost::mutex::scoped_lock cond_lock(cond_mutex);
time_point abs_time = get_abs_time(timeout);
wait_for_state_ = s;

while(s != state_) {
while(wait_for_state_ != Unknown) {
if(cond.wait_until(cond_lock,abs_time) == boost::cv_status::timeout)
{
break;
}
}
if( s!= state_){
if(wait_for_state_ != Unknown){
if(getHeartbeatInterval() == 0){
switchState(s);
return -1;
Expand Down