Creating a basic component

This article, along with ‘Creating a Catalog‘ and ‘RigidPart’ are an introduction to developing your own catalog, templates and components.

Platform component exercise

It is intended to develop a platform component which moves along the X axis back and forth. The movement will be performed using three different approaches:

  • Step(float deltatime)
  • Experior.Core.Timer.Translate
  • PLC.OnReceived

1. Create a Box

 public Platform(PlatformInfo info) : base(info)
 {
   this.info = info;

   if (info.friction == null)
       info.friction = new Friction() { Coefficient = Coefficients.Sticky };
            
   _base = new Experior.Core.Parts.Box(System.Windows.Media.Colors.SlateGray, 2f, 0.1f, 1f)
    {
      Color = System.Windows.Media.Colors.DarkGreen,
      Rigid = true,
      Visible = true,
      Friction = info.friction
     };
    Add(_base, new Vector3(0, 0, 0));
 }

2. Motion (Keyboard approach)

  • Step(float deltatime)
  • Reset()
  • KeyDown(KeyEventArgs e)

 

  • Step method is called by the physics engine every ≈16ms
 public override void Step(float deltatime)
        {
            var deltaMovement = Speed * deltatime * direction;
            _base.LocalPosition += new Vector3(deltaMovement,0 ,0);
        }

        public override void Reset()
        {
            _base.LocalPosition = new Vector3(0, 0, 0);
        }

        public override void KeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Z)
                direction = -1;

            if (e.Key == Key.C)
                direction = 1;

            if (e.Key == Key.X)
                direction = 0;
        }

3. Motion (Timer approach)

  • Experior.Core.Timer.Translate timer

 

  • timer.Add(RigidPart part)

 

 timer = new Timer.Translate();

 timer.Add(_base);

 public override void KeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.C)
            {
                timer.Start(() =>
                {
                    Log.Write("Forward motion completed");
                }, new Vector3(1.5f, 0, 0), 1f);
            }

            if (e.Key == Key.Z)
            {
                {
                    timer.Start(() =>
                    {
                        Log.Write("Backward motion completed");
                    }, new Vector3(-3f, 0, 0), 0.3f);
                }
            }
        }

3. Motion (PLC Signal approach)

  • Experior.Core.Communication.PLC.Input

 

  • Input.OnReceived

 if (info.inputposition == null)
     info.inputposition = new Input() { DataSize = DataSize.DINT, Description = "Position from PLC" };

 Add(info.inputposition);
 info.inputposition.OnReceived += InputPosition_OnReceived;
 
 
  private void InputPosition_OnReceived(Input sender, object value)
  {
   var pos = System.Convert.ToInt32(value);
   _base.LocalPosition = new Vector3(pos, 0, 0);
  }

Previous Workflow creating a simple component