STM32 example: cleaner node instantiation

This commit is contained in:
Pavel Kirienko
2015-10-09 01:51:37 +03:00
parent 70232725e6
commit 47da7f17df
@@ -15,17 +15,12 @@ namespace
uavcan_stm32::CanInitHelper<128> can; uavcan_stm32::CanInitHelper<128> can;
typedef uavcan::Node<16384> Node; constexpr unsigned NodePoolSize = 16384;
uavcan::LazyConstructor<Node> node_; uavcan::Node<NodePoolSize>& getNode()
Node& getNode()
{ {
if (!node_.isConstructed()) static uavcan::Node<NodePoolSize> node(can.driver, uavcan_stm32::SystemClock::instance());
{ return node;
node_.construct<uavcan::ICanDriver&, uavcan::ISystemClock&>(can.driver, uavcan_stm32::SystemClock::instance());
}
return *node_;
} }
void init() void init()
@@ -52,10 +47,8 @@ class : public chibios_rt::BaseStaticThread<8192>
{ {
void configureNodeInfo() void configureNodeInfo()
{ {
Node& node = app::getNode(); getNode().setNodeID(64);
getNode().setName("org.uavcan.stm32_test_stm32f107");
node.setNodeID(64);
node.setName("org.uavcan.stm32_test_stm32f107");
/* /*
* Software version * Software version
@@ -66,7 +59,7 @@ class : public chibios_rt::BaseStaticThread<8192>
swver.vcs_commit = GIT_HASH; swver.vcs_commit = GIT_HASH;
swver.optional_field_flags = swver.OPTIONAL_FIELD_FLAG_VCS_COMMIT; swver.optional_field_flags = swver.OPTIONAL_FIELD_FLAG_VCS_COMMIT;
node.setSoftwareVersion(swver); getNode().setSoftwareVersion(swver);
lowsyslog("Git commit hash: 0x%08x\n", GIT_HASH); lowsyslog("Git commit hash: 0x%08x\n", GIT_HASH);
@@ -80,7 +73,7 @@ class : public chibios_rt::BaseStaticThread<8192>
board::readUniqueID(uid); board::readUniqueID(uid);
std::copy(std::begin(uid), std::end(uid), std::begin(hwver.unique_id)); std::copy(std::begin(uid), std::end(uid), std::begin(hwver.unique_id));
node.setHardwareVersion(hwver); getNode().setHardwareVersion(hwver);
lowsyslog("UDID:"); lowsyslog("UDID:");
for (auto b : hwver.unique_id) for (auto b : hwver.unique_id)
@@ -98,15 +91,13 @@ public:
*/ */
configureNodeInfo(); configureNodeInfo();
Node& node = app::getNode();
/* /*
* Initializing the UAVCAN node - this may take a while * Initializing the UAVCAN node - this may take a while
*/ */
while (true) while (true)
{ {
// Calling start() multiple times is OK - only the first successfull call will be effective // Calling start() multiple times is OK - only the first successfull call will be effective
int res = node.start(); int res = getNode().start();
if (res < 0) if (res < 0)
{ {
@@ -122,7 +113,7 @@ public:
/* /*
* Time synchronizer * Time synchronizer
*/ */
static uavcan::GlobalTimeSyncSlave time_sync_slave(node); static uavcan::GlobalTimeSyncSlave time_sync_slave(getNode());
{ {
const int res = time_sync_slave.start(); const int res = time_sync_slave.start();
if (res < 0) if (res < 0)
@@ -135,10 +126,10 @@ public:
* Main loop * Main loop
*/ */
lowsyslog("UAVCAN node started\n"); lowsyslog("UAVCAN node started\n");
node.setModeOperational(); getNode().setModeOperational();
while (true) while (true)
{ {
const int spin_res = node.spin(uavcan::MonotonicDuration::fromMSec(5000)); const int spin_res = getNode().spin(uavcan::MonotonicDuration::fromMSec(5000));
if (spin_res < 0) if (spin_res < 0)
{ {
lowsyslog("Spin failure: %i\n", spin_res); lowsyslog("Spin failure: %i\n", spin_res);
@@ -147,9 +138,9 @@ public:
lowsyslog("Time sync master: %u\n", unsigned(time_sync_slave.getMasterNodeID().get())); lowsyslog("Time sync master: %u\n", unsigned(time_sync_slave.getMasterNodeID().get()));
lowsyslog("Memory usage: free=%u used=%u worst=%u\n", lowsyslog("Memory usage: free=%u used=%u worst=%u\n",
node.getAllocator().getNumFreeBlocks(), getNode().getAllocator().getNumFreeBlocks(),
node.getAllocator().getNumUsedBlocks(), getNode().getAllocator().getNumUsedBlocks(),
node.getAllocator().getPeakNumUsedBlocks()); getNode().getAllocator().getPeakNumUsedBlocks());
lowsyslog("CAN errors: %lu %lu\n", lowsyslog("CAN errors: %lu %lu\n",
static_cast<unsigned long>(can.driver.getIface(0)->getErrorCount()), static_cast<unsigned long>(can.driver.getIface(0)->getErrorCount()),