Esempio n. 1
0
static void runForever(const uavcan_linux::NodePtr& node)
{
    /*
     * Subscribing to the UAVCAN logging topic
     */
    auto log_handler = [](const uavcan::ReceivedDataStructure<uavcan::protocol::debug::LogMessage>& msg)
    {
        std::cout << msg << std::endl;
    };
    auto log_sub = node->makeSubscriber<uavcan::protocol::debug::LogMessage>(log_handler);

    /*
     * Printing when other nodes enter the network or change status
     */
    struct NodeStatusMonitor : public uavcan::NodeStatusMonitor
    {
        explicit NodeStatusMonitor(uavcan::INode& node) : uavcan::NodeStatusMonitor(node) { }

        void handleNodeStatusChange(const NodeStatusChangeEvent& event) override
        {
            std::cout << "Remote node NID " << int(event.node_id.get()) << " changed status: "
                      << int(event.old_status.status_code) << " --> "
                      << int(event.status.status_code) << std::endl;
        }
    };

    NodeStatusMonitor nsm(*node);
    ENFORCE(0 == nsm.start());

    /*
     * Adding a stupid timer that does nothing once a minute
     */
    auto do_nothing_once_a_minute = [&node](const uavcan::TimerEvent&)
    {
        node->logInfo("timer", "Another minute passed...");
        // coverity[dont_call]
        node->setVendorSpecificStatusCode(static_cast<std::uint16_t>(std::rand())); // Setting to an arbitrary value
    };
    auto timer = node->makeTimer(uavcan::MonotonicDuration::fromMSec(60000), do_nothing_once_a_minute);

    /*
     * Spinning forever
     */
    while (true)
    {
        const int res = node->spin(uavcan::MonotonicDuration::getInfinite());
        if (res < 0)
        {
            node->logError("spin", "Error %*", res);
        }
    }
}
static void runForever(const uavcan_linux::NodePtr& node)
{
    Monitor mon(node);
    ENFORCE(0 == mon.start());
    while (true)
    {
        const int res = node->spin(uavcan::MonotonicDuration::getInfinite());
        if (res < 0)
        {
            node->logError("spin", "Error %*", res);
        }
    }
}
Esempio n. 3
0
static void runForever(const uavcan_linux::NodePtr& node)
{
    auto sub_log      = makePrintingSubscriber<uavcan::protocol::debug::LogMessage>(node);
    auto sub_air_data = makePrintingSubscriber<uavcan::equipment::air_data::StaticAirData>(node);
    auto sub_mag      = makePrintingSubscriber<uavcan::equipment::ahrs::Magnetometer>(node);
    auto sub_gnss_fix = makePrintingSubscriber<uavcan::equipment::gnss::Fix>(node);
    auto sub_gnss_aux = makePrintingSubscriber<uavcan::equipment::gnss::Auxiliary>(node);

    while (true)
    {
        const int res = node->spin(uavcan::MonotonicDuration::getInfinite());
        if (res < 0)
        {
            node->logError("spin", "Error %*", res);
        }
    }
}
static void runMainNode(const uavcan_linux::NodePtr& node)
{
    std::cout << "Running main node" << std::endl;

    auto timer = node->makeTimer(uavcan::MonotonicDuration::fromMSec(10000), [&node](const uavcan::TimerEvent&)
        {
            node->logInfo("timer", "Your time is running out.");
            // coverity[dont_call]
            node->setVendorSpecificStatusCode(static_cast<std::uint16_t>(std::rand()));
        });

    /*
     * We know that in this implementation, VirtualCanDriver inherits uavcan::IRxFrameListener, so we can simply
     * restore the reference to ITxQueueInjector using dynamic_cast. In other implementations this may be
     * unacceptable, so a reference to ITxQueueInjector will have to be passed using some other means.
     */
    if (node->getDispatcher().getRxFrameListener() == nullptr)
    {
        throw std::logic_error("RX frame listener is not configured");
    }
    ITxQueueInjector& tx_injector = dynamic_cast<ITxQueueInjector&>(*node->getDispatcher().getRxFrameListener());

    while (true)
    {
        const int res = node->spin(uavcan::MonotonicDuration::fromMSec(1));
        if (res < 0)
        {
            node->logError("spin", "Error %*", res);
        }
        // TX queue transfer occurs here.
        tx_injector.injectTxFramesInto(*node);
    }
}
Esempio n. 5
0
 explicit Monitor(uavcan_linux::NodePtr node)
     : uavcan::NodeStatusMonitor(*node)
     , timer_(node->makeTimer(uavcan::MonotonicDuration::fromMSec(500),
                              std::bind(&Monitor::redraw, this, std::placeholders::_1)))
 { }