-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #235 from rebeccaRossRobotics/feature/expose_metadata
Add returning Metadata to subscription callback
- Loading branch information
Showing
5 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
...phal-Heartbeat-Subscriber-With-Metadata/OpenCyphal-Heartbeat-Subscriber-With-Metadata.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* This example shows reception of a OpenCyphal heartbeat message via CAN. | ||
* | ||
* Hardware: | ||
* - Arduino MKR family board, e.g. MKR VIDOR 4000 | ||
* - Arduino MKR CAN shield | ||
*/ | ||
|
||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include <SPI.h> | ||
|
||
#include <107-Arduino-Cyphal.h> | ||
#include <107-Arduino-MCP2515.h> | ||
#include <107-Arduino-CriticalSection.h> | ||
|
||
/************************************************************************************** | ||
* NAMESPACE | ||
**************************************************************************************/ | ||
|
||
using namespace uavcan::node; | ||
|
||
/************************************************************************************** | ||
* CONSTANTS | ||
**************************************************************************************/ | ||
|
||
static int const MKRCAN_MCP2515_CS_PIN = 3; | ||
static int const MKRCAN_MCP2515_INT_PIN = 7; | ||
|
||
/************************************************************************************** | ||
* FUNCTION DECLARATION | ||
**************************************************************************************/ | ||
|
||
void onReceiveBufferFull(CanardFrame const &); | ||
void onHeartbeat_1_0_Received(Heartbeat_1_0 const & msg, TransferMetadata const & metadata); | ||
|
||
/************************************************************************************** | ||
* GLOBAL VARIABLES | ||
**************************************************************************************/ | ||
|
||
ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); }, | ||
[]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); }, | ||
[](uint8_t const data) { return SPI.transfer(data); }, | ||
micros, | ||
onReceiveBufferFull, | ||
nullptr); | ||
|
||
Node::Heap<Node::DEFAULT_O1HEAP_SIZE> node_heap; | ||
Node node_hdl(node_heap.data(), node_heap.size(), micros, [] (CanardFrame const & frame) { return mcp2515.transmit(frame); }); | ||
|
||
Subscription heartbeat_subscription = node_hdl.create_subscription<Heartbeat_1_0>(onHeartbeat_1_0_Received); | ||
/************************************************************************************** | ||
* SETUP/LOOP | ||
**************************************************************************************/ | ||
|
||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
while(!Serial) { } | ||
delay(1000); | ||
Serial.println("|---- OpenCyphal Heartbeat Subscription With Metadata Example ----|"); | ||
|
||
/* Setup SPI access */ | ||
SPI.begin(); | ||
pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT); | ||
digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH); | ||
|
||
/* Attach interrupt handler to register MCP2515 signaled by taking INT low */ | ||
pinMode(MKRCAN_MCP2515_INT_PIN, INPUT_PULLUP); | ||
attachInterrupt(digitalPinToInterrupt(MKRCAN_MCP2515_INT_PIN), []() { mcp2515.onExternalEventHandler(); }, LOW); | ||
|
||
/* Initialize MCP2515 */ | ||
mcp2515.begin(); | ||
mcp2515.setBitRate(CanBitRate::BR_250kBPS_16MHZ); | ||
mcp2515.setNormalMode(); | ||
|
||
Serial.println("setup finished"); | ||
} | ||
|
||
void loop() | ||
{ | ||
/* Process all pending OpenCyphal actions. | ||
*/ | ||
{ | ||
CriticalSection crit_sec; | ||
node_hdl.spinSome(); | ||
} | ||
} | ||
|
||
/************************************************************************************** | ||
* FUNCTION DEFINITION | ||
**************************************************************************************/ | ||
|
||
void onReceiveBufferFull(CanardFrame const & frame) | ||
{ | ||
node_hdl.onCanFrameReceived(frame); | ||
} | ||
|
||
void onHeartbeat_1_0_Received(Heartbeat_1_0 const & msg, TransferMetadata const & metadata) | ||
{ | ||
char msg_buf[70]; | ||
snprintf( | ||
msg_buf, | ||
sizeof(msg_buf), | ||
"Node ID= %d, Uptime = %d, Health = %d, Mode = %d, VSSC = %d", | ||
metadata.remote_node_id, | ||
msg.uptime, | ||
msg.health.value, | ||
msg.mode.value, | ||
msg.vendor_specific_status_code); | ||
|
||
Serial.println(msg_buf); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* This software is distributed under the terms of the MIT License. | ||
* Copyright (c) 2020-2023 LXRobotics. | ||
* Author: Alexander Entinger <[email protected]> | ||
* Contributors: https://github.com/107-systems/107-Arduino-Cyphal/graphs/contributors. | ||
*/ | ||
|
||
#ifndef ARDUINO_CYPHAL_TRANSFER_METADATA_HPP_ | ||
#define ARDUINO_CYPHAL_TRANSFER_METADATA_HPP_ | ||
|
||
struct TransferMetadata final | ||
{ | ||
uint16_t remote_node_id; | ||
// More stuff may appear here in the future! | ||
}; | ||
|
||
#endif // ARDUINO_CYPHAL_TRANSFER_METADATA_HPP_ |