File: track.c

package info (click to toggle)
motion 3.2.9-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,912 kB
  • ctags: 1,533
  • sloc: ansic: 14,340; sh: 724; makefile: 216
file content (828 lines) | stat: -rw-r--r-- 24,807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/*	track.c
 *
 *	Experimental motion tracking.
 *
 *	Copyright 2000, Jeroen Vreeken
 *	This program is published under the GNU Public license
 */

#include <math.h>
#include <termios.h>
#include "motion.h"

#ifndef WITHOUT_V4L
#include "pwc-ioctl.h"
#endif


struct trackoptions track_template = {
	dev:            -1,             /* dev open */
	port:           NULL,           /* char *port */
	motorx:         0,              /* int motorx */
	motory:		0,		/* int motory */
	maxx:           0,              /* int maxx; */
	maxy:		0,		/* int maxy; */
	speed:          TRACK_SPEED,    /* speed */
	stepsize:       TRACK_STEPSIZE, /* stepsize */
	active:         0,              /* auto tracking active */
	minmaxfound:    0,  /* flag for minmax values stored for pwc based camera */
	step_angle_x:   10, /* step angle in degrees X-axis that camera moves during auto tracking */
	step_angle_y:   10, /* step angle in degrees Y-axis that camera moves during auto tracking */
	move_wait:      10   /* number of frames to disable motion detection after camera moving */
};


/* Add your own center and move functions here: */
static int stepper_center(struct context *cnt, int xoff, int yoff ATTRIBUTE_UNUSED);
static int stepper_move(struct context *cnt, int dev, struct coord *cent, struct images *imgs);
static int iomojo_center(struct context *cnt, int xoff, int yoff);
static int iomojo_move(struct context *cnt, int dev, struct coord *cent, struct images *imgs);
#ifndef WITHOUT_V4L
static int lqos_center(struct context *cnt, int dev, int xoff, int yoff);
static int lqos_move(struct context *cnt, int dev, struct coord *cent, struct images *imgs, int manual);
#ifdef MOTION_V4L2
static int uvc_center(struct context *cnt, int dev, int xoff, int yoff);
static int uvc_move(struct context *cnt, int dev, struct coord *cent, struct images *imgs, int manual);
#endif /* MOTION_V4L2 */
#endif /* WITHOUT_V4L */

/* Add a call to your functions here: */
int track_center(struct context *cnt, int dev, int manual, int xoff, int yoff)
{
	if (!manual && !cnt->track.active)
		return 0;
	if (cnt->track.type == TRACK_TYPE_STEPPER){
		int ret;
		ret = stepper_center(cnt, xoff, yoff);
		if (ret < 1) {
				motion_log(LOG_ERR, 1, "track_center: internal error (stepper_center)");
				return 0;		
		}
		else return ret;	
	}
#ifndef WITHOUT_V4L	
	else if (cnt->track.type == TRACK_TYPE_PWC)
		return lqos_center(cnt, dev, xoff, yoff);
#ifdef MOTION_V4L2
	else if (cnt->track.type == TRACK_TYPE_UVC)
		return uvc_center(cnt, dev, xoff, yoff);
#endif /* MOTION_V4L2 */
#endif /* WITHOUT_V4L */
	else if (cnt->track.type == TRACK_TYPE_IOMOJO)
		return iomojo_center(cnt, xoff, yoff);
	else if (cnt->track.type == TRACK_TYPE_GENERIC)
		return 10; // FIX ME. I chose to return something reasonable.

	motion_log(LOG_ERR, 1, "track_center: internal error, %d is not a known track-type", cnt->track.type);

	return 0;
}

