Skip to content

Commit

Permalink
Merge pull request #46 from 107-systems/adapt-to-107-Arduino-UAVCAN
Browse files Browse the repository at this point in the history
Adapt library to directly deal with CanardFrame type frames
  • Loading branch information
aentinger authored Oct 2, 2020
2 parents 104e8b1 + bb5d646 commit bd5d3f2
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void spi_select ();
void spi_deselect ();
uint8_t spi_transfer (uint8_t const);
void onExternalEvent ();
bool transmitCanFrame(uint32_t const, uint8_t const *, uint8_t const);
bool transmitCanFrame(CanardFrame const &);

/**************************************************************************************
* GLOBAL VARIABLES
Expand All @@ -39,10 +39,11 @@ bool transmitCanFrame(uint32_t const, uint8_t const *, uint8_t const);
ArduinoMCP2515 mcp2515(spi_select,
spi_deselect,
spi_transfer,
micros,
nullptr,
nullptr);

ArduinoUAVCAN uavcan(13, micros, transmitCanFrame);
ArduinoUAVCAN uavcan(13, transmitCanFrame);

Heartbeat_1_0 hb;

Expand Down Expand Up @@ -118,7 +119,7 @@ void onExternalEvent()
mcp2515.onExternalEventHandler();
}

bool transmitCanFrame(uint32_t const id, uint8_t const * data, uint8_t const len)
bool transmitCanFrame(CanardFrame const & frame)
{
return mcp2515.transmit(id, data, len);
return mcp2515.transmit(frame);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void spi_select ();
void spi_deselect ();
uint8_t spi_transfer (uint8_t const);
void onExternalEvent ();
void onReceiveBufferFull (uint32_t const, uint8_t const *, uint8_t const);
void onReceiveBufferFull (CanardFrame const &);
void onHeatbeat_1_0_Received(CanardTransfer const &, ArduinoUAVCAN &);

/**************************************************************************************
Expand All @@ -40,10 +40,11 @@ void onHeatbeat_1_0_Received(CanardTransfer const &, ArduinoUAVCAN &);
ArduinoMCP2515 mcp2515(spi_select,
spi_deselect,
spi_transfer,
micros,
onReceiveBufferFull,
nullptr);

ArduinoUAVCAN uavcan(13, micros, nullptr);
ArduinoUAVCAN uavcan(13, nullptr);

/**************************************************************************************
* SETUP/LOOP
Expand Down Expand Up @@ -101,9 +102,9 @@ void onExternalEvent()
mcp2515.onExternalEventHandler();
}

void onReceiveBufferFull(uint32_t const id, uint8_t const * data, uint8_t const len)
void onReceiveBufferFull(CanardFrame const & frame)
{
uavcan.onCanFrameReceived(id, data, len);
uavcan.onCanFrameReceived(frame);
}

void onHeatbeat_1_0_Received(CanardTransfer const & transfer, ArduinoUAVCAN & /* uavcan */)
Expand Down
25 changes: 16 additions & 9 deletions examples/UAVCAN-Service-Client/UAVCAN-Service-Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ static int const MKRCAN_MCP2515_INT_PIN = 7;
* FUNCTION DECLARATION
**************************************************************************************/

void spi_select ();
void spi_deselect ();
uint8_t spi_transfer (uint8_t const);
void onExternalEvent ();
bool transmitCanFrame(uint32_t const, uint8_t const *, uint8_t const);
void spi_select ();
void spi_deselect ();
uint8_t spi_transfer (uint8_t const);
void onExternalEvent ();
void onReceiveBufferFull(CanardFrame const &);
bool transmitCanFrame (CanardFrame const &);
void onExecuteCommand_1_0_Response_Received(CanardTransfer const &, ArduinoUAVCAN &);

/**************************************************************************************
Expand All @@ -41,10 +42,11 @@ void onExecuteCommand_1_0_Response_Received(CanardTransfer const &, ArduinoUA
ArduinoMCP2515 mcp2515(spi_select,
spi_deselect,
spi_transfer,
nullptr,
micros,
onReceiveBufferFull,
nullptr);

ArduinoUAVCAN uavcan(13 /* local node id */, micros, transmitCanFrame);
ArduinoUAVCAN uavcan(13 /* local node id */, transmitCanFrame);

