单击下面的链接可下载感知模拟独立包:
感知模拟的这个独立软件包版本允许独立于操作系统版本进行更频繁的更新,并带来新功能,其中包括:
记录和播放:能够记录使用感知模拟执行的操作,以便使用最少的代码以自动方式进行播放。
验证程序:在播放记录内容期间,感知模拟可以回调到测试代码中,你可以在该代码中使用记录内容中嵌入的元数据来验证状态。
视频流式处理:感知模拟控制用户界面可以显示 HoloLens 2 设备或模拟器的实时视图,并允许使用键盘、鼠标或游戏手柄进行直接控制,这与 HoloLens 2 模拟器类似。 此外还提供视频客户端 API 以允许自动测试捕获屏幕截图。
完整的本机 API:除了继续提供托管代码客户端 API 之外,此版本还包括用于扩展感知模拟或用于在 C++ 中编写测试的标头和库。
新的 REST 客户端:连接到远程设备时,新的本机 REST 客户端提供更高的稳健性,并允许从本机代码和托管代码进行访问。
系统要求
60MB 的磁盘空间
Windows 10 版本 2004 或更高版本
注意
此版本的感知模拟与 Windows 10 和 11 操作系统中包含的版本并行安装和运行。 “开始”菜单上的“感知模拟控制”的快捷方式会启动并使用此预览版本,后者存储在 \Program Files\Microsoft Perception Simulation
中。 如果从混合现实门户、HoloLens 2 模拟器或 \Windows\System32\PerceptionSimulation
启动开发人员工具,则会运行旧版本。 若要使用此预览版本,请务必使用 \Program Files\Microsoft Perception Simulation
下的引用程序集或标头和库来构建任何测试代码。
注意
此版本与 HoloLens 2 兼容,无需在 HoloLens 2 头戴显示设备上安装任何新内容。 HoloLens 2 的未来更新将在此预览版本中启用场景理解功能。
安装
预览版安装程序会安装以下组件:
感知模拟控制应用:此应用是用于手动控制模拟和创建记录的用户界面。
感知模拟驱动程序:其中包括感知模拟头戴显示设备驱动程序和感知模拟 6-DOF 控制器驱动程序。 这两个驱动程序都用于 Windows Mixed Reality。
与 HoloLens 2 配合使用的空间映射记录:这些记录与 HoloLens 2 模拟器提供的记录相同。
场景理解房间捕获:这些捕获将在未来的 HoloLens 2 更新中启用。
感知模拟本机 SDK:其中包括 C++ 标头和库,可用于编写工具、应用和测试自动化,从而通过本机代码以编程方式驱动感知模拟。
感知模拟托管 SDK:其中包括托管引用程序集,可用于编写工具、应用和测试自动化,从而通过托管代码以编程方式驱动感知模拟。
手动控制
若要使用键盘、鼠标或游戏手柄控制 Windows Mixed Reality 或 HoloLens 2,请执行以下操作:
启动“感知模拟控制”应用。
从窗口顶部的下拉列表中选择目标产品。 对于此版本,请对现有 HoloLens 2 设备使用“HoloLens 2 (旧版)”。
对于远程设备(例如 HoloLens),请输入 HoloLens 设备或模拟器的 IP 地址。
确保目标设备上启用了“设备门户”。 默认情况下,此功能在 HoloLens 2 模拟器中处于启用状态,但需要在物理 HoloLens 设备上手动启用,或者在使用“Windows Mixed Reality (远程)”时在目标电脑上手动启用。
将头戴显示设备开关切换为“开”。
创建记录
可以使用“感知模拟控制”应用创建手动感知模拟操作的记录,然后手动播放该记录或在自动测试中这样做:
连接到目标设备。
向下滚动窗口,直到看到“记录”部分。
按“记录”按钮。
选择一个文件夹,为该记录提供一个文件名。
使用“感知模拟控制”在 UX 中手动执行操作 - 环顾四周、交互等。
在记录期间,你可以选择在记录中插入验证步骤:选择“添加验证”按钮,然后填写出现的弹出 UI。
选择“停止”按钮。 新的记录将保存到磁盘并自动加载。 点击“播放”可播放操作。
创建测试自动化
若要使用托管代码创建测试自动化,请执行以下操作:
在 Visual Studio 中创建新的 .NET Framework 4.7 托管代码项目。
添加对
PerceptionSimulation.Interop
、PerceptionSimulationManager.Interop
和SimulationStream.Interop
的引用。 你可以在托管 SDK 文件夹 (%ProgramFiles%\Microsoft Perception Simulation\Sdk\References
) 中找到它们。将托管代码项目中的现有项链接添加到
PerceptionSimulationManager.dll
,并将其设置为“如果较新则复制”。添加
using Microsoft.PerceptionSimulation
。
以下是基本示例 C# 代码,用于连接到远程 HoloLens,执行“主屏幕”(系统)手势,然后播放记录:
using Microsoft.PerceptionSimulation;
using System;
namespace SimulationManagedTestApp
{
internal class Program
{
static void Main(string[] args)
{
string server = "http://192.168.0.1";
ISimulationRestClient client = PerceptionSimulationManager.CreatePerceptionSimulationRestClient(server, null, null, StreamDataTypes.All, null);
client.Connect();
ISimulationStreamSink sink = ((ISimulationStreamSinkFactory)client).CreateSimulationStreamSink();
IPerceptionSimulationManager manager = PerceptionSimulationManager.CreatePerceptionSimulationManager(sink);
manager.Human.LeftHand.Activated = true;
manager.Human.LeftHand.PerformGesture(SimulatedGesture.Home);
ISimulationRecording recording = PerceptionSimulationManager.LoadPerceptionSimulationRecording("C:\\Tools\\Recordings\\test.xef", ((ISimulationStreamSinkFactory)client), null);
recording.Play();
// Instead of just sleeping here, you probably want to register an ISimulationRecordingCallback object to get state changes of the recording, and perhaps a callback to
// get validation request callbacks by implementing ISimulationRecordingCallback2 and providing that object to the LoadPerceptionSimulationRecording call.
System.Threading.Thread.Sleep(2500);
client.Disconnect();
Console.ReadLine();
}
}
}
注意
这与现有示例代码类似,但使用 v3 中的新 ISimulationRestClient
。 此客户端实现 ISimulationStreamSinkFactory
,因此适合为需要接收器的方法获取接收器。
下一个示例与上一个示例执行相同的操作,但连接到本地计算机上的 Windows Mixed Reality:
using Microsoft.PerceptionSimulation;
using System;
namespace SimulationManagedTestApp
{
internal class Program
{
static void Main(string[] args)
{
string platformId = "B1DE8CAA-898F-474F-811A-F041FE3FD40D";
string headsetId = PerceptionSimulationManager.InstallSimulatedHeadset(platformId, string.Empty);
ISimulationStreamSink sink = PerceptionSimulationManager.CreateControlStreamForDevice(headsetId);
IPerceptionSimulationManager manager = PerceptionSimulationManager.CreatePerceptionSimulationManager(sink);
manager.Human.LeftHand.Activated = true;
manager.Human.LeftHand.PerformGesture(SimulatedGesture.Home);
// Instead of just sleeping here, you probably want to register an ISimulationRecordingCallback object to get state changes of the recording, and perhaps a callback to
// get validation request callbacks by implementing ISimulationRecordingCallback2 and providing that object to the LoadPerceptionSimulationRecording call.
System.Threading.Thread.Sleep(2500);
client.Disconnect();
Console.ReadLine();
}
}
}
平台 ID 存储在注册表中的 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Perception Simulation\Platforms
下。 不使用键名称,而是使用内部的 ID 值。 例如,Windows Mixed Reality 的平台 ID 位于 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Perception Simulation\Platforms\WMR (ID = "B1DE8CAA-898F-474F-811A-F041FE3FD40D")
。 可以使用返回 SimulationPlatform
结构数组的 PerceptionSimulationManager.GetAvailablePlatforms()
在代码中枚举平台。 每个 SimulationPlatform
都提供有关平台的各种详细信息,其中包括 PlatformId
。 对于本机代码,可以使用 PerceptionSimulationSettings::GetAvailablePlatforms()
。 API 的本机版本返回一个计数,并可选择填充调用方提供的数组。 你可以调用 API 一次来获取计数,然后再次调用它来填充数组。
你可以浏览 PerceptionSimulationManager.Interop
库来查看可用的类和方法。 这包括:
IPerceptionSimulationManager2
,允许你添加接收器来接收模拟数据、加载场景理解数据、发送音频数据以及以编程方式注入验证请求。注意
HoloLens 和 Windows Mixed Reality 目前不支持某些功能。
ISimulatedSixDofController3
,允许设置运动控制器上模拟按钮的位置。ISimulationAudioVideoClient
,其中包括将纹理保存到磁盘的方法。注意
若要在 HoloLens 上进行视频流式处理,你必须使用物理设备,或者必须在 HoloLens 2 模拟器中启用硬件图形加速。 默认情况下,模拟器会尝试启用硬件图形加速。
ISimulationRecordingCallback2
,测试或应用程序可以实现它来处理记录播放期间的验证请求。
对于本机 C++ 代码,请执行以下操作:
包括正确的标头 -
PerceptionSimulationSettings.h
和PerceptionSimulationManager.h
链接适当的库 - 至少链接
PerceptionSimulationSettings.lib
和PerceptionSimulationManager.lib
。 如果使用验证程序功能,则还需要 PerceptionSimulationValidators.lib。 如果使用音频/视频功能,则需要 PerceptionSimulationOutput.lib为 HoloLens 或远程 Windows Mixed Reality 电脑创建
PerceptionSimulationRestClient
,或为本地设备调用InstallSimulatedHeadset
。InstallSimulatedHeadset
函数会返回一个头戴显示设备 ID,你可以在创建与设备通信的控制流时使用该 ID。为设备创建控制流。
创建感知模拟管理器的实例。
用于初始化模拟的基本示例代码:
// Simulation headers
#include <perceptionsimulation.h>
#include <perceptionsimulationmanager.h>
#include <PerceptionSimulationSettings.h>
// These would most likely be placed in a header and exist within a class scope
winrt::com_ptr<IPerceptionSimulation2> m_spSimulation;
winrt::com_ptr<IPerceptionSimulationManager> m_spSimulationManager;
winrt::com_ptr<ISimulationStreamSink> m_spStreamSink;
// The following are not needed if you intend to play back .xef files
winrt::com_ptr<ISimulatedHuman> m_spHuman;
winrt::com_ptr<ISimulatedHead2> m_spHead;
winrt::com_ptr<ISimulatedHand3> m_spLeftHand;
winrt::com_ptr<ISimulatedHand3> m_spRightHand;
winrt::com_ptr<ISimulatedEyes> m_spEyes;
std::string platformId = L"B1DE8CAA-898F-474F-811A-F041FE3FD40D"; // Windows Mixed Reality
// This would most likely be in a setup/initialization function. This can be done from any thread.
winrt::com_ptr<IPerceptionSimulation> spSimulation;
WCHAR deviceId[MAX_PATH] = { 0 };
winrt::check_hresult(PerceptionSimulationSettings::InstallSimulatedHeadset(platformId, nullptr, MAX_PATH, deviceId, m_spHeadset.put()));
winrt::check_hresult(CoCreateInstance(__uuidof(PerceptionSimulation), nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(spSimulation.put())));
m_spSimulation = spSimulation.as<IPerceptionSimulation2>();
// Create a control stream for the device
wil::unique_bstr path(devicePath);
winrt::check_hresult(m_spSimulation->CreateControlStreamForDevice(deviceId, STREAM_PRIORITY_LOW, m_spStreamSink.put()));
// Create the main Perception Simulation Manager object from which we'll get control of the simulated human and devices like controllers
winrt::check_hresult(CreatePerceptionSimulationManager(m_spStreamSink.get(), m_spSimulationManager.put()));
// If you intend to play .xef files instead of 'script' a test in code, stop here.
winrt::check_hresult(m_spSimulationManager->get_Human(m_spHuman.put()));
winrt::com_ptr<ISimulatedHead> spHead;
winrt::check_hresult(m_spHuman->get_Head(spHead.put()));
spHead.as(m_spHead);
winrt::check_hresult(m_spHead->get_Eyes(m_spEyes.put()));
winrt::com_ptr<ISimulatedHand> spHand;
winrt::check_hresult(m_spHuman->get_RightHand(spHand.put()));
spHand.as(m_spRightHand);
// This code would most likely be part of a single test case
// Activate the right hand
m_spRightHand->put_Activated(TRUE);
// Perform the system gesture.
spHand->PerformGesture(SIMULATED_GESTURE::SIMULATED_GESTURE_HOME);
// In your cleanup/shutdown path, you should remove the simulated headset (or ideally remove it only if it wasn't already present at the start of the test)
// so you leave the machine in the same state that you found it.
PerceptionSimulationSettings::RemoveSimulatedHeadset(deviceId);