/* Add a call to your functions here: */
int track_move(struct context *cnt, int dev, struct coord *cent, struct images *imgs, int manual)
{
	if (!manual && !cnt->track.active)
		return 0;
	if (cnt->track.type == TRACK_TYPE_STEPPER)
		return stepper_move(cnt, dev, cent, imgs);
#ifndef WITHOUT_V4L
	else if (cnt->track.type == TRACK_TYPE_PWC)
		return lqos_move(cnt, dev, cent, imgs, manual);
#ifdef MOTION_V4L2
	else if (cnt->track.type == TRACK_TYPE_UVC)
		return uvc_move(cnt, dev, cent, imgs, manual);
#endif /* MOTION_V4L2 */
#endif /* WITHOUT_V4L */
	else if (cnt->track.type == TRACK_TYPE_IOMOJO)
		return iomojo_move(cnt, dev, cent, imgs);
	else if (cnt->track.type == TRACK_TYPE_GENERIC)
		return cnt->track.move_wait; // FIX ME. I chose to return something reasonable.

	motion_log(LOG_ERR, 1, "track_move: internal error, %d is not a known track-type", cnt->track.type);

	return 0;
}


/******************************************************************************

	Stepper motor on serial port

******************************************************************************/


static int stepper_command(struct context *cnt, int motor, int command, int n)
{
	char buffer[3];
	time_t timeout=time(NULL);

	buffer[0]=motor;
	buffer[1]=command;
	buffer[2]=n;
	if (write(cnt->track.dev, buffer, 3)!=3)
		return -1;

	while (read(cnt->track.dev, buffer, 1)!=1 && time(NULL) < timeout+1);
	if (time(NULL) >= timeout+2) {
		motion_log(LOG_ERR, 1, "Status byte timeout!");
		return 0;
	}
	return buffer[0];
}


static int stepper_status(struct context *cnt, int motor)
{
	return stepper_command(cnt, motor, STEPPER_COMMAND_STATUS, 0);
}


static int stepper_center(struct context *cnt, int x_offset, int y_offset)
{
	struct termios adtio;

	if (cnt->track.dev<0) {
		motion_log(LOG_INFO, 1, "Try to open serial device %s", cnt->track.port);
		
		if ((cnt->track.dev=open(cnt->track.port, O_RDWR | O_NOCTTY)) < 0) {
			motion_log(LOG_ERR, 1, "Unable to open serial device %s", cnt->track.port);
			return -1;
		}

		bzero (&adtio, sizeof(adtio));
		adtio.c_cflag= STEPPER_BAUDRATE | CS8 | CLOCAL | CREAD;
		adtio.c_iflag= IGNPAR;
		adtio.c_oflag= 0;
		adtio.c_lflag= 0;	/* non-canon, no echo */
		adtio.c_cc[VTIME]=0;	/* timer unused */
		adtio.c_cc[VMIN]=0;	/* blocking read until 1 char */
		tcflush (cnt->track.dev, TCIFLUSH);

		if (tcsetattr(cnt->track.dev, TCSANOW, &adtio) < 0) {
			motion_log(LOG_ERR, 1, "Unable to initialize serial device %s", cnt->track.port);
			return -1;
		}
		motion_log(LOG_INFO, 1, "Opened serial device %s and initialize", cnt->track.port);
	}

	/* x-axis */
	
	stepper_command(cnt, cnt->track.motorx, STEPPER_COMMAND_SPEED, cnt->track.speed);
	stepper_command(cnt, cnt->track.motorx, STEPPER_COMMAND_LEFT_N, cnt->track.maxx);

	while (stepper_status(cnt, cnt->track.motorx) & STEPPER_STATUS_LEFT);

	stepper_command(cnt, cnt->track.motorx, STEPPER_COMMAND_RIGHT_N,
	                cnt->track.maxx / 2 + x_offset * cnt->track.stepsize);

	while (stepper_status(cnt, cnt->track.motorx) & STEPPER_STATUS_RIGHT);

	/* y-axis */

	stepper_command(cnt, cnt->track.motory, STEPPER_COMMAND_SPEED, cnt->track.speed);
	stepper_command(cnt, cnt->track.motory, STEPPER_COMMAND_UP_N, cnt->track.maxy);

	while (stepper_status(cnt, cnt->track.motory) & STEPPER_STATUS_UP)
	
	stepper_command(cnt, cnt->track.motory, STEPPER_COMMAND_DOWN_N,
			cnt->track.maxy / 2 + y_offset * cnt->track.stepsize);
		
	while (stepper_status(cnt, cnt->track.motory) & STEPPER_STATUS_DOWN);
	
	return cnt->track.move_wait;
}