/**************************************************************************************
* SETUP/LOOP
Expand Down Expand Up @@ -109,9 +111,14 @@ void onExternalEvent()
mcp2515.onExternalEventHandler();
}

bool transmitCanFrame(uint32_t const id, uint8_t const * data, uint8_t const len)
void onReceiveBufferFull(CanardFrame const & frame)
{
return mcp2515.transmit(id, data, len);
uavcan.onCanFrameReceived(frame);
}

bool transmitCanFrame(CanardFrame const & frame)
{
return mcp2515.transmit(frame);
}

void onExecuteCommand_1_0_Response_Received(CanardTransfer const & transfer, ArduinoUAVCAN & /* uavcan */)
Expand Down
25 changes: 16 additions & 9 deletions examples/UAVCAN-Service-Server/UAVCAN-Service-Server.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ static int const MKRCAN_MCP2515_INT_PIN = 7;
* FUNCTION DECLARATION
**************************************************************************************/

void spi_select ();
void spi_deselect ();
uint8_t spi_transfer (uint8_t const);
void onExternalEvent ();
bool transmitCanFrame(uint32_t const, uint8_t const *, uint8_t const);
void spi_select ();
void spi_deselect ();
uint8_t spi_transfer (uint8_t const);
void onExternalEvent ();
void onReceiveBufferFull(CanardFrame const &);
bool transmitCanFrame (CanardFrame const &);
void onExecuteCommand_1_0_Request_Received(CanardTransfer const &, ArduinoUAVCAN &);

/**************************************************************************************
Expand All @@ -41,10 +42,11 @@ void onExecuteCommand_1_0_Request_Received(CanardTransfer const &, ArduinoUAV
ArduinoMCP2515 mcp2515(spi_select,
spi_deselect,
spi_transfer,
nullptr,
micros,
onReceiveBufferFull,
nullptr);

ArduinoUAVCAN uavcan(13 /* local node id */, micros, transmitCanFrame);
ArduinoUAVCAN uavcan(13 /* local node id */, transmitCanFrame);

/**************************************************************************************
* SETUP/LOOP
Expand Down Expand Up @@ -103,9 +105,14 @@ void onExternalEvent()
mcp2515.onExternalEventHandler();
}

bool transmitCanFrame(uint32_t const id, uint8_t const * data, uint8_t const len)
void onReceiveBufferFull(CanardFrame const & frame)
{
return mcp2515.transmit(id, data, len);
uavcan.onCanFrameReceived(frame);
}

bool transmitCanFrame(CanardFrame const & frame)
{
return mcp2515.transmit(frame);
}

void onExecuteCommand_1_0_Request_Received(CanardTransfer const & transfer, ArduinoUAVCAN & uavcan)
Expand Down
1 change: 0 additions & 1 deletion extras/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
##########################################################################

set(TEST_SRCS
src/util/micros.cpp
src/util/Types.cpp

src/test_main.cpp
Expand Down
3 changes: 3 additions & 0 deletions extras/test/include/test/util/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <vector>
#include <iostream>

#include <libcanard/canard.h>

/**************************************************************************************
* NAMESPACE
**************************************************************************************/
Expand All @@ -42,6 +44,7 @@ typedef std::vector<CanFrame> CanFrameVect;

std::ostream & operator << (std::ostream & os, CanFrame const & f);
std::ostream & operator << (std::ostream & os, CanFrameVect const & fv);
CanardFrame toCanardFrame(uint32_t const id, std::vector<uint8_t> const & data);

/**************************************************************************************
* NAMESPACE
Expand Down
30 changes: 0 additions & 30 deletions extras/test/include/test/util/micros.h

This file was deleted.

18 changes: 9 additions & 9 deletions extras/test/src/test_ExecuteCommand_ServiceClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include <test/util/Const.h>
#include <test/util/Types.h>
#include <test/util/micros.h>

#include <ArduinoUAVCAN.h>

Expand All @@ -34,14 +33,15 @@ static uavcan_node_ExecuteCommand_1_0_Response response;
* PRIVATE FUNCTION DEFINITION
**************************************************************************************/

static bool transmitCanFrame(uint32_t const id, uint8_t const * data, uint8_t const len)
static bool transmitCanFrame(CanardFrame const & f)
{
util::CanFrame frame{id, std::vector<uint8_t>(data, data + len)};
util::CanFrame const frame{f.extended_can_id,
std::vector<uint8_t>(reinterpret_cast<uint8_t const *>(f.payload), reinterpret_cast<uint8_t const *>(f.payload) + f.payload_size)};
can_frame_vect.push_back(frame);
return true;
}

