sub_node.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2015 Pavel Kirienko <pavel.kirienko@gmail.com>
  3. */
  4. #include <gtest/gtest.h>
  5. #include <uavcan/node/node.hpp>
  6. #include <uavcan/node/sub_node.hpp>
  7. #include <uavcan/protocol/node_status_monitor.hpp>
  8. #include "test_node.hpp"
  9. #include "../protocol/helpers.hpp"
  10. static void registerTypes()
  11. {
  12. uavcan::GlobalDataTypeRegistry::instance().reset();
  13. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::NodeStatus> _reg2;
  14. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::GetNodeInfo> _reg4;
  15. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::GetDataTypeInfo> _reg5;
  16. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::debug::LogMessage> _reg6;
  17. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::GetTransportStats> _reg7;
  18. uavcan::DefaultDataTypeRegistrator<uavcan::protocol::RestartNode> _reg8;
  19. }
  20. TEST(SubNode, Basic)
  21. {
  22. registerTypes();
  23. InterlinkedTestNodesWithSysClock nodes;
  24. uavcan::protocol::SoftwareVersion swver;
  25. swver.major = 0;
  26. swver.minor = 1;
  27. swver.vcs_commit = 0xDEADBEEF;
  28. std::cout << "sizeof(uavcan::SubNode<0>): " << sizeof(uavcan::SubNode<0>) << std::endl;
  29. /*
  30. * uavcan::Node
  31. */
  32. uavcan::Node<1024> node1(nodes.can_a, nodes.clock_a);
  33. node1.setName("com.example");
  34. node1.setNodeID(1);
  35. node1.setSoftwareVersion(swver);
  36. /*
  37. * uavcan::SubNode
  38. */
  39. uavcan::SubNode<1024> node2(nodes.can_b, nodes.clock_b);
  40. BackgroundSpinner bgspinner(node2, node1);
  41. bgspinner.startPeriodic(uavcan::MonotonicDuration::fromMSec(10));
  42. uavcan::NodeStatusMonitor node_status_monitor(node2);
  43. ASSERT_LE(0, node_status_monitor.start());
  44. /*
  45. * Init the first node
  46. */
  47. ASSERT_FALSE(node1.isStarted());
  48. ASSERT_EQ(-uavcan::ErrNotInited, node1.spin(uavcan::MonotonicDuration::fromMSec(20)));
  49. ASSERT_LE(0, node1.start());
  50. ASSERT_TRUE(node1.isStarted());
  51. ASSERT_LE(0, node1.spin(uavcan::MonotonicDuration::fromMSec(2000)));
  52. ASSERT_EQ(1, node_status_monitor.findNodeWithWorstHealth().get());
  53. /*
  54. * Some logging
  55. */
  56. SubscriberWithCollector<uavcan::protocol::debug::LogMessage> log_sub(node2);
  57. ASSERT_LE(0, log_sub.start());
  58. node1.getLogger().setLevel(uavcan::protocol::debug::LogLevel::DEBUG);
  59. node1.logInfo("test", "6 * 9 = 42");
  60. ASSERT_LE(0, node1.spin(uavcan::MonotonicDuration::fromMSec(20)));
  61. ASSERT_LE(0, node2.spin(uavcan::MonotonicDuration::fromMSec(20)));
  62. ASSERT_TRUE(log_sub.collector.msg.get());
  63. std::cout << *log_sub.collector.msg << std::endl;
  64. }