Skip to content

Commit

Permalink
RSDK-1825 - add slam wrapper (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
purplenicole730 authored Aug 1, 2023
1 parent b784dc4 commit 00d36f4
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@viamrobotics/sdk",
"version": "0.2.5-next.0",
"version": "0.2.5-next.1",
"description": "",
"main": "./dist/main.umd.js",
"module": "./dist/main.es.js",
Expand Down
21 changes: 2 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,29 +325,12 @@ export { type Servo, ServoClient } from './components/servo';
*
* Generated with https://github.com/improbable-eng/grpc-web
*
* @example
*
* ```ts
* import { grpc } from '@improbable-eng/grpc-web';
*
* const client = {}; // replace with a connected robot client
*
* const request = new slamApi.GetPositionRequest();
* request.setName('myslam');
*
* client.slamService.getPosition(
* request,
* new grpc.Metadata(),
* (error, response) => {
* // do something with error or response
* }
* );
* ```
*
* @deprecated Use {@link SlamClient} instead.
* @alpha
* @group Raw Protobufs
*/
export { default as slamApi } from './gen/service/slam/v1/slam_pb';
export { type GetPositionResponse, SlamClient } from './services/slam';

/**
* Raw Protobuf interfaces for a Vision service.
Expand Down
2 changes: 1 addition & 1 deletion src/services/navigation/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface Navigation extends Resource {
/**
* Set the mode the robot is operating in.
*
* @param mode -
* @param mode - The mode for the service to operate in.
*/
setMode: (mode: ModeMap[keyof ModeMap], extra?: StructType) => Promise<void>;

Expand Down
3 changes: 3 additions & 0 deletions src/services/slam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type { Slam } from './slam/slam';
export type { GetPositionResponse } from './slam/types';
export { SlamClient } from './slam/client';
100 changes: 100 additions & 0 deletions src/services/slam/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import pb from '../../gen/service/slam/v1/slam_pb';
import { SLAMServiceClient } from '../../gen/service/slam/v1/slam_pb_service';
import { RobotClient } from '../../robot';
import type { Options, StructType } from '../../types';
import { doCommandFromClient, promisify } from '../../utils';
import type { Slam } from './slam';

/**
* A gRPC-web client for a SLAM service.
*
* @group Clients
*/
export class SlamClient implements Slam {
private client: SLAMServiceClient;
private readonly name: string;
private readonly options: Options;

constructor(client: RobotClient, name: string, options: Options = {}) {
this.client = client.createServiceClient(SLAMServiceClient);
this.name = name;
this.options = options;
}

private get service() {
return this.client;
}

async getPosition() {
const { service } = this;

const request = new pb.GetPositionRequest();
request.setName(this.name);

this.options.requestLogger?.(request);

const response = await promisify<
pb.GetPositionRequest,
pb.GetPositionResponse
>(service.getPosition.bind(service), request);

return response.toObject();
}

async getPointCloudMap() {
const { service } = this;

const request = new pb.GetPointCloudMapRequest();
request.setName(this.name);

this.options.requestLogger?.(request);

const response = await promisify<
pb.GetPointCloudMapRequest,
pb.GetPointCloudMapResponse
>(service.getPointCloudMap.bind(service), request);

return response.getPointCloudPcdChunk_asU8();
}

async getInternalState() {
const { service } = this;

const request = new pb.GetInternalStateRequest();
request.setName(this.name);

this.options.requestLogger?.(request);

const response = await promisify<
pb.GetInternalStateRequest,
pb.GetInternalStateResponse
>(service.getInternalState.bind(service), request);

return response.getInternalStateChunk_asU8();
}

async getLatestMapInfo() {
const { service } = this;

const request = new pb.GetLatestMapInfoRequest();
request.setName(this.name);

this.options.requestLogger?.(request);

const response = await promisify<
pb.GetLatestMapInfoRequest,
pb.GetLatestMapInfoResponse
>(service.getLatestMapInfo.bind(service), request);

const timestamp = response.getLastMapUpdate();
if (!timestamp) {
throw new Error('no map update');
}
return new Date(timestamp.getSeconds() * 1e3 + timestamp.getNanos() / 1e6);
}

async doCommand(command: StructType): Promise<StructType> {
const { service } = this;
return doCommandFromClient(service, this.name, command, this.options);
}
}
26 changes: 26 additions & 0 deletions src/services/slam/slam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Resource } from '../../types';
import type { GetPositionResponse } from './types';

/**
* A service that allows your robot to create a map of its surroundings and find
* its location within that map.
*/
export interface Slam extends Resource {
/**
* Get the current position of the specified source component in the point
* cloud SLAM map.
*/
getPosition: () => Promise<GetPositionResponse>;

/** Get the point cloud SLAM map. */
getPointCloudMap: () => Promise<Uint8Array>;

/**
* Get the internal state of the SLAM algorithm required to continue
* mapping/localization.
*/
getInternalState: () => Promise<Uint8Array>;

/** Get the timestamp of the last update to the point cloud SLAM map. */
getLatestMapInfo: () => Promise<Date>;
}
3 changes: 3 additions & 0 deletions src/services/slam/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pb from '../../gen/service/slam/v1/slam_pb';

export type GetPositionResponse = pb.GetPositionResponse.AsObject;

0 comments on commit 00d36f4

Please sign in to comment.