void onExecuteCommand_1_0_Response_Received(CanardTransfer const & transfer, ArduinoUAVCAN & /* uavcan */)
static void onExecuteCommand_1_0_Response_Received(CanardTransfer const & transfer, ArduinoUAVCAN & /* uavcan */)
{
ExecuteCommand_1_0::Response const received_response = ExecuteCommand_1_0::Response::create(transfer);
response.status = received_response.data.status;
Expand All @@ -54,7 +54,7 @@ void onExecuteCommand_1_0_Response_Received(CanardTransfer const & transfer, Ard
TEST_CASE("A '435.ExecuteCommand.1.0' request is sent to a server", "[execute-command-client-01]")
{
uavcan_node_ExecuteCommand_1_0_Response_init(&response);
ArduinoUAVCAN uavcan(util::LOCAL_NODE_ID, util::micros, transmitCanFrame);
ArduinoUAVCAN uavcan(util::LOCAL_NODE_ID, transmitCanFrame);

std::string const cmd_1_param = "I want a double espresso with cream";
ExecuteCommand_1_0::Request req_1;
Expand Down Expand Up @@ -95,8 +95,8 @@ TEST_CASE("A '435.ExecuteCommand.1.0' request is sent to a server", "[execute-co
/* Feed back the command response to the uavcan node. In a
* real system the answer would come back from the remote node.
*/
uint8_t const data_1[] = {0x02, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xE0};
uavcan.onCanFrameReceived(0x126CC69B, data_1, sizeof(data_1));
std::vector<uint8_t> const data_1{0x02, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xE0};
uavcan.onCanFrameReceived(util::toCanardFrame(0x126CC69B, data_1));

/* Check if the expected response has been indeed received. */
REQUIRE(response.status == arduino::_107_::uavcan::to_integer(ExecuteCommand_1_0::Response::Status::NOT_AUTHORIZED));
Expand Down Expand Up @@ -140,8 +140,8 @@ TEST_CASE("A '435.ExecuteCommand.1.0' request is sent to a server", "[execute-co
/* Feed back the command response to the uavcan node. In a
* real system the answer would come back from the remote node.
*/
uint8_t const data_2[] = {0x05, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xE1};
uavcan.onCanFrameReceived(0x126CC69B, data_2, sizeof(data_2));
std::vector<uint8_t> const data_2{0x05, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xE1};
uavcan.onCanFrameReceived(util::toCanardFrame(0x126CC69B, data_2));

/* Check if the expected response has been indeed received. */
REQUIRE(response.status == arduino::_107_::uavcan::to_integer(ExecuteCommand_1_0::Response::Status::BAD_STATE));
Expand Down
12 changes: 6 additions & 6 deletions extras/test/src/test_ExecuteCommand_ServiceServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include <test/util/Const.h>
#include <test/util/Types.h>
#include <test/util/micros.h>

#include <ArduinoUAVCAN.h>

Expand All @@ -34,14 +33,15 @@ static uavcan_node_ExecuteCommand_1_0_Request request;
* PRIVATE FUNCTION DEFINITION
**************************************************************************************/

static bool transmitCanFrame(uint32_t const id, uint8_t const * data, uint8_t const len)
static bool transmitCanFrame(CanardFrame const & f)
{
util::CanFrame frame{id, std::vector<uint8_t>(data, data + len)};
util::CanFrame const frame{f.extended_can_id,
std::vector<uint8_t>(reinterpret_cast<uint8_t const *>(f.payload), reinterpret_cast<uint8_t const *>(f.payload) + f.payload_size)};
response_can_frame = frame;
return true;
}

void onExecuteCommand_1_0_Request_Received(CanardTransfer const & transfer, ArduinoUAVCAN & uavcan)
static void onExecuteCommand_1_0_Request_Received(CanardTransfer const & transfer, ArduinoUAVCAN & uavcan)
{
ExecuteCommand_1_0::Request received_request = ExecuteCommand_1_0::Request::create(transfer);

Expand All @@ -68,7 +68,7 @@ void onExecuteCommand_1_0_Request_Received(CanardTransfer const & transfer, Ardu
TEST_CASE("A '435.ExecuteCommand.1.0' request is received from a client", "[execute-command-server-01]")
{
uavcan_node_ExecuteCommand_1_0_Request_init(&request);
ArduinoUAVCAN uavcan(util::LOCAL_NODE_ID, util::micros, transmitCanFrame);
ArduinoUAVCAN uavcan(util::LOCAL_NODE_ID, transmitCanFrame);

/* Subscribe to incoming server requests. */
REQUIRE(uavcan.subscribe<ExecuteCommand_1_0::Request>(onExecuteCommand_1_0_Request_Received) == true);
Expand All @@ -91,7 +91,7 @@ TEST_CASE("A '435.ExecuteCommand.1.0' request is received from a client", "[exec
std::end (SERVER_REQUEST_CAN_FRAMES),
[&uavcan](util::CanFrame const frame)
{
uavcan.onCanFrameReceived(frame.id, &frame.data.front(), frame.data.size());
uavcan.onCanFrameReceived(util::toCanardFrame(frame.id, frame.data));
});

/* Check if the correct request has been received. */
Expand Down
16 changes: 8 additions & 8 deletions extras/test/src/test_Heartbeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <test/util/Const.h>
#include <test/util/Types.h>
#include <test/util/micros.h>

#include <ArduinoUAVCAN.h>

Expand All @@ -31,14 +30,15 @@ static CanardNodeID hb_node_id = 0;
* PRIVATE FUNCTION DEFINITION
**************************************************************************************/

static bool transmitCanFrame(uint32_t const id, uint8_t const * data, uint8_t const len)
static bool transmitCanFrame(CanardFrame const & f)
{
util::CanFrame frame{id, std::vector<uint8_t>(data, data + len)};
util::CanFrame const frame{f.extended_can_id,
std::vector<uint8_t>(reinterpret_cast<uint8_t const *>(f.payload), reinterpret_cast<uint8_t const *>(f.payload) + f.payload_size)};
can_frame = frame;
return true;
}

void onHeatbeat_1_0_Received(CanardTransfer const & transfer, ArduinoUAVCAN & /* uavcan */)
static void onHeatbeat_1_0_Received(CanardTransfer const & transfer, ArduinoUAVCAN & /* uavcan */)
{
Heartbeat_1_0 const received_hb = Heartbeat_1_0::create(transfer);

Expand All @@ -55,7 +55,7 @@ void onHeatbeat_1_0_Received(CanardTransfer const & transfer, ArduinoUAVCAN & /*

TEST_CASE("A '32085.Heartbeat.1.0.uavcan' message is sent", "[heartbeat-01]")
{
ArduinoUAVCAN uavcan(util::LOCAL_NODE_ID, util::micros, transmitCanFrame);
ArduinoUAVCAN uavcan(util::LOCAL_NODE_ID, transmitCanFrame);

Heartbeat_1_0 hb;
hb.data.uptime = 9876;
Expand Down Expand Up @@ -86,7 +86,7 @@ TEST_CASE("A '32085.Heartbeat.1.0.uavcan' message is sent", "[heartbeat-01]")
TEST_CASE("A '32085.Heartbeat.1.0.uavcan' message is received", "[heartbeat-02]")
{
uavcan_node_Heartbeat_1_0_init(&hb_data);
ArduinoUAVCAN uavcan(util::LOCAL_NODE_ID, util::micros, nullptr);
ArduinoUAVCAN uavcan(util::LOCAL_NODE_ID, nullptr);

REQUIRE(uavcan.subscribe<Heartbeat_1_0>(onHeatbeat_1_0_Received));

Expand All @@ -99,8 +99,8 @@ TEST_CASE("A '32085.Heartbeat.1.0.uavcan' message is received", "[heartbeat-02]"
* sudo ip link set up vcan0
* candump -decaxta vcan0
*/
uint8_t const data[] = {0x39, 0x05, 0x00, 0x00, 0x5E, 0x05, 0x00, 0xE1};
uavcan.onCanFrameReceived(0x107D553B, data, sizeof(data));
std::vector<uint8_t> const data{0x39, 0x05, 0x00, 0x00, 0x5E, 0x05, 0x00, 0xE1};
uavcan.onCanFrameReceived(util::toCanardFrame(0x107D553B, data));

REQUIRE(hb_node_id == 59);
REQUIRE(hb_data.uptime == 1337);
Expand Down
Loading

0 comments on commit bd5d3f2

Please sign in to comment.