Skip to content

Commit 1f6b967

Browse files
authored
DOCS-2782: Check code for motor quickstart (#3228)
1 parent 1e13fbf commit 1f6b967

File tree

2 files changed

+155
-11
lines changed

2 files changed

+155
-11
lines changed

docs/get-started/control-motor.md

Lines changed: 153 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ For the `fake` model, there are no required attributes.
8080
{{< /expand >}}
8181
{{< expand "Step 4: Configure a motor" >}}
8282

83-
[Add a motor component](/components/motor/) that supports the type of motor and motor driver you're using.
83+
Add a `motor` component that supports the type of motor and motor driver you're using.
8484
Look through the [**Supported Models**](/components/motor/#available-models) to determine the model of component to configure.
8585
For example, if you are using a standard DC motor (brushed or brushless) wired to a typical GPIO pin-controlled motor driver, configure a [`gpio` motor](/components/motor/gpio/):
8686

8787
![The CONFIGURE tab of the Viam app populated with a configured gpio motor.](/get-started/quickstarts/configure-motor.png)
8888

89-
Follow the motor driver manufacturer's data sheet to properly wire your motor driver to your board and to your motor.
89+
Follow the motor driver manufacturer's data sheet to wire your motor driver to your board and to your motor.
9090
Follow the [model's documentation](/components/motor/) to configure the attributes so that the computer can send signals to the motor.
9191

9292
If you do not have a physical motor, use the [`fake` motor model](/components/motor/fake/).
@@ -100,20 +100,20 @@ You can control your motor directly from the Viam app, using the mobile app, or
100100

101101
### Option 1: Control from the app
102102

103-
Navigate to your machine's **CONTROL** tab in the Viam app and use the **Power %** slider to set the motor's speed.
103+
Navigate to your machine's **CONTROL** tab in the Viam app and click on the motor panel.
104+
Then use the **Power %** slider to set the motor's speed.
104105
Use the **Backwards** and **Forwards** buttons to change the direction.
105106

106107
{{<gif webm_src="/get-started/quickstarts/motor-control.webm" mp4_src="/get-started/quickstarts/motor-control.mp4" alt="Using the slider, Backwards, and Forwards buttons on the Viam app to control the direction and speed of a configured motor" class="aligncenter" min-height="750px">}}
107108

108109
### Option 2: Control from the mobile app
109110

110-
You can use [the Viam mobile app](/fleet/#the-viam-mobile-app) to control your motor's speed and direction directly from your smart device.
111+
You can use [the Viam mobile app](/fleet/#the-viam-mobile-app) to control your motor's speed and direction directly from your smart phone.
111112

112113
Open the Viam mobile app and log in to your account.
113-
Select the location where your machine is assigned.
114-
Choose your machine from the list and use the mobile interface to adjust the motor settings.
114+
Select the location that your machine is in from the **Locations** tab.
115115

116-
Select the location that your machine is assigned to from the **Locations** tab.
116+
Choose your machine from the list and use the mobile interface to adjust the motor settings.
117117

118118
{{<gif webm_src="/get-started/quickstarts/mobile-app-motor-control.webm" mp4_src="/get-started/quickstarts/mobile-app-motor-control.mp4" alt="Using an example machine on the Viam mobile app to set the direction and speed of a configured motor using the slider on the user interface" max-height="50px" max-width="200px" class="HELLO aligncenter">}}
119119

@@ -133,7 +133,6 @@ import asyncio
133133
import time
134134

135135
from viam.robot.client import RobotClient
136-
from viam.rpc.dial import Credentials, DialOptions
137136
from viam.components.motor import Motor
138137

139138

@@ -183,8 +182,8 @@ import (
183182
"time"
184183

185184
"go.viam.com/rdk/logging"
185+
"go.viam.com/utils/rpc"
186186
"go.viam.com/rdk/robot/client"
187-
"go.viam.com/rdk/utils"
188187
"go.viam.com/rdk/components/motor")
189188

190189
func main() {
@@ -235,6 +234,151 @@ func main() {
235234
}
236235
```
237236

237+
{{% /tab %}}
238+
{{% tab name="TypeScript" %}}
239+
240+
<file>package.json</file>:
241+
242+
```json {class="line-numbers linkable-line-numbers"}
243+
{
244+
"name": "test-rover",
245+
"version": "1.0.0",
246+
"description": "",
247+
"main": "index.js",
248+
"scripts": {
249+
"start": "esbuild ./main.ts --bundle --outfile=static/main.js --servedir=static",
250+
"test": "echo \"Error: no test specified\" && exit 1"
251+
},
252+
"author": "Viam Docs Team",
253+
"license": "ISC",
254+
"devDependencies": {
255+
"esbuild": "*"
256+
},
257+
"dependencies": {
258+
"@viamrobotics/sdk": "*"
259+
}
260+
}
261+
```
262+
263+
<file>static/index.html</file>:
264+
265+
```html {class="line-numbers linkable-line-numbers"}
266+
<!doctype html>
267+
<html>
268+
<head>
269+
<title>Drive a Rover</title>
270+
<link rel="icon" href="favicon.ico" />
271+
</head>
272+
<body>
273+
<div id="main">
274+
<button id="main-button" disabled="true">Click me</button>
275+
</div>
276+
<script type="module" src="main.js"></script>
277+
</body>
278+
</html>
279+
```
280+
281+
<file>main.ts</file>:
282+
283+
```ts {class="line-numbers linkable-line-numbers"}
284+
// This code must be run in a browser environment.
285+
286+
import * as VIAM from "@viamrobotics/sdk";
287+
288+
// This function gets the button element
289+
function button() {
290+
return <HTMLButtonElement>document.getElementById("main-button");
291+
}
292+
293+
const main = async () => {
294+
const host = "ADDRESS_FROM_VIAM_APP";
295+
296+
const machine = await VIAM.createRobotClient({
297+
host,
298+
credential: {
299+
type: "api-key",
300+
// Replace "<API-KEY>" (including brackets) with your machine's API key
301+
payload: "<API-KEY>",
302+
},
303+
// Replace "<API-KEY-ID>" (including brackets) with your machine's API key ID
304+
authEntity: "<API-KEY-ID>",
305+
signalingAddress: "https://app.viam.com:443",
306+
});
307+
308+
button().onclick = async () => {
309+
// Instantiate the motor client
310+
// Replace with the name of a motor on your machine.
311+
const name = "motor-1";
312+
const motorClient = new VIAM.MotorClient(machine, name);
313+
314+
// Turn the motor at 35% power forwards
315+
await motorClient.setPower(0.35);
316+
// Let the motor spin for 4 seconds, then stop the motor
317+
const sleep = (ms: number) =>
318+
new Promise((resolve) => setTimeout(resolve, ms));
319+
await sleep(4000);
320+
await motorClient.stop();
321+
};
322+
button().disabled = false;
323+
};
324+
325+
main().catch((error) => {
326+
console.error("encountered an error:", error);
327+
});
328+
```
329+
330+
{{% /tab %}}
331+
{{% tab name="C++" %}}
332+
333+
```cpp
334+
#include <boost/optional.hpp>
335+
#include <string>
336+
#include <vector>
337+
#include <unistd.h>
338+
#include <viam/sdk/robot/client.hpp>
339+
#include <viam/sdk/components/motor.hpp>
340+
341+
using namespace viam::sdk;
342+
343+
int main() {
344+
std::string host("muddy-snow-main.7kp7y4p393.viam.cloud");
345+
DialOptions dial_opts;
346+
dial_opts.set_entity(std::string("<API-KEY-ID>"));
347+
/* Replace "<API-KEY-ID>" (including brackets) with your machine's api key id */
348+
Credentials credentials("api-key", "<API-KEY>");
349+
/* Replace "<API-KEY>" (including brackets) with your machine's api key */
350+
dial_opts.set_credentials(credentials);
351+
boost::optional<DialOptions> opts(dial_opts);
352+
Options options(0, opts);
353+
354+
auto machine = RobotClient::at_address(host, options);
355+
356+
std::cout << "Resources:\n";
357+
for (const Name& resource : machine->resource_names()) {
358+
std::cout << "\t" << resource << "\n";
359+
}
360+
361+
std::string motor_name("motor-1");
362+
363+
std::cout << "Getting motor: " << motor_name << std::endl;
364+
std::shared_ptr<Motor> motor;
365+
try {
366+
// Get the motor client
367+
motor = machine->resource_by_name<Motor>(motor_name);
368+
// Turn the motor at 35% power forwards
369+
motor->set_power(0.35);
370+
// Let the motor spin for 4 seconds
371+
sleep(3);
372+
// Stop the motor
373+
motor->stop();
374+
} catch (const std::exception& e) {
375+
std::cerr << "Failed to find " << motor_name << ". Exiting." << std::endl;
376+
throw;
377+
}
378+
return EXIT_SUCCESS;
379+
}
380+
```
381+
238382
{{% /tab %}}
239383
{{< /tabs >}}
240384

docs/get-started/drive-rover.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ If you have a different base name, update the name in your code.
440440
```ts {class="line-numbers linkable-line-numbers"}
441441
// This function moves a base component in a square.
442442
async function moveInSquare(client: VIAM.RobotClient) {
443-
// Replace with the name of a motor on your machine.
443+
// Replace with the name of the base on your machine.
444444
const name = "viam_base";
445445
const baseClient = new VIAM.BaseClient(client, name);
446446

@@ -932,7 +932,7 @@ import * as VIAM from "@viamrobotics/sdk";
932932

933933
// This function moves a base component in a square.
934934
async function moveInSquare(client: VIAM.RobotClient) {
935-
// Replace with the name of a motor on your machine.
935+
// Replace with the name of the base on your machine.
936936
const name = "viam_base";
937937
const baseClient = new VIAM.BaseClient(client, name);
938938

0 commit comments

Comments
 (0)