-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'bus-load' into road-to-canhalla
- Loading branch information
Showing
8 changed files
with
215 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- | ||
|
||
#ifndef __CAN_MESSAGE_NOTIFIER_HPP__ | ||
#define __CAN_MESSAGE_NOTIFIER_HPP__ | ||
|
||
#include "CanMessage.hpp" | ||
|
||
namespace roboticslab | ||
{ | ||
|
||
/** | ||
* @ingroup CanBusSharerLib | ||
* @brief Implementation-agnostic consumer for TX CAN transfers. | ||
* | ||
* Implementors can use this class to forward implementation-specific CAN message | ||
* structures from the internal TX buffer (if any) to the point of consumption | ||
* and the final CAN read routines. | ||
* | ||
* Cf. @ref CanSenderDelegate. | ||
*/ | ||
class CanMessageNotifier | ||
{ | ||
public: | ||
//! Virtual destructor. | ||
virtual ~CanMessageNotifier() = default; | ||
|
||
//! Notify observers that a new CAN message has arrived. | ||
virtual bool notifyMessage(const can_message & msg) = 0; | ||
}; | ||
|
||
} // namespace roboticslab | ||
|
||
#endif // __CAN_MESSAGE_NOTIFIER_HPP__ |
39 changes: 39 additions & 0 deletions
39
libraries/YarpPlugins/CanBusControlboard/BusLoadMonitor.cpp
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- | ||
|
||
#include "BusLoadMonitor.hpp" | ||
|
||
#include <cmath> | ||
|
||
using namespace roboticslab; | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
namespace | ||
{ | ||
inline unsigned long computeLength(unsigned int len) | ||
{ | ||
// 44-bit base frame + 3-bit intermission field + stuff bits, see https://w.wiki/GDt | ||
return 8 * len + 44 + std::floor((34 + 8 * len - 1) / 4.0) + 3; | ||
} | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
bool BusLoadMonitor::notifyMessage(const can_message & msg) | ||
{ | ||
bits += computeLength(msg.len); | ||
return true; | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
void BusLoadMonitor::run() | ||
{ | ||
double rate = bits.exchange(0) / getPeriod(); | ||
auto & b = prepare(); | ||
b.clear(); | ||
b.addFloat64(rate / bitrate); | ||
write(); | ||
} | ||
|
||
// ----------------------------------------------------------------------------- |
47 changes: 47 additions & 0 deletions
47
libraries/YarpPlugins/CanBusControlboard/BusLoadMonitor.hpp
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- | ||
|
||
#ifndef __BUS_LOAD_MONITOR_HPP__ | ||
#define __BUS_LOAD_MONITOR_HPP__ | ||
|
||
#include <atomic> | ||
|
||
#include <yarp/os/Bottle.h> | ||
#include <yarp/os/PeriodicThread.h> | ||
#include <yarp/os/PortWriterBuffer.h> | ||
|
||
#include "CanMessageNotifier.hpp" | ||
|
||
namespace roboticslab | ||
{ | ||
|
||
/** | ||
* @ingroup CanBusControlboard | ||
* @brief Periodically sends CAN bus load stats through a YARP port. | ||
*/ | ||
class BusLoadMonitor final : public yarp::os::PeriodicThread, | ||
public yarp::os::PortWriterBuffer<yarp::os::Bottle>, | ||
public CanMessageNotifier | ||
{ | ||
public: | ||
//! Constructor. | ||
BusLoadMonitor(double period) : yarp::os::PeriodicThread(period), bitrate(1.0) | ||
{ } | ||
|
||
//! Tell observers a new CAN message has arrived. | ||
virtual bool notifyMessage(const can_message & msg) override; | ||
|
||
void setBitrate(unsigned int bitrate) | ||
{ this->bitrate = bitrate; } | ||
|
||
protected: | ||
//! The thread will invoke this periodically. | ||
virtual void run() override; | ||
|
||
private: | ||
unsigned int bitrate; | ||
std::atomic<unsigned long> bits; | ||
}; | ||
|
||
} // namespace roboticslab | ||
|
||
#endif // __BUS_LOAD_MONITOR_HPP__ |
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