微软机器人开发套件教程翻译

微软机器人开发套件教程翻译
Microsoft Robotics Studio Tutorials
 
教程1    访问服务
当你使用Microsoft Robotics Studio (MRS)编写机器人应用程序时,协调一组服务的输入输出是十分方便快捷的。服务体现了与提供特定功能的进程之间的交互,或代表了软件和硬件的接口。
 
 
在本教程中,你将学习如何使用一个基本服务,该服务接收一个简单的 bumper (以下称为接触传感器)的输出,并将结果发送到另一个服务,以便在屏幕上显示。你将通过订阅事件通知来使用该传感器的输出信号(这就意味着你必须注册以便接收事件消息)。
 
在本教程中,你将会学到:
 
1,辨识需要使用的服务
2,订阅希望接收的传感器服务信号
3,编写针对传感器信号通知的处理,并在控制台应用程序中显示输出
 
在本教程中我们将使用接触传感器并输出结果文字到控制台,当然你也可以应用到其它类型的传感器。 我们将说明如何连接到一个接触传感器。
 
大体上,使用一个服务需要三个步骤
1,选择服务的代理DLL并添加到项目引用
2,添加using命令以引用该服务
3,最后,设置一个端口(port)以便和服务通讯,端口是服务之间消息通讯的机制
 
前提
硬件
本教程是基于以下机器人硬件平台的,SDK中提供了针对这些硬件的服务。你也可以使用提供了同等服务的硬件。
n         LEGO® MINDSTORMS® RCX
n         fischertechnik®
 
你需要一个微控制器(microcontroller,brick)和一个接触传感器。
要设置硬件以及这些硬件与PC的通讯,见前一节“硬件设置”。
 
软件
本教程使用C#, 你可以使用Visual Studio 2005简易版、标准版、专业版和团队版软件进行开发。
 
开始
在MRS中选择MS Robotics Studio Command Prompt,打开一个特殊的控制台命令提示窗口,其目录为MRS安装根目录
 
进入samples目录并启动dssnewservice创建第一个服务,这将帮助你创建一个服务模板。
 
 
 >cd Samples
 >dssnewservice /s:MyTutorial1 
 
 
打开C#编辑器,在MyTutorial1目录中选择Tutorial1解决方案
 
 
步骤1 添加引用
当你创建一个示例服务后,有几个机器人DLL将被缺省的添加到项目中。 在本教程中,我们将使用RoboticsCommon库以便使用接触传感器。在编辑器中添加项目引用如下:
 
RoboticsCommon.Proxy.dll
 
在MyTutorial1.cs最上面,添加using命令,将通用ContactSensor契约(contract)作为接触传感器服务的基础。
 
using bumper = Microsoft.Robotics.Services.ContactSensor.Proxy;
 