static int stepper_move(struct context *cnt, int dev, struct coord *cent, struct images *imgs)
{
	int command = 0;
	int n = 0;

	if (dev < 0){
		motion_log(LOG_INFO, 1, "No device started yet , try stepper_center()");	
		if (stepper_center(cnt, 0, 0) < 0){
			motion_log(LOG_ERR, 1, "Stepper_center() failed to initialize stepper device.");	
			return(0);
		}
		motion_log(LOG_INFO, 1, "stepper_center() succeed , device started");	
	}
	
	/* x-axis */
	
	if (cent->x < imgs->width / 2) {
		command = STEPPER_COMMAND_LEFT_N;
		n = imgs->width / 2 - cent->x;
	}

	if (cent->x > imgs->width / 2) {
		command = STEPPER_COMMAND_RIGHT_N;
		n = cent->x - imgs->width / 2;
	}

	n = n * cnt->track.stepsize / imgs->width;

	if (n) 	stepper_command(cnt, cnt->track.motorx, command, n);

	/* y-axis */

	if (cent->y < imgs->height / 2) {
		command = STEPPER_COMMAND_UP_N;
		n = imgs->height / 2 - cent->y;
	}

	if (cent->y > imgs->height / 2) {
		command = STEPPER_COMMAND_DOWN_N;
		n = cent->y - imgs->height / 2;
	}
	
	n = n * cnt->track.stepsize / imgs->height;

	if (n) stepper_command(cnt, cnt->track.motory, command, n);	
	
	
	return cnt->track.move_wait;
}

/******************************************************************************

	Iomojo Smilecam on serial port

******************************************************************************/

static char iomojo_command(struct context *cnt, char *command, int len, int ret)
{
	char buffer[1];
	time_t timeout = time(NULL);

	if (write(cnt->track.dev, command, len) != len)
		return 0;

	if (ret) {
		while (read(cnt->track.dev, buffer, 1) != 1 && time(NULL) < timeout + 2);
		
		if (time(NULL) >= timeout + 2) {
			motion_log(LOG_ERR, 1, "Return byte timeout!");
			return 0;
		}
	}
	return buffer[0];
}

static void iomojo_setspeed(struct context *cnt, int speed)
{
	char command[3];
	
	command[0] = IOMOJO_SETSPEED_CMD;
	command[1] = cnt->track.iomojo_id;
	command[2] = speed;
	
	if (iomojo_command(cnt, command, 3, 1)!=IOMOJO_SETSPEED_RET)
		motion_log(LOG_ERR, 1, "Unable to set camera speed");
}

static void iomojo_movehome(struct context *cnt)
{
	char command[2];
	
	command[0] = IOMOJO_MOVEHOME;
	command[1] = cnt->track.iomojo_id;

	iomojo_command(cnt, command, 2, 0);
}

static int iomojo_center(struct context *cnt, int x_offset, int y_offset)
{
	struct termios adtio;
	char command[5], direction=0;

	if (cnt->track.dev<0) {
		if ((cnt->track.dev=open(cnt->track.port, O_RDWR | O_NOCTTY)) < 0) {
			motion_log(LOG_ERR, 1, "Unable to open serial device %s", cnt->track.port);
			return 0;
		}
		bzero (&adtio, sizeof(adtio));
		adtio.c_cflag = IOMOJO_BAUDRATE | CS8 | CLOCAL | CREAD;
		adtio.c_iflag = IGNPAR;
		adtio.c_oflag = 0;
		adtio.c_lflag = 0;      /* non-canon, no echo */
		adtio.c_cc[VTIME] = 0;  /* timer unused */
		adtio.c_cc[VMIN] = 0;   /* blocking read until 1 char */
		tcflush(cnt->track.dev, TCIFLUSH);
		if (tcsetattr(cnt->track.dev, TCSANOW, &adtio) < 0) {
			motion_log(LOG_ERR, 1, "Unable to initialize serial device %s", cnt->track.port);
			return 0;
		}
	}

	iomojo_setspeed(cnt, 40);
	iomojo_movehome(cnt);

	if (x_offset || y_offset) {
		if (x_offset > 0)
			direction |= IOMOJO_DIRECTION_RIGHT;
		else {
			direction |= IOMOJO_DIRECTION_LEFT;
			x_offset *= -1;
		}

		if (y_offset > 0)
			direction |= IOMOJO_DIRECTION_UP;
		else {
			direction |= IOMOJO_DIRECTION_DOWN;
			y_offset *= -1;
		}

		if (x_offset > 180)
			x_offset = 180;

		if (y_offset > 60)
			y_offset = 60;

		command[0] = IOMOJO_MOVEOFFSET_CMD;
		command[1] = cnt->track.iomojo_id;
		command[2] = direction;
		command[3] = x_offset;
		command[4] = y_offset;
		iomojo_command(cnt, command, 5, 0);
	}

	return cnt->track.move_wait;
}

