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

Update docs for modular driver example consolidation #239

Merged
merged 10 commits into from
Jun 25, 2024
45 changes: 25 additions & 20 deletions examples/modular-drivers/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Viam Micro-RDK ESP32 Sensor Modules Example
# Viam Micro-RDK Modular Driver Examples

## (In)stability Notice

> **Warning** The Viam Micro-RDK is currently in beta.

## Overview

This project in this repository was generated from the [Micro-RDK
Module
Template](https://github.com/viamrobotics/micro-rdk/tree/main/templates/module),
This project in this repository was generated from the
[Micro-RDK Module Template](/templates/module),
and demonstrates how to produce modular resources for the Micro-RDK.

## Usage
Expand Down Expand Up @@ -36,11 +35,12 @@ index 79fbc5c..949b5ab 100644
smol = "1.2"
futures-lite = "1.12.0"
micro-rdk = {version = "0.0.3", git = "https://github.com/viamrobotics/micro-rdk.git", features = ["esp32"]}
+micro-rdk-esp32-sensor-examples = { git = "https://github.com/viam-labs/micro-rdk-esp32-sensor-examples" }
+micro-rdk-modular-driver-example = { git = "https://github.com/viamrobotics/micro-rdk/tree/main/examples/modular-drivers" }
```

Rebuild the project per the above Micro-RDK Development Setup
instructions and reflash the board.
instructions and reflash the board. We will be using the Wifi
RSSI Sensor and free heap sensor for this example.

### Instantiating the Sensors

Expand Down Expand Up @@ -94,7 +94,7 @@ can find this code on the `Code Sample` page for your robot):
## Implementation Walkthrough

This project was created by using the [Micro-RDK Module
Template](https://github.com/viamrobotics/micro-rdk/tree/main/templates/module)
Template](/templates/module)
and `cargo generate`:

``` shell
Expand All @@ -103,33 +103,39 @@ $ cargo generate --git https://github.com/viamrobotics/micro-rdk templates/modul
```

When prompted by the template, the project was named
`micro-rdk-esp32-sensor-examples` and `esp32` selected for the target
`micro-rdk-modular-driver-example` and `esp32` selected for the target
platform. The generated project has the form of a library crate, where
`src/lib.rs` defines an initially empty implementation of the
well-known Micro-RDK module entry point `register_models`:

https://github.com/viam-labs/micro-rdk-esp32-sensor-examples/blob/02d7c8e487a48ac7c8d527a5c7b750d6c2357a27/src/lib.rs#L3-L5
``` rust
use micro_rdk::common::registry::{ComponentRegistry, RegistryError};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⬆️ - This still says micro-rdk-esp32-sensor-examples.


pub fn register_models(_registry: &mut ComponentRegistry) -> Result<(), RegistryError> {
Ok(())
}
```

The generated project also includes a `package.metadata` section in
its `Cargo.toml` which identifies the library crate as being a
Micro-RDK module:

https://github.com/viam-labs/micro-rdk-esp32-sensor-examples/blob/02d7c8e487a48ac7c8d527a5c7b750d6c2357a27/Cargo.toml#L12-L13
https://github.com/viamrobotics/micro-rdk/blob/fbc1783258bfefc027fd25a8cc9a1b37f6ea0524/examples/modular-drivers/Cargo.toml#L18-L19

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be relative?

A subsequent commit adds definitions of the
[FreeHeapSensor](https://github.com/viam-labs/micro-rdk-esp32-sensor-examples/blob/9cc59d56cc35ab7cf0c471b613c5f3c2ab2ed95b/src/wifi_rssi_sensor.rs)
[FreeHeapSensor](src/free_heap_sensor.rs)
and
[WifiRSSISensor](https://github.com/viam-labs/micro-rdk-esp32-sensor-examples/blob/9cc59d56cc35ab7cf0c471b613c5f3c2ab2ed95b/src/free_heap_sensor.rs)
[WifiRSSISensor](src/wifi_rssi_sensor.rs)

That commit also introduces a crate-local `register_model` function
That commit also introduces a crate-local `register_models` function
for each sensor:

- `FreeHeapSensor`: https://github.com/viam-labs/micro-rdk-esp32-sensor-examples/blob/9cc59d56cc35ab7cf0c471b613c5f3c2ab2ed95b/src/free_heap_sensor.rs#L21-L25
- `WifiRSSISensor`: https://github.com/viam-labs/micro-rdk-esp32-sensor-examples/blob/9cc59d56cc35ab7cf0c471b613c5f3c2ab2ed95b/src/wifi_rssi_sensor.rs#L19-L23
- `FreeHeapSensor`: https://github.com/viamrobotics/micro-rdk/blob/fbc1783258bfefc027fd25a8cc9a1b37f6ea0524/examples/modular-drivers/src/free_heap_sensor.rs#L23-27
- `WifiRSSISensor`: https://github.com/viamrobotics/micro-rdk/blob/fbc1783258bfefc027fd25a8cc9a1b37f6ea0524/examples/modular-drivers/src/wifi_rssi_sensor.rs#L23C1-L27C2

Finally, the top level `register_models` entry point is updated to delegate to the `register_model` function for both sensors:
Finally, the top level `register_models` entry point is updated to delegate to the `register_models` function for both sensors:

https://github.com/viam-labs/micro-rdk-esp32-sensor-examples/blob/9cc59d56cc35ab7cf0c471b613c5f3c2ab2ed95b/src/lib.rs#L6-L10
https://github.com/viamrobotics/micro-rdk/blob/fbc1783258bfefc027fd25a8cc9a1b37f6ea0524/examples/modular-drivers/src/lib.rs#L10C1-L18C2

The Micro-RDK module is now ready to be used in a Micro-RDK project,
just by adding it as an ordinary dependency in the `dependencies`
Expand All @@ -138,6 +144,5 @@ section of the project's `Cargo.toml` file, as noted in the

## Related Reading

Please see
https://github.com/viam-labs/micro-rdk-modular-driver-examples for
another example of modular Micro-RDK sensors.
Please see the [moisture sensor](src/moisture_sensor.rs) and [water pump](src/water_pump.rs) for
more examples of modular Micro-RDK drivers.
8 changes: 4 additions & 4 deletions templates/module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ by this template will be automatically invoked at startup.

## Tutorial

Please see the [ESP32 Sensors
Example](https://github.com/viam-labs/micro-rdk-esp32-sensor-examples)
to see an example Micro-RDK module project. The
[README](https://github.com/viam-labs/micro-rdk-esp32-sensor-examples/blob/main/README.md)
Please see the [Modular Driver
Examples](/examples/modular-drivers)
to see some example Micro-RDK module projects. The
[README](/examples/modular-drivers/README.md)
provides a walkthrough of how to implement a module for the Micro-RDK.

## Caveats
Expand Down
Loading