Skip to content

Commit

Permalink
add getreadings to movement and power sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
martha-johnston committed Nov 3, 2023
1 parent 369f33f commit e250b62
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 74 deletions.
4 changes: 2 additions & 2 deletions buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ deps:
- remote: buf.build
owner: viamrobotics
repository: api
commit: 9196e89cb02f40d6b38344d637fdb3fc
digest: shake256:45153f9922a876347f1b3cf7db9d35a76ff045e5a72b83fc7d6e89d1e899ff53b2d3930c36f7ce8aac704fb57f60dd9cbdba61224c8df51b1044a1ff9882ba15
commit: db4372e839fc4ddab817a38a397686fb
digest: shake256:7983589dddbde5534f0f04a4c43e1cb94f185b11f1ff6d0fe8ba50fa5fdb22711a60c009b5d428b20fd251b497e2259605a065556013a4fb9daa1f2ce6b10168
- remote: buf.build
owner: viamrobotics
repository: goutils
Expand Down
44 changes: 21 additions & 23 deletions src/components/movement-sensor/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { MovementSensorServiceClient } from '../../gen/component/movementsensor/
import type { Options, StructType } from '../../types';
import pb from '../../gen/component/movementsensor/v1/movementsensor_pb';
import { promisify, doCommandFromClient } from '../../utils';
import type { MovementSensor, MovementSensorReadings } from './movement-sensor';
import {
GetReadingsRequest,
GetReadingsResponse,
} from '../../gen/common/v1/common_pb';
import type { MovementSensor } from './movement-sensor';