static int iomojo_move(struct context *cnt, int dev, struct coord *cent, struct images *imgs)
{
	char command[5];
	int direction = 0;
	int nx = 0, ny = 0;
	int i;
	
	if (dev < 0)
		if (iomojo_center(cnt, 0, 0) < 0)
			return 0;

	if (cent->x < imgs->width / 2) {
		direction |= IOMOJO_DIRECTION_LEFT;
		nx = imgs->width / 2 - cent->x;
	}

	if (cent->x > imgs->width / 2) {
		direction |= IOMOJO_DIRECTION_RIGHT;
		nx = cent->x - imgs->width / 2;
	}

	if (cent->y < imgs->height / 2) {
		direction |= IOMOJO_DIRECTION_DOWN;
		ny = imgs->height / 2 - cent->y;
	}

	if (cent->y > imgs->height / 2) {
		direction |= IOMOJO_DIRECTION_UP;
		ny = cent->y - imgs->height / 2;
	}

	nx = nx * 72 / imgs->width;
	ny = ny * 72 / imgs->height;

	if (nx || ny) {
		if (nx > 180)
			nx = 180;

		if (ny > 60)
			ny = 60;

		command[0] = IOMOJO_MOVEOFFSET_CMD;
		command[1] = cnt->track.iomojo_id;
		command[2] = direction;
		command[3] = nx;
		command[4] = ny;
		iomojo_command(cnt, command, 5, 0);

		/* Number of frames to skip while moving */
		if (ny >= nx)
			i = 25 * ny / 90;
		else
			i = 25 * nx / 90;
		return i;
	}

	return 0;
}

/******************************************************************************

	Logitech QuickCam Orbit camera tracking code by folkert@vanheusden.com

******************************************************************************/
#ifndef WITHOUT_V4L
static int lqos_center(struct context *cnt, int dev, int x_angle, int y_angle)
{
	int reset = 3;
	struct pwc_mpt_angles pma;
	struct pwc_mpt_range pmr;

	if (cnt->track.dev==-1) {

		if (ioctl(dev, VIDIOCPWCMPTRESET, &reset) == -1) {
			motion_log(LOG_ERR, 1, "Failed to reset camera to starting position! Reason");
			return 0;
		}

		SLEEP(6,0)

		if (ioctl(dev, VIDIOCPWCMPTGRANGE, &pmr) == -1) {
			motion_log(LOG_ERR, 1, "failed VIDIOCPWCMPTGRANGE");
			return 0;
		}

		cnt->track.dev = dev;
		cnt->track.minmaxfound = 1;
		cnt->track.panmin = pmr.pan_min;
		cnt->track.panmax = pmr.pan_max;
		cnt->track.tiltmin = pmr.tilt_min;
		cnt->track.tiltmax = pmr.tilt_max;
	}

	if (ioctl(dev, VIDIOCPWCMPTGANGLE, &pma) == -1)
		motion_log(LOG_ERR, 1, "ioctl VIDIOCPWCMPTGANGLE");
	
	pma.absolute = 1;

	if (x_angle * 100 < cnt->track.panmax && x_angle * 100 > cnt->track.panmin)
		pma.pan = x_angle * 100;

	if (y_angle * 100 < cnt->track.tiltmax && y_angle * 100 > cnt->track.tiltmin)
		pma.tilt = y_angle * 100;

	if (ioctl(dev, VIDIOCPWCMPTSANGLE, &pma) == -1) {
		motion_log(LOG_ERR, 1, "Failed to pan/tilt camera! Reason");
		return 0;
	}

	return cnt->track.move_wait;
}

