You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If your physical or virtual hardware is not [already supported](/operate/modules/configure-modules/) by an existing {{< glossary_tooltip term_id="module" text="module" >}}, you can create a new module to add support for it.
40
+
If your physical or virtual hardware is not supported by an existing registry {{< glossary_tooltip term_id="module" text="module" >}}, you can create a new module to add support for it.
41
41
42
42
{{% hiddencontent %}}
43
43
If you want to create a "custom module", this page provides instructions for creating one in Python and Go.
44
44
{{% /hiddencontent %}}
45
45
46
-
This page provides instructions for creating and uploading a module in Python or Go.
46
+
This page provides instructions for creating a module in Python or Go.
47
47
For C++ module examples, see the [C++ examples directory on GitHub](https://github.com/viamrobotics/viam-cpp-sdk/tree/main/src/viam/examples/).
48
48
If you want to create a module for use with a microcontroller, see [Modules for ESP32](/operate/modules/advanced/micro-module/).
49
49
50
50
**Example module:** With each step of thise guide, you have instruction for creating a {{< glossary_tooltip term_id="module" text="module" >}} which does two things:
51
51
52
-
1.Opens an image file from a configured path on your machine
52
+
1.Gets an image from a configured path on your machine
53
53
2. Returns a random number
54
54
55
55
## Prerequisites
@@ -67,7 +67,6 @@ Authenticate your CLI session with Viam using one of the following options:
67
67
{{% expand "A running machine connected to Viam." %}}
68
68
69
69
You can write a module without a machine, but to test your module you'll need a [machine](/operate/install/setup/).
70
-
Make sure to physically connect your sensor to your machine's computer to prepare your machine for testing.
71
70
72
71
{{% snippet "setup.md" %}}
73
72
@@ -76,6 +75,7 @@ Make sure to physically connect your sensor to your machine's computer to prepar
76
75
{{< expand "For Python developers: Use Python 3.11+" >}}
77
76
78
77
If you plan to write your module using Python, you need Python version 3.11 or newer installed on your computer to use the code generation tool in this guide.
78
+
79
79
You can check by running `python3 --version` or `python --version` in your terminal.
80
80
81
81
{{< /expand >}}
@@ -84,7 +84,7 @@ You can check by running `python3 --version` or `python --version` in your termi
84
84
85
85
While not required, we recommend starting by writing a test script to check that you can connect to and control your hardware from your computer, perhaps using the manufacturer's API or other low-level code.
86
86
87
-
**Example module:** For the example module the test script will test how to open an image in the same folder and how to print a random number.
87
+
**Example module:** For the example module the test script will open an image in the same folder and print a random number.
@@ -145,11 +145,6 @@ The module takes the functionality of the script and maps it to a standardized A
145
145
146
146
Review the available [component APIs](/dev/reference/apis/#component-apis) and choose the one whose methods map most closely to the functionality you need.
147
147
148
-
For example, if you just need to get readings or other data and don't need any other endpoints, you could use the [sensor API](/dev/reference/apis/components/sensor/), which contains only the `GetReadings` method (as well as the methods that all Viam resources implement: `Reconfigure`, `DoCommand`, `GetResourceName`, and `Close`).
149
-
150
-
You do not need to fully implement all the methods of an API.
151
-
For example, if you want to use the [camera API](/dev/reference/apis/components/camera/) because you want to return images, but your camera does not get point cloud data, you can implement the `GetImage` method but for the `GetPointCloud` method you can return an "unimplemented" error.
152
-
153
148
If you need a method that is not in your chosen API, you can use the flexible `DoCommand` (which is built into all component APIs) to create custom commands.
154
149
See [Run control logic](/docs/operate/modules/support-hardware/) for more information.
155
150
@@ -159,8 +154,12 @@ You need a way to return an image and you need a way to return a number.
159
154
If you look at the [camera API](/dev/reference/apis/components/camera/), you can see the `GetImage` method, which returns an image.
160
155
That will work for the image.
161
156
157
+
The camera API also has a few other methods.
158
+
You do not need to fully implement all the methods of an API.
159
+
For example, this camera does not use point cloud data, so for methods like `GetPointCloud` it will return an "unimplemented" error.
160
+
162
161
The [sensor API](/dev/reference/apis/components/sensor/) includes the `GetReadings` method.
163
-
You can return a number with that
162
+
You can return the random number with that.
164
163
165
164
Note that the camera API can't return a number and the sensor API can't return an image.
166
165
Each model can implement only one API, but your module can contain multiple modular resources.
@@ -189,10 +188,10 @@ viam module generate
189
188
| Module name | Choose a name that describes the set of {{< glossary_tooltip term_id="resource" text="resources" >}} it supports. |
190
189
| Language | Choose the programming language for the module. The CLI supports `Python` and `Golang`. |
191
190
| Visibility | Choose `Private` to share only with your organization, or `Public` to share publicly with all organizations. If you are testing, choose `Private`. |
192
-
| Namespace/Organization ID | Navigate to your organization settings through the menu in upper right corner of the page. Find the **Public namespace** (or create one if you haven't already) and copy that string. If you use the organization ID, you must still create a public namespace first. |
193
-
| Resource to add to the module (API) | The [component API](/dev/reference/apis/#component-apis) your module will implement. See [How to design your module](./#how-to-design-your-module) for more information. |
191
+
| Namespace/Organization ID | Navigate to your organization settings through the menu in upper right corner of the page. Find the **Public namespace** (or create one if you haven't already) and copy that string. If you use the organization ID, you must still create a public namespace first if you wish to share the module publicly. |
192
+
| Resource to add to the module (API) | The [component API](/dev/reference/apis/#component-apis) your module will implement. See [Choose an API](#choose-an-api) for more information. |
194
193
| Model name | Name your component model based on what it supports, for example, if it supports a model of ultrasonic sensor called "XYZ Sensor 1234" you could call your model `xyz_1234` or similar. Must be all-lowercase and use only alphanumeric characters (`a-z` and `0-9`), hyphens (`-`), and underscores (`_`). |
195
-
| Enable cloud build | If you select `Yes` (recommended) and push the generated files (including the <file>.github</file> folder) and create a release of the format `vX.X.X`, the module will build and upload to the Viam registry and be available for all Viam-supported architectures without you needing to build for each architecture. `Yes` also makes it easier to [upload](/operate/modules/deploy-module/) using PyInstaller by creating a build entrypoint script. You can select `No` if you want to always build the module yourself before uploading it. Fort more information see [Update and manage modules](/operate/modules/advanced/manage-modules/). |
194
+
| Enable cloud build | If you select `Yes` (recommended) and push the generated files (including the <file>.github</file> folder) and create a release of the format `X.X.X`, the module will build for [all architectures specified in the meta.json build file](/operate/modules/advanced/metajson/). You can select `No` if you want to always build the module yourself before uploading it. For more information see [Update and manage modules](/operate/modules/advanced/manage-modules/). |
196
195
| Register module | Select `Yes` unless you are creating a local-only module for testing purposes and do not intend to upload it. Registering a module makes its name and metadata appear in the registry; uploading the actual code that powers the module is a separate step. If you decline to register the module at this point, you can run [`viam module create`](/dev/tools/cli/#module) to register it later. |
197
196
198
197
{{% /expand %}}
@@ -250,6 +249,15 @@ hello-world/
250
249
└── setup.sh
251
250
```
252
251
252
+
If you want to understand the module structure, here's what each file does:
253
+
254
+
-**<FILE>README.md</FILE>**: Documentation template that gets uploaded to the registry when you upload the module.
255
+
-**<FILE>meta.json</FILE>**: Module metadata that gets uploaded to the registry when you upload the module.
256
+
-**<FILE>main.py</FILE>** and **<FILE>hello_camera.py</FILE>**: Core code that registers the module and resource and provides the model implementation.
257
+
-**<FILE>setup.sh</FILE>** and **<FILE>requirements.txt</FILE>**: Setup script that creates a virtual environment and installs the dependencies listed in <FILE>requirements.txt</FILE>.
258
+
-**<FILE>build.sh</FILE>**: Build script that packages the code for upload.
259
+
-**<FILE>run.sh</FILE>**: Script that runs <FILE>setup.sh</FILE> and then executes the module from <FILE>main.py</FILE>.
260
+
253
261
{{% /tab %}}
254
262
{{% tab name="Go" %}}
255
263
@@ -267,6 +275,12 @@ hello-world/
267
275
└── meta.json
268
276
```
269
277
278
+
If you want to understand the module structure, here's what each file does:
279
+
280
+
-**<FILE>README.md</FILE>**: Documentation template that gets uploaded to the registry when you upload the module.
281
+
-**<FILE>meta.json</FILE>**: Module metadata that gets uploaded to the registry when you upload the module.
282
+
- TODO
283
+
270
284
{{% /tab %}}
271
285
{{< /tabs >}}
272
286
@@ -456,7 +470,6 @@ Save the file.
456
470
{{% /tab %}}
457
471
{{< /tabs >}}
458
472
459
-
460
473
You can now delete the temporary <file>hello-world/hello-world</file> folder and all its contents.
461
474
462
475
### Implement the components
@@ -472,11 +485,11 @@ When logged in you can also download the module's source code to inspect it.
472
485
473
486
Generally you will add your custom logic in these files:
474
487
475
-
| File | Description |
476
-
| ---- | ----------- |
477
-
| <file>/src/models/<model-name>.py</file> | Set up the configuration options for the model and implement the API methods for the model. |
478
-
|`setup.sh` and `run.sh`| Add any logic for installing or running other software for your module. |
479
-
|`requirements.txt`| Add any python packages that are required for your module. They will be installed by `setup.sh`. |
0 commit comments