利用Microsoft Robotics Studio远程控制机器人
2009-05-21 08:30:05 来源:WEB开发网在DssNewService工具中利用这个Contract Identifier可以生成实现了这个Contract的服务,输入如下的命令行:
DssNewService.exe /service:MyRobotMotor /dir:samplesMyRobotMotor /alt:"http://schemas.microsoft.com/robotics/2006/05/motor.html"
然后打开生成的项目,按下面的说明进行相关更改就可以顺利使用了这个项目了。
添加brick Service proxy的引用到项目,并且保证项目中有RoboticsCommon proxy的引用。
图4-添加服务代理的引用
为brick service proxy添加命名空间
1 using brick = Robotics.MyBrickService.Proxy;
添加brick service为伙伴服务
1 [Partner("MyBrickService",
2 Contract = brick.Contract.Identifier,
3 CreationPolicy = PartnerCreationPolicy.UseExistingOrCreate,
4 Optional = false)]
5 brick.MyBrickServiceOperations _myBrickPort = new brick.MyBrickServiceOperations();
6
实现 SetMotorPower消息
1 [ServiceHandler(ServiceHandlerBehavior.Exclusive)]
2 public IEnumerator SetMotorPowerHandler(motor.SetMotorPower setMotorPower)
3 {
4 //flip direction if necessary
5 double revPow = setMotorPower.Body.TargetPower;
6 if (_state.ReversePolarity)
7 {
8 revPow *= -1.0;
9 }
10
11 //update state
12 _state.CurrentPower = revPow;
13
14 //convert to native units
15 int power = (int)Math.Round(revPow * _state.PowerScalingFactor);
16
17 //send hardware specific motor data
18 brick.SetMotor motordata = new brick.SetMotor();
19 motordata.PowerSetpoint = power;
20
21 yield return Arbiter.Choice(
22 _myBrickPort.SendMotorCommand(motordata),
23 delegate(DefaultUpdateResponseType success)
24 {
25 setMotorPower.ResponsePort.Post(success);
26 },
27 delegate(Fault failure)
28 {
29 setMotorPower.ResponsePort.Post(failure);
30 }
31 );
32
33 yield break;
34 }
35
更多精彩
赞助商链接