static int lqos_move(struct context *cnt, int dev, struct coord *cent, struct images *imgs, int manual)
{
	int delta_x = cent->x - (imgs->width / 2);
	int delta_y = cent->y - (imgs->height / 2);
	int move_x_degrees, move_y_degrees;
	struct pwc_mpt_angles pma;
	struct pwc_mpt_range pmr;

	/* If we are on auto track we calculate delta, otherwise we use user input in degrees times 100 */
	if (!manual) {
		if (delta_x > imgs->width * 3/8 && delta_x < imgs->width * 5/8)
			return 0;
		if (delta_y > imgs->height * 3/8 && delta_y < imgs->height * 5/8)
			return 0;

		move_x_degrees = delta_x * cnt->track.step_angle_x * 100 / (imgs->width / 2);
		move_y_degrees = -delta_y * cnt->track.step_angle_y * 100 / (imgs->height / 2);
	} else {
		move_x_degrees = cent->x * 100;
		move_y_degrees = cent->y * 100;
	}
	
	/* If we never checked for the min/max values for pan/tilt we do it now */
	if (cnt->track.minmaxfound == 0) {
		if (ioctl(dev, VIDIOCPWCMPTGRANGE, &pmr) == -1) {
			motion_log(LOG_ERR, 1, "failed VIDIOCPWCMPTGRANGE");
			return 0;
		}
		cnt->track.minmaxfound = 1;
		cnt->track.panmin = pmr.pan_min;
		cnt->track.panmax = pmr.pan_max;
		cnt->track.tiltmin = pmr.tilt_min;
		cnt->track.tiltmax = pmr.tilt_max;
	}

	/* Get current camera position */
	if (ioctl(dev, VIDIOCPWCMPTGANGLE, &pma) == -1)
		motion_log(LOG_ERR, 1, "ioctl VIDIOCPWCMPTGANGLE");


	/* Check current position of camera and see if we need to adjust
	   values down to what is left to move */
	if (move_x_degrees<0 && (cnt->track.panmin - pma.pan) > move_x_degrees)
		move_x_degrees = (cnt->track.panmin - pma.pan);

	if (move_x_degrees>0 && (cnt->track.panmax - pma.pan) < move_x_degrees)
		move_x_degrees = (cnt->track.panmax - pma.pan);

	if (move_y_degrees<0 && (cnt->track.tiltmin - pma.tilt) > move_y_degrees)
		move_y_degrees = (cnt->track.tiltmin - pma.tilt);

	if (move_y_degrees>0 && (cnt->track.tiltmax - pma.tilt) < move_y_degrees)
		move_y_degrees = (cnt->track.tiltmax - pma.tilt);
		
	/* Move camera relative to current position */
	pma.absolute = 0;
	pma.pan = move_x_degrees;
	pma.tilt = move_y_degrees;

	if (ioctl(dev, VIDIOCPWCMPTSANGLE, &pma) == -1) {
		motion_log(LOG_ERR, 1, "Failed to pan/tilt camera! Reason");
		return 0;
	}

	return cnt->track.move_wait;
}
/******************************************************************************

	Logitech QuickCam Sphere camera tracking code by oBi
	
	Modify by Dirk Wesenberg(Munich) 30.03.07 
	- for new API in uvcvideo 
	- add Trace-steps for investigation
******************************************************************************/
#ifdef MOTION_V4L2

