node.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (C) 2014 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> // Compilability test
  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(Node, 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::Node<0>): " << sizeof(uavcan::Node<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. * Companion test node
  38. */
  39. uavcan::Node<1024> node2(nodes.can_b, nodes.clock_b);
  40. node2.setName("foobar");
  41. node2.setNodeID(2);
  42. node2.setSoftwareVersion(swver);
  43. BackgroundSpinner bgspinner(node2, node1);
  44. bgspinner.startPeriodic(uavcan::MonotonicDuration::fromMSec(10));
  45. uavcan::NodeStatusMonitor node_status_monitor(node2);
  46. ASSERT_LE(0, node_status_monitor.start());
  47. /*
  48. * Init the second node - network is empty
  49. */
  50. ASSERT_LE(0, node2.start());
  51. ASSERT_FALSE(node_status_monitor.findNodeWithWorstHealth().isValid());
  52. /*
  53. * Init the first node
  54. */
  55. ASSERT_FALSE(node1.isStarted());
  56. ASSERT_EQ(-uavcan::ErrNotInited, node1.spin(uavcan::MonotonicDuration::fromMSec(20)));
  57. ASSERT_LE(0, node1.start());
  58. ASSERT_TRUE(node1.isStarted());
  59. ASSERT_LE(0, node1.spin(uavcan::MonotonicDuration::fromMSec(2000)));
  60. ASSERT_EQ(1, node_status_monitor.findNodeWithWorstHealth().get());
  61. /*
  62. * Some logging
  63. */
  64. SubscriberWithCollector<uavcan::protocol::debug::LogMessage> log_sub(node2);
  65. ASSERT_LE(0, log_sub.start());
  66. node1.getLogger().setLevel(uavcan::protocol::debug::LogLevel::DEBUG);
  67. node1.logInfo("test", "6 * 9 = 42");
  68. ASSERT_LE(0, node1.spin(uavcan::MonotonicDuration::fromMSec(20)));
  69. ASSERT_LE(0, node2.spin(uavcan::MonotonicDuration::fromMSec(20)));
  70. ASSERT_TRUE(log_sub.collector.msg.get());
  71. std::cout << *log_sub.collector.msg << std::endl;
  72. }