Skip to content

Commit

Permalink
Fixes for physical joystick (ONVIF)- issue #189
Browse files Browse the repository at this point in the history
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;
  • Loading branch information
Orvani committed May 11, 2022
1 parent b73cb93 commit a0c4a3f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
5 changes: 3 additions & 2 deletions MainForm_Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand Down
53 changes: 43 additions & 10 deletions PTZController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal PTZSettings2Camera PTZSettings
}
}

internal bool DigitalPTZ => PTZSettings == null;
internal bool DigitalPTZ = false;


public PTZController(CameraWindow cameraControl)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down

0 comments on commit a0c4a3f

Please sign in to comment.