-
Notifications
You must be signed in to change notification settings - Fork 45
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
DOCS-514: Add C++ MR create examples #2277
DOCS-514: Add C++ MR create examples #2277
Conversation
Overall readability score: 56.56 (🟢 +0)
View detailed metrics🟢 - Shows an increase in readability
Averages:
View metric targets
|
docs/registry/create/_index.md
Outdated
#include <boost/log/trivial.hpp> | ||
|
||
// Within your main() program, before logging anything, accept command line arguments to control log severity: | ||
set_logger_severity_from_args(argc, argv); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Or is this only required when performing debug
-level logging, and not required otherwise?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this should only be necessary when you want debug
logging.
{{% alert title="Important" color="note" %}} | ||
|
||
You must define all functions belonging to a built-in resource subtype's API if defining a new model. | ||
Otherwise, the class will not instantiate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this true for C++, like it is for Python and Go?
If so, is the next line about throw
-ing a runtime_error
correct? (Found here)
If not, what notice should be here, if any?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good question. What do we mean by a built-in resource subtype's API
? More specifically, what is the source of truth for this?
If we look at the header files for a built-in resource for our source of truth, there are definitely APIs that don't need to be re-implemented (see, e.g., resource_registration()
and dynamic_api()
. What a user does need to define are all the pure virtual methods.
If we look at what the definition of, say, a camera
is based on camera.proto
, then yes, you need to define all the functions.
On the below point, throw
ing a runtime_error
is a totally valid way of avoiding putting in an actual implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I think we mean "camera (et al) API", which does indeed sounds like the hpp
file definitions, like camera.hpp
.
Ive clarified this blurb with your guidance, specifically around "implement this one but not that one."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me from a docs perspective - for the cpp files do we need a header file?
Ok - then @andf-viam let's also include the headers here. Based on https://github.com/viamrobotics/module-example-cpp/blob/main/src/wifi.hpp I think this shouldn't be much |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to submit review from a while ago 🤦 sorry!
docs/registry/create/_index.md
Outdated
{{% /tab %}} | ||
{{% tab name="C++" %}} | ||
|
||
To create a new resource model, you need to implement your model's **client** interface in a file called `my_modular_resource.cpp`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be misunderstanding but I don't think this is right. The client code represents a connection to the server to call on a particular instance of a resource. To define a model broadly I think we should be implementing the resource base class, e.g. src/viam/sdk/components/camera/*camera*.hpp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I too may be misunderstanding! I want to try to leave initial mention at cpp
because it's wholly possible to write a module using just one cpp
file.
But certainly many uses will also include hpp
files, so I've added a new sentence on line 76-77 below to mention. LMK if I'm close!
I think with the example code below now showing example hpp
code as well, this might be sufficient, but LMK!
docs/registry/create/_index.md
Outdated
|
||
To ensure the client interface you create returns the expected results, use the appropriate client interface defined in <file>components/\<resource-name\>/client.cpp</file> or <file>services/\<resource-name\>/client.cpp</file> in the [Viam C++ SDK](https://github.com/viamrobotics/viam-cpp-sdk/tree/main/src/viam/sdk) as a reference. | ||
|
||
For example, the `base` component client is defined in the [<file>components/base/client.cpp</file>](https://github.com/viamrobotics/viam-cpp-sdk/blob/main/src/viam/sdk/components/base/client.cpp) file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should link to the header files (*.hpp
) rather than implementation files (*.cpp
).
docs/registry/create/_index.md
Outdated
@@ -74,14 +87,21 @@ See [Valid APIs to implement in your model](#valid-apis-to-implement-in-your-mod | |||
|
|||
Find the subtype API as defined in the relevant <file>components/\<resource-name\>/\<resource-name>\.py</file> or <file>services/\<resource-name\>/<resource-name>.py</file> file in the [Python SDK](https://github.com/viamrobotics/viam-python-sdk). | |||
|
|||
For example, the `base` component subtype is defined in [<file>viam-python-sdk/src/viam/components/base/base.py</file>](https://github.com/viamrobotics/viam-python-sdk/blob/main/src/viam/components/base/base.py). | |||
For example, the `base` component subtype is defined in [<file>components/base/base.py</file>](https://github.com/viamrobotics/viam-python-sdk/blob/main/src/viam/components/base/base.py#L11). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The language component subtype
is being used for each of the languages here but I think we mean component API
?
The following example module registers a modular resource implementing Viam's built-in [Base API](/build/configure/components/base/#api) (`rdk:component:base`) as a new model, `"MyBase"`. | ||
|
||
<details> | ||
<summary>Click to view sample code for <file>my_base.cpp</file></summary> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to include a definition of MyBase
, either as a my_base.hpp
header file or defined at top of the .cpp
file. Right now your code sample is referencing data members of the class (e.g., left_
and right_
) that aren't actually defined anywhere that I can see in this doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a my_base.hpp
header file, based on this example!
docs/registry/create/_index.md
Outdated
Model mybase_model("acme", "demo", "mybase"); | ||
|
||
std::shared_ptr<ModelRegistration> mybase_mr = std::make_shared<ModelRegistration>( | ||
ResourceType("Base"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ResourceType
is no longer a thing as of this afternoon, so we'll want to remove this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gone!
docs/registry/create/_index.md
Outdated
#include <boost/log/trivial.hpp> | ||
|
||
// Within your main() program, before logging anything, accept command line arguments to control log severity: | ||
set_logger_severity_from_args(argc, argv); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this should only be necessary when you want debug
logging.
docs/registry/create/_index.md
Outdated
// To log a simple message at debug level: | ||
BOOST_LOG_TRIVIAL(debug) << "Starting module with debug level logging"; | ||
// To log a message that contains a variable at info level: | ||
BOOST_LOG_TRIVIAL(info) << "Detected the following variable value: " << my_var; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that not all types can be logged in this way, only those that implement <<
|
||
```cpp {class="line-numbers linkable-line-numbers"} | ||
#include <boost/log/trivial.hpp> | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per Benji, all std::cout
messages are silently forwarded up to the Viam app via gRPC. Happy to add any additional context here if you have any!
I hear this is changing in the near/medium term, just file a ticket when this changes and I'll update!
@stuqdog @benjirewis Hi folks! Thanks so much for your help, both of you! Here's an update based on Benji's complex module code (but with everything but the Thanks so much both of you for the help and guidance!! |
63695e0
to
355ac4b
Compare
{{% /tab %}} | ||
{{% tab name="C++" %}} | ||
|
||
To create a new resource model, start by referencing the corresponding built-in resource's implementation in the [Viam C++ SDK](https://github.com/viamrobotics/viam-cpp-sdk/tree/main/src/viam/sdk). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, lines 68-73 are basically the same as lines 101-103. This will change when I move on to DOCS - 1527 (point to base.X files not client.X files) after this, where one or the other section will be removed. Keeping here just to match other two languages in the interim. With DOCS - 1527 all three will use similar, shorter, non-replicated copy! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me! I haven't tested the sample code however, have you tested to make sure it works?
docs/registry/create/_index.md
Outdated
|
||
std::vector<std::string> MyBase::validate(ResourceConfig cfg) { | ||
// Custom validation can be done by specifying a validate function at the | ||
// time of resource registration (see complex/main.cpp) like this one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// time of resource registration (see complex/main.cpp) like this one. | |
// time of resource registration (see main.cpp) like this one. |
docs/registry/create/_index.md
Outdated
</details> | ||
<br> | ||
|
||
Additional example modules are available in the [C++ SDK GitHub repository](https://github.com/viamrobotics/viam-cpp-sdk/tree/main/src/viam/examples/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional example modules are available in the [C++ SDK GitHub repository](https://github.com/viamrobotics/viam-cpp-sdk/tree/main/src/viam/examples/). | |
Additional example modules are available in the [C++ SDK GitHub repository](https://github.com/viamrobotics/viam-cpp-sdk/tree/main/src/viam/examples/modules). |
docs/registry/create/_index.md
Outdated
|
||
cd $(dirname $0) | ||
# get bundled .so files from this directory | ||
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH-}:$PWD |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you find this line to be necessary for building? I don't really know what it does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, we're not building any .so
s for this example, I'll remove this!
(tested the compiled module using a run.sh
without this line, works as expected 👍 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah did you want to remove this export
and the comment above?
docs/registry/create/_index.md
Outdated
{{% /tab %}} | ||
{{% tab name="C++" %}} | ||
|
||
Messages sent to `std::cout` in your C++ code are automatically sent to the Viam app over gRPC when a network connection is available. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not exactly true, and apologies for my probably misleading explanation. std::cout
messages certainly go to STDOUT. The RDK gathers all the STDOUT output from the process and logs it (no gRPC involved). We will soon have a custom C++ logger that logs over gRPC back to the parent when possible and logs to STDOUT otherwise. But, std::cout
messages will never be sent back over gRPC, only messages logged through that not-yet-existing logger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Removed mention of gRPC
👍 Happy to circle back to this later and update when you make the upcoming custom logger change!
We technical writers are actually somewhat discouraged from documenting the future; something about leaving room for science fiction writers. 🤷 😂
It do! Registered component from module
|
@stuqdog @benjirewis Thanks both for the help and feedback! Benji, back to you for a follow-up round following your comments, thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! LGTM % potentially removing that export
.
docs/registry/create/_index.md
Outdated
|
||
cd $(dirname $0) | ||
# get bundled .so files from this directory | ||
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH-}:$PWD |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah did you want to remove this export
and the comment above?
@benjirewis Thanks! Yep, removed from test code but forgot to remove here. Thanks for catching! |
You can view a rendered version of the docs from this PR at https://docs-test.viam.dev/2277 |
Add C++ example code to Create docs for MR.
module-example-cpp
repo.rdk:service:base
, which should berdk:component:base
instead, per Rand.rdk:components:*
elsewhere in the docs, these should use singularcomponent
.CODE SAMPLES:
QUESTIONS:
NOTES: