Tutorial: 3 - Getting Started Pt 2

Basic Control System


In this section we will cover how to create a basic heater control system that toggles depending on a digital temperature input. Below a flow chart can be seen that describes the flow of information for the process.


Ladder Logic Implementation

In ladder logic this flow chart an be simply implemented as seen below:


JavaScript Implementation

The JavaScript code, similar to the previous tutorial, sets the heater on based off of the temperature input. The temperature interlock is scanned approximately once a second.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Arcx - Getting Started Example</title>
</head>
<body>

</body>
<script type="text/javascript">

if (typeof ($wat) == 'object')
{
    $hw = $wat.load('hw')[0]; //load our hw object
}

var M01_heater;

var I01_temperature = $hw.digInputs[0]; // Temperature Input : 1 - LOW, 0 - NORMAL
var Q01_heater = $hw.digOutputs[0]; // Heat Output

//using an interval we can "scan" approximately every 1s
setInterval(function()
{    
     // Inputs vars are updated based on the physical input state change

    // Heater Control
    if (I01_temperature.state) 
    {
    console.log("Turn Heat On");
    M01_heater = 1;
    } 
    else 
    {
        M01_heater = 0;
    console.log("Turn Heat Off");
    }

    // Outputs
    if(M01_heater)
    {
        Q01_heater.set(); //set our output
    }
    else
    {
        Q01_heater.clear(); //set our output
    }

    console.log("Output: " + Q01.state);
}, 100*10); 100ms*10 = 1sec, note this can be set to any value 

</script>
</html>


Adding Logic


In JavaScript, adding logic to code is very simple. Below, the flow chart depicts the same logic with an added pressure interlock. If both the pressure and temperature are high then the heater will turn on, otherwise the heater will stop.


Ladder Logic Implementation

To add the new interlock we just need to put a pressure sensor in series with the temperature sensor.


JavaScript Implementation

The JavaScript code is just as simple as we only have to AND our current conditional evaluation with the new pressure input.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Arcx - Getting Started Example</title>
</head>
<body>

</body>
<script type="text/javascript">


if (typeof ($wat) == 'object')
{
    $hw = $wat.load('hw')[0]; //load our hw object
}

var M01_heater;

var I01_temperature = $hw.digInputs[0]; // Temperature Input : 1 - LOW, 0 - NORMAL
var I11_pressure = $hw.digInputs[1]; // Pressure Input : 1 - NORMAL, 0 - HIGH
var Q01_heater = $hw.digOutputs[0]; // Heat Output

//using an interval we can "scan" approximately every 1s
setInterval(function()
{    
    // Heater control
    if (I01_temperature.state && I11_pressure.state) //here we add our extra inputs
    { 
    console.log("Turn Heat On");
    M01_heater = 1;
    } 
    else 
    {
        M01_heater = 0;
    console.log("Turn Heat Off");
    }

    // Outputs
    if(M01_heater)
    {
        Q01_heater.set(); //set our output
    }
    else
    {
        Q01_heater.clear(); //set our output
    }

    console.log("Output: " + Q01.state);
}, 100*10); //100ms*10 = 1sec, note this can be set to any value 

</script>
</html>


Removing Scan Time with Events (Interrupts)


The beauty of using JavaScript is we can easily create event based interlocks that are evaluated only on state changes. This allows our code to do other tasks in asynchronously while still controlling systems. The code below is the same as above but this time evaluations occur only when the temperature changes. For a refresher on event based coding, head back to 1 - Hello World.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Arcx - Getting Started Example</title>
</head>
<body>

</body>
<script type="text/javascript">


if (typeof ($wat) == 'object')
{
    $hw = $wat.load('hw')[0]; //load our hw object
}

var M01_heater;

var I01_temperature = $hw.digInputs[0]; // Temperature Input : 1 - LOW, 0 - NORMAL
var I11_pressure = $hw.digInputs[1]; // Pressure Input : 1 - NORMAL, 0 - HIGH
var Q01_heater = $hw.digOutputs[0]; // Heat Output

//connect inputs to interlocks 
I01_temperature.stateChanged.connect(I01_temperature, interlock);
I11_pressure.stateChanged.connect(I11_pressure, interlock);

//this function is called when either of our 2 inputs receive edge triggers (rising and falling)
function interlock(){
    if (I01_temperature.state && I11_pressure.state) 
    { 
        console.log("Turn Heat On");
        M01_heater = 1;
    } 
    else 
    {
        M01_heater = 0;
        console.log("Turn Heat Off");
    }
    outputNetwork();
}

function outputNetwork()
{
  // Network 2: Outputs
    if(M01_heater)
    {
        Q01_heater.set(); //set our output
    }
    else
    {
        Q01_heater.clear(); //set our output
    }
}
</script>
</html>