Skip to content

Basler Camera Nodes for Node-RED ​

@rosepetal/node-red-contrib-camera-basler provides 3 nodes for Node-RED that receive the frames of Basler GigE cameras, send them software triggers and change their parameters.

Installation ​

bash
cd ~/.node-red
npm install @rosepetal/node-red-contrib-camera-basler

The package is not on the public npm registry. Its releases are published to Rosepetal's private npm registry, so npm needs that registry configured for the @rosepetal scope, with credentials for it. Restart Node-RED after the installation.

The cameras are driven by two services that run as Docker containers, from two images tagged with the version of the package. When Start Basler is pressed in a node's dialog, the images that are not on the host yet are pulled from Rosepetal's private container registry, so Docker on the host must be logged in to it.

From a clone of the repository ​

The repository keeps the archive of the Basler pylon SDK, basler-driver/pylon-25.11.0_linux-x86_64.tar.gz, in Git LFS, so clone it with Git LFS installed. Then, on the host that runs Node-RED:

bash
./install.sh

The script:

  1. Extracts the pylon SDK into basler-driver/pylon-sdk/. It stops when that folder already has files in it.
  2. Builds the Docker images of the discovery service and of the camera publisher. It first removes the basler-discovery container, the camera containers and the images of a previous build.
  3. Runs npm install in node-red/, which fetches the nats client.
  4. Installs node-red/ into the first Node-RED user directory it finds, ~/.node-red first. When it finds none, it prints the npm install command to run in that directory.

./install.sh --node-red-only runs only steps 3 and 4. Restart Node-RED afterwards. The nodes start the services from the images tagged with the version of the package, as described above, and pull them when the host does not have them.

Requirements ​

  • Linux x86-64 host with Docker Engine. The user that runs Node-RED must be allowed to run docker: the nodes call the Docker command line.
  • A NATS server that listens on port 4222 of that host. The nodes connect to nats://127.0.0.1:4222, or to the URL in the NATS_URL environment variable of the Node-RED process; the two services always connect to nats://localhost:4222.
  • Node-RED on the same host, with the host's /dev/shm visible to it: basler-collector reads the frames from there.
  • Node.js 14 or later.
  • Basler GigE cameras on a network interface of the host. The discovery service lists the devices that report a MAC address and whose vendor name contains basler or is empty.

Nodes ​

The three nodes appear under the RP Basler category in the editor.

NodePurpose
basler-collectorStart and stop the services and the cameras from its dialog, and emit every frame of the selected cameras as a raw image
basler-triggerSend a software trigger to the selected cameras for each incoming message
basler-configSet camera parameters (region of interest, exposure, gain, acquisition mode or any GenICam node) from constants, the message or the context

How it works ​

The nodes do not open the cameras: two services that run as Docker containers on the same host do. The discovery service (container basler-discovery) lists the cameras with the pylon SDK and starts or stops one publisher container per camera (basler-<MAC>, with the MAC address in upper case and hyphens); each publisher grabs the frames of its camera, converts them to BGR8, writes them to shared memory (/dev/shm) and announces them through the NATS server. basler-collector copies each announced frame from shared memory into a message, and basler-trigger and basler-config send their requests to the publisher through NATS.

The README of the repository describes the services and their NATS subjects.

Quick start ​

[basler-collector] → [debug]

[inject] → [basler-trigger]
  1. Start the NATS server before Node-RED.
  2. Add a basler-collector node and open it. Press Start Basler and wait for the panel to show Running: after some seconds the cameras found on the network are listed under Available Cameras.
  3. Press Start on a camera whose status is Free. A camera in No access needs Fix IP first. The status changes to Controlled (ours) once the publisher container runs; the publisher waits 10 seconds before it opens the camera.
  4. Tick the Select checkbox of the camera, close the dialog and deploy. The node shows subscribed 1 and emits one message per frame, with the image in msg.payload.
  5. To take images on demand, set the TriggerMode of the camera to On with a basler-config node (parameter Custom…), then add a basler-trigger node, select the same camera in its dialog and send it a message.

The publisher does not change the trigger and acquisition settings of the camera when it starts, so the camera delivers frames as it is configured. Use basler-config to change them.

Restarts and reconnection ​

The containers are created with the restart policy unless-stopped: the discovery service and the publishers of the cameras that were started come back when Docker or the host restarts, without opening a dialog again. A publisher waits 10 seconds after it starts before it opens its camera.

When the link with a camera drops (cable, power), its publisher keeps running and tries to open the camera again until it is back. It then applies again the parameters it had received from basler-config and resumes the acquisition. Meanwhile basler-collector shows reconnecting. The publisher does not repair the IP address of the camera by itself: when the camera stays in No access, use Fix IP. A publisher that starts while its camera is unreachable stays alive and follows the same procedure.

On a camera that has a counter, the publisher also watches for a stream that stops delivering frames while the camera still counts frame or exposure events: after 15 seconds without a frame it reopens the camera, and when that happens more than five times in a row it exits so that Docker starts its container again. For this it selects one of the counters of the camera, Counter1 or Counter2, and sets its CounterEventSource.

Image format ​

msg.payload of basler-collector is a raw image object:

javascript
{
  data: Buffer,        // Raw pixel data, 8 bits per channel in B, G, R order
  width: number,       // Image width in pixels
  height: number,      // Image height in pixels
  channels: 3,
  colorSpace: "BGR",
  dtype: "uint8"
}

It is the object that the nodes of @rosepetal/node-red-contrib-image-tools pass between them, so the output of basler-collector can be wired to them.