menu
info Documentation

Automatically connecting to a CubeComputer during Hardware-in-the-loop

There can be a need for users to automatically connect to a CubeComputer to set up a HIL connection. This functionality is especially useful for users with access to the Command Line Interface version of D2S2 for automated testing of systems. There are two methods of automatically connecting when performing HIL.

1. Connection via the Automation Scheduler

One of the most straightforward methods of automatically connecting is via the Automation Scheduler. This is unfortunately the only method available to standard license users. While this method is functional, the scheduler may silently fail (i.e. no error is raised and the simulation continues) if the CubeComputer fails to connect.

The three steps below are scheduled one second apart to ensure the COM port and interface type are configured before the connection is attempted. Note that HilInterface accepts an integer value: 0 = Hub (not currently used), 1 = UART, 2 = CAN.

    "AutomationSchedule": {
      "$type": "System.Collections.Generic.List`1[[D2S2.Simulation.ScheduledPropertySet, D2S2Library]], mscorlib",
      "$values": [
        {
          "$type": "D2S2.Simulation.ScheduledPropertySet, D2S2",
          "Time": "2026-02-18T15:16:01",
          "ModelObject": "ExampleSat CubeComputer",
          "Property": "ComPortName",
          "Value": "COM 3"
        },
        {
          "$type": "D2S2.Simulation.ScheduledPropertySet, D2S2",
          "Time": "2026-02-18T15:16:02",
          "ModelObject": "ExampleSat CubeComputer",
          "Property": "HilInterface",
          "Value": 1
        },
        {
          "$type": "D2S2.Simulation.ScheduledPropertySet, D2S2",
          "Time": "2026-02-18T15:16:03",
          "ModelObject": "ExampleSat CubeComputer",
          "Property": "Connected",
          "Value": true
        }
      ]
    }

2. Connection via a script

Establishing the HIL connection via a custom script is a more reliable method. This method is unfortunately restricted to those with a developer license, as with CLI access. The example below illustrates a script which inherits from the LoggingScript base class. The script connects to the HIL system and subsequently logs the scenario state as the simulation runs:

using D2S2.Model.Satellite;
using D2S2.Model.Satellite.CubeSpace;
using D2S2.Shared;

namespace D2S2.Simulation.CubeSpace
{
    [DisplayName("HillConnect")]
    [Description("")]
    public class HillConnect : LoggingScript
    {

        public HillConnect()
        {
        }

        protected override void Run()
        {
            if (!(SimulationManager.Model is SatelliteSimulation))
                throw new System.Exception("Root simulation model is not a Satellite Simulation");

            SatelliteSimulation simRoot = SimulationManager.Model as SatelliteSimulation;

            if (simRoot.Satellites.Count != 1)
                throw new System.Exception("This script can only be applied to a scenario with a single satellite");

            SatelliteModelBase sat = simRoot.Satellites[0];

            // find out if current satellite in scenario is SIL or HIL CubeComputer
            CubeComputer5Hil cc5 = null;
            foreach (SatelliteComponent comp in sat.Components)
            {
                if (comp is CubeComputer5Hil)
                    cc5 = (CubeComputer5Hil)comp;
            }

            if (cc5 == null)
                throw new System.Exception("The satellite in the scenario does not have a CubeComputer.");

            // cc5 is guaranteed non-null here
            cc5.ComPortName = "COM 3";
            cc5.HilInterface = CommsInterfaceType.UART;

            //connecting
            try
            {
                cc5.Connected = true;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("Could not connect.", ex);
            }

            base.Run(); //runs the logger script as per normal
        }
    }
}