public SensorPacketQuerier(RoombaCommandExecutor cmdExecutor, SensorPacket sensorPacket, int sensorPacketSize, int frequency)
 {
     this.cmdExecutor      = cmdExecutor;
     this.sensorPacket     = sensorPacket;
     this.sensorPacketSize = sensorPacketSize;
     this.frequency        = frequency;
 }
Beispiel #2
0
            /// <summary>
            /// Update the sensor state. The robot will send back one of four different sensor data packets in response to a Sensor command,
            /// depending on the value of the packet code data byte. The data bytes are specified below in the order in which they will be sent.
            /// A packet code value of 0 sends all of the data bytes. A value of 1 through 3 sends a subset of the sensor data.
            /// Some of the sensor data values are 16 bit values. These values are sent as two bytes, high byte first.
            /// </summary>
            /// <param name="packet"></param>
            public void UpdateSensorState(SensorPacket packet)
            {
                Sensors(packet);

                switch (packet)
                {
                case SensorPacket.ALL_SENSORS:
                    robot.Receive(ref sensor_state, 0, 26);
                    break;

                case SensorPacket.PACKET_1:
                    robot.Receive(ref sensor_state, 0, 10);
                    break;

                case SensorPacket.PACKET_2:
                    robot.Receive(ref sensor_state, 10, 6);
                    break;

                case SensorPacket.PACKET_3:
                    robot.Receive(ref sensor_state, 16, 10);
                    break;

                default:
                    break;
                }
            }
Beispiel #3
0
        public void SubscribeToSensorPacket(SensorPacket sensorPacket, int sensorPacketSize, int frequency,
                                            SensorPacketQuerier.SensorDataReceivedDelegate dataReceivedDelegate)
        {
            SensorPacketQuerier querier = new SensorPacketQuerier(cmdExecutor, sensorPacket, sensorPacketSize, frequency);

            querier.SensorDataReceived += dataReceivedDelegate;
            queriers.Push(querier);
            querier.Start();
        }
Beispiel #4
0
        public byte[] querySensorPacket(SensorPacket sensorPacket, int packetSize)
        {
            byte[] parameters = new byte[]
            {
                (byte)sensorPacket
            };

            lock (querySensorLock)
            {
                this.ExecCommand(RoombaCommand.QuerySensorPacket, parameters);
                return(this.serialPortController.Read(packetSize));
            }
        }
Beispiel #5
0
        public void Parse(byte[] data)
        {
            int length;
            int pointer = 0;

            while (pointer < data.Length)
            {
                SensorPacket packet = (SensorPacket)data[pointer];
                length = Lengths[packet];
                byte[] payload = new byte[length];
                Array.Copy(data, ++pointer, payload, 0, length);
                pointer += length;
                Feeds[packet].OnNext(payload);
            }
        }
Beispiel #6
0
        public static async void InsertSensorPacket(SensorPacket packetData)
        {
            long count = victims.Find(v => v.VictimID == packetData.SensorID).CountDocuments();

            if (count > 0)
            {
                var filter = Builders <Victim> .Filter.Eq(v => v.VictimID, packetData.SensorID);

                var update = Builders <Victim> .Update.Push(v => v.SensorReads, packetData);

                await victims.FindOneAndUpdateAsync(filter, update);

                Console.WriteLine(String.Format("Saved packet with for sensor {0}", packetData.SensorID));
                packetData.Print();
            }
        }