/**
* A gRPC-web client for the MovementSensor component.
Expand Down Expand Up @@ -195,30 +200,23 @@ export class MovementSensorClient implements MovementSensor {
}

async getReadings(extra = {}) {
const readingFunctions = {
position: this.getPosition.bind(this),
linearVelocity: this.getLinearVelocity.bind(this),
angularVelocity: this.getAngularVelocity.bind(this),
linearAcceleration: this.getLinearAcceleration.bind(this),
compassHeading: this.getCompassHeading.bind(this),
orientation: this.getOrientation.bind(this),
};

const tasks = Object.entries(readingFunctions).flatMap(([field, func]) =>
(async () => {
try {
return [[field, await func(extra)]];
} catch (error) {
if (!(error as Error).message.includes('Unimplemented')) {
throw error;
}
return [];
}
})()
const { movementsensorService } = this;
const request = new GetReadingsRequest();
request.setName(this.name);
request.setExtra(Struct.fromJavaScript(extra));

this.options.requestLogger?.(request);

const response = await promisify<GetReadingsRequest, GetReadingsResponse>(
movementsensorService.getReadings.bind(movementsensorService),
request
);
const entries = await Promise.all(tasks);

return Object.fromEntries(entries.flat()) as MovementSensorReadings;
const result: Record<string, unknown> = {};
for (const [key, value] of response.getReadingsMap().entries()) {
result[key] = value.toJavaScript();
}
return result;
}

async doCommand(command: StructType): Promise<StructType> {
Expand Down
19 changes: 5 additions & 14 deletions src/components/movement-sensor/movement-sensor.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import type { Orientation, StructType, Vector3 } from '../../types';
import type { Sensor } from '../sensor';
import type { Resource, Orientation, StructType, Vector3 } from '../../types';
import pb from '../../gen/component/movementsensor/v1/movementsensor_pb';

export type MovementSensorPosition = pb.GetPositionResponse.AsObject;
export type MovementSensorProperties = pb.GetPropertiesResponse.AsObject;

// https://github.com/microsoft/TypeScript/issues/15300
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type MovementSensorReadings = {
position?: MovementSensorPosition;
linearVelocity?: Vector3;
angularVelocity?: Vector3;
linearAcceleration?: Vector3;
compassHeading?: number;
orientation?: Orientation;
};

/**
* Represents any sensor that reports information about the robot's direction,
* position, and/or speed.
*/
export interface MovementSensor extends Sensor {
export interface MovementSensor extends Resource {
/** Get linear velocity across x/y/z axes */
getLinearVelocity(extra?: StructType): Promise<Vector3>;

Expand Down Expand Up @@ -50,4 +38,7 @@ export interface MovementSensor extends Sensor {

/** Get linear acceleration across x/y/z axes */
getLinearAcceleration(extra?: StructType): Promise<Vector3>;

/** Return the readings of a sensor. */
getReadings(extra?: StructType): Promise<Record<string, unknown>>;
}
1 change: 0 additions & 1 deletion src/components/movementsensor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export type {
MovementSensorPosition,
MovementSensorProperties,
MovementSensorReadings,
MovementSensor,
} from './movement-sensor/movement-sensor';
export { MovementSensorClient } from './movement-sensor/client';
46 changes: 22 additions & 24 deletions src/components/power-sensor/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { PowerSensorServiceClient } from '../../gen/component/powersensor/v1/pow
import type { Options, StructType } from '../../types';
import pb from '../../gen/component/powersensor/v1/powersensor_pb';
import { promisify, doCommandFromClient } from '../../utils';
import type { PowerSensor, PowerSensorReadings } from './power-sensor';
import {
GetReadingsRequest,
GetReadingsResponse,
} from '../../gen/common/v1/common_pb';
import type { PowerSensor } from './power-sensor';

/**
* A gRPC-web client for the MovementSensor component.
* A gRPC-web client for the PowerSensor component.
*
* @group Clients
*/
Expand Down Expand Up @@ -76,29 +80,23 @@ export class PowerSensorClient implements PowerSensor {
}

async getReadings(extra = {}) {
const readings: Record<string, any> = {};
try {
[readings['voltage'], readings['isAc']] = await this.getVoltage(extra);
} catch (error) {
if (!(error as Error).message.includes('Unimplemented')) {
throw error;
}
}
try {
[readings['current'], readings['isAc']] = await this.getCurrent(extra);
} catch (error) {
if (!(error as Error).message.includes('Unimplemented')) {
throw error;
}
}
try {
readings['power'] = await this.getPower(extra);
} catch (error) {
if (!(error as Error).message.includes('Unimplemented')) {
throw error;
}
const { powersensorService } = this;
const request = new GetReadingsRequest();
request.setName(this.name);
request.setExtra(Struct.fromJavaScript(extra));

this.options.requestLogger?.(request);

const response = await promisify<GetReadingsRequest, GetReadingsResponse>(
powersensorService.getReadings.bind(powersensorService),
request
);

const result: Record<string, unknown> = {};
for (const [key, value] of response.getReadingsMap().entries()) {
result[key] = value.toJavaScript();
}
return readings as PowerSensorReadings;
return result;
}

async doCommand(command: StructType): Promise<StructType> {
Expand Down
9 changes: 2 additions & 7 deletions src/components/power-sensor/power-sensor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import type { StructType } from '../../types';
import type { Sensor } from '../sensor';

export type PowerSensorReadings = {
voltage?: number;
current?: number;
isAc?: boolean;
power?: number;
};

/** Represents any sensor that reports voltage, current, and/or power */
export interface PowerSensor extends Sensor {
/** Get Voltage in volts and a boolean that returns true if AC */
Expand All @@ -16,4 +9,6 @@ export interface PowerSensor extends Sensor {
getCurrent(extra?: StructType): Promise<readonly [number, boolean]>;
/** Get Power in watts */
getPower(extra?: StructType): Promise<number>;
/** Return the readings of a sensor. */
getReadings(extra?: StructType): Promise<Record<string, unknown>>;
}
1 change: 0 additions & 1 deletion src/components/powersensor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type {
PowerSensorReadings,
PowerSensor,
} from './power-sensor/power-sensor';
export { PowerSensorClient } from './power-sensor/client';
2 changes: 0 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ export {
type MovementSensor,
type MovementSensorProperties,
type MovementSensorPosition,
type MovementSensorReadings,
MovementSensorClient,
} from './components/movementsensor';

Expand All @@ -168,7 +167,6 @@ export {
export { default as powerSensorApi } from './gen/component/powersensor/v1/powersensor_pb';
export {
type PowerSensor,
type PowerSensorReadings,
PowerSensorClient,
} from './components/powersensor';

Expand Down

0 comments on commit e250b62

Please sign in to comment.