openharmony 鸿蒙 js-apis-mechanicManager-sys

2026-08-25 浏览 (1)

@ohos.distributedHardware.mechanicManager (机械体控制模块)(系统接口)

本模块提供与机械体设备交互的能力,包括设备连接管理、控制和监控功能。

说明:

导入模块

import { mechanicManager } from '@kit.MechanicKit';

mechanicManager.setUserOperation

setUserOperation(operation: Operation, mac: string, params: string): void

设置用户的连接或断开操作。

需要权限:ohos.permission.CONNECT_MECHANIC_HARDWARE

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
operationOperation操作类型。
macstring机械体设备MAC地址。
paramsstring操作参数。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
201Permission denied.
202Not system application.
33300001Service exception.

示例:

console.info('User operate');
mechanicManager.setUserOperation(mechanicManager.Operation.CONNECT, "58:51:9e:e7:79:6d", "operatingParams");
console.info('User operation was successful');

mechanicManager.setCameraTrackingLayout

setCameraTrackingLayout(trackingLayout: CameraTrackingLayout): void

设置当前机械体设备摄像头跟踪布局。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
trackingLayoutCameraTrackingLayout摄像头跟踪布局。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.
33300003Feature not supported.

示例:

console.info('Set layout');
mechanicManager.setCameraTrackingLayout(mechanicManager.CameraTrackingLayout.LEFT);
console.info('Set layout successful');

mechanicManager.rotate

rotate(mechId: number, angles: RotationAngles, duration: number): Promise<Result>

将机械体设备旋转到相对角度。使用Promise异步回调。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
mechIdnumber机械体设备ID。
anglesRotationAngles相对当前位置的旋转角度。
durationnumber旋转持续时间,单位:ms。

返回值:

类型说明
Promise<Result>Promise对象,返回旋转执行结果。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.

示例:

console.info('Start rotate');
let degree: mechanicManager.RotationAngles = {
  yaw: 0.1 * Math.PI,
  roll: 0.0,
  pitch: 0.0
}
mechanicManager.rotate(0, degree, 500)
  .then((result: mechanicManager.Result) => {
    console.info(`'Rotate result:' ${result}`);
  });
console.info('End rotation');

mechanicManager.rotateToEulerAngles

rotateToEulerAngles(mechId: number, angles: EulerAngles, duration: number): Promise<Result>

将机械体设备旋转到绝对角度。使用Promise异步回调。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
mechIdnumber机械体设备ID。
anglesEulerAngles绝对角度。
durationnumber旋转持续时间,单位:ms。

返回值:

类型说明
Promise<Result>Promise对象,返回旋转执行结果。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.

示例:

let degree: mechanicManager.EulerAngles = {
  yaw: 0.9 * Math.PI,
  roll: 0.9 * Math.PI,
  pitch: 0.9 * Math.PI
}
mechanicManager.rotateToEulerAngles(0, degree, 500)
  .then((result: mechanicManager.Result) => {
    console.info(`'Rotate result:' ${result}`);
  });
console.info('End rotation');

mechanicManager.getMaxRotationTime

getMaxRotationTime(mechId: number): number

获取机械体设备的最大连续旋转持续时间。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
mechIdnumber机械体设备ID。

返回值:

类型说明
number最大旋转持续时间,单位:ms。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.

示例:

console.info('Query maximum rotation time');
let maxTime = mechanicManager.getMaxRotationTime(0);
console.info(`'Query maximum rotation time successful, maximum time:' ${maxTime}`);

mechanicManager.getMaxRotationSpeed

getMaxRotationSpeed(mechId: number): RotationSpeed

获取机械体设备的最大旋转速度。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
mechIdnumber机械体设备ID。

返回值:

类型说明
RotationSpeed最大旋转速度,仅返回速度的绝对值。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.

示例:

console.info('Query rotation speed');
let speedLimit: mechanicManager.RotationSpeed = mechanicManager.getMaxRotationSpeed(0);
console.info(`'Query rotation speed successful, speed limit information:' ${speedLimit}`);

mechanicManager.rotateBySpeed

rotateBySpeed(mechId: number, speed: RotationSpeed, duration: number): Promise<Result>

以指定速度旋转当前机械体设备。使用Promise异步回调。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
mechIdnumber机械体设备ID。
speedRotationSpeed指定旋转速度。最大旋转速度不超过getMaxRotationSpeed的返回值大小,如果超过最大速度,默认取最大值。
durationnumber旋转持续时间,单位:ms。最大时间不超过getMaxRotationTime的返回值大小,如果超过最大时间,默认取最大值。

返回值:

类型说明
Promise<Result>Promise对象,返回旋转执行结果。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.

示例:

