利用Microsoft Robotics Studio远程控制机器人
2009-05-21 08:30:05 来源:WEB开发网订阅服务
我们创建的大多数的服务是为了获取传感器的数据为目的的,这就需要这些服务去订阅Birck Service,他们使用上面所实现的自定义订阅来订阅相应的传感器数据,下面我们看一个MyRobotBumper服务,这个服务实现了Robotics Common中的ContactSensorArray服务的Contract,和上面所说Motor Service的创建方式类似。
1 using bumper = Microsoft.Robotics.Services.ContactSensor.Proxy;
2 using brick = Robotics.MyBrickService.Proxy;
3 using submgr = Microsoft.Dss.Services.SubscriptionManager;
4
5 private void SubscribeToNXT()
6 {
7 // Create a notification port
8 brick..MyBrickServiceOperations _notificationPort = new brick.MyBrickServiceOperations();
9 //create a custom subscription request
10 brick.MySubscribeRequestType request = new brick.MySubscribeRequestType();
11 //select only the sensor and ports we want
12 //NOTE: this name must match the names you define in MyBrickService
13 request.Sensors = new List();
14 foreach (bumper.ContactSensor sensor in _state.Sensors)
15 {
16 //Use Identifier as the port number of the sensor
17 request.Sensors.Add("TOUCH" + sensor.Identifier);
18 }
19 //Subscribe to the brick and wait for a response
20 Activate(
21 Arbiter.Choice(_myBrickPort.SelectiveSubscribe(request, _notificationPort),
22 delegate(SubscribeResponseType Rsp)
23 {
24 //update our state with subscription status
25 subscribed = true;
26 LogInfo("MyRobotBumper subscription success");
27 //Subscription was successful, start listening for sensor change notifications
28 Activate(
29 Arbiter.Receive
30 (true, _notificationPort, SensorNotificationHandler)
31 );
32 },
33 delegate(Fault F)
34 {
35 LogError("MyRobotBumper subscription failed");
36 })
37 );
38 }
39 private void SensorNotificationHandler(brick.Replace notify)
40 {
41 //update state
42 foreach (bumper.ContactSensor sensor in _state.Sensors)
43 {
44 bool newval = notify.Body.SensorPort[sensor.Identifier - 1] == 1 ? true : false;
45 bool changed = (sensor.pssed != newval);
46 sensor.TimeStamp = DateTime.Now;
47 sensor.pssed = newval;
48 if (changed)
49 {
50 //notify subscribers on any bumper pssed or unpssed
51 _subMgrPort.Post(new submgr.Submit(sensor, DsspActions.UpdateRequest));
52 }
53 }
54 }
55
更多精彩
赞助商链接