示例#1
0
        public DistributedClocksOpMode(DeviceTypeDCOpMode opMode)
        {
            var assignActivate = EsiUtilities.ParseHexDecString(opMode.AssignActivate);

            this.Name        = opMode.Name;
            this.Description = opMode.Desc;

            // Cyclic mode
            this.CycleTimeSyncUnit_IsEnabled = (assignActivate & 0x100) > 0;
            this.CycleTimeSyncUnit           = (int)(Math.Pow(10, 9) / 100); // improve! remove magic number

            // SYNC 0
            this.CycleTimeSync0_IsEnabled = (assignActivate & 0x200) > 0;
            this.CycleTimeSync0           = Convert.ToInt32(opMode.CycleTimeSync0?.Value);
            this.CycleTimeSync0_Factor    = Convert.ToInt32(opMode.CycleTimeSync0?.Factor);
            this.ShiftTimeSync0           = Convert.ToInt32(opMode.ShiftTimeSync0?.Value);
            this.ShiftTimeSync0_Factor    = Convert.ToInt32(opMode.ShiftTimeSync0?.Factor);
            this.ShiftTimeSync0_Input     = Convert.ToBoolean(opMode.ShiftTimeSync0?.Input);

            // SYNC 1
            this.CycleTimeSync1_IsEnabled = (assignActivate & 0x400) > 0;
            this.CycleTimeSync1           = Convert.ToInt32(opMode.CycleTimeSync1?.Value);
            this.CycleTimeSync1_Factor    = Convert.ToInt32(opMode.CycleTimeSync1?.Factor);
            this.ShiftTimeSync1           = Convert.ToInt32(opMode.ShiftTimeSync1?.Value);
            this.ShiftTimeSync1_Factor    = Convert.ToInt32(opMode.ShiftTimeSync1?.Factor);
            this.ShiftTimeSync1_Input     = Convert.ToBoolean(opMode.ShiftTimeSync1?.Input);
        }
示例#2
0
        public override void EvaluateSettings()
        {
            var dcOpMode           = _slaveInfo.Esi.Dc.OpMode.Where(x => x.Name == SelectedOpModeId).First();
            var syncManagerPdosSet = dcOpMode.Sm ?? new DeviceTypeDCOpModeSM[] { };

            if (syncManagerPdosSet.Count() > 0)
            {
                _slaveInfo.DynamicData.Pdos.ToList().ForEach(x => x.SyncManager = -1);

                foreach (var syncManagerPdos in syncManagerPdosSet)
                {
                    int syncManager = syncManagerPdos.No;

                    foreach (var smPdo in syncManagerPdos.Pdo)
                    {
                        var index           = (ushort)EsiUtilities.ParseHexDecString(smPdo.Value);
                        var currentOsFactor = (ushort)(smPdo.OSFacSpecified ? smPdo.OSFac : 1);

                        for (ushort osFactorIndex = 1; osFactorIndex <= currentOsFactor; osFactorIndex++)
                        {
                            var indexOffset = (ushort)(osFactorIndex - 1);
                            _slaveInfo.DynamicData.Pdos.Where(x => x.Index == index + indexOffset).First().SyncManager = syncManager;
                        }
                    }
                }
            }
        }
