Ejemplo n.º 1
0
        /// <summary>
        /// Subscribe to appropriate sensor type and port on NXT
        /// Hook up notifications
        /// and then post success or failure
        /// <param name="successPort"></param>
        /// </summary>
        private IEnumerator <ITask> SubscribeToNXT(Port <bool> successPort)
        {
            if (!_subscribed)
            {
                //create a custom subscription request
                nxt.CustomSubscribeRequestType request = new nxt.CustomSubscribeRequestType();

                //select only the sensor and port we want
                //NOTE: this name must match the NXT sensor name. (see NXT readme)
                request.Sensors = new Collection <nxt.SensorDefinition>();

                foreach (LegoNxtBumperConfig config in _bumperConfigState.BumperConfigList)
                {
                    nxt.SensorDefinition sensor = new nxt.SensorDefinition();
                    sensor.Type = config.SensorType;
                    sensor.Port = config.HardwareIdentifier;
                    request.Sensors.Add(sensor);
                }

                //Subscribe to the NXT and wait for a response
                yield return
                    (Arbiter.Choice(_legoPort.SelectiveSubscribe(request, _notificationPort),
                                    delegate(SubscribeResponseType Rsp)
                {
                    //update our state with subscription status
                    _subscribed = true;
                },
                                    delegate(Fault fail)
                {
                    LogError("Bumper subscription failed", fail);
                    successPort.Post(false);
                }
                                    ));

                if (_subscribed)
                {
                    LogInfo("Bumper subscription success");

                    //Subscription was successful, start listening for sensor change notifications
                    Activate(
                        Arbiter.Receive <nxt.Configure>(true, _notificationPort, SensorNotificationHandler)
                        );
                }
            }

            successPort.Post(_subscribed);

            yield break;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Subscribe to appropriate sensor type and port on NXT
        /// </summary>
        private void SubscribeToNXT()
        {
            // Create a notification port
            legoNXT.LegoNxtOperations _notificationPort = new legoNXT.LegoNxtOperations();

            //create a custom subscription request
            legoNXT.CustomSubscribeRequestType request = new legoNXT.CustomSubscribeRequestType();

            //select only the sensor and port we want
            //NOTE: this name must match the NXT sensor name. (see NXT readme)
            request.Sensors = new Collection <legoNXT.SensorDefinition>();
            legoNXT.SensorDefinition sensor = new legoNXT.SensorDefinition();
            sensor.Type = legoNXT.SensorDefinition.SensorType.Encoder;
            sensor.Port = _state.HardwareIdentifier;
            request.Sensors.Add(sensor);

            //Subscribe to the NXT and wait for a response
            Activate(
                Arbiter.Choice(_legoPort.SelectiveSubscribe(request, _notificationPort),
                               delegate(SubscribeResponseType Rsp)
            {
                //update our state with subscription status
                _subscribed = true;

                LogInfo(sensor.Type + sensor.Port + " subscription success");

                //Subscription was successful, start listening for sensor change notifications
                _mainInterleave.CombineWith(new Interleave(
                                                new ExclusiveReceiverGroup(
                                                    Arbiter.Receive <legoNXT.Configure>(true, _notificationPort, SensorNotificationHandler)
                                                    ),
                                                new ConcurrentReceiverGroup()
                                                ));

                //Activate(

                //);
            },
                               delegate(Fault F)
            {
                LogError(sensor.Type + sensor.Port + "subscription failed");
            }
                               )
                );
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Subscribe to appropriate sensor type and port on NXT
        /// </summary>
        private void SubscribeToNXT()
        {
            // Create a notification port
            legoNXT.LegoNxtOperations _notificationPort = new legoNXT.LegoNxtOperations();

            //create a custom subscription request
            legoNXT.CustomSubscribeRequestType request = new legoNXT.CustomSubscribeRequestType();

            //select only the sensor and port we want
            //NOTE: this name must match the NXT sensor name. (see NXT readme)
            request.Sensors = new Collection <legoNXT.SensorDefinition>();

            legoNXT.SensorDefinition sensor1 = new legoNXT.SensorDefinition();
            sensor1.Type = legoNXT.SensorDefinition.SensorType.Motor;
            sensor1.Port = _state.Motor1Port;
            request.Sensors.Add(sensor1);

            legoNXT.SensorDefinition sensor2 = new legoNXT.SensorDefinition();
            sensor2.Type = legoNXT.SensorDefinition.SensorType.Motor;
            sensor2.Port = _state.Motor2Port;
            request.Sensors.Add(sensor2);

            //Subscribe to the NXT and wait for a response
            Activate(
                Arbiter.Choice(_legoPort.SelectiveSubscribe(request, _notificationPort),
                               delegate(SubscribeResponseType Rsp)
            {
                //update our state with subscription status
                _subscribed = true;

                LogInfo("lego backup subscription success");

                //Subscription was successful, start listening for sensor change notifications
                Activate(
                    Arbiter.Receive <legoNXT.Configure>(true, _notificationPort, SensorNotificationHandler)
                    );
            },
                               delegate(Fault F)
            {
                LogError(sensor1.Type + sensor1.Port + "subscription failed");
            }
                               )
                );
        }