Esempio n. 1
0
        /// <summary>
        /// Service Start
        /// </summary>
        protected override void Start()
        {
            // Listen on the main port for requests and call the appropriate handler.
            Interleave mainInterleave = ActivateDsspOperationHandlers();

            // Publish the service to the local Node Directory
            DirectoryInsert();

            // display HTTP service Uri
            LogInfo(LogGroups.Console, "Service uri: ");


            //open Scribbler Communications port
            _scribblerComm    = new ScribblerComm();
            _scribblerComPort = new ScribblerDataPort();
            _scribblerComPort = _scribblerComm.Open(6, 38400);

            //add custom handlers to interleave
            mainInterleave.CombineWith(new Interleave(
                                           new TeardownReceiverGroup(),
                                           new ExclusiveReceiverGroup(
                                               Arbiter.ReceiveWithIterator <SensorNotification>(true, _scribblerComPort, SensorNotificationHandler),
                                               Arbiter.ReceiveWithIterator <SetMotor>(true, _mainPort, SetMotorHandler),
                                               Arbiter.ReceiveWithIterator <Ping>(true, _mainPort, PingHandler),
                                               Arbiter.ReceiveWithIterator <SetLED>(true, _mainPort, SetLEDHandler),
                                               Arbiter.ReceiveWithIterator <PlayTone>(true, _mainPort, PlayToneHandler)
                                               ),
                                           new ConcurrentReceiverGroup()
                                           ));

            //play startup tone
            _scribblerComm.SendCommand(new ScribblerCommand((byte)ScribblerHelper.Commands.SET_SPEAKER, 20, 100, 200));
        }
Esempio n. 2
0
        protected override void Start()
        {
            LogVerbose(LogGroups.Console, "Configuring Lego Contact Sensor Array... ");

            #region Initialize Service State
            bool saveState = false;
            if (_bumperConfigState == null)
            {
                _bumperConfigState = new LegoNxtBumperState();
                _bumperConfigState.ContactSensorArrayState = new bumper.ContactSensorArrayState();
                saveState = true;
            }
            else if (_bumperConfigState.Status != LegoNxtBumperStatus.Uninitialized)
            {
                _bumperConfigState.Status = LegoNxtBumperStatus.Uninitialized;
                saveState = true;
            }

            if (saveState)
            {
                SaveState(_bumperConfigState);
                saveState = false;
            }

            #endregion

            // Listen for each of my handlers
            Interleave mainInterleave = ActivateDsspOperationHandlers();

            // Add the internal only handlers
            mainInterleave.CombineWith(
                new Interleave(
                    new ExclusiveReceiverGroup(
                        Arbiter.Receive <UpdateStatus>(true, _internalPort, UpdateStatusHandler)
                        ),
                    new ConcurrentReceiverGroup()));

            // Publish the service to the local Node Directory
            DirectoryInsert();

            #region Wait for Configuration to be completed
            Port <bool> donePort = new Port <bool>();
            SpawnIterator <Port <bool> >(donePort, InitializeBumperConnection);
            Activate(Arbiter.Receive(false, donePort,
                                     delegate(bool success)
            {
                if (success)
                {
                    // display HTTP service Uri
                    LogInfo(LogGroups.Console, "Service uri: ");
                }
                else
                {
                    LogError(LogGroups.Console, "LEGO Bumper service failed to start.");
                    _mainPort.Post(new DsspDefaultDrop());
                }
            }));
            #endregion
        }