示例#3
0
        public static void CreateDynamicData(string esiDirectoryPath, IExtensionFactory extensionFactory, SlaveInfo slaveInfo)
        {
            string          name;
            string          description;
            List <SlavePdo> pdoSet;

            byte[] base64ImageData;

            // find ESI
            if (slaveInfo.Csa != 0)
            {
                (slaveInfo.SlaveEsi, slaveInfo.SlaveEsi_Group) = EsiUtilities.FindEsi(esiDirectoryPath, slaveInfo.Manufacturer, slaveInfo.ProductCode, slaveInfo.Revision);
            }

            //
            pdoSet          = new List <SlavePdo>();
            base64ImageData = new byte[] { };

            name        = slaveInfo.SlaveEsi.Type.Value;
            description = slaveInfo.SlaveEsi.Name.FirstOrDefault()?.Value;

            if (description.StartsWith(name))
            {
                description = description.Substring(name.Length);
            }
            else if (string.IsNullOrWhiteSpace(description))
            {
                description = "no description available";
            }

            // PDOs
            foreach (DataDirection dataDirection in Enum.GetValues(typeof(DataDirection)))
            {
                IEnumerable <PdoType> pdoTypeSet = null;

                switch (dataDirection)
                {
                case DataDirection.Output:
                    pdoTypeSet = slaveInfo.SlaveEsi.RxPdo;
                    break;

                case DataDirection.Input:
                    pdoTypeSet = slaveInfo.SlaveEsi.TxPdo;
                    break;
                }

                foreach (PdoType pdoType in pdoTypeSet)
                {
                    int      syncManager;
                    ushort   pdoIndex;
                    string   pdoName;
                    SlavePdo slavePdo;

                    var osMax = Convert.ToUInt16(pdoType.OSMax);

                    if (osMax == 0)
                    {
                        pdoName     = pdoType.Name.First().Value;
                        pdoIndex    = (ushort)EsiUtilities.ParseHexDecString(pdoType.Index.Value);
                        syncManager = pdoType.SmSpecified ? pdoType.Sm : -1;

                        slavePdo = new SlavePdo(slaveInfo, pdoName, pdoIndex, osMax, pdoType.Fixed, pdoType.Mandatory, syncManager);

                        pdoSet.Add(slavePdo);

                        IList <SlaveVariable> slaveVariableSet = pdoType.Entry.Select(x =>
                        {
                            var variableIndex = (ushort)EsiUtilities.ParseHexDecString(x.Index.Value);
                            var subIndex      = Convert.ToByte(x.SubIndex);
                            //// Improve. What about -1 if SubIndex does not exist?
                            return(new SlaveVariable(slavePdo, x.Name?.FirstOrDefault()?.Value, variableIndex, subIndex, dataDirection, EcUtilities.GetOneDasDataTypeFromEthercatDataType(x.DataType?.Value), (byte)x.BitLen));
                        }).ToList();

                        slavePdo.SetVariableSet(slaveVariableSet);
                    }
                    else
                    {
                        for (ushort indexOffset = 0; indexOffset <= osMax - 1; indexOffset++)
                        {
                            pdoName     = $"{pdoType.Name.First().Value} [{indexOffset}]";
                            pdoIndex    = (ushort)((ushort)EsiUtilities.ParseHexDecString(pdoType.Index.Value) + indexOffset);
                            syncManager = pdoType.SmSpecified ? pdoType.Sm : -1;
                            var indexOffset_Tmp = indexOffset;

                            slavePdo = new SlavePdo(slaveInfo, pdoName, pdoIndex, osMax, pdoType.Fixed, pdoType.Mandatory, syncManager);

                            pdoSet.Add(slavePdo);

                            IList <SlaveVariable> slaveVariableSet = pdoType.Entry.Select(x =>
                            {
                                var variableIndex = (ushort)EsiUtilities.ParseHexDecString(x.Index.Value);
                                var subIndex      = (byte)(byte.Parse(x.SubIndex) + indexOffset_Tmp);
                                //// Improve. What about -1 if SubIndex does not exist?
                                return(new SlaveVariable(slavePdo, x.Name.FirstOrDefault()?.Value, variableIndex, subIndex, dataDirection, EcUtilities.GetOneDasDataTypeFromEthercatDataType(x.DataType?.Value), (byte)x.BitLen));
                            }).ToList();

                            slavePdo.SetVariableSet(slaveVariableSet);
                        }
                    }
                }
            }

            // image data
            if (slaveInfo.SlaveEsi_Group.ItemElementName == ItemChoiceType1.ImageData16x14)
            {
                base64ImageData = (byte[])slaveInfo.SlaveEsi_Group.Item;
            }

            if (slaveInfo.SlaveEsi.ItemElementName.ToString() == nameof(ItemChoiceType1.ImageData16x14))
            {
                base64ImageData = (byte[])slaveInfo.SlaveEsi.Item;
            }

            // attach dynamic data
            slaveInfo.DynamicData = new SlaveInfoDynamicData(name, description, pdoSet, base64ImageData);

            // execute extension logic
            ExtensibilityHelper.UpdateSlaveExtensions(extensionFactory, slaveInfo);

            slaveInfo.SlaveExtensionSet.ToList().ForEach(slaveExtension =>
            {
                slaveExtension.EvaluateSettings();
            });
        }