console.info('Start rotate');
let degree: mechanicManager.RotationSpeed = {
  yawSpeed: 3 * Math.PI,
  rollSpeed: 3 * Math.PI,
  pitchSpeed: 3 * Math.PI
}
mechanicManager.rotateBySpeed(0, degree, 500)
  .then((result) => {
    console.info(`'Rotate result:' ${result}`);
  });
console.info('Rotate finish');

mechanicManager.stopMoving

stopMoving(mechId: number): Promise<void>

停止机械体设备的移动。使用Promise异步回调。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
mechIdnumber机械体设备ID。

返回值:

类型说明
Promise<void>Promise对象,无返回结果。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.

示例:

console.info('Stop moving');
mechanicManager.stopMoving(0)
  .then(() => {
    console.info('Get stop complete');
  });
console.info('Stop succeeded');

mechanicManager.getCurrentAngles

getCurrentAngles(mechId: number): EulerAngles

获取指定机械体设备相对于初始位置的当前旋转角度。

说明:

初始位置:机械体设备初始化的位置,坐标为(0, 0, 0)。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
mechIdnumber机械体设备ID。

返回值:

类型说明
EulerAngles机械体设备的当前旋转角度。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.

示例:

console.info('Query current location');
let currentAngles: mechanicManager.EulerAngles = mechanicManager.getCurrentAngles(0);
console.info(`'Query current location, location:' ${currentAngles}`);

mechanicManager.getRotationLimits

getRotationLimits(mechId: number): RotationLimits

获取指定机械体设备相对于初始位置的最大旋转角度。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
mechIdnumber机械体设备ID。

返回值:

类型说明
RotationLimits最大旋转角度。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.

示例:

console.info('Query rotation limit information');
let degreeLimit: mechanicManager.RotationLimits = mechanicManager.getRotationLimits(0);
console.info(`'Query the rotation limit information successfully, limit information:' ${degreeLimit}`);

mechanicManager.on('rotationAxesStatusChange')

on(type: 'rotationAxesStatusChange', callback: Callback<RotationAxesStateChangeInfo>): void

注册旋转轴状态变化事件的回调监听。使用callback异步回调。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
type'rotationAxesStatusChange'注册监听事件的类型。取值为:'rotationAxesStatusChange'。
callbackCallback<RotationAxesStateChangeInfo>回调函数,返回机械体设备旋转轴状态变化信息。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.

示例:

console.info('Register Axis Status listener');
mechanicManager.on("rotationAxesStatusChange", (result: mechanicManager.RotationAxesStateChangeInfo) => {
  console.info(`'result:' ${result}`);
});
console.info('Successful registration');

mechanicManager.off('rotationAxesStatusChange')

off(type: 'rotationAxesStatusChange', callback?: Callback<RotationAxesStateChangeInfo>): void

取消注册旋转轴状态变化事件的回调监听。使用callback异步回调。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
type'rotationAxesStatusChange'取消注册监听事件的类型。取值为:'rotationAxesStatusChange'。
callbackCallback<RotationAxesStateChangeInfo>mechanicManager.off('rotationAxesStatusChange')注册的回调函数。不填时默认取消所有注册的回调函数。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.

示例:

console.info('Unregister Axis Status listener');
mechanicManager.off("rotationAxesStatusChange", (result: mechanicManager.RotationAxesStateChangeInfo) => {
  console.info(`'result:' ${result}`);
});
console.info('Unregister successfully');

mechanicManager.getRotationAxesStatus

getRotationAxesStatus(mechId: number): RotationAxesStatus

获取当前机械体设备旋转轴的状态。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
mechIdnumber机械体设备ID。

返回值:

类型说明
RotationAxesStatus旋转轴状态。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.

示例:

console.info('Query the rotation axis status');
let axisStatus: mechanicManager.RotationAxesStatus = mechanicManager.getRotationAxesStatus(0);
console.info(`'Query the rotation axis status successfully, axis state:' ${axisStatus}`);

mechanicManager.searchTarget21+

searchTarget(target: TargetInfo, params: SearchParams): Promise<SearchResult>

旋转机械体一周搜索目标。使用Promise异步回调。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

参数:

参数名类型必填说明
targetTargetType目标人脸信息。
paramsSearchParams搜索方向。

返回值:

类型说明
Promise<SearchResult>Promise对象,返回搜索的结果。

错误码:

以下的错误码的详细介绍请参见机械体控制模块错误码通用错误码

错误码ID错误信息
202Not system application.
33300001Service exception.
33300002Device not connected.
33300003Feature not supported.
33300004Camera not opened.

示例:

let targetInfo: mechanicManager.TargetInfo = {
    targetType: mechanicManager.TargetType.HUMAN_FACE
};
let searchParams: mechanicManager.SearchParams = {
    direction: mechanicManager.SearchDirection.DEFAULT
}
mechanicManager.searchTarget(targetInfo,
    searchParams).then((searchResult) => {
    console.info(`'result:' ${searchResult}`);
});