步骤2 建立一个关联关系(Partner Relationship
建立一个接触传感器关联关系,关联关系描述了两个服务之间的关系。将高亮部分代码插入MyTutorial私有操作部分(在建立工程时已经事先定义)
 
[ServicePort("/MyTutorial1")]
private MyTutorial1Operations _mainPort = new MyTutorial1Operations();
 
[Partner("bumper", Contract = bumper.Contract.Namespace,
     CreationPolicy = PartnerCreationPolicy.UseExisting)]
private bumper.ContactSensorArrayOperations _bumperPort = new bumper.ContactSensorArrayOperations();
 
public MyTutorial1(WsapServiceCreationPort creationPort) :
  base(creationPort)
{
  CreateSuccess();
}
 
浏览项目时,你会发现一个描述文件(manifest file)Mytutorial1.manifest.xml。该描述文件决定了应用程序运行时哪些服务需要启动。为了将我们服务的关联关系绑定到硬件, 我们需要决定启用哪种服务契约(service contract)。这些服务契约可能因你使用的硬件不同而不同。
 
LEGO RCX,
http://schemas.microsoft.com/robotics/2006/06/legorcxcontactsensor.html".
 
fischertechnik 
"http://schemas.microsoft.com/2006/06/ftbumper.html".
 
<? xml version = "1.0" ?>
< Manifest
    xmlns = "http://schemas.microsoft.com/xw/2004/10/manifest.html"
    xmlns:wsap = "http://schemas.microsoft.com/xw/2004/10/wsap.html"
    >
  < CreateServiceList >
    < ServiceRecordType >
      < wsap:Contract > http://schemas.microsoft.com/robotics/2006/06/roboticstutorial1.html</wsap:Contract>
    </ ServiceRecordType >
 
    <!-- Start the Lego RCX Bumper with bumper default on port 1 -->
    < ServiceRecordType >
      < wsap:Contract > http://schemas.microsoft.com/robotics/2006/06/legorcxcontactsensor.html</wsap:Contract>
    </ ServiceRecordType >
 
  </ CreateServiceList >
</ Manifest >
 
步骤3 服务启动初始化
当服务启动时,每个服务都有一个启动流程。本教程的服务将在启动时订阅接触传感器服务。在启动流程中,插入SubscribeToBumpers函数以便订阅传感器服务
 
protected override void Start()
{
    // Activate the default handlers supplied by the base class: DefaultLookup and DefaultDrop
    ActivateWsapOperationHandlers();
 
    // Start listening for bumpers
    SubscribeToBumpers();
 
    // Insert ourselves into the directory so that others can find us
    DirectoryInsert();
 
    // display HTTP service Uri
    LogInfo(LogGroups.Activation, "Service uri: " +
             base.FindServiceAliasFromScheme(Uri.UriSchemeHttp));
}
 
在启动流程中的其它函数是创建dssnewservice模板时加入的,在服务教程中你可以了解到更多。
 
步骤4  写订阅
在Start函数下加入SubscribeToBumpers函数,该函数流程内容如下,下面对其中几个功能进行分解讲解
 
void SubscribeToBumpers()
{
    // Create bumper notification port
    bumper.ContactSensorArrayOperations bumperNotificationPort = new bumper.ContactSensorArrayOperations();
 
    // Subscribe to the bumper service, send notifications to bumperNotificationPort
    _bumperPort.Subscribe(bumperNotificationPort);
 
    // Start listening for Bumper Update notifications
    Activate(
        Arbiter.Receive<bumper.Update>
            (true, bumperNotificationPort, BumperHandler));
}
 
1,  通过创建一个BumperServiceOperations实例建立一个事件通知端口,,该实例用于接收传感器的事件和通告
    // Create notification port
    contactsensor.ContactSensorArrayOperations bumperNotificationPort = new contactsensor.ContactSensorArrayOperations();
 
2,  订阅_bumperPort,并要求发送通告到bimperNotificationPort
_bumperPort.Subscribe(bumperNotificationPort);
 
3,  最后使用Activate建立传感器事件的处理器(handler),Activate建立注册端口以及仲裁器(arbiters)之间关系的通用函数。此处使用的接收仲裁器将传感器的通知信号发送到处理器。
4,   
// Start listening for Bumper Update notifications
    Activate(
        Arbiter.Receive<bumper.Update>
            (true, bumperNotificationPort, BumperHandler));
 
到现在为止,接收到来自传感器的事件通知将调用BumperHandler进行处理
// Start listening for Bumper notifications
    Activate(
        Arbiter.Receive<bumper.Update>
            (true, bumperNotificationPort, BumperHandler));
 
步骤5 建立处理器
在MyTutorial1.cs最后,加入一个事件处理器。在接收到传感器信号后,处理器将在屏幕上显示一个MessageBox
 
        ///<summary>
        /// Handle Bumper Notifications
        ///</summary>
        ///<param name="notification"></param>
        private void BumperHandler(bumper.Update notification)
        {
            if (notification.Body.Pressed)
                     LogConsole("Ouch - the bumper was pressed.");
        }
    }
}
<end of file>
试试看
按F5编译运行该服务,按下接触传感器
 
 
如果一切正常,你将看到每次按传感器都有一个消息弹出
 
在/RoboticsTutorials目录下可以找到完整的代码,以便与你的代码进行比较。
 
总结
在该教程中,你应学到
n         建立一个新的应用程序(实际上也是一个服务)并与其它服务通讯
n         建立一个事件订阅以及一个事件处理器
 
LEGO and MINDSTORMS are trademarks of the LEGO Group. © 2006 The LEGO Group.
fischertechnik is the trademark of fischertechnik GmbH.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值