From a0c4a3f79fa29c8105f943c7795ee0cdd9b3e235 Mon Sep 17 00:00:00 2001 From: Orvani Date: Wed, 11 May 2022 11:13:50 +0300 Subject: [PATCH 1/2] Fixes for physical joystick (ONVIF)- issue #189 Fixes for physical joystick (ONVIF)- issue #189 1. Fix bug - pt did not receive a s stop command; Activate stop command on joystick release; (physical joystick got defined as DigitalPTZ for some reason) changed internal bool DigitalPTZ => PTZSettings == null; to internal bool DigitalPTZ = false; 2. Feature: Change pan & tilt speed in correlation with joystick position; used tanh function for more sensitive transformation of joystick position to pan/tilt speed; --- MainForm_Commands.cs | 5 +++-- PTZController.cs | 53 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/MainForm_Commands.cs b/MainForm_Commands.cs index 8997f4de..de38e907 100644 --- a/MainForm_Commands.cs +++ b/MainForm_Commands.cs @@ -178,7 +178,8 @@ private void CheckJoystick() { } var d = Math.Sqrt((x*x) + (y*y)); - if (d > 20) + + if (d > 2) //20) { angle = Math.Atan2(y, x); } @@ -199,7 +200,7 @@ private void CheckJoystick() { } cw.Calibrating = true; - cw.PTZ.SendPTZDirection(angle); + cw.PTZ.SendPTZDirection(angle, x, y); if (!cw.PTZ.DigitalPTZ) _needstop = _sentdirection = true; } diff --git a/PTZController.cs b/PTZController.cs index 29b3a967..5967bb1a 100644 --- a/PTZController.cs +++ b/PTZController.cs @@ -111,7 +111,7 @@ internal PTZSettings2Camera PTZSettings } } - internal bool DigitalPTZ => PTZSettings == null; + internal bool DigitalPTZ = false; public PTZController(CameraWindow cameraControl) @@ -163,7 +163,7 @@ public void ResetONVIF() _ptzSettings = null; } - public void SendPTZDirection(double angle) + public void SendPTZDirection(double angle, int xPos = 0, int yPos = 0) { if (_cameraControl.Camobject.settings.ptzrotate90) { @@ -232,8 +232,8 @@ public void SendPTZDirection(double angle) if (cmd != Enums.PtzCommand.Stop) { //ignore - continuous - if (_lastCommand == cmd) - return; + //if (_lastCommand == cmd) + // return; } } switch (_cameraControl.Camobject.ptz) @@ -254,7 +254,7 @@ public void SendPTZDirection(double angle) ProcessPelco(cmd, false); return; case -5://ONVIF - ProcessOnvif(cmd); + ProcessOnvif(cmd, xPos, yPos); break; case -6: //none - ignore @@ -370,7 +370,7 @@ internal bool DigitalZoom } - public void SendPTZCommand(Enums.PtzCommand command) + public void SendPTZCommand(Enums.PtzCommand command, int x= 0, int y= 0) { if (_cameraControl.Camera == null) return; @@ -751,16 +751,49 @@ void IAMMove(VideoCaptureDevice d, CameraControlProperty p, int i) private Enums.PtzCommand _lastOnvifCommand = Enums.PtzCommand.Center; private DateTime _lastOnvifCommandSent = DateTime.MinValue; + private float _lastPanSpeed = 0.0f; + private float _lastTiltSpeed = 0.0f; - void ProcessOnvif(Enums.PtzCommand command) + void ProcessOnvif(Enums.PtzCommand command, int xPos=0, int yPos=0) { + + var panSpeed = (float)0.5; + var tiltSpeed = (float)0.5; + if (!_cameraControl.ONVIFConnected) return; - if (command == _lastOnvifCommand && _lastOnvifCommandSent > DateTime.UtcNow.AddSeconds(-4)) + if (xPos != 0) + { + xPos = Math.Abs(xPos); + if (xPos > 100) + xPos = 100; + if (xPos < 0) + xPos = 0; + + panSpeed = (float)(0.5 * (1 + (Math.Tanh((xPos - 50)/ 20)) )); + + } + + if (yPos != 0) + { + yPos = Math.Abs(yPos); + if (yPos > 100) + yPos = 100; + if (yPos < 0) + yPos = 0; + + tiltSpeed = (float)(0.5 * (1 + (Math.Tanh((yPos - 50) / 20)))); + } + + if (command == _lastOnvifCommand && _lastOnvifCommandSent > DateTime.UtcNow.AddSeconds(-4) && + panSpeed == _lastPanSpeed && tiltSpeed == _lastTiltSpeed) return; _lastOnvifCommand = command; _lastOnvifCommandSent = DateTime.UtcNow; + _lastPanSpeed = panSpeed; + _lastTiltSpeed = tiltSpeed; + var od = _cameraControl?.ONVIFDevice; var ptz = od?.PTZ; @@ -775,8 +808,8 @@ void ProcessOnvif(Enums.PtzCommand command) try { _lastCommand = command; - var panSpeed = (float)0.5; - var tiltSpeed = (float)0.5; + //var panSpeed = (float)0.5; + //var tiltSpeed = (float)0.5; var zoomSpeed = (float)1; switch (command) { From b86c4ace3500916d80c7bcdaf4b649510382ab21 Mon Sep 17 00:00:00 2001 From: Orvani Date: Mon, 16 May 2022 11:03:46 +0300 Subject: [PATCH 2/2] Fixes for pphysical joystick (ONVIF)- issue #189 - update fix bug - physical ptz missing stop command, without break of digital PTZ internal bool DigitalPTZ => _cameraControl.Camobject.ptz == -1; --- PTZController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PTZController.cs b/PTZController.cs index 5967bb1a..eeffa13d 100644 --- a/PTZController.cs +++ b/PTZController.cs @@ -111,7 +111,7 @@ internal PTZSettings2Camera PTZSettings } } - internal bool DigitalPTZ = false; + internal bool DigitalPTZ => _cameraControl.Camobject.ptz == -1; public PTZController(CameraWindow cameraControl)