mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-22 06:27:35 +08:00
Panic broadcaster moved to header
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#ifndef UAVCAN_PROTOCOL_PANIC_BROADCASTER_HPP_INCLUDED
|
||||
#define UAVCAN_PROTOCOL_PANIC_BROADCASTER_HPP_INCLUDED
|
||||
|
||||
#include <uavcan/debug.hpp>
|
||||
#include <uavcan/node/publisher.hpp>
|
||||
#include <uavcan/node/timer.hpp>
|
||||
#include <uavcan/protocol/Panic.hpp>
|
||||
@@ -19,9 +20,19 @@ class UAVCAN_EXPORT PanicBroadcaster : private TimerBase
|
||||
Publisher<protocol::Panic> pub_;
|
||||
protocol::Panic msg_;
|
||||
|
||||
void publishOnce();
|
||||
void publishOnce()
|
||||
{
|
||||
const int res = pub_.broadcast(msg_);
|
||||
if (res < 0)
|
||||
{
|
||||
pub_.getNode().registerInternalFailure("Panic pub failed");
|
||||
}
|
||||
}
|
||||
|
||||
virtual void handleTimerEvent(const TimerEvent&);
|
||||
virtual void handleTimerEvent(const TimerEvent&)
|
||||
{
|
||||
publishOnce();
|
||||
}
|
||||
|
||||
public:
|
||||
explicit PanicBroadcaster(INode& node)
|
||||
@@ -37,16 +48,41 @@ public:
|
||||
* @param short_reason Short ASCII string that describes the reason of the panic, 7 characters max.
|
||||
* If the string exceeds 7 characters, it will be truncated.
|
||||
*/
|
||||
void panic(const char* short_reason_description);
|
||||
void panic(const char* short_reason_description)
|
||||
{
|
||||
msg_.reason_text.clear();
|
||||
const char* p = short_reason_description;
|
||||
while (p && *p)
|
||||
{
|
||||
if (msg_.reason_text.size() == msg_.reason_text.capacity())
|
||||
{
|
||||
break;
|
||||
}
|
||||
msg_.reason_text.push_back(protocol::Panic::FieldTypes::reason_text::ValueType(*p));
|
||||
p++;
|
||||
}
|
||||
|
||||
UAVCAN_TRACE("PanicBroadcaster", "Panicking with reason '%s'", getReason().c_str());
|
||||
|
||||
publishOnce();
|
||||
startPeriodic(MonotonicDuration::fromMSec(protocol::Panic::BROADCASTING_INTERVAL_MS));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop broadcasting immediately.
|
||||
*/
|
||||
void dontPanic(); // Where's my towel
|
||||
void dontPanic() // Where's my towel
|
||||
{
|
||||
stop();
|
||||
msg_.reason_text.clear();
|
||||
}
|
||||
|
||||
bool isPanicking() const;
|
||||
bool isPanicking() const { return isRunning(); }
|
||||
|
||||
const typename protocol::Panic::FieldTypes::reason_text& getReason() const;
|
||||
const typename protocol::Panic::FieldTypes::reason_text& getReason() const
|
||||
{
|
||||
return msg_.reason_text;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
|
||||
*/
|
||||
|
||||
#include <uavcan/protocol/panic_broadcaster.hpp>
|
||||
#include <uavcan/debug.hpp>
|
||||
|
||||
namespace uavcan
|
||||
{
|
||||
|
||||
void PanicBroadcaster::publishOnce()
|
||||
{
|
||||
const int res = pub_.broadcast(msg_);
|
||||
if (res < 0)
|
||||
{
|
||||
pub_.getNode().registerInternalFailure("Panic pub failed");
|
||||
}
|
||||
}
|
||||
|
||||
void PanicBroadcaster::handleTimerEvent(const TimerEvent&)
|
||||
{
|
||||
publishOnce();
|
||||
}
|
||||
|
||||
void PanicBroadcaster::panic(const char* short_reason_description)
|
||||
{
|
||||
msg_.reason_text.clear();
|
||||
const char* p = short_reason_description;
|
||||
while (p && *p)
|
||||
{
|
||||
if (msg_.reason_text.size() == msg_.reason_text.capacity())
|
||||
{
|
||||
break;
|
||||
}
|
||||
msg_.reason_text.push_back(protocol::Panic::FieldTypes::reason_text::ValueType(*p));
|
||||
p++;
|
||||
}
|
||||
|
||||
UAVCAN_TRACE("PanicBroadcaster", "Panicking with reason '%s'", getReason().c_str());
|
||||
|
||||
publishOnce();
|
||||
startPeriodic(MonotonicDuration::fromMSec(protocol::Panic::BROADCASTING_INTERVAL_MS));
|
||||
}
|
||||
|
||||
void PanicBroadcaster::dontPanic()
|
||||
{
|
||||
stop();
|
||||
msg_.reason_text.clear();
|
||||
}
|
||||
|
||||
bool PanicBroadcaster::isPanicking() const
|
||||
{
|
||||
return isRunning();
|
||||
}
|
||||
|
||||
const typename protocol::Panic::FieldTypes::reason_text& PanicBroadcaster::getReason() const
|
||||
{
|
||||
return msg_.reason_text;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user