Skip to content

Commit 06a78c9

Browse files
authored
Merge pull request #155 from earocorn/onvif-relative
Add ONVIF relative PTZ fallback and sensor location output
2 parents a1cafaf + e0dcfd0 commit 06a78c9

File tree

3 files changed

+80
-35
lines changed

3 files changed

+80
-35
lines changed

sensors/video/sensorhub-driver-onvif/src/main/java/org/sensorhub/impl/sensor/onvif/OnvifCameraConfig.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Developer are Copyright (C) 2014 the Initial Developer. All Rights Reserved.
1919
import org.sensorhub.api.comm.ICommConfig;
2020
import org.sensorhub.api.config.DisplayInfo;
2121
import org.sensorhub.api.config.DisplayInfo.Required;
22+
import org.sensorhub.api.sensor.PositionConfig;
2223
import org.sensorhub.api.sensor.SensorConfig;
2324
import org.sensorhub.impl.comm.TCPConfig;
2425
import java.util.*;
@@ -33,6 +34,15 @@ Developer are Copyright (C) 2014 the Initial Developer. All Rights Reserved.
3334
* @since May 22, 2017
3435
*/
3536
public class OnvifCameraConfig extends SensorConfig {
37+
38+
@DisplayInfo(desc = "Geographic position configuration")
39+
public PositionConfig positionConfig = new PositionConfig();
40+
41+
@Override
42+
public PositionConfig.LLALocation getLocation() {
43+
return positionConfig.location;
44+
}
45+
3646
@Required
3747
@DisplayInfo(label="ONVIF Connection Options", desc="Configure ONVIF remote address and port")
3848
public OnvifConfig networkConfig = new OnvifConfig();

sensors/video/sensorhub-driver-onvif/src/main/java/org/sensorhub/impl/sensor/onvif/OnvifCameraDriver.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,41 @@ public class OnvifCameraDriver extends AbstractSensorModule<OnvifCameraConfig>
7676
float zoomMax = 2f;
7777
float zoomMin = -1f;
7878

79+
public void absolutePanTilt(float panDegrees, float tiltDegrees) {
80+
float pan = degtoGeneric(panDegrees, 0);
81+
float tilt = degtoGeneric(tiltDegrees, 1);
82+
83+
Vector2D targetPanTilt = new Vector2D();
84+
85+
targetPanTilt.setSpace(ptzProfile.getPTZConfiguration().getDefaultAbsolutePantTiltPositionSpace());
86+
targetPanTilt.setX(pan);
87+
targetPanTilt.setY(tilt);
88+
var position = getCurrentPtzPosition();
89+
position.setPanTilt(targetPanTilt);
90+
91+
camera.getPtz().absoluteMove(ptzProfile.getToken(), position, ptzControlInterface.speed);
92+
}
93+
94+
public void absolutePan(float panDegrees) {
95+
absolutePanTilt(panDegrees, getCurrentPtzPosition().getPanTilt().getY());
96+
}
97+
98+
public void absoluteTilt(float tiltDegrees) {
99+
absolutePanTilt(getCurrentPtzPosition().getPanTilt().getX(), tiltDegrees);
100+
}
101+
102+
private PTZVector getCurrentPtzPosition() {
103+
PTZVector position;
104+
try {
105+
PTZStatus status = camera.getPtz().getStatus(ptzProfile.getToken());
106+
position = status.getPosition();
107+
} catch (Exception e) {
108+
position = new PTZVector();
109+
}
110+
111+
return position;
112+
}
113+
79114
// Type: 0 = pan, 1 = tilt, 2 zoom
80115
public float degtoGeneric(float in, int type) {
81116
float min;
@@ -214,7 +249,8 @@ public boolean isConnected() {
214249

215250
// This method override may not be necessary
216251
@Override
217-
protected void afterInit() {
252+
protected void afterInit() throws SensorHubException {
253+
super.afterInit();
218254
updateSensorDescription();
219255
}
220256

sensors/video/sensorhub-driver-onvif/src/main/java/org/sensorhub/impl/sensor/onvif/OnvifPtzControl.java

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ protected void init()
230230
commandData.getItem(VideoCamHelper.TASKING_PTZ_POS).removeComponent(VideoCamHelper.TASKING_ZOOM);
231231
log.debug("Removed absolute z");
232232
}
233-
if (devicePtzConfig.getDefaultRelativePanTiltTranslationSpace() == null) {
233+
if (devicePtzConfig.getDefaultRelativePanTiltTranslationSpace() == null
234+
// If absolute PTZ available, we can use simple logic to simulate relative PTZ
235+
&& devicePtzConfig.getDefaultAbsolutePantTiltPositionSpace() == null) {
234236
// Remove relative PT
235237
commandData.removeComponent(VideoCamHelper.TASKING_RPAN);
236238
commandData.removeComponent(VideoCamHelper.TASKING_RTILT);
@@ -306,29 +308,12 @@ protected boolean execCommand(DataBlock command) throws CommandException
306308
// ABSOLUTE PAN
307309
if (itemID.equals(VideoCamHelper.TASKING_PAN))
308310
{
309-
float pan = parent.degtoGeneric(data.getFloatValue(), 0);
310-
Vector2D targetPanTilt = new Vector2D();
311-
312-
targetPanTilt.setSpace(config.getDefaultAbsolutePantTiltPositionSpace());
313-
targetPanTilt.setX(pan);
314-
targetPanTilt.setY(position.getPanTilt().getY());
315-
position.setPanTilt(targetPanTilt);
316-
317-
ptz.absoluteMove(ptzProfile.getToken(), position, speed);
318-
311+
parent.absolutePan(data.getFloatValue());
319312
}
320313
// ABSOLUTE TILT
321314
else if (itemID.equals(VideoCamHelper.TASKING_TILT))
322315
{
323-
float tilt = parent.degtoGeneric(data.getFloatValue(), 1);
324-
Vector2D targetPanTilt = new Vector2D();
325-
326-
targetPanTilt.setSpace(config.getDefaultAbsolutePantTiltPositionSpace());
327-
targetPanTilt.setX(position.getPanTilt().getX());
328-
targetPanTilt.setY(tilt);
329-
position.setPanTilt(targetPanTilt);
330-
331-
camera.getPtz().absoluteMove(ptzProfile.getToken(), position, speed);
316+
parent.absoluteTilt(data.getFloatValue());
332317
}
333318
// ABSOLUTE ZOOM
334319
else if (itemID.equals(VideoCamHelper.TASKING_ZOOM))
@@ -345,28 +330,42 @@ else if (itemID.equals(VideoCamHelper.TASKING_ZOOM))
345330
// RELATIVE PAN
346331
else if (itemID.equals(VideoCamHelper.TASKING_RPAN))
347332
{
348-
float rpan = data.getFloatValue() / (parent.panMax - parent.panMin) * 2;
349-
Vector2D targetPanTilt = new Vector2D();
333+
float rpan = data.getFloatValue();
334+
try {
335+
Vector2D targetPanTilt = new Vector2D();
350336

351-
targetPanTilt.setX(rpan);
352-
targetPanTilt.setY(0.0f);
353-
targetPanTilt.setSpace(config.getDefaultRelativePanTiltTranslationSpace());
354-
position.setPanTilt(targetPanTilt);
337+
targetPanTilt.setX(rpan);
338+
targetPanTilt.setY(0.0f);
339+
targetPanTilt.setSpace(config.getDefaultRelativePanTiltTranslationSpace());
340+
position.setPanTilt(targetPanTilt);
355341

356-
camera.getPtz().relativeMove(ptzProfile.getToken(), position, speed);
342+
camera.getPtz().relativeMove(ptzProfile.getToken(), position, speed);
343+
} catch (Exception e) {
344+
if (devicePtzConfig.getDefaultAbsolutePantTiltPositionSpace() != null) {
345+
float pan = parent.ptzPosOutput.getLatestRecord().getFloatValue(1) + rpan;
346+
parent.absolutePan(pan);
347+
}
348+
}
357349
}
358350
// RELATIVE TILT
359351
else if (itemID.equals(VideoCamHelper.TASKING_RTILT))
360352
{
361-
float rtilt = data.getFloatValue() / (parent.tiltMax - parent.tiltMin) * 2;
362-
Vector2D targetPanTilt = new Vector2D();
353+
float rtilt = data.getFloatValue();
354+
try {
355+
Vector2D targetPanTilt = new Vector2D();
363356

364-
targetPanTilt.setX(0.0f);
365-
targetPanTilt.setY(rtilt);
366-
targetPanTilt.setSpace(config.getDefaultRelativePanTiltTranslationSpace());
367-
position.setPanTilt(targetPanTilt);
357+
targetPanTilt.setX(0.0f);
358+
targetPanTilt.setY(rtilt);
359+
targetPanTilt.setSpace(config.getDefaultRelativePanTiltTranslationSpace());
360+
position.setPanTilt(targetPanTilt);
368361

369-
camera.getPtz().relativeMove(ptzProfile.getToken(), position, speed);
362+
camera.getPtz().relativeMove(ptzProfile.getToken(), position, speed);
363+
} catch (Exception e) {
364+
if (devicePtzConfig.getDefaultAbsolutePantTiltPositionSpace() != null) {
365+
float tilt = parent.ptzPosOutput.getLatestRecord().getFloatValue(2) + rtilt;
366+
parent.absoluteTilt(tilt);
367+
}
368+
}
370369
}
371370
// RELATIVE ZOOM
372371
else if (itemID.equals(VideoCamHelper.TASKING_RZOOM))

0 commit comments

Comments
 (0)