Esempio n. 3
0
        private void subscribeCamera(CameraInfo argCi)
        {
            // Subscribe to the simulator camera, using a handler
            // that will only update the GUI if this camera is
            // selected (because there is currently no way to
            // unsubscribe from a camera.
            //if (ci.Camera.IsRealTimeCamera)
            //{
            CameraInfo ci = argCi;

            ci.Port = new Port <System.Drawing.Bitmap>();
            ci.Camera.Subscribe(ci.Port);
            Interleave interleave = new Interleave(new ExclusiveReceiverGroup(), new ConcurrentReceiverGroup());

            interleave.CombineWith(
                new Interleave(new ExclusiveReceiverGroup(
                                   Arbiter.Receive(true, ci.Port,
                                                   delegate(System.Drawing.Bitmap inbmp)
            {
                if (interleave.PendingExclusiveCount <= 1 && curCamera == ci)
                {
                    if (DateTime.Now.Subtract(lastFrameTime).Milliseconds >= frameInterval)
                    {
                        lastFrameTime = DateTime.Now;
                        updateImageDisplay(inbmp);
                    }
                }
            })),
                               new ConcurrentReceiverGroup()));
            Arbiter.Activate(throttledQueue, interleave);
            //}
            //else
            //{
            //    // If it's not a real time camera, we have to start a loop
            //    // to query it
            //    new Thread(new ThreadStart(delegate()
            //    {
            //        var resultPort = new PortSet<System.Drawing.Bitmap, Exception>();
            //        while (shouldStay)
            //        {
            //            if (curCamera == ci)
            //            {
            //                // The throttledQueue has only 1 thread, so concurrent execution
            //                // will not happen here if the updateImageDisplay handler cannot
            //                // keep up with the rate at which we're querying frames.
            //                ci.Camera.CaptureScene(System.Drawing.Imaging.ImageFormat.Bmp, resultPort);
            //                Arbiter.Activate(throttledQueue, Arbiter.Choice(resultPort,
            //                    updateImageDisplay,
            //                    delegate(Exception e)
            //                    {
            //                        Console.WriteLine(e);
            //                    }));
            //            }
            //            Thread.Sleep(frameInterval);
            //        }
            //    })).Start();
            //}
        }
Esempio n. 4
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");
            }
                               )
                );
        }
Esempio n. 5
0
        /// <summary>
        /// Service start
        /// </summary>
        protected override void Start()
        {
            if (_state == null)
            {
                _state = new ChrUm6OrientationSensorState();
                _state.ChrUm6OrientationSensorConfig          = new ChrUm6OrientationSensorConfig();
                _state.ChrUm6OrientationSensorConfig.CommPort = 0;

                SaveState(_state);
            }
            else
            {
                // Clear old Chr readings
                _state.Quaternion = null;
            }

            _httpUtilities = DsspHttpUtilitiesService.Create(Environment);

            if (_state.ChrUm6OrientationSensorConfig == null)
            {
                _state.ChrUm6OrientationSensorConfig = new ChrUm6OrientationSensorConfig();
            }

            // Publish the service to the local Node Directory
            DirectoryInsert();

            _chrConnection = new ChrConnection(_state.ChrUm6OrientationSensorConfig, _chrDataPort);

            SpawnIterator(ConnectToChrUm6OrientationSensor);

            // Listen on the main port for requests and call the appropriate handler.
            Interleave mainInterleave = ActivateDsspOperationHandlers();

            mainInterleave.CombineWith(new Interleave(new ExclusiveReceiverGroup(
                                                          Arbiter.Receive <short[]>(true, _chrDataPort, DataReceivedHandler),
                                                          Arbiter.Receive <Exception>(true, _chrDataPort, ExceptionHandler),
                                                          Arbiter.Receive <string>(true, _chrDataPort, MessageHandler)
                                                          ),
                                                      new ConcurrentReceiverGroup()));

            //base.Start(); -- can't have it here, we already started mainInterleave and added to directory via DirectoryInsert
        }
