Skip to Main Content
Cognex 支持中心
Logo

Script - Saving Energy In Presentation Application

Controlling light illumination based on timer and ToF sensor

2025/04/16

Details

In the current hands-free application, there may be instances where we need to reduce the energy consumption of our solutions.

In such cases, we can use a script that monitors the period of inactivity beneath the reader and turns off the lights after a specified time. If something is detected under the reader, the lights will be turned on again. These scripts are designed to work with readers equipped with HPIT lights.

To implement this, two scripts are required:

 

Data Formatting Script:

'use strict';
const ENTER = '\x0D\x0A';
function onResult (decodeResults, readerProperties, output) {
 if (decodeResults[0].decoded)
   {
   // Just output the code 
       output.content = decodeResults[0].content + ENTER;

   if(serialHandler !== null){
     // We use the serialHandeler (COM1) to start a timer. ENERGY_SAVING_TIME is defined in the COMMS Script
     serialHandler.setTimer(ENERGY_SAVING_TIME);
   }
   }    
}

 

Communication Script:

'use strict';

const LOG = true;

/* Energy saving parameters */
const ENERGY_SAVING_TIME = 300;// The time (in seconds) after a last scan when the energy saving should be enabled
const MIN_DISTANCE_MM = 50; // Min distance from HPIT where the operator should wave his hands to re-enable the readers
const MAX_DISTANCE_MM = 1200; // Max distance from HPIT where the operator should wave his hands to re-enable the readers

const POLL_TIME = 0.1; // Poll the distance sensor every x seconds to see if we need to re-enable the reader

const SECONDARY1_IP = '192.168.1.101'; // Specify address of possible secondary reader.

// Define the DMCC commands to be sent to the secondary (if any is present)
const DMCC_LIGHTS_ON = '||>SET LIGHT.DIRECT ON ON ON ON\r\n';
const DMCC_LIGHTS_OFF = '||>SET LIGHT.DIRECT OFF OFF OFF OFF\r\n';

const DMCC_AIMER_ON = '||>SET LIGHT.AIMER 1\r\n';
const DMCC_AIMER_OFF = '||>SET LIGHT.AIMER 0\r\n';

// variables to control the behaviour 
let serialHandler = null;
let secondaryHandler = null;
let energySavingMode = false


function CommHandler (localName) {
 return {
   onConnect: function (peerName) {
     if (peerName === 'COM1') {
       serialHandler = this;
       // Start the energy save timer since we also want to go out when we start up but don't read
       serialHandler.setTimer(ENERGY_SAVING_TIME);
       return true;
     }
     const peerIP = peerName.split(':')[0];
     if (peerIP === SECONDARY1_IP) {
       secondaryHandler = this;
       logMsg('Secondary Reader 1 Connected');
       return true;
     } 
     return false;
   },
   onDisconnect: function () {
     if(this === serialHandler){
       serialHandler = null;
       logMsg('Serial Handler Disconnected');
     } else if (this === secondaryHandler) {
       secondaryHandler = null;
       logMsg('Secondary Reader 1 Disconnected');
     } 
   },
   onError: function (errorMsg) {},
   onExpectedData: function (inputString) {},
   onUnexpectedData: function (inputString) {},
   onTimer: function () {
     if (this === serialHandler){
       if(energySavingMode === false){
           enableEnergySaving();
           serialHandler.setTimer(POLL_TIME);
       } else if(energySavingMode == true){
         // Check the distance and see if we have to re-enable
         let currentDistance = dmccGet('HEIGHT-SENSOR.CURRENT-MEASUREMENT').response;
         //logMsg('Current distance ' + currentDistance);
         if(currentDistance >= MIN_DISTANCE_MM && currentDistance <= MAX_DISTANCE_MM){
           // re-enable the lights
           disableEnergySaving();
         } else {
           // we aren't allowed to disable the energy mode.. restart the timer to check again later
           serialHandler.setTimer(POLL_TIME);
         }
       }

     }
   },
   onEncoder: function () {}
 };
}

function enableEnergySaving(){
 // Turn of the Aimer and HPIT lights
 dmccSet('LIGHT.AIMER' , 0)
 dmccSet('LIGHT.DIRECT','OFF OFF OFF OFF');
 energySavingMode = true;

 // Also send the commands to the secondary (if connected)
 if (secondaryHandler !== null) {
   secondaryHandler.send(DMCC_AIMER_OFF);
   secondaryHandler.send(DMCC_LIGHTS_OFF);
 }

 logMsg("In Energy Saving mode");
}

function disableEnergySaving(){
// Turn onn the Aimer and HPIT Lights.
 dmccSet('LIGHT.AIMER' , 1)
 dmccSet('LIGHT.DIRECT','ON ON ON ON');

 // Also send the commands to the secondary (if connected)
 if (secondaryHandler !== null) {
   secondaryHandler.send(DMCC_AIMER_ON);
   secondaryHandler.send(DMCC_LIGHTS_ON);
 }

 energySavingMode = false;
 if(serialHandler !== null){
   // Restart the timer since we have disaabled the energy mode
   serialHandler.setTimer(ENERGY_SAVING_TIME);
 }
 logMsg("Disabled energy Saving mode");
}

function logMsg (msg) {
 if (LOG) {
   console.log(msg);
 }
}