Example #1
0
            public virtual int Update(ACommand Command, Ev3Brick Brick, int Port, int StartIndex)
            {
                Brick.SensorDevice(Port).ConnectedPort = (Ev3Device.INPORT)Port;
                Brick.SensorDevice(Port).IsConnected   = true;

                return(2);
            }
Example #2
0
        /// <summary>
        /// Set sensor data into controller view model.
        /// </summary>
        /// <param name="ViewModel"></param>
        public void UpdateSensorViewModel(Ev3ControllerMainViewModel ViewModel)
        {
            var Brick = Ev3Brick.GetInstance();

            for (int index = 0; index < 4; index++)
            {
                var DeviceViewModel = ViewModel.SensorViewModelArray[index];
                try
                {
                    var Device = Brick.SensorDeviceArray[index];
                    DeviceViewModel.PortName         = Device.Port;
                    DeviceViewModel.DeviceName       = Device.Device;
                    DeviceViewModel.IsConnected      = true;
                    DeviceViewModel.SensorValue1     = Device.Value1;
                    DeviceViewModel.SensorValue1Unit = "";
                    DeviceViewModel.SensorValue2     = Device.Value2;
                    DeviceViewModel.SensorValue2Unit = "";
                    DeviceViewModel.SensorValue3     = Device.Value3;
                    DeviceViewModel.SensorValue3Unit = "";
                }
#pragma warning disable 168
                catch (NullReferenceException ex)
                {
                    DeviceViewModel.IsConnected      = false;
                    DeviceViewModel.SensorValue1     = 0;
                    DeviceViewModel.SensorValue1Unit = "";
                    DeviceViewModel.SensorValue2     = 0;
                    DeviceViewModel.SensorValue2Unit = "";
                    DeviceViewModel.SensorValue3     = 0;
                    DeviceViewModel.SensorValue3Unit = "";
                }
#pragma warning restore
            }
        }
        /// <summary>
        /// Updater sensor data, especially gyro sensor, device information of Ev3Brick sent
        /// by GetGyroSensor command.
        /// </summary>
        /// <param name="Command">GetGyroSensor command data.</param>
        /// <param name="Brick">Ev3Brick object to set received data.</param>
        public override void Update(ACommand Command, Ev3Brick Brick)
        {
            Debug.Assert(Command != null);
            Debug.Assert(Brick != null);

            if (Command is Command_50_01)
            {
                int Index        = 0;
                int DataTopIndex = 4;
                int DevNum       = Command.ResData[DataTopIndex++];
                for (Index = 0; Index < DevNum; Index++)
                {
                    int   DataIndex = DataTopIndex + (Index * 3);
                    byte  Port      = Command.ResData[DataIndex++];
                    short Velocity  = (short)
                                      ((ushort)Command.ResData[DataIndex] +
                                       (((ushort)Command.ResData[DataIndex + 1]) << 8));
                    var Device = Brick.SensorDevice(Port);
                    Device.ConnectedPort = (Ev3Device.INPORT)Port;
                    Device.IsConnected   = true;
                    Device.Value2        = Velocity;
                    Device.DeviceType    = Ev3SensorDevice.DEVICE_TYPE.SENSOR_DEVICE_GYRO;
                }
            }
        }
        /// <summary>
        /// Update motor device information of Ev3 Brick by GetMotorPower command,
        /// especially motor output power.
        /// </summary>
        /// <param name="Command"></param>
        /// <param name="Brick"></param>
        public override void Update(ACommand Command, Ev3Brick Brick)
        {
            Debug.Assert(Command != null);
            Debug.Assert(Brick != null);

            if (Command is Command_10_01)
            {
                int Index        = 0;
                int DataTopIndex = 4;
                for (Index = 0; Index < 4; Index++)
                {
                    int  DataIndex  = DataTopIndex + Index * 2;
                    byte Connection = Command.ResData[DataIndex++];
                    int  Power      = (int)((sbyte)(Command.ResData[DataIndex]));

                    var Device = Brick.MotorDevice(Index);
                    Device.ConnectedPort = (Ev3Device.OUTPORT)Index;
                    if (0x00 == Connection)
                    {
                        Device.IsConnected = false;
                        Device.Power       = 0;
                    }
                    else
                    {
                        Device.IsConnected = true;
                        Device.Power       = Power;
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Set motor data into controller view model.
        /// </summary>
        /// <param name="ViewModel"></param>
        public void UpdateMotorViewModel(Ev3ControllerMainViewModel ViewModel)
        {
            var Brick = Ev3Brick.GetInstance();

            for (int index = 0; index < 4; index++)
            {
                var DeviceViewModel = ViewModel.MotorViewModelArray[index];
                try
                {
                    var Device = Brick.MotorDeviceArray[index];
                    DeviceViewModel.PortName          = Device.Port;
                    DeviceViewModel.DeviceName        = Device.Device;
                    DeviceViewModel.CurrentOutput     = Device.Power;
                    DeviceViewModel.IsConnected       = Device.IsConnected;
                    DeviceViewModel.CurrentOutputUnit = @"%";
                }
#pragma warning disable 168
                catch (NullReferenceException ex)
                {
                    DeviceViewModel.IsConnected       = false;
                    DeviceViewModel.TargetOutput      = 0;
                    DeviceViewModel.TargetOutputUnit  = "";
                    DeviceViewModel.CurrentOutput     = 0;
                    DeviceViewModel.CurrentOutputUnit = "";
                }
#pragma warning restore
            }
        }
 /// <summary>
 /// Reset Ev3Brick instance.
 /// </summary>
 public static void ResetInstance()
 {
     if (null != _Instance)
     {
         _Instance._Battery = null;
         _Instance._Led     = null;
         _Instance._State   = null;
         _Instance._Version = null;
         if (null != _Instance._SensorDevice)
         {
             for (int index = 0; index < 4; index++)
             {
                 _Instance._SensorDevice[index] = null;
             }
             _Instance._SensorDevice = null;
         }
         if (null != _Instance._MotorDevice)
         {
             for (int index = 0; index < 4; index++)
             {
                 _Instance._MotorDevice[index] = null;
             }
             _Instance._MotorDevice = null;
         }
         _Instance = null;
     }
 }
Example #7
0
        /// <summary>
        /// Update safe state of Ev3 Brick sent by GetSafeState command.
        /// </summary>
        /// <param name="Command">GetSafeState command data.</param>
        /// <param name="Brick">Ev3Brick object to received data.</param>
        public override void Update(ACommand Command, Ev3Brick Brick)
        {
            Debug.Assert(Command != null);
            Debug.Assert(Brick != null);

            if (Command is Command_A0_00)
            {
                int  DataTopIndex = 4;
                byte SafeState    = Command.ResData[DataTopIndex];

                switch (SafeState)
                {
                case 0x00:
                    Brick.State.State = Model.SafeState.SAFE_STATE.SAFE_STATE_SAFE;
                    break;

                case 0x01:
                    Brick.State.State = Model.SafeState.SAFE_STATE.SAFE_STATE_ATTN;
                    break;

                case 0x02:
                    Brick.State.State = Model.SafeState.SAFE_STATE.SAFE_STATE_WARN;
                    break;

                case 0x03:
                    Brick.State.State = Model.SafeState.SAFE_STATE.SAFE_STATE_STOP;
                    break;

                default:
                    Brick.State.State = Model.SafeState.SAFE_STATE.SAFE_STATE_UNKNOWN;
                    break;
                }
            }
        }
 /// <summary>
 /// Returns Ev3Brick instance, singleton object.
 /// </summary>
 /// <returns></returns>
 public static Ev3Brick GetInstance()
 {
     if (null == Ev3Brick._Instance)
     {
         _Instance = new Ev3Brick();
     }
     return(_Instance);
 }
Example #9
0
        /// <summary>
        /// Set safe state.
        /// </summary>
        /// <param name="ViewModel"></param>
        public void UpdateSafeStateViewModel(Ev3ControllerMainViewModel ViewModel)
        {
            var Brick = Ev3Brick.GetInstance();

            ViewModel.SafeStateViewModel.IsConnected = true;
            ViewModel.SafeStateViewModel.SafetyState = Brick.State.StateName;
            ViewModel.SafeStateViewModel.ImageSource = Brick.State.StateImage;
        }
Example #10
0
        /// <summary>
        /// Update AppVersion property in Brick, Ev3Brick object.
        /// </summary>
        /// <param name="Command"></param>
        /// <param name="Brick"></param>
        public override void Update(ACommand Command, Ev3Brick Brick)
        {
            Debug.Assert(Command != null);
            Debug.Assert(Brick != null);

            if (Command is Command_02_00)
            {
                Brick.Version.Major = Command.ResData[4];
                Brick.Version.Minor = Command.ResData[5];
            }
        }
Example #11
0
            public override int Update(ACommand Command, Ev3Brick Brick, int Port, int StartIndex)
            {
                var Device = Brick.SensorDevice(Port);

                Device.Value1     = Command.ResData[StartIndex + 2];
                Device.Value2     = Command.ResData[StartIndex + 3];
                Device.Value3     = Command.ResData[StartIndex + 4];
                Device.DeviceType = Ev3SensorDevice.DEVICE_TYPE.SENSOR_DEVICE_COLOR;

                return(base.Update(Command, Brick, Port, StartIndex) + 3);
            }
Example #12
0
        /// <summary>
        /// Update sensor data by connected device data included in response data.
        /// </summary>
        /// <param name="Command">GetSonicSensor command data.</param>
        /// <param name="Brick">Ev3Brick object to set received data.</param>
        public override void Update(ACommand Command, Ev3Brick Brick)
        {
            Debug.Assert(Command != null);
            Debug.Assert(Brick != null);

            if (Command is Command_F0_00)
            {
                int Index        = 0;
                int DataTopIndex = 4;
                int DataOffset   = 0;

                BrickUpdater_F0_00_DevBase Updater = null;
                for (Index = 0; Index < 4; Index++)
                {
                    byte DeviceType = Command.ResData[DataTopIndex];
                    switch (DeviceType)
                    {
                    case 0x20:
                        Updater = new BrickUpdater_F0_00_Ultrasonic();
                        break;

                    case 0x30:
                        Updater = new BrickUpdater_F0_00_Color();
                        break;

                    case 0x40:
                        Updater = new BrickUpdater_F0_00_Touch();
                        break;

                    case 0x50:
                        Updater = new BrickUpdater_F0_00_Gyro();
                        break;

                    default:
                        Updater = null;
                        break;
                    }
                    try
                    {
                        DataOffset = Updater.Update(Command, Brick, Index, DataTopIndex);
                    }
                    catch (NullReferenceException ex)
                    {
                        Console.WriteLine(ex.Message);
                        DataOffset = 2;
                    }
                    DataTopIndex += DataOffset;
                }
            }
        }
Example #13
0
            public override int Update(ACommand Command, Ev3Brick Brick, int Port, int StartIndex)
            {
                short Distance = (short)
                                 ((ushort)Command.ResData[StartIndex + 2] +
                                  (((ushort)Command.ResData[StartIndex + 3]) << 8));
                byte IsListen = Command.ResData[StartIndex + 4];
                var  Device   = Brick.SensorDevice(Port);

                Device.Value1     = Distance;
                Device.Value2     = IsListen;
                Device.Value3     = 0;
                Device.DeviceType = Ev3SensorDevice.DEVICE_TYPE.SENSOR_DEVICE_ULTRASONIC;

                return(base.Update(Command, Brick, Port, StartIndex) + 3);
            }
Example #14
0
        public override void Update(ACommand Command, Ev3Brick Brick)
        {
            Debug.Assert(Command != null);
            Debug.Assert(Brick != null);

            if (Command is Command_04_00)
            {
                short Voltage = (short)(ushort)
                                ((ushort)Command.ResData[4] + ((ushort)Command.ResData[5] << 8));
                short Current = (short)(ushort)
                                ((ushort)Command.ResData[6] + ((ushort)Command.ResData[7] << 8));

                Brick.Battery.Voltage = Voltage;
                Brick.Battery.Current = Current;
            }
        }
Example #15
0
        /// <summary>
        /// Set target motor and steering value to Ev3Brick instance.
        /// </summary>
        /// <param name="ViewModel"></param>
        public void UpdateTargetMotor(Ev3ControllerMainViewModel ViewModel)
        {
            var Brick = Ev3Brick.GetInstance();

            Brick.Output.MotorOutput = ViewModel.MotorSteerViewModel.TargetMotorOutput;
            Brick.Output.Steering    = ViewModel.MotorSteerViewModel.TargetSteer;

            foreach (Ev3MotorDeviceViewModel MotorDeviceViewModel in
                     ViewModel.MotorViewModelArray)
            {
                if (MotorDeviceViewModel.IsConnected)
                {
                    MotorDeviceViewModel.TargetOutput = ViewModel.MotorSteerViewModel.TargetMotorOutput;
                }
            }
        }
Example #16
0
            public override int Update(ACommand Command, Ev3Brick Brick, int Port, int StartIndex)
            {
                short Angle = (short)
                              ((ushort)Command.ResData[StartIndex + 2] +
                               (((ushort)Command.ResData[StartIndex + 3]) << 8));
                short Speed = (short)
                              ((ushort)Command.ResData[StartIndex + 4] +
                               (((ushort)Command.ResData[StartIndex + 5]) << 8));

                var Device = Brick.SensorDevice(Port);

                Device.Value1     = Angle;
                Device.Value2     = Speed;
                Device.Value3     = 0;
                Device.DeviceType = Ev3SensorDevice.DEVICE_TYPE.SENSOR_DEVICE_GYRO;

                return(base.Update(Command, Brick, Port, StartIndex) + 4);
            }
        /// <summary>
        /// Update sensor device information of Ev3 Brick by GetSensors command.
        /// </summary>
        /// <param name="Command">GetSensors command.</param>
        /// <param name="Brick">Object to set data.</param>
        public override void Update(ACommand Command, Ev3Brick Brick)
        {
            Debug.Assert(Command != null);
            Debug.Assert(Brick != null);

            if (Command is Command_0E_00)
            {
                int Index        = 0;
                int DataTopIndex = 4;
                for (Index = 0; Index < 4; Index++)
                {
                    var Device = Brick.SensorDevice(Index);
                    Device.ConnectedPort = (Ev3Device.INPORT)Index;

                    DEVICE_TYPE DeviceType = DEVICE_TYPE.SENSOR_DEVICE_NO_DEVICE;
                    byte        Type       = Command.ResData[DataTopIndex + Index];
                    bool        HasValue   = DeviceTypeDictionary.ContainsKey(Type);
                    if (HasValue)
                    {
                        DeviceType = DeviceTypeDictionary[Type];
                    }
                    else
                    {
                        DeviceType = DEVICE_TYPE.SENSOR_DEVICE_UNKNOWN;
                    }
                    Device.DeviceType = DeviceType;
                    switch (DeviceType)
                    {
                    case DEVICE_TYPE.SENSOR_DEVICE_ULTRASONIC:
                    case DEVICE_TYPE.SENSOR_DEVICE_GYRO:
                    case DEVICE_TYPE.SENSOR_DEVICE_TOUCH:
                    case DEVICE_TYPE.SENSOR_DEVICE_COLOR:
                    case DEVICE_TYPE.SENSOR_DEVICE_HT_NXT_ACCEL:
                    case DEVICE_TYPE.SENSOR_DEVICE_NXT_TEMP:
                        Device.IsConnected = true;
                        break;

                    default:
                        Device.IsConnected = false;
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Update motor device information of Ev3 Brick by GetMotors command.
        /// </summary>
        /// <param name="Command">GetMotors command.</param>
        /// <param name="Brick">Object to set data.</param>
        public override void Update(ACommand Command, Ev3Brick Brick)
        {
            Debug.Assert(Command != null);
            Debug.Assert(Brick != null);

            if (Command is Command_0C_00)
            {
                int Index        = 0;
                int DataTopIndex = 4;
                for (Index = 0; Index < 4; Index++)
                {
                    var Device = Brick.MotorDevice(Index);
                    Device.ConnectedPort = (Ev3Device.OUTPORT)Index;

                    DEVICE_TYPE DeviceType = DEVICE_TYPE.MOTOR_DEVICE_NO_DEVICE;
                    byte        Type       = Command.ResData[DataTopIndex + Index];
                    bool        HasValue   = DeviceTypeDictionary.ContainsKey(Type);
                    if (HasValue)
                    {
                        DeviceType = DeviceTypeDictionary[Type];
                    }
                    else
                    {
                        DeviceType = DEVICE_TYPE.MOTOR_DEVICE_UNKNOWN;
                    }
                    Device.DeviceType = DeviceType;
                    switch (DeviceType)
                    {
                    case DEVICE_TYPE.MOTOR_DEVICE_MEDIUM_MOTOR:
                    case DEVICE_TYPE.MOTOR_DEVICE_LARGE_MOTOR:
                    case DEVICE_TYPE.MOTOR_DEVICE_UNADJUSTED:
                        Device.IsConnected = true;
                        break;

                    default:
                        Device.IsConnected = false;
                        break;
                    }
                }
            }
        }
Example #19
0
        public override void Update(ACommand Command, Ev3Brick Brick)
        {
            Debug.Assert(Command != null);
            Debug.Assert(Brick != null);

            if (Command is Command_40_00)
            {
                int Index        = 0;
                int DataTopIndex = 4;
                int DevNum       = Command.ResData[DataTopIndex++];
                for (Index = 0; Index < DevNum; Index++)
                {
                    int  DataIndex = DataTopIndex + (Index * 2);
                    byte Port      = Command.ResData[DataIndex++];
                    byte IsTouch   = Command.ResData[DataIndex];
                    var  Device    = Brick.SensorDevice(Port);
                    Device.ConnectedPort = (Ev3Device.INPORT)Port;
                    Device.IsConnected   = true;
                    Device.Value1        = IsTouch;
                    Device.DeviceType    = Ev3SensorDevice.DEVICE_TYPE.SENSOR_DEVICE_TOUCH;
                }
            }
        }
Example #20
0
 public abstract void Update(ACommand Command, Ev3Brick Brick);
 /// <summary>
 /// Update motor device information of Ev3 Brick by GetMotorPower command,
 /// especially motor output power.
 /// </summary>
 /// <param name="Command"></param>
 /// <param name="Brick"></param>
 public override void Update(ACommand Command, Ev3Brick Brick)
 {
     Debug.Assert(Command != null);
     Debug.Assert(Brick != null);
     //Nothing to check in this response data.
 }
Example #22
0
 public override void Update(ACommand Command, Ev3Brick Brick)
 {
     //Nothing to update in this function.
 }
Example #23
0
 /// <summary>
 /// Update Brick data by command data, Command.
 /// </summary>
 /// <param name="Command"></param>
 /// <param name="Brick"></param>
 public override void Update(ACommand Command, Ev3Brick Brick)
 {
     Console.WriteLine(
         string.Format("Nothing to do when {0} command received.", Command.Name));
 }