Esempio n. 6
0
        /// <summary>
        /// Service Start
        /// </summary>
        protected override void Start()
        {
            if (_state == null)
            {
                //initialize state
                _state           = new ScribblerState();
                _state.ComPort   = 0;
                _state.RobotName = null;

                //motors initially stopped
                _state.MotorLeft  = 100;
                _state.MotorRight = 100;

                SaveState(_state);
            }

            // display HTTP service Uri
            LogInfo(LogGroups.Console, "Service uri: ");

            //open Scribbler Communications port
            if (ConnectToScribbler())
            {
                // Listen for a single Serial port request with an acknowledgement
                Activate(Arbiter.ReceiveWithIterator <SendScribblerCommand>(false, _scribblerComPort, SendScribblerCommandHandler));

                PollTimer           = new System.Timers.Timer();
                PollTimer.Interval  = TimerDelay;
                PollTimer.AutoReset = true;
                PollTimer.Elapsed  += new System.Timers.ElapsedEventHandler(PollTimer_Elapsed);
                PollTimer.Start();


                //play startup tone
                PlayToneBody startTone = new PlayToneBody(200, 1000, 2000);
                _mainPort.Post(new PlayTone(startTone));

                //debug
                //ScribblerCommand cmd = new ScribblerCommand((byte)ScribblerHelper.Commands.GET_INFO);
                //SendScribblerCommand sendcmd = new SendScribblerCommand(cmd);
                //_scribblerComPort.Post(sendcmd);


                //fix state
                _state.MotorLeft  = 100;
                _state.MotorRight = 100;
                _state.LEDLeft    = false;
                _state.LEDRight   = false;
                _state.LEDCenter  = false;
            }
            else
            {
                //no scribbler found. Open state page for manual settings.
                //OpenServiceInBrowser();
            }


            // Listen on the main port for requests and call the appropriate handler.
            Interleave mainInterleave = ActivateDsspOperationHandlers();

            //for HttpPost
            _httpUtilities = DsspHttpUtilitiesService.Create(Environment);

            // Publish the service to the local Node Directory
            DirectoryInsert();

            //add custom handlers to interleave
            mainInterleave.CombineWith(new Interleave(
                                           new TeardownReceiverGroup(),
                                           new ExclusiveReceiverGroup(
                                               Arbiter.ReceiveWithIterator <SetMotors>(true, _mainPort, SetMotorHandler),
                                               Arbiter.ReceiveWithIterator <SetLED>(true, _mainPort, SetLEDHandler),
                                               Arbiter.ReceiveWithIterator <SetAllLEDs>(true, _mainPort, SetAllLEDsHandler),
                                               Arbiter.ReceiveWithIterator <PlayTone>(true, _mainPort, PlayToneHandler),
                                               Arbiter.ReceiveWithIterator <SetName>(true, _mainPort, SetNameHandler),
                                               Arbiter.ReceiveWithIterator <ScribblerResponseMessage>(true, _mainPort, ScribblerResponseHandler)
                                               ),
                                           new ConcurrentReceiverGroup()
                                           ));
        }
Esempio n. 7
0
        /// <summary>
        /// Service Start
        /// </summary>
        protected override void Start()
        {
            if (_state == null)
            {
                //initialize state
                _state           = new ScribblerState();
                _state.ComPort   = 0;
                _state.RobotName = null;

                //motors initially stopped
                _state.MotorLeft  = 100;
                _state.MotorRight = 100;

                //_state.LightLeftConfig = new SensorConfig();
                //_state.LightRightConfig = new SensorConfig();
                //_state.LightCenterConfig = new SensorConfig();

                SaveState(_state);
            }

            // Listen on the main port for requests and call the appropriate handler.
            Interleave mainInterleave = ActivateDsspOperationHandlers();

            // Publish the service to the local Node Directory
            DirectoryInsert();

            // display HTTP service Uri
            LogInfo(LogGroups.Console, "Service uri: ");

            //open Scribbler Communications port
            if (ConnectToScribbler())
            {
                // Listen for a single Serial port request with an acknowledgement
                Activate(Arbiter.ReceiveWithIterator <SendScribblerCommand>(false, _scribblerComPort, SendScribblerCommandHandler));


                //add custom handlers to interleave
                mainInterleave.CombineWith(new Interleave(
                                               new TeardownReceiverGroup(),
                                               new ExclusiveReceiverGroup(
                                                   Arbiter.ReceiveWithIterator <SetMotor>(true, _mainPort, SetMotorHandler),
                                                   Arbiter.ReceiveWithIterator <SetLED>(true, _mainPort, SetLEDHandler),
                                                   Arbiter.ReceiveWithIterator <PlayTone>(true, _mainPort, PlayToneHandler),
                                                   //Arbiter.ReceiveWithIterator<ConfigureSensor>(true, _mainPort, ConfigureSensorHandler),
                                                   Arbiter.ReceiveWithIterator <SetName>(true, _mainPort, SetNameHandler)
                                                   ),
                                               new ConcurrentReceiverGroup()
                                               ));


                PollTimer           = new System.Timers.Timer();
                PollTimer.Interval  = TimerDelay;
                PollTimer.AutoReset = true;
                PollTimer.Elapsed  += new System.Timers.ElapsedEventHandler(PollTimer_Elapsed);
                PollTimer.Start();


                //play startup tone
                PlayToneBody startTone = new PlayToneBody(200, 1000, 2000);
                PlayTone     sendcmd   = new PlayTone();
                sendcmd.Body = startTone;
                _mainPort.Post(sendcmd);
            }
        }