static int uvc_center(struct context *cnt, int dev, int x_angle, int y_angle)
{
	/* CALC ABSOLUTE MOVING : Act.Position +/- delta to request X and Y */
	int move_x_degrees = 0, move_y_degrees = 0;

	union pantilt {
		struct {
			short pan;
			short tilt;
		} s16;
		int value;
	};
	union pantilt pan;

	if (cnt->track.dev==-1) {

		int reset = 3; //0-non reset, 1-reset pan, 2-reset tilt, 3-reset pan&tilt
		struct v4l2_control control_s;

		control_s.id = V4L2_CID_PANTILT_RESET;
		control_s.value = (unsigned char) reset;

		if (ioctl(dev, VIDIOC_S_CTRL, &control_s) < 0) {
			motion_log(LOG_ERR, 1, "Failed to reset camera to starting position! Reason");
			return 0;
		}
		motion_log(LOG_DEBUG, 1, "Reseting camera to starting position");

		SLEEP(8,0)

		/* Get camera range */
		struct v4l2_queryctrl queryctrl;

		queryctrl.id = V4L2_CID_PAN_RELATIVE;
		if (ioctl(dev, VIDIOC_QUERYCTRL, &queryctrl) < 0 ) {
			motion_log(LOG_ERR, 1, "ioctl querycontrol error %d",errno);
			return 0;
		}
		motion_log(LOG_DEBUG, 1, "Getting camera range");
		

		/* DWe 30.03.07 The orig request failed : 
		* must be VIDIOC_G_CTRL separate for pan and tilt or via VIDIOC_G_EXT_CTRLS - now for 1st manual 
		* Range X = -70 to +70 degrees              
		* Y = -30 to +30 degrees  
		*/	
		
//		//get mininum
//		pan.value = queryctrl.minimum;

		cnt->track.panmin = -4480 / INCPANTILT;
		cnt->track.tiltmin = -1920 / INCPANTILT;
//		//get maximum
		cnt->track.panmax = 4480 / INCPANTILT; 
		cnt->track.tiltmax = 1920 / INCPANTILT;
//		pan.value = queryctrl.maximum;

		cnt->track.dev = dev;
		cnt->track.pan_angle = 0;
		cnt->track.tilt_angle = 0;
		cnt->track.minmaxfound = 1;

	}

	struct v4l2_control control_s;

	motion_log(LOG_ERR, 1, "INPUT_PARAM_ABS pan_min %d,pan_max %d,tilt_min %d,tilt_max %d ", cnt->track.panmin, cnt->track.panmax, cnt->track.tiltmin, cnt->track.tiltmax );
	motion_log(LOG_ERR, 1, "INPUT_PARAM_ABS X_Angel %d, Y_Angel %d ", x_angle, y_angle);

	if (x_angle <= cnt->track.panmax && x_angle >= cnt->track.panmin)
		move_x_degrees = x_angle - (cnt->track.pan_angle);

	if (y_angle <= cnt->track.tiltmax && y_angle >= cnt->track.tiltmin)
		move_y_degrees = y_angle - (cnt->track.tilt_angle);
			

	/*
	tilt up: - value
	tilt down: + value
	pan left: - value
	pan right: + value
	*/
	pan.s16.pan = -move_x_degrees * INCPANTILT;
	pan.s16.tilt = -move_y_degrees * INCPANTILT;
	
	motion_log(LOG_ERR, 1, "For_SET_ABS move_X %d,move_Y %d", move_x_degrees, move_y_degrees);
		
	/* DWe 30.03.07 Must be broken in diff calls, because 
    	- one call for both is not accept via VIDIOC_S_CTRL -> maybe via VIDIOC_S_EXT_CTRLS
    	- The Webcam or uvcvideo does not like a call with a zero-move 
	*/
    
	if (move_x_degrees != 0 ) {
		control_s.id = V4L2_CID_PAN_RELATIVE;
	//	control_s.value = pan.value;
		control_s.value = pan.s16.pan;
		if (ioctl(dev, VIDIOC_S_CTRL, &control_s) < 0) {
			motion_log(LOG_ERR, 1, "Failed to move camera!");
			return 0;
		}
	}

	/* DWe 30.03.07 We must wait a little,before we set the next CMD, otherwise PAN is mad ... */ 	
	if ((move_x_degrees != 0) && (move_y_degrees != 0)) {
		SLEEP (1,0);
	}   
	
	if (move_y_degrees != 0 ) {
		control_s.id = V4L2_CID_TILT_RELATIVE;
	//	control_s.value = pan.value;
		control_s.value = pan.s16.tilt;
		if (ioctl(dev, VIDIOC_S_CTRL, &control_s) < 0) {
			motion_log(LOG_ERR, 1, "Failed to move camera!");
			return 0;
		}	
	
	}

	motion_log(LOG_ERR, 1,"Found MINMAX = %d", cnt->track.minmaxfound); 

	if (cnt->track.dev!=-1) {
		motion_log(LOG_ERR, 1," Before_ABS_Y_Angel : x= %d , Y= %d , ", cnt->track.pan_angle, cnt->track.tilt_angle );
		if (move_x_degrees != -1) { 
			cnt->track.pan_angle += move_x_degrees;
		}
		if (move_x_degrees != -1) { 
			cnt->track.tilt_angle += move_y_degrees;
		}
		motion_log(LOG_ERR, 1," After_ABS_Y_Angel : x= %d , Y= %d , ", cnt->track.pan_angle, cnt->track.tilt_angle );	
	}

	return cnt->track.move_wait;
}