Beispiel #7
0
        private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
        {
            try
            {
                Console.WriteLine("NEW PACKET: ");
                var input = string.Format("0{0:X}", eventArgs.BluetoothAddress);
                // System.Console.WriteLine(input);
                var output = string.Join(":", Enumerable.Range(0, 6).Reverse()
                                         .Select(i => input.Substring(i * 2, 2)));
                var test = string.Join(":", output.Split(':'));
                Console.WriteLine("  BT_ADDR : {0}", output); //return string.Format("0x{0:X}", temp);
                Console.WriteLine("  SIGNAL  : {0}", eventArgs.RawSignalStrengthInDBm);

                List <byte> packetData = new List <byte>();
                int         count      = 0;
                foreach (var section in eventArgs.Advertisement.DataSections)
                {
                    var    dataReader = DataReader.FromBuffer(section.Data);
                    byte[] buffer     = new byte[section.Data.Length];
                    dataReader.ReadBytes(buffer);
                    packetData.AddRange(buffer);
                    string hex    = BitConverter.ToString(buffer);
                    string packet = hex.Replace("-", ":");
                    Console.WriteLine("  DATA {0}  : {1}", count, packet);
                    count++;
                }
                Console.WriteLine("  STR     : {0}", Encoding.Default.GetString(packetData.ToArray()));
                Console.WriteLine("");
                Console.WriteLine("");
                Console.WriteLine("Inserting packets");
                for (int i = 1; i < 100; i++)
                {
                    SensorPacket pc = new SensorPacket();
                    pc.SensorID = i;
                    DBWrapper.InsertSensorPacket(pc);
                }
            } catch (Exception ex)
            {
                Console.WriteLine("Error reeiving packet");
            }
        }
        private void AnalyzeData(SensorPacket[] sensorData)
        {
            // Get the approximate current time from the packets
            _currentTime = GetCurrentTime(sensorData);

            // Make sure the Arduino is there
            ArduinoControlSender.Instance.CheckArduinoStatus();

            #region Automation Decision Making
            // Get the averages of greenhouse readings
            _avgTemp  = GetTemperatureAverage(sensorData);
            _avgLight = GetLightAverage(sensorData);

            // Determine what state we need to go to and then create a KVP for it and send it
            GreenhouseState goalTempState = StateMachineContainer.Instance.Temperature.DetermineState(_avgTemp);
            if (goalTempState == GreenhouseState.HEATING || goalTempState == GreenhouseState.COOLING || goalTempState == GreenhouseState.WAITING_FOR_DATA)
            {
                _tempState = new KeyValuePair <IStateMachine, GreenhouseState>(StateMachineContainer.Instance.Temperature, goalTempState);
                // Send the KVP to the control sender
                ArduinoControlSender.Instance.SendCommand(_tempState);
            }

            // Get state for lighting state machines, send commands
            foreach (LightingStateMachine stateMachine in StateMachineContainer.Instance.LightStateMachines)
            {
                // Get the packet from the zone we're currently operating on
                SensorPacket    packet         = sensorData.Where(p => p.Zone == stateMachine.Zone).Single();
                double          lightingValue  = packet.Light;
                GreenhouseState goalLightState = stateMachine.DetermineState(_currentTime, lightingValue);
                if (goalLightState == GreenhouseState.LIGHTING || goalLightState == GreenhouseState.SHADING || goalLightState == GreenhouseState.WAITING_FOR_DATA)
                {
                    _lightState = new KeyValuePair <ITimeBasedStateMachine, GreenhouseState>(stateMachine, goalLightState);
                    ArduinoControlSender.Instance.SendCommand(_lightState);
                }
            }

            // Get states for watering state machines, send commands
            foreach (WateringStateMachine stateMachine in StateMachineContainer.Instance.WateringStateMachines)
            {
                // Get the packet from the zone we're currently operating on
                SensorPacket packet        = sensorData.Where(p => p.Zone == stateMachine.Zone).Single();
                double       moistureValue = (packet.Probe1 + packet.Probe2) / 2;

                // Get the state we need to transition into, then go send a command appropriate to that
                GreenhouseState goalWaterState = stateMachine.DetermineState(_currentTime, moistureValue);
                if (goalWaterState == GreenhouseState.WATERING || goalWaterState == GreenhouseState.WAITING_FOR_DATA)
                {
                    _waterState = new KeyValuePair <ITimeBasedStateMachine, GreenhouseState>(stateMachine, goalWaterState);
                    ArduinoControlSender.Instance.SendCommand(_waterState);
                }
            }

            // Get state for shading state machine, send commands
            GreenhouseState goalShadeState = StateMachineContainer.Instance.Shading.DetermineState(_avgTemp);
            if (goalShadeState == GreenhouseState.SHADING || goalShadeState == GreenhouseState.WAITING_FOR_DATA)
            {
                _shadeState = new KeyValuePair <IStateMachine, GreenhouseState>(StateMachineContainer.Instance.Shading, goalShadeState);
                ArduinoControlSender.Instance.SendCommand(_shadeState);
            }
            #endregion
        }
Beispiel #9
0
 /// <summary>
 /// Update the sensor state.
 /// </summary>
 /// <param name="packet"></param>
 public void Update(SensorPacket packet)
 {
     robot.SCI.UpdateSensorState(packet);
 }
Beispiel #10
0
 /// <summary>
 /// Requests the SCI to send a packet of sensor data bytes. The user can select one of four different sensor packets. The sensor
 /// data packets are explained in more detail in the next section. The SCI must be in passive, safe, or full mode to accept this
 /// command. This command does not change the mode.
 /// </summary>
 /// <param name="packet"></param>
 public void Sensors(SensorPacket packet)
 {
     robot.Send(new byte[] { 142, (byte)packet });
 }
Beispiel #11
0
 public void QuerySensor(SensorPacket sensorID)
 {
     QueueCommandParameter(OpCode.Sensors, (int)SensorPacket.BatteryCharge);
     SendCommand();
 }
Beispiel #12
0
 /// <summary>
 /// Update the sensor state.
 /// </summary>
 /// <param name="packet"></param>
 public void Update(SensorPacket packet)
 {
     robot.SCI.UpdateSensorState(packet);
 }
Beispiel #13
0
            /// <summary>
            /// Update the sensor state. The robot will send back one of four different sensor data packets in response to a Sensor command,
            /// depending on the value of the packet code data byte. The data bytes are specified below in the order in which they will be sent.
            /// A packet code value of 0 sends all of the data bytes. A value of 1 through 3 sends a subset of the sensor data.
            /// Some of the sensor data values are 16 bit values. These values are sent as two bytes, high byte first.
            /// </summary>
            /// <param name="packet"></param>
            public void UpdateSensorState(SensorPacket packet)
            {
                Sensors(packet);

                switch (packet)
                {
                    case SensorPacket.ALL_SENSORS:
                        robot.Receive(ref sensor_state, 0, 26);
                        break;
                    case SensorPacket.PACKET_1:
                        robot.Receive(ref sensor_state, 0, 10);
                        break;
                    case SensorPacket.PACKET_2:
                        robot.Receive(ref sensor_state, 10, 6);
                        break;
                    case SensorPacket.PACKET_3:
                        robot.Receive(ref sensor_state, 16, 10);
                        break;
                    default:
                        break;
                }
            }
Beispiel #14
0
 /// <summary>
 /// Requests the SCI to send a packet of sensor data bytes. The user can select one of four different sensor packets. The sensor
 /// data packets are explained in more detail in the next section. The SCI must be in passive, safe, or full mode to accept this
 /// command. This command does not change the mode.
 /// </summary>
 /// <param name="packet"></param>
 public void Sensors(SensorPacket packet)
 {
     robot.Send(new byte[] { 142, (byte)packet });
 }