Esempio n. 8
0
 private void subscribeCamera(CameraInfo argCi)
 {
     // Subscribe to the simulator camera, using a handler
     // that will only update the GUI if this camera is
     // selected (because there is currently no way to 
     // unsubscribe from a camera.
     //if (ci.Camera.IsRealTimeCamera)
     //{
     CameraInfo ci = argCi;
     ci.Port = new Port<System.Drawing.Bitmap>();
     ci.Camera.Subscribe(ci.Port);
     Interleave interleave = new Interleave(new ExclusiveReceiverGroup(), new ConcurrentReceiverGroup());
     interleave.CombineWith(
         new Interleave(new ExclusiveReceiverGroup(
             Arbiter.Receive(true, ci.Port,
             delegate(System.Drawing.Bitmap inbmp)
             {
                 if (interleave.PendingExclusiveCount <= 1 && curCamera == ci)
                     if (DateTime.Now.Subtract(lastFrameTime).Milliseconds >= frameInterval)
                     {
                         lastFrameTime = DateTime.Now;
                         updateImageDisplay(inbmp);
                     }
             })),
         new ConcurrentReceiverGroup()));
     Arbiter.Activate(throttledQueue, interleave);
     //}
     //else
     //{
     //    // If it's not a real time camera, we have to start a loop
     //    // to query it
     //    new Thread(new ThreadStart(delegate()
     //    {
     //        var resultPort = new PortSet<System.Drawing.Bitmap, Exception>();
     //        while (shouldStay)
     //        {
     //            if (curCamera == ci)
     //            {
     //                // The throttledQueue has only 1 thread, so concurrent execution
     //                // will not happen here if the updateImageDisplay handler cannot
     //                // keep up with the rate at which we're querying frames.
     //                ci.Camera.CaptureScene(System.Drawing.Imaging.ImageFormat.Bmp, resultPort);
     //                Arbiter.Activate(throttledQueue, Arbiter.Choice(resultPort,
     //                    updateImageDisplay,
     //                    delegate(Exception e)
     //                    {
     //                        Console.WriteLine(e);
     //                    }));
     //            }
     //            Thread.Sleep(frameInterval);
     //        }
     //    })).Start();
     //}
 }
Esempio n. 9
0
        protected override void Start()
        {
            //configure default state
            if (_state == null)
            {
                _state = new LegoNxtBatteryState();
                _state.PollDelayTime   = 10000; //10 seconds
                _state.MaxBatteryPower = 9.0;

                SaveState(_state);
            }

            // Listen on the main port for requests and call the appropriate handler.
            Interleave mainInterleave = ActivateDsspOperationHandlers();

            //listen on alternate service port for requests and call the appropriate handler.
            mainInterleave.CombineWith(new Interleave(
                                           new TeardownReceiverGroup(
                                               Arbiter.Receive <DsspDefaultDrop>(false, _batteryPort, DefaultDropHandler)
                                               ),
                                           new ExclusiveReceiverGroup(
                                               Arbiter.ReceiveWithIterator <battery.Replace>(true, _batteryPort, ReplaceHandler),
                                               Arbiter.ReceiveWithIterator <battery.Subscribe>(true, _batteryPort, SubscribeHandler)
                                               ),
                                           new ConcurrentReceiverGroup(
                                               Arbiter.ReceiveWithIterator <battery.Get>(true, _batteryPort, GetHandler),
                                               Arbiter.Receive <DsspDefaultLookup>(true, _batteryPort, DefaultLookupHandler)
                                               )
                                           ));

            // Publish the service to the local Node Directory
            DirectoryInsert();

            // display HTTP service Uri
            LogInfo(LogGroups.Console, "Service uri: ");

            //wait until the nxt is connected to start polling for the battery level

            // Create a temporary notification port
            legoNXT.LegoNxtOperations _notificationPort = new legoNXT.LegoNxtOperations();

            Activate(
                Arbiter.Choice(_legoPort.Subscribe(_notificationPort),
                               delegate(SubscribeResponseType Rsp)
            {
                LogInfo("Lego NXT battery initial subscription success.  Waiting for NXT to connect.");

                //Subscription was successful, start listening for sensor change notifications
                Activate(
                    Arbiter.Receive <legoNXT.Configure>(false, _notificationPort,
                                                        delegate(legoNXT.Configure msg)
                {
                    //start polling
                    LogInfo("NXT Battery service notified of NXT connection");
                    _timerPort.Post(DateTime.Now);
                    Activate(Arbiter.Receive(true, _timerPort, TimerHandler));
                })
                    );
            },
                               delegate(Fault F)
            {
                LogError("Lego NXT battery initial subscription failed");
            }
                               )
                );
        }