Skip to content

Commit 2c86432

Browse files
RSDK-3657: TS SDK Gantry API changes (#128)
1 parent 2f66155 commit 2c86432

File tree

6 files changed

+190
-31
lines changed

6 files changed

+190
-31
lines changed

buf.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ deps:
99
- remote: buf.build
1010
owner: viamrobotics
1111
repository: api
12-
commit: 77bcd1d0f47a437e8c58639004a13451
13-
digest: shake256:9d311393e8b744ced31428355d42d3a997a92ee5cc9ecfa10cb30e656ad265986c7d758ecc3ad5ed6de1ebf2d1ae514a1fca9dee128244c4420a56b5c5066a5e
12+
commit: 1ea16dd720f04cefa61f380ccc375d6f
13+
digest: shake256:c51fd611a013929061384c1630d5e421a1083a62b6d5e4256c0a50b8abd4e8b91a944495918bbaa165d32398bbd6990a0c9b71f5275df9375386f2b050e67a6f
1414
- remote: buf.build
1515
owner: viamrobotics
1616
repository: goutils

codesamples/apis.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
"func": "getProperties",
3232
"args": []
3333
},
34+
"gantry": {
35+
"client": "GantryClient",
36+
"func": "getLengths",
37+
"args": []
38+
},
3439
"sensor": {
3540
"client": "SensorClient",
3641
"func": "getReadings",

src/components/gantry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type { Gantry } from './gantry/gantry';
2+
export { GantryClient } from './gantry/client';

src/components/gantry/client.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { Struct } from 'google-protobuf/google/protobuf/struct_pb';
2+
import type { RobotClient } from '../../robot';
3+
import pb from '../../gen/component/gantry/v1/gantry_pb';
4+
import { GantryServiceClient } from '../../gen/component/gantry/v1/gantry_pb_service';
5+
import type { Options, StructType } from '../../types';
6+
import { doCommandFromClient, promisify } from '../../utils';
7+
import type { Gantry } from './gantry';
8+
9+
/**
10+
* A gRPC-web client for the Gantry component.
11+
*
12+
* @group Clients
13+
*/
14+
export class GantryClient implements Gantry {
15+
private client: GantryServiceClient;
16+
private readonly name: string;
17+
private readonly options: Options;
18+
19+
constructor(client: RobotClient, name: string, options: Options = {}) {
20+
this.client = client.createServiceClient(GantryServiceClient);
21+
this.name = name;
22+
this.options = options;
23+
}
24+
25+
private get GantryService() {
26+
return this.client;
27+
}
28+
29+
async getPosition(extra = {}) {
30+
const gantryService = this.GantryService;
31+
const request = new pb.GetPositionRequest();
32+
request.setName(this.name);
33+
request.setExtra(Struct.fromJavaScript(extra));
34+
35+
this.options.requestLogger?.(request);
36+
37+
const response = await promisify<
38+
pb.GetPositionRequest,
39+
pb.GetPositionResponse
40+
>(gantryService.getPosition.bind(gantryService), request);
41+
42+
return response.getPositionsMmList();
43+
}
44+
45+
async moveToPosition(
46+
positionsMm: number[],
47+
speedsMmPerSec: number[],
48+
extra = {}
49+
) {
50+
const gantryService = this.GantryService;
51+
52+
const request = new pb.MoveToPositionRequest();
53+
request.setName(this.name);
54+
request.setPositionsMmList(positionsMm);
55+
request.setSpeedsMmPerSecList(speedsMmPerSec);
56+
request.setExtra(Struct.fromJavaScript(extra));
57+
58+
this.options.requestLogger?.(request);
59+
60+
await promisify<pb.MoveToPositionRequest, pb.MoveToPositionResponse>(
61+
gantryService.moveToPosition.bind(gantryService),
62+
request
63+
);
64+
}
65+
66+
async home(extra = {}) {
67+
const gantryService = this.GantryService;
68+
const request = new pb.HomeRequest();
69+
request.setName(this.name);
70+
request.setExtra(Struct.fromJavaScript(extra));
71+
72+
this.options.requestLogger?.(request);
73+
74+
const response = await promisify<pb.HomeRequest, pb.HomeResponse>(
75+
gantryService.home.bind(gantryService),
76+
request
77+
);
78+
79+
return response.getHomed();
80+
}
81+
82+
async getLengths(extra = {}) {
83+
const gantryService = this.GantryService;
84+
const request = new pb.GetLengthsRequest();
85+
request.setName(this.name);
86+
request.setExtra(Struct.fromJavaScript(extra));
87+
88+
this.options.requestLogger?.(request);
89+
90+
const response = await promisify<
91+
pb.GetLengthsRequest,
92+
pb.GetLengthsResponse
93+
>(gantryService.getLengths.bind(gantryService), request);
94+
95+
return response.getLengthsMmList();
96+
}
97+
98+
async stop(extra = {}) {
99+
const gantryService = this.GantryService;
100+
const request = new pb.StopRequest();
101+
request.setName(this.name);
102+
request.setExtra(Struct.fromJavaScript(extra));
103+
104+
this.options.requestLogger?.(request);
105+
106+
await promisify<pb.StopRequest, pb.StopResponse>(
107+
gantryService.stop.bind(gantryService),
108+
request
109+
);
110+
}
111+
112+
async isMoving() {
113+
const gantryService = this.GantryService;
114+
const request = new pb.IsMovingRequest();
115+
request.setName(this.name);
116+
117+
this.options.requestLogger?.(request);
118+
119+
const response = await promisify<pb.IsMovingRequest, pb.IsMovingResponse>(
120+
gantryService.isMoving.bind(gantryService),
121+
request
122+
);
123+
return response.getIsMoving();
124+
}
125+
126+
async doCommand(command: StructType): Promise<StructType> {
127+
const gantryService = this.GantryService;
128+
return doCommandFromClient(gantryService, this.name, command, this.options);
129+
}
130+
}

src/components/gantry/gantry.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { Resource, StructType } from '../../types';
2+
3+
/** Represents a physical gantry that exists in three-dimensional space. */
4+
export interface Gantry extends Resource {
5+
/**
6+
* Move each axis of the gantry to the positionsMm at the speeds in
7+
* speedsMmPerSec
8+
*
9+
* @param positionsMm - The goal positions for each axis of the gantry.
10+
* @param speedsMmPerSec - The desired speed for each axis to move to the
11+
* respective position in positionsMm.
12+
*/
13+
moveToPosition: (
14+
positionsMm: number[],
15+
speedsMmPerSec: number[],
16+
extra?: StructType
17+
) => Promise<void>;
18+
19+
/** @returns The current position of each axis. */
20+
getPosition: (extra?: StructType) => Promise<number[]>;
21+
22+
/**
23+
* Runs the homing sequence to find the start and end positions of the gantry
24+
* axis.
25+
*
26+
* @returns A bool representing whether the gantry has run the homing sequence
27+
* successfully.
28+
*/
29+
home: (extra?: StructType) => Promise<boolean>;
30+
31+
/** @returns The lengths of the axes of the gantry in millimeters. */
32+
getLengths: (extra?: StructType) => Promise<number[]>;
33+
34+
/** Stops the motion of the gantry. */
35+
stop: (extra?: StructType) => Promise<void>;
36+
37+
/** Get if the gantry is currently moving. */
38+
isMoving: () => Promise<boolean>;
39+
}

src/main.ts

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ export {
9090
EncoderClient,
9191
} from './components/encoder';
9292

93+
/**
94+
* Raw Protobuf interfaces for a Gantry component.
95+
*
96+
* Generated with https://github.com/improbable-eng/grpc-web
97+
*
98+
* @deprecated Use {@link GantryClient} instead.
99+
* @alpha
100+
* @group Raw Protobufs
101+
*/
102+
export { default as gantryApi } from './gen/component/gantry/v1/gantry_pb';
103+
export { type Gantry, GantryClient } from './components/gantry';
104+
93105
/**
94106
* Raw Protobuf interfaces for a Motor component.
95107
*
@@ -171,35 +183,6 @@ export { default as sensorsApi } from './gen/service/sensors/v1/sensors_pb';
171183
export { default as streamApi } from './gen/proto/stream/v1/stream_pb';
172184
export { type Stream, StreamClient } from './extra/stream';
173185

174-
/**
175-
* Raw Protobuf interfaces for a Gantry component.
176-
*
177-
* Generated with https://github.com/improbable-eng/grpc-web
178-
*
179-
* @example
180-
*
181-
* ```ts
182-
* import { grpc } from '@improbable-eng/grpc-web';
183-
*
184-
* const client = {}; // replace with a connected robot client
185-
*
186-
* const request = new gantryApi.GetLengthsRequest();
187-
* request.setName('mygantry');
188-
*
189-
* client.gantryService.getLengths(
190-
* request,
191-
* new grpc.Metadata(),
192-
* (error, response) => {
193-
* // do something with error or response
194-
* }
195-
* );
196-
* ```
197-
*
198-
* @alpha
199-
* @group Raw Protobufs
200-
*/
201-
export { default as gantryApi } from './gen/component/gantry/v1/gantry_pb';
202-
203186
/**
204187
* Raw Protobuf interfaces for a Generic component.
205188
*

0 commit comments

Comments
 (0)