diff --git a/zh-cn/application-dev/distributedservice/mechanicManager-guide.md b/zh-cn/application-dev/distributedservice/mechanicManager-guide.md new file mode 100644 index 0000000000000000000000000000000000000000..1e5354792d192cde8c328f17bf986f95ba6b6643 --- /dev/null +++ b/zh-cn/application-dev/distributedservice/mechanicManager-guide.md @@ -0,0 +1,253 @@ +# 机械设备管理开发指南 + +## 机械设备管理简介 + +机械设备管理模块支持对连接的机械设备进行控制和交互,包括连接管理、控制和监控。 + +机械设备管理提供如下五大功能: + +- **设备发现**
+ 获取当前连接的机械设备列表,监听设备连接状态。 + +- **设备控制**
+ 提供机械设备的操作接口,支持旋转、停止及用户操作设置。 + +- **设备监听**
+ 监听机械设备的状态变化,包括旋转轴状态和跟踪事件。 + +- **设备信息查询**
+ 查询机械设备的基本信息、当前角度和旋转限制。 + +- **摄像头跟踪**
+ 开启或关闭摄像头跟踪功能,并设置摄像头跟踪布局。 + +### 运作机制 + +机械设备管理模块通过 `mechanicManager` 提供的接口实现对设备的操作和状态监听。开发者需要根据业务场景选择合适的接口进行调用。 + +### 约束与限制 + + 使用机械设备管理能力需要确保设备已连接。 + 某些功能(如摄像头跟踪)可能需要系统应用权限。 + 设备的操作可能受限于设备的物理限制或系统限制。 + +## 设备发现开发指导 + +### 场景概述 + +开发者可以调用 `getAttachedMechDevices` 接口,获取当前连接的机械设备列表。 + +### 接口说明 + +- `getAttachedMechDevices(): MechInfo[]`: 获取当前连接的机械设备列表。 +- `on('attachStateChange', callback)`: 监听设备连接状态变化。 + +### 开发步骤 + +1. 导入 `mechanicManager` 模块。 + + ```ts + import mechanicManager from '@kit.MechanicKit'; + ``` + +2. 获取当前连接的机械设备列表,并监听设备的连接状态变化。 + + ```ts + try { + const devices = mechanicManager.getAttachedMechDevices(); + console.log('Connected devices:', devices); + } catch (err) { + console.error('Error fetching devices:', err); + } + ``` + +3. 监听设备连接状态变化。 + + ```ts + mechanicManager.on('attachStateChange', (info) => { + console.log('Device state changed:', info); + }); + ``` + +## 设备控制开发指导 + +### 场景概述 + +开发者可以通过接口控制机械设备,例如旋转设备、停止设备、设置用户操作。 + +### 接口说明 + +- `rotate(mechId, angles, duration)`: 按相对角度旋转设备。 +- `rotateToEulerAngles(mechId, angles, duration)`: 按绝对角度旋转设备。 +- `stopMoving(mechId)`: 停止设备运动。 +- `setUserOperation(operation, mac, params)`: 设置用户操作。 + +### 开发步骤 + +1. 按相对角度旋转设备。 + + ```ts + const mechId = 1; + const angles = { yaw: Math.PI / 2, pitch: 0, roll: 0 }; + const duration = 2000; + + mechanicManager.rotate(mechId, angles, duration) + .then(result => console.log('Rotation result:', result)) + .catch(err => console.error('Rotation error:', err)); + ``` +2. 按绝对角度旋转设备。 + + ```ts + const mechId = 1; + const angles = { yaw: Math.PI / 2, pitch: 0, roll: 0 }; + const duration = 2000; + + mechanicManager.rotateToEulerAngles(mechId, angles, duration) + .then(result => console.log('Rotation result:', result)) + .catch(err => console.error('Rotation error:', err)); + ``` + +3. 停止设备运动。 + + ```ts + mechanicManager.stopMoving(mechId) + .then(() => console.log('Device stopped')) + .catch(err => console.error('Stop error:', err)); + ``` +4. 设置用户操作。 + + ```ts + const operation = CONNECT; + const mac = '1111'; + const params = 'aaa'; + + mechanicManager.setUserOperation(operation, mac, params) + .then(result => console.log('Rotation result:', result)) + .catch(err => console.error('Rotation error:', err)); + ``` + +## 设备监听开发指导 + +### 场景概述 + +开发者可以监听机械设备的状态变化,例如旋转轴状态变化、跟踪事件等。 + +### 接口说明 + +- `on('rotationAxesStatusChange', callback)`: 监听旋转轴状态变化。 +- `on('trackingEvent', callback)`: 监听跟踪事件。 + +### 开发步骤 + +1. 监听旋转轴状态变化。 + + ```ts + mechanicManager.on('rotationAxesStatusChange', (info) => { + console.log('Rotation axes status changed:', info); + }); + ``` + +2. 监听跟踪事件。 + + ```ts + mechanicManager.on('trackingEvent', (eventInfo) => { + console.log('Tracking event:', eventInfo); + }); + ``` + +## 设备信息查询开发指导 + +### 场景概述 + +开发者可以通过接口查询机械设备的基本信息、当前角度、旋转限制等。 + +### 接口说明 + +- `getCurrentAngles(mechId)`: 获取设备当前角度。 +- `getRotationLimits(mechId)`: 获取设备旋转限制。 +- `getRotationAxesStatus(mechId)`: 获取旋转轴状态。 + +### 开发步骤 + +1. 查询设备当前角度。 + + ```ts + const mechId = 1; + + try { + const angles = mechanicManager.getCurrentAngles(mechId); + console.log('Current angles:', angles); + } catch (err) { + console.error('Error fetching angles:', err); + } + ``` + +2. 查询设备旋转限制。 + + ```ts + try { + const limits = mechanicManager.getRotationLimits(mechId); + console.log('Rotation limits:', limits); + } catch (err) { + console.error('Error fetching rotation limits:', err); + } + ``` + +3. 查询旋转轴状态。 + + ```ts + try { + const status = mechanicManager.getRotationAxesStatus(mechId); + console.log('Rotation axes status:', status); + } catch (err) { + console.error('Error fetching axes status:', err); + } + ``` + +## 摄像头跟踪开发指导 + +### 场景概述 + +开发者可以通过 `setCameraTrackingEnabled` 和 `setCameraTrackingLayout` 接口开启摄像头跟踪功能,并设置跟踪布局。 + +### 接口说明 + +- **开启或关闭摄像头跟踪** + ```ts + setCameraTrackingEnabled(isEnabled: boolean): void; + ``` + +- **设置摄像头跟踪布局** + ```ts + setCameraTrackingLayout(trackingLayout: CameraTrackingLayout): void; + ``` + +### 开发步骤 + +1. 导入 `mechanicManager` 模块。 + + ```ts + import mechanicManager from '@kit.MechanicKit'; + ``` + +2. 开启摄像头跟踪。 + + ```ts + try { + mechanicManager.setCameraTrackingEnabled(true); + console.log('Camera tracking enabled'); + } catch (err) { + console.error('Error enabling camera tracking:', err); + } + ``` + +3. 设置摄像头跟踪布局。 + + ```ts + try { + mechanicManager.setCameraTrackingLayout(mechanicManager.CameraTrackingLayout.MIDDLE); + console.log('Camera tracking layout set to middle'); + } catch (err) { + console.error('Error setting camera tracking layout:', err); + } + ```