Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/service examples #231

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions examples/OpenCyphal-Service-Client/OpenCyphal-Service-Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
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); });
Node node_hdl(node_heap.data(), node_heap.size(), micros, [] (CanardFrame const & frame) { return mcp2515.transmit(frame); }, 28);

ServiceClient<ExecuteCommand::Request_1_1> srv_client = node_hdl.create_service_client<ExecuteCommand::Request_1_1, ExecuteCommand::Response_1_1>(
2*1000*1000UL,
Expand All @@ -63,6 +63,8 @@ void setup()
{
Serial.begin(9600);
while(!Serial) { }
delay(1000);
Serial.println("|---- OpenCyphal Service Client Example ----|");

/* Setup SPI access */
SPI.begin();
Expand All @@ -78,17 +80,7 @@ void setup()
mcp2515.setBitRate(CanBitRate::BR_250kBPS_16MHZ);
mcp2515.setNormalMode();

/* Request some coffee. */
std::string const cmd_param("I want a double espresso with cream!");
ExecuteCommand::Request_1_1 req;
req.command = 0xCAFE;
std::copy_n(cmd_param.begin(),
std::min(cmd_param.length(), req.parameter.capacity()),
req.parameter.begin());


if (!srv_client->request(27 /* remote node id */, req))
Serial.println("Coffee request failed.");
Serial.println("setup finished");
}

void loop()
Expand All @@ -99,6 +91,26 @@ void loop()
CriticalSection crit_sec;
node_hdl.spinSome();
}

/* Publish the request once/second */
static unsigned long prev = 0;
unsigned long const now = millis();
if(now - prev > 1000)
{
prev = now;

/* Request some coffee. */
Serial.println("Requesting some coffee");
std::string const cmd_param("I want a double espresso with cream!");
ExecuteCommand::Request_1_1 req;
req.command = 0xCAFE;
std::copy(cmd_param.begin(), cmd_param.end(), std::back_inserter(req.parameter));


if (!srv_client->request(27 /* remote node id */, req)) {
Serial.println("Coffee request failed.");
}
}
}

/**************************************************************************************
Expand Down
20 changes: 19 additions & 1 deletion examples/OpenCyphal-Service-Server/OpenCyphal-Service-Server.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ArduinoMCP2515 mcp2515([]() { digitalWrite(MKRCAN_MCP2515_CS_PIN, LOW); },
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); });
Node node_hdl(node_heap.data(), node_heap.size(), micros, [] (CanardFrame const & frame) { return mcp2515.transmit(frame); }, 27);

ServiceServer execute_command_srv = node_hdl.create_service_server<ExecuteCommand::Request_1_1, ExecuteCommand::Response_1_1>(
2*1000*1000UL,
Expand All @@ -63,9 +63,12 @@ void setup()
{
Serial.begin(9600);
while(!Serial) { }
delay(1000);
Serial.println("|---- OpenCyphal Service Server Example ----|");

/* Setup SPI access */
SPI.begin();

pinMode(MKRCAN_MCP2515_CS_PIN, OUTPUT);
digitalWrite(MKRCAN_MCP2515_CS_PIN, HIGH);

Expand All @@ -77,6 +80,8 @@ void setup()
mcp2515.begin();
mcp2515.setBitRate(CanBitRate::BR_250kBPS_16MHZ);
mcp2515.setNormalMode();

Serial.println("setup finished");
}

void loop()
Expand All @@ -101,6 +106,19 @@ void onReceiveBufferFull(CanardFrame const & frame)
ExecuteCommand::Response_1_1 onExecuteCommand_1_1_Request_Received(ExecuteCommand::Request_1_1 const & req)
{
ExecuteCommand::Response_1_1 rsp;
Serial.println("Coffee has been requested");

std::string parameter;

std::copy(
req.parameter.begin(),
req.parameter.end(),
std::back_inserter(parameter));

for (uint8_t i = 0; i < parameter.size(); i++) {
Serial.print(parameter[i]);
}
Serial.println("");

if (req.command == 0xCAFE)
rsp.status = ExecuteCommand::Response_1_1::STATUS_SUCCESS;
Expand Down