static int uvc_move(struct context *cnt, int dev, struct coord *cent, struct images *imgs, int manual)
{
	/* RELATIVE MOVING : Act.Position +/- X and Y */
	
	int delta_x = cent->x - (imgs->width / 2);
	int delta_y = cent->y - (imgs->height / 2);
	int move_x_degrees, move_y_degrees;
	
	/* DWe 30.03.07 Does the request of act.position from WebCam work ? luvcview shows at every position 180 :( */
	/*		Now we init the Web by call Reset, so we can sure, that we are at x/y = 0,0                 */
	/* 		Don't worry, if the WebCam make a sound - over End at PAN  - hmmm, should it be normal ...? */
	/* 		PAN Value 7777 in relative will init also a want reset for CAM - it will be "0" after that  */  
	if (( cnt->track.minmaxfound != 1) || (cent->x == 7777 )) {
		int reset = 3; //0-non reset, 1-reset pan, 2-reset tilt, 3-reset pan&tilt
		struct v4l2_control control_s;

		control_s.id = V4L2_CID_PANTILT_RESET;
		control_s.value = (unsigned char) reset;

		if (ioctl(dev, VIDIOC_S_CTRL, &control_s) < 0) {
			motion_log(LOG_ERR, 1, "Failed to reset camera to starting position! Reason");
			return 0;
		}
		motion_log(LOG_DEBUG, 1, "Reseting camera to starting position");
		
		/* set the "helpvalue" back to null because after reset CAM should be in x=0 and not 70 */
		cent->x = 0;
		SLEEP(8,0);
		
		/* DWe 30.03.07 The orig request failed : 
		* must be VIDIOC_G_CTRL separate for pan and tilt or via VIDIOC_G_EXT_CTRLS - now for 1st manual 
		* Range X = -70 to +70 degrees              
		*	Y = -30 to +30 degrees  
		*/	

		cnt->track.panmin = -4480 / INCPANTILT;
		cnt->track.tiltmin = -1920 / INCPANTILT;
		cnt->track.panmax = 4480 / INCPANTILT; 
		cnt->track.tiltmax = 1920 / INCPANTILT;
		cnt->track.dev = dev;
		cnt->track.pan_angle = 0;
		cnt->track.tilt_angle = 0;
		cnt->track.minmaxfound = 1;
	}

	
	/* If we are on auto track we calculate delta, otherwise we use user input in degrees */
	if (!manual) {
		if (delta_x > imgs->width * 3/8 && delta_x < imgs->width * 5/8)
			return 0;
		if (delta_y > imgs->height * 3/8 && delta_y < imgs->height * 5/8)
			return 0;

		move_x_degrees = delta_x * cnt->track.step_angle_x / (imgs->width / 2);
		move_y_degrees = -delta_y * cnt->track.step_angle_y / (imgs->height / 2);
	} else {
		move_x_degrees = cent->x;
		move_y_degrees = cent->y;
	}

	union pantilt {
		struct {
			short pan;
			short tilt;
		} s16;
		int value;
	};

	struct v4l2_control control_s;
	union pantilt pan;

	if (cnt->track.minmaxfound == 1) {
	/* Check current position of camera and see if we need to adjust
	values down to what is left to move */
		if (move_x_degrees<0 && (cnt->track.panmin - cnt->track.pan_angle) > move_x_degrees)
			move_x_degrees = (cnt->track.panmin - cnt->track.pan_angle);

		if (move_x_degrees>0 && (cnt->track.panmax - cnt->track.pan_angle) < move_x_degrees)
			move_x_degrees = (cnt->track.panmax - cnt->track.pan_angle);

		if (move_y_degrees<0 && (cnt->track.tiltmin - cnt->track.tilt_angle) > move_y_degrees)
			move_y_degrees = (cnt->track.tiltmin - cnt->track.tilt_angle);

		if (move_y_degrees>0 && (cnt->track.tiltmax - cnt->track.tilt_angle) < move_y_degrees)
			move_y_degrees = (cnt->track.tiltmax - cnt->track.tilt_angle);
	}


	motion_log(LOG_ERR, 1, "For_SET_REL pan_min %d,pan_max %d,tilt_min %d,tilt_max %d ", cnt->track.panmin, cnt->track.panmax, cnt->track.tiltmin, cnt->track.tiltmax );
	motion_log(LOG_ERR, 1, "For_SET_REL track_pan_Angel %d, track_tilt_Angel %d ", cnt->track.pan_angle, cnt->track.tilt_angle);	
	motion_log(LOG_ERR, 1, "For_SET_REL move_X %d,move_Y %d", move_x_degrees, move_y_degrees);
	/*
	tilt up: - value
	tilt down: + value
	pan left: - value
	pan right: + value
	*/

	pan.s16.pan = -move_x_degrees * INCPANTILT;
	pan.s16.tilt = -move_y_degrees * INCPANTILT;
	
	/* DWe 30.03.07 Must be broken in diff calls, because 
    	   - one call for both is not accept via VIDIOC_S_CTRL -> maybe via VIDIOC_S_EXT_CTRLS
    	   - The Webcam or uvcvideo does not like a call with a zero-move 
    	*/

	if (move_x_degrees != 0) {

		control_s.id = V4L2_CID_PAN_RELATIVE;

//	control_s.value = pan.value;
		control_s.value = pan.s16.pan;
		motion_log(LOG_ERR, 1," dev %d,addr= %d, control_S= %d,Wert= %d,", dev,VIDIOC_S_CTRL, &control_s, pan.s16.pan ); 
		if (ioctl(dev, VIDIOC_S_CTRL, &control_s) < 0) {
	    	    motion_log(LOG_ERR, 1, "Failed to move camera!");
		    return 0;
		}
	}
	
	/* DWe 30.03.07 We must wait a little,before we set the next CMD, otherwise PAN is mad ... */ 	
        if ((move_x_degrees != 0) && (move_y_degrees != 0)) {
	    SLEEP (1,0);
	}   


	if (move_y_degrees != 0) {

	control_s.id = V4L2_CID_TILT_RELATIVE;

//	control_s.value = pan.value;
	control_s.value = pan.s16.tilt;
		motion_log(LOG_ERR, 1," dev %d,addr= %d, control_S= %d, Wert= %d, ", dev,VIDIOC_S_CTRL, &control_s, pan.s16.tilt ); 
		if (ioctl(dev, VIDIOC_S_CTRL, &control_s) < 0) {
		motion_log(LOG_ERR, 1, "Failed to move camera!");
		return 0;
		}
	}
	
  
    		motion_log(LOG_ERR, 1,"Found MINMAX = %d", cnt->track.minmaxfound); 
		if (cnt->track.minmaxfound == 1) {
			motion_log(LOG_ERR, 1," Before_REL_Y_Angel : x= %d , Y= %d", cnt->track.pan_angle, cnt->track.tilt_angle  );
		
		    	if (move_x_degrees != 0){ 
			    cnt->track.pan_angle += -pan.s16.pan / INCPANTILT;
			}	
			if (move_y_degrees != 0){
			    cnt->track.tilt_angle += -pan.s16.tilt / INCPANTILT;
			}    
			motion_log(LOG_ERR, 1," After_REL_Y_Angel : x= %d , Y= %d", cnt->track.pan_angle, cnt->track.tilt_angle  );
		}

	return cnt->track.move_wait;
}
#endif /* MOTION_V4L2 */
#endif /* WITHOUT_V4L */