panic_broadcaster.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_PROTOCOL_PANIC_BROADCASTER_HPP_INCLUDED
6 #define UAVCAN_PROTOCOL_PANIC_BROADCASTER_HPP_INCLUDED
7 
8 #include <uavcan/debug.hpp>
10 #include <uavcan/node/timer.hpp>
11 #include <uavcan/protocol/Panic.hpp>
12 
13 namespace uavcan
14 {
19 {
21  protocol::Panic msg_;
22 
23  void publishOnce()
24  {
25  const int res = pub_.broadcast(msg_);
26  if (res < 0)
27  {
28  pub_.getNode().registerInternalFailure("Panic pub failed");
29  }
30  }
31 
32  virtual void handleTimerEvent(const TimerEvent&)
33  {
34  publishOnce();
35  }
36 
37 public:
39  : TimerBase(node)
40  , pub_(node)
41  { }
42 
50  void panic(const char* short_reason_description,
51  MonotonicDuration broadcasting_period = MonotonicDuration::fromMSec(100),
53  {
54  msg_.reason_text.clear();
55  const char* p = short_reason_description;
56  while (p && *p)
57  {
58  if (msg_.reason_text.size() == msg_.reason_text.capacity())
59  {
60  break;
61  }
62  msg_.reason_text.push_back(protocol::Panic::FieldTypes::reason_text::ValueType(*p));
63  p++;
64  }
65 
66  UAVCAN_TRACE("PanicBroadcaster", "Panicking with reason '%s'", getReason().c_str());
67 
68  pub_.setTxTimeout(broadcasting_period);
69  pub_.setPriority(priority);
70 
71  publishOnce();
72  startPeriodic(broadcasting_period);
73  }
74 
78  void dontPanic() // Where's my towel
79  {
80  stop();
81  msg_.reason_text.clear();
82  }
83 
84  bool isPanicking() const { return isRunning(); }
85 
86  const typename protocol::Panic::FieldTypes::reason_text& getReason() const
87  {
88  return msg_.reason_text;
89  }
90 };
91 
92 }
93 
94 #endif // UAVCAN_PROTOCOL_PANIC_BROADCASTER_HPP_INCLUDED
void setPriority(const TransferPriority prio)
Publisher< protocol::Panic > pub_
void setTxTimeout(MonotonicDuration tx_timeout)
virtual void registerInternalFailure(const char *msg)=0
void panic(const char *short_reason_description, MonotonicDuration broadcasting_period=MonotonicDuration::fromMSec(100), const TransferPriority priority=TransferPriority::Default)
static const TransferPriority Default
This priority is used by default.
Definition: transfer.hpp:38
const protocol::Panic::FieldTypes::reason_text & getReason() const
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
virtual void handleTimerEvent(const TimerEvent &)
int broadcast(const DataType &message)
Definition: publisher.hpp:52


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39