继之前分享的一篇《Robotics Studio学习教程:第九天——用DSS 改写自行走小车的 Activity》, 我们将继续分享下一篇《Robotics Studio学习教程:第十天——利用 DSS 建立虚拟环境(1)》,让我继续开始我们学习Visual Programming Language,以及使用Robotics Studio学习开发机器人应用的道路吧。
[Robotics Studio] DSS with VSE -- Day10
第十天, 我们开始利用 DSS 玩弄虚拟环境...
首先建立 VS 2008 DSS Service 2.0 项目, 就取名叫 VSE_Example 吧, 但是这回我们在项目建立时的对话框, 选择我们要使用 "Simulation Engine" 这个内建的 DSS Service 作为 Partner Service, 你可以切换对话框的 tab 到 Partners , 然后在 Services: 下输入 engine (就会过滤到只剩下有 engine 的 DSS Service) , 然后点选 Add as partner , 就会出现在右边当中,
如下图:
之后点选 ok, 你会注意到 VS 2008 帮你产生的项目多参考了 SimulationEngine.Proxy, 以及多了下面这样的 code:
- /// <summary>
- /// SimulationEngine partner
- /// </summary>
- [Partner("SimulationEngine", Contract = engine.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.UseExistingOrCreate)]
- engine.SimulationEnginePort _simulationEnginePort = new engine.SimulationEnginePort();
所谓的 Partner Service , 表示该 DSS Service 需要搭配的其他 DSS Service, 注意我们使用的 CreationPolicy (生成原则) 是 UseExistingOrCreate , 表示没找到该 Service 的话就生一个新的, 否则就用现有的(跟其他 DSS Service 共享).
现在你可以执行它, 或是建置它, 然后在 VPL 当中拉出来放着, 直接执行.
执行结果就是, 会跑出 Visual Simulation Environment , 但是画面都是...嘿嘿...ㄜ...黑黑的...
这是因为 Simulation Engine 只是启动虚拟环境, 比照之前, 我们还少了光源, 还少了地板, 还少了天空, 还少了其他等等等.
为了要加上这些东西, 我们要加入一些等一下会用到的参考 DLL , 在方案的参考上右键单击点选加入参考, 选择浏览, 然后选择你的 Robotics Developer Studio 安装目录下的 bin , 你会看到一堆 DLL , 我们要加入两个, 分别是 PhysicsEngine.dll (负责物理引擎) , RoboticsCommon.dll , SimulationCommon.dll, SimulationEngine.dll 这几个.
接着在 VSE_Example.cs 的 using 新增 (你也可以等一下写 code 的时候再右键单击点选 "解析" , VS 2008 会去帮你搜寻参考 DLL , 加上正确的 using (如果有复选题也会让你自己挑选))
- using Microsoft.Robotics.Simulation.Engine;
- using Microsoft.Robotics.PhysicalModel;
- using Microsoft.Robotics.Simulation.Physics;
ok, 我们在 VSE_ExampleService 这个 class 内部加上如下的 code, 批注我就直接写在 code 当中:
- /// <summary>
- /// 設定一個鏡頭觀景窗 CameraView
- /// </summary>
- private void SetupCamera()
- {
- CameraView view = new CameraView();
- view.EyePosition = new Vector3(-1.65f, 1.63f, -0.29f);
- view.LookAtPoint = new Vector3(-0.68f, 1.38f, -0.18f);
- SimulationEngine.GlobalInstancePort.Update(view);
- }
- /// <summary>
- /// 加上天空 , 太陽
- /// </summary>
- void AddSky()
- {
- //加上天空
- SkyDomeEntity sky = new SkyDomeEntity("skydome.dds", "sky_diff.dds");
- SimulationEngine.GlobalInstancePort.Insert(sky);
- //用平行光源 (無限遠處的光源) 來模擬太陽
- LightSourceEntity sun = new LightSourceEntity();
- sun.State.Name = "Sun";
- sun.Type = LightSourceEntityType.Directional;
- sun.Color = new Vector4(0.8f, 0.8f, 0.8f, 1);
- sun.Direction = new Vector3(0.5f, -.75f, 0.5f);
- SimulationEngine.GlobalInstancePort.Insert(sun);
- }
- /// <summary>
- /// 加上地面
- /// </summary>
- void AddGround()
- {
- // 產生一個寬廣的地面
- HeightFieldEntity ground = new HeightFieldEntity(
- "simple ground", // name
- "03RamieSc.dds", // texture image
- new MaterialProperties("ground",
- 0.2f, // restitution
- 0.5f, // dynamic friction
- 0.5f) // static friction
- );
- SimulationEngine.GlobalInstancePort.Insert(ground);
- }
现在我们要在启动时呼叫这些函式, 所以 VSE_ExampleService.Start() 的程序代码改为如下:
- /// <summary>
- /// Service start
- /// </summary>
- protected override void Start()
- {
- //
- // Add service specific initialization here
- //
- base.Start();
- //加入一些必要的物體.
- SetupCamera();
- AddSky();
- AddGround();
- }
恩, 这次执行以后, 就会看到一些东西了...
明天再来为它加点东西, 而且来跟 VPL 整合.
让我们继续一下章教程:
《Robotics Studio学习教程:第十天——利用 DSS 建立虚拟环境(2)》