RotationAngles

相对当前位置的旋转角度。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型只读可选说明
yawnumber偏航角,范围为[-2*Math.PI, 2*Math.PI],以弧度为单位。
rollnumber横滚角,范围为[-2*Math.PI, 2*Math.PI],以弧度为单位。
pitchnumber俯仰角,范围为[-2*Math.PI, 2*Math.PI],以弧度为单位。

EulerAngles

相对于初始位置的绝对欧拉角。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型只读可选说明
yawnumber偏航角,范围为[-2*Math.PI, 2*Math.PI],以弧度为单位。
rollnumber横滚角,范围为[-2*Math.PI, 2*Math.PI],以弧度为单位。
pitchnumber俯仰角,范围为[-2*Math.PI, 2*Math.PI],以弧度为单位。

RotationSpeed

旋转速度。负值表示顺时针旋转,正值表示逆时针旋转。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型只读可选说明
yawSpeednumber偏航速度,以弧度每秒为单位。
rollSpeednumber横滚速度,以弧度每秒为单位。
pitchSpeednumber俯仰速度,以弧度每秒为单位。

RotationLimits

相对于初始位置的旋转角度限制。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型只读可选说明
negativeYawMaxnumber最大偏航旋转角度(负方向),范围为[-2*Math.PI, 0],以弧度为单位。如果值小于或等于 -2*Math.PI,则没有限制。
positiveYawMaxnumber最大偏航旋转角度(正方向),范围为[0, 2*Math.PI],以弧度为单位。如果值大于或等于 2*Math.PI,则没有限制。
negativeRollMaxnumber最大横滚旋转角度(负方向),范围为[-2*Math.PI, 0],以弧度为单位。如果值小于或等于 -2*Math.PI,则没有限制。
positiveRollMaxnumber最大横滚旋转角度(正方向),范围为[0, 2*Math.PI],以弧度为单位。如果值大于或等于 2*Math.PI,则没有限制。
negativePitchMaxnumber最大俯仰旋转角度(负方向),范围为[-2*Math.PI, 0],以弧度为单位。如果值小于或等于 -2*Math.PI,则没有限制。
positivePitchMaxnumber最大俯仰旋转角度(正方向),范围为[0, 2*Math.PI],以弧度为单位。如果值大于或等于 2*Math.PI,则没有限制。

RotationAxesStatus

旋转轴状态。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型只读可选说明
yawEnabledboolean是否启用了偏航轴,true表示已启用,false表示已禁用。
rollEnabledboolean是否启用了横滚轴,true表示已启用,false表示已禁用。
pitchEnabledboolean是否启用了俯仰轴,true表示已启用,false表示已禁用。
yawLimitedRotationAxisLimited偏航轴限位状态。
rollLimitedRotationAxisLimited横滚轴限位状态。
pitchLimitedRotationAxisLimited俯仰轴限位状态。

RotationAxisLimited

旋转轴限位状态的枚举。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称说明
NOT_LIMITED0无限制
NEGATIVE_LIMITED1负方向受限,可以向相反方向转动。
POSITIVE_LIMITED2正方向受限,可以向相反方向转动。

RotationAxesStateChangeInfo

旋转轴状态变化信息。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型只读可选说明
mechIdnumber机械体设备ID。
statusRotationAxesStatus旋转轴状态。

Operation

用户操作的枚举。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称说明
CONNECT0连接。
DISCONNECT1断开。

Result

旋转执行结果的枚举。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称说明
COMPLETED0执行完成。
INTERRUPTED1执行被打断。
LIMITED2执行受最大旋转角度限制。
TIMEOUT3执行超时。
SYSTEM_ERROR100系统错误。

TargetType21+

目标人脸信息的枚举。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型说明
HUMAN_FACEint0目标人脸信息。

SearchDirection21+

系统默认的搜索方向。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型说明
DEFAULTint0系统默认方向
LEFTWARDint1左向,即顺时针方向。
RIGHTWARDint2右向,即逆时针方向。

TargetInfo21+

搜索目标的相关信息。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型只读可选说明
targetTypeTargetType搜索目标的相关信息。

SearchParams21+

指定搜索的方向。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型只读可选说明
directionSearchDirection搜索的方向。

SearchResult21+

显示搜索命令的执行结果。

系统能力:SystemCapability.Mechanic.Core

系统接口:该接口为系统接口。

名称类型只读可选说明
targetCountnumber搜索到的目标数量。

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 Readme-CN

openharmony 鸿蒙 js-apis-mechanicManager

openharmony 鸿蒙 errorcode-mechanic

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/KPvudxxA