Skip to content

Commit

Permalink
RDSK-3562: Add localization mode detection and new map update workflo…
Browse files Browse the repository at this point in the history
  • Loading branch information
pstrutz committed Jul 11, 2023
1 parent c6b58f4 commit 16a78d9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
10 changes: 5 additions & 5 deletions web/frontend/package-lock.json

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

4 changes: 2 additions & 2 deletions web/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@improbable-eng/grpc-web": "~0.15.*",
"@viamrobotics/prime": "~0.2.*",
"@viamrobotics/rpc": "~0.1.*",
"@viamrobotics/sdk": "0.2.0-pre.1",
"@viamrobotics/sdk": "0.2.3-rc.0",
"google-protobuf": "~3.*.*",
"tailwindcss": "~3.3.*",
"three": "~0.152.*",
Expand All @@ -35,7 +35,7 @@
"@typescript-eslint/parser": "^5.60.1",
"@viamrobotics/prime": "0.2.19",
"@viamrobotics/rpc": "0.1.37",
"@viamrobotics/sdk": "0.2.2",
"@viamrobotics/sdk": "0.2.3-rc.0",
"@viamrobotics/typescript-config": "^0.0.3",
"cypress": "12.16.0",
"eslint": "8.43.0",
Expand Down
4 changes: 2 additions & 2 deletions web/frontend/src/api/motion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type Client, commonApi, motionApi, robotApi } from '@viamrobotics/sdk';
import { Struct } from 'google-protobuf/google/protobuf/struct_pb';
import { getSLAMPosition } from './slam';
import { getPosition } from './slam';
import { rcLogConditionally } from '@/lib/log';

export const moveOnMap = async (robotClient: Client, name: string, componentName: string, x: number, y: number) => {
Expand All @@ -12,7 +12,7 @@ export const moveOnMap = async (robotClient: Client, name: string, componentName
request.setName('builtin');

// set pose in frame
const lastPose = await getSLAMPosition(robotClient, name);
const lastPose = await getPosition(robotClient, name);

const destination = new commonApi.Pose();
destination.setX(x * 1000);
Expand Down
18 changes: 17 additions & 1 deletion web/frontend/src/api/slam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const getPointCloudMap = (robotClient: Client, name: string) => {
});
};

export const getSLAMPosition = async (robotClient: Client, name: string) => {
export const getPosition = async (robotClient: Client, name: string) => {
const request = new slamApi.GetPositionRequest();
request.setName(name);

Expand All @@ -74,3 +74,19 @@ export const getSLAMPosition = async (robotClient: Client, name: string) => {

return response?.getPose();
};

export const getLatestMapInfo = async (robotClient: Client, name: string) => {
const request = new slamApi.GetLatestMapInfoRequest();
request.setName(name);
const response = await new Promise<slamApi.GetLatestMapInfoResponse | null >((resolve, reject) => {
robotClient.slamService.getLatestMapInfo(request, (error, res) => {
if (error) {
reject(error);
} else {
resolve(res);
}
});
});
return response?.getLastMapUpdate();
};

48 changes: 39 additions & 9 deletions web/frontend/src/components/slam/index.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<script lang="ts">
/* eslint-disable require-atomic-updates */
import * as THREE from 'three';
import { commonApi, type ServiceError } from '@viamrobotics/sdk';
import { copyToClipboard } from '@/lib/copy-to-clipboard';
import { filterSubtype } from '@/lib/resource';
import { getPointCloudMap, getSLAMPosition } from '@/api/slam';
import { getPointCloudMap, getPosition, getLatestMapInfo } from '@/api/slam';
import { moveOnMap, stopMoveOnMap } from '@/api/motion';
import { notify } from '@viamrobotics/prime';
import { setAsyncInterval } from '@/lib/schedule';
Expand All @@ -13,7 +14,7 @@ import Collapse from '@/lib/components/collapse.svelte';
import PCD from '@/components/pcd/pcd-view.svelte';
import Slam2dRenderer from './2d-renderer.svelte';
import { useRobotClient, useDisconnect } from '@/hooks/robot-client';
import { Timestamp } from 'google-protobuf/google/protobuf/timestamp_pb';
export let name: string;
const { robotClient, operations } = useRobotClient();
Expand All @@ -29,6 +30,9 @@ let refresh2dRate = 'manual';
let refresh3dRate = 'manual';
let pointcloud: Uint8Array | undefined;
let pose: commonApi.Pose | undefined;
let lastTimestamp = new Timestamp();
let show2d = false;
let show3d = false;
let showAxes = true;
Expand All @@ -50,11 +54,24 @@ const deleteDestinationMarker = () => {
};
const refresh2d = async () => {
try {
const [map, nextPose] = await Promise.all([
getPointCloudMap($robotClient, name),
getSLAMPosition($robotClient, name),
]);
const mapTimestamp = await getLatestMapInfo($robotClient, name);
let nextPose;
/*
* The map timestamp is compared to the last timestamp
* to see if a change has been made to the pointcloud map.
* A new call to getPointCloudMap is made if an update has occured.
*/
if (mapTimestamp?.getSeconds() === lastTimestamp.getSeconds()) {
nextPose = await getPosition($robotClient, name);
} else {
[pointcloud, nextPose] = await Promise.all([
getPointCloudMap($robotClient, name),
getPosition($robotClient, name),
]);
}
/*
* The pose is returned in millimeters, but we need
Expand All @@ -63,9 +80,10 @@ const refresh2d = async () => {
nextPose?.setX(nextPose.getX() / 1000);
nextPose?.setY(nextPose.getY() / 1000);
nextPose?.setZ(nextPose.getZ() / 1000);
pointcloud = map;
pose = nextPose;
if (mapTimestamp) {
lastTimestamp = mapTimestamp;
}
} catch (error) {
refreshErrorMessage2d = error !== null && typeof error === 'object' && 'message' in error
? `${refreshErrorMessage} ${error.message}`
Expand All @@ -75,7 +93,19 @@ const refresh2d = async () => {
const refresh3d = async () => {
try {
pointcloud = await getPointCloudMap($robotClient, name);
const mapTimestamp = await getLatestMapInfo($robotClient, name);
/*
* The map timestamp is compared to the last timestamp
* to see if a change has been made to the pointcloud map.
* A new call to getPointCloudMap is made if an update has occured.
*/
if (mapTimestamp?.getSeconds() !== lastTimestamp.getSeconds()) {
pointcloud = await getPointCloudMap($robotClient, name);
}
if (mapTimestamp) {
lastTimestamp = mapTimestamp;
}
} catch (error) {
refreshErrorMessage3d = error !== null && typeof error === 'object' && 'message' in error
? `${refreshErrorMessage} ${error.message}`
Expand Down

0 comments on commit 16a78d9

Please sign in to comment.