示例#4
0
        public DistributedClocksParameters CalculateDcParameters(ref byte[] assignActivate, uint cycleFrequency)
        {
            var cycleTime0 = 0U;
            var cycleTime1 = 0U;
            var shiftTime0 = 0;
            var dcOpMode   = _slaveInfo.Esi.Dc.OpMode.Where(x => x.Name == SelectedOpModeId).First();

            assignActivate = null;

            if (dcOpMode != null)
            {
                var assignActivate_Tmp = (int)EsiUtilities.ParseHexDecString(dcOpMode.AssignActivate);

                if (assignActivate_Tmp == 0)
                {
                    assignActivate = new byte[] { 0 };
                }
                else
                {
                    assignActivate = new byte[]
                    {
                        Convert.ToByte(assignActivate_Tmp & 0xff),
                        Convert.ToByte((assignActivate_Tmp & 0xff00) >> 8)
                    };
                }

                var cycleTimeSyncUnit = Convert.ToInt32(Math.Pow(10, 9) / cycleFrequency);

                cycleTime0 = Convert.ToUInt32(dcOpMode.CycleTimeSync0?.Value);

                if (dcOpMode.CycleTimeSync0 != null && cycleTime0 == 0)
                {
                    if (dcOpMode.CycleTimeSync0.Factor >= 0)
                    {
                        cycleTime0 = Convert.ToUInt32(cycleTimeSyncUnit * Math.Abs(dcOpMode.CycleTimeSync0.Factor));
                    }
                    else
                    {
                        cycleTime0 = Convert.ToUInt32(cycleTimeSyncUnit / Math.Abs(dcOpMode.CycleTimeSync0.Factor));
                    }
                }

                // shiftTime0
                shiftTime0 = Convert.ToInt32(dcOpMode.ShiftTimeSync0?.Value);

                if (dcOpMode.ShiftTimeSync0 != null && dcOpMode.ShiftTimeSync0.FactorSpecified)
                {
                    if (dcOpMode.ShiftTimeSync0.Factor >= 0)
                    {
                        shiftTime0 += Convert.ToInt32(cycleTime0 * Math.Abs(dcOpMode.ShiftTimeSync0.Factor));
                    }
                    else
                    {
                        shiftTime0 += Convert.ToInt32(cycleTime0 / Math.Abs(dcOpMode.ShiftTimeSync0.Factor));
                    }
                }

                // shiftTime1
                int shiftTime1 = Convert.ToInt32(dcOpMode.ShiftTimeSync1?.Value);

                if (dcOpMode.ShiftTimeSync1 != null && dcOpMode.ShiftTimeSync1.FactorSpecified)
                {
                    // for future use
                }

                // cycleTime1
                cycleTime1 = Convert.ToUInt32(dcOpMode.CycleTimeSync1?.Value);

                if (dcOpMode.CycleTimeSync1 != null && cycleTime1 == 0)
                {
                    if (dcOpMode.CycleTimeSync1.Factor >= 0)
                    {
                        cycleTime1 = Convert.ToUInt32(cycleTime0 * Math.Abs(dcOpMode.CycleTimeSync1.Factor));
                    }
                    else
                    {
                        cycleTime1 = Convert.ToUInt32(cycleTimeSyncUnit / Math.Abs(dcOpMode.CycleTimeSync1.Factor));
                    }

                    cycleTime1 = Convert.ToUInt32(cycleTime1 - cycleTime0 + shiftTime1);
                }
            }

            return(new DistributedClocksParameters(cycleTime0, cycleTime1, shiftTime0));
        }