Skip to content

Instantly share code, notes, and snippets.

@philhartung
Last active March 5, 2024 23:47
Show Gist options
  • Save philhartung/5e381a742a4b7db2022ec7851a7142f0 to your computer and use it in GitHub Desktop.
Save philhartung/5e381a742a4b7db2022ec7851a7142f0 to your computer and use it in GitHub Desktop.
Script to Control an ONVIF PTZ Camera with a Logitech Extreme 3D Pro
const hid = require('node-hid');
const { Cam } = require('onvif');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const argv = yargs(hideBin(process.argv))
.option('hostname', {
alias: 'h',
describe: 'Server hostname',
demandOption: true,
type: 'string',
})
.option('port', {
alias: 'p',
describe: 'Server port',
demandOption: true,
type: 'number',
})
.option('username', {
alias: 'u',
describe: 'Username for authentication',
demandOption: true,
type: 'string',
})
.option('password', {
alias: 'pw',
describe: 'Password for authentication',
demandOption: true,
type: 'string',
}).argv;
const device = new hid.HID(1133, 49685);
const camera = new Cam({
hostname: argv.hostname,
port: argv.port,
username: argv.username,
password: argv.password
}, function(err) {
if (err) {
console.log(err);
return;
}
setInterval(updateCameraPosition, 100);
});
let xVelocity = 0;
let yVelocity = 0;
let zoom = 0;
let throttle = 0;
let isMoving = false;
const updateCameraPosition = () => {
if (xVelocity !== 0 || yVelocity !== 0 || zoom !== 0) {
const options = { x: xVelocity, y: yVelocity, zoom };
console.log(options);
camera.continuousMove(options);
}
};
const processData = (buf) => {
const inputs = {
roll: ((buf[1] & 0x03) << 8) + buf[0],
pitch: ((buf[2] & 0x0f) << 6) + ((buf[1] & 0xfc) >> 2),
yaw: buf[3],
throttle: -buf[5] + 255,
};
updateVelocityAndZoom(inputs);
manageCameraMovement()
};
device.on('data', processData);
function updateVelocityAndZoom(inputs) {
throttle = inputs.throttle / 255;
xVelocity = adjustVelocity(inputs.roll - 512, throttle);
yVelocity = adjustVelocity(inputs.pitch - 512, throttle);
zoom = Math.round((inputs.yaw - 128) / 128);
}
function adjustVelocity(value, throttle) {
let velocity = (value / -512) * throttle;
return Math.round(velocity * 4) / 4;
}
function manageCameraMovement() {
const hasMovement = xVelocity !== 0 || yVelocity !== 0 || zoom !== 0;
if (!hasMovement && isMoving) {
console.log('Stopping movement');
camera.stop();
isMoving = false;
} else if (hasMovement && !isMoving) {
console.log('Starting movement');
isMoving = true;
updateCameraPosition();
}
}
@philhartung
Copy link
Author

Example usage: node ptz --hostname 192.168.0.206 --port 2000 --username admin --password admin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment