|
| 1 | +/* |
| 2 | + * This example shows reception of a OpenCyphal heartbeat message via CAN. |
| 3 | + * |
| 4 | + * Hardware: |
| 5 | + * - Arduino MKR family board, e.g. MKR VIDOR 4000 |
| 6 | + * - Arduino MKR CAN shield |
| 7 | + */ |
| 8 | + |
| 9 | +/************************************************************************************** |
| 10 | + * INCLUDE |
| 11 | + **************************************************************************************/ |
| 12 | + |
| 13 | +#include <SPI.h> |
| 14 | + |
| 15 | +#include <107-Arduino-Cyphal.h> |
| 16 | +#include <107-Arduino-MCP2515.h> |
| 17 | +#include <107-Arduino-CriticalSection.h> |
| 18 | + |
| 19 | +/************************************************************************************** |
| 20 | + * NAMESPACE |
| 21 | + **************************************************************************************/ |
| 22 | + |
| 23 | +using namespace uavcan::node; |
| 24 | + |
| 25 | +/************************************************************************************** |
| 26 | + * CONSTANTS |
| 27 | + **************************************************************************************/ |
| 28 | + |
| 29 | +static int const MKRCAN_MCP2515_CS_PIN = 3; |
| 30 | +static int const MKRCAN_MCP2515_INT_PIN = 7; |
| 31 | + |
| 32 | +/************************************************************************************** |
| 33 | + * FUNCTION DECLARATION |
| 34 | + **************************************************************************************/ |
| 35 | + |
| 36 | +void onReceiveBufferFull(CanardFrame const &); |
| 37 | +void onHeartbeat_1_0_Received(Heartbeat_1_0 const & msg, TransferMetadata const & metadata); |
| 38 | + |
| 39 | +/************************************************************************************** |
| 40 | + * GLOBAL VARIABLES |
| 41 | + **************************************************************************************/ |
| 42 | + |
| 43 | +ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); }, |
| 44 | + []() { digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); }, |
| 45 | + [](uint8_t const data) { return SPI.transfer(data); }, |
| 46 | + micros, |
| 47 | + onReceiveBufferFull, |
| 48 | + nullptr); |
| 49 | + |
| 50 | +Node::Heap<Node::DEFAULT_O1HEAP_SIZE> node_heap; |
| 51 | +Node node_hdl(node_heap.data(), node_heap.size(), micros, [] (CanardFrame const & frame) { return mcp2515.transmit(frame); }); |
| 52 | + |
| 53 | +Subscription heartbeat_subscription = node_hdl.create_subscription<Heartbeat_1_0>(onHeartbeat_1_0_Received); |
| 54 | +/************************************************************************************** |
| 55 | + * SETUP/LOOP |
| 56 | + **************************************************************************************/ |
| 57 | + |
| 58 | +void setup() |
| 59 | +{ |
| 60 | + Serial.begin(9600); |
| 61 | + while(!Serial) { } |
| 62 | + delay(1000); |
| 63 | + Serial.println("|---- OpenCyphal Heartbeat Subscription With Metadata Example ----|"); |
| 64 | + |
| 65 | + /* Setup SPI access */ |
| 66 | + SPI.begin(); |
| 67 | + pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT); |
| 68 | + digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); |
| 69 | + |
| 70 | + /* Attach interrupt handler to register MCP2515 signaled by taking INT low */ |
| 71 | + pinMode(MKRCAN_MCP2515_INT_PIN, INPUT_PULLUP); |
| 72 | + attachInterrupt(digitalPinToInterrupt(MKRCAN_MCP2515_INT_PIN), []() { mcp2515.onExternalEventHandler(); }, LOW); |
| 73 | + |
| 74 | + /* Initialize MCP2515 */ |
| 75 | + mcp2515.begin(); |
| 76 | + mcp2515.setBitRate(CanBitRate::BR_250kBPS_16MHZ); |
| 77 | + mcp2515.setNormalMode(); |
| 78 | + |
| 79 | + Serial.println("setup finished"); |
| 80 | +} |
| 81 | + |
| 82 | +void loop() |
| 83 | +{ |
| 84 | + /* Process all pending OpenCyphal actions. |
| 85 | + */ |
| 86 | + { |
| 87 | + CriticalSection crit_sec; |
| 88 | + node_hdl.spinSome(); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/************************************************************************************** |
| 93 | + * FUNCTION DEFINITION |
| 94 | + **************************************************************************************/ |
| 95 | + |
| 96 | +void onReceiveBufferFull(CanardFrame const & frame) |
| 97 | +{ |
| 98 | + node_hdl.onCanFrameReceived(frame); |
| 99 | +} |
| 100 | + |
| 101 | +void onHeartbeat_1_0_Received(Heartbeat_1_0 const & msg, TransferMetadata const & metadata) |
| 102 | +{ |
| 103 | + char msg_buf[70]; |
| 104 | + snprintf( |
| 105 | + msg_buf, |
| 106 | + sizeof(msg_buf), |
| 107 | + "Node ID= %d, Uptime = %d, Health = %d, Mode = %d, VSSC = %d", |
| 108 | + metadata.remote_node_id, |
| 109 | + msg.uptime, |
| 110 | + msg.health.value, |
| 111 | + msg.mode.value, |
| 112 | + msg.vendor_specific_status_code); |
| 113 | + |
| 114 | + Serial.println(msg_buf); |
| 115 | +} |
0 commit comments