Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
810 | - | 1 | #!/usr/bin/perl |
2 | #!/usr/bin/perl -d:ptkdb |
||
3 | |||
4 | ############################################################################### |
||
5 | # |
||
6 | # mktrack.pl - Tracking Antenne |
||
7 | # |
||
8 | # Copyright (C) 2009 Rainer Walther (rainerwalther-mail@web.de) |
||
9 | # |
||
10 | # Creative Commons Lizenz mit den Zusaetzen (by, nc, sa) |
||
11 | # |
||
12 | # Es ist Ihnen gestattet: |
||
13 | # * das Werk vervielfältigen, verbreiten und öffentlich zugänglich machen |
||
14 | # * Abwandlungen bzw. Bearbeitungen des Inhaltes anfertigen |
||
15 | # |
||
16 | # Zu den folgenden Bedingungen: |
||
17 | # * Namensnennung. |
||
18 | # Sie müssen den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen. |
||
19 | # * Keine kommerzielle Nutzung. |
||
20 | # Dieses Werk darf nicht für kommerzielle Zwecke verwendet werden. |
||
21 | # * Weitergabe unter gleichen Bedingungen. |
||
22 | # Wenn Sie den lizenzierten Inhalt bearbeiten oder in anderer Weise umgestalten, |
||
23 | # verändern oder als Grundlage für einen anderen Inhalt verwenden, |
||
24 | # dürfen Sie den neu entstandenen Inhalt nur unter Verwendung von Lizenzbedingungen |
||
25 | # weitergeben, die mit denen dieses Lizenzvertrages identisch oder vergleichbar sind. |
||
26 | # |
||
27 | # Im Falle einer Verbreitung müssen Sie anderen die Lizenzbedingungen, unter welche dieses |
||
28 | # Werk fällt, mitteilen. Am Einfachsten ist es, einen Link auf diese Seite einzubinden. |
||
29 | # |
||
30 | # Jede der vorgenannten Bedingungen kann aufgehoben werden, sofern Sie die Einwilligung |
||
31 | # des Rechteinhabers dazu erhalten. |
||
32 | # |
||
33 | # Diese Lizenz lässt die Urheberpersönlichkeitsrechte unberührt. |
||
34 | # |
||
35 | # Weitere Details zur Lizenzbestimmung gibt es hier: |
||
36 | # Kurzform: http://creativecommons.org/licenses/by-nc-sa/3.0/de/ |
||
37 | # Komplett: http://creativecommons.org/licenses/by-nc-sa/3.0/de/legalcode |
||
38 | # |
||
39 | ############################################################################### |
||
40 | # 2009-02-14 0.0.1 rw created |
||
41 | # 2009-04-01 0.1.0 rw RC1 |
||
42 | # 2009-06-14 0.1.1 rw Tilt-Servo added |
||
43 | # 2009-08-15 0.1.2 rw Flip Pan/Tilt servo for >180 degree |
||
44 | # config servo direction and range |
||
45 | # optional get antenna home position from Map-config |
||
46 | # Signal handler replaced by command-queue |
||
47 | # 2009-09-30 0.1.3 rw Commandline parameter added |
||
48 | # 2009-10-07 0.1.4 rw COM-Port > 9 |
||
49 | # Servo Speed, neutral position and 8 bit position |
||
50 | # Add Coldstart command |
||
51 | # Relax servo at shutdown |
||
52 | # PortSetSkip config |
||
53 | # 2010-01-02 0.1.5 rw bugfix |
||
54 | # 2010-03-12 0.1.6 rw Pololu Maestro-6 Controller |
||
55 | # 2010-07-29 0.1.7 rw change ServoTest/GpsWait sequence |
||
56 | # |
||
57 | ############################################################################### |
||
58 | |||
59 | $Version{'track.pl'} = "0.1.6 - 2010-03-12"; |
||
60 | |||
61 | |||
62 | if ( $0 =~ /track.pl$/i ) |
||
63 | { |
||
64 | # Program wurde direkt aufgerufen |
||
65 | |||
66 | # change working directory to program path |
||
67 | my $Cwd = substr ($0, 0, rindex ($0, "track.pl")); |
||
68 | chdir $Cwd; |
||
69 | |||
70 | # set path for local Perl libs |
||
71 | push @INC, $Cwd . "perl/lib"; |
||
72 | } |
||
73 | |||
74 | |||
75 | # Packages |
||
76 | use Time::HiRes qw(usleep gettimeofday); # http://search.cpan.org/~jhi/Time-HiRes-1.9719/HiRes.pm |
||
77 | use threads; # http://search.cpan.org/~jdhedden/threads-1.72/threads.pm |
||
78 | # http://perldoc.perl.org/threads.html |
||
79 | use threads::shared; # http://search.cpan.org/~jdhedden/threads-shared-1.28/shared.pm |
||
80 | use Thread::Queue; # http://search.cpan.org/dist/Thread-Queue-2.11/lib/Thread/Queue.pm |
||
81 | use Math::Trig; |
||
82 | use Geo::Ellipsoid; # http://search.cpan.org/dist/Geo-Ellipsoid-1.12/lib/Geo/Ellipsoid.pm |
||
83 | # http://www.kompf.de/gps/distcalc.html |
||
84 | # http://www.herrmann-w.de/Geocaching/Downloads/Richt.XLS |
||
85 | # http://williams.best.vwh.net/avform.htm |
||
86 | # http://williams.best.vwh.net/gccalc.html |
||
87 | # http://de.wikipedia.org/wiki/Orthodrome |
||
88 | if ( $^O =~ /Win32/i ) |
||
89 | { |
||
90 | require Win32::SerialPort; # http://search.cpan.org/dist/Win32-SerialPort |
||
91 | } |
||
92 | else |
||
93 | { |
||
94 | require Device::SerialPort; # http://search.cpan.org/~cook/Device-SerialPort-1.04/SerialPort.pm |
||
95 | } |
||
96 | |||
97 | require "mkcomm.pl"; # MK communication |
||
98 | |||
99 | # Sharing for Threads |
||
100 | share (@ServoPos); |
||
101 | share (%MkTrack); |
||
102 | |||
103 | # Queue for receiving commands |
||
104 | $TrackQueue = Thread::Queue->new(); |
||
105 | |||
106 | # Commandline |
||
107 | my %CmdLine = @ARGV; |
||
108 | |||
109 | # |
||
110 | # Parameter |
||
111 | # |
||
112 | |||
113 | $SysTimerResolution = 1000; # System Timer resolution in us |
||
114 | $TrackInterval = 50; # Tracking in ms |
||
115 | |||
116 | # Com Port for Pololu Mikro-Servoboard |
||
117 | # http://www.shop.robotikhardware.de/shop/catalog/product_info.php?cPath=65&products_id=118 |
||
118 | my $ComPort = $Cfg->{'track'}->{'Port'} || "COM8"; |
||
119 | |||
120 | # Servo parameter |
||
121 | $ServoPan = 0; # Servo channel Pan |
||
122 | $ServoTilt = 1; # Servo channel Tilt |
||
123 | $MkTrack{'ServoPan'} = $ServoPan; |
||
124 | $MkTrack{'ServoTilt'} = $ServoTilt; |
||
125 | @ServoSpeed = (200/40, 200/40, 200/40, 200/40, 200/40, 200/40, 200/40, 200/40); # ms/degree |
||
126 | $ServoConstPpm = 20; # PPM protocol overhead in ms |
||
127 | |||
128 | # Servo Parameter defaults |
||
129 | @ServoPar = ( { Min => 1000, Mid => 1500, Max => 2000, Speed => 0, Accel => 0, }, # Servo 0 |
||
130 | { Min => 1000, Mid => 1500, Max => 2000, Speed => 0, Accel => 0, }, # Servo 1 |
||
131 | { Min => 1000, Mid => 1500, Max => 2000, Speed => 0, Accel => 0, }, # Servo 2 |
||
132 | { Min => 1000, Mid => 1500, Max => 2000, Speed => 0, Accel => 0, }, # Servo 3 |
||
133 | { Min => 1000, Mid => 1500, Max => 2000, Speed => 0, Accel => 0, }, # Servo 4 |
||
134 | { Min => 1000, Mid => 1500, Max => 2000, Speed => 0, Accel => 0, }, # Servo 5 |
||
135 | { Min => 1000, Mid => 1500, Max => 2000, Speed => 0, Accel => 0, }, # Servo 6 |
||
136 | { Min => 1000, Mid => 1500, Max => 2000, Speed => 0, Accel => 0, }, # Servo 7 |
||
137 | ); |
||
138 | |||
139 | # Read configuration |
||
140 | $ServoPar[$ServoPan]{'Min'} = $Cfg->{'track'}->{'ServoPanLeft'} || "1000"; |
||
141 | $ServoPar[$ServoPan]{'Mid'} = $Cfg->{'track'}->{'ServoPanMiddle'} || "1500"; |
||
142 | $ServoPar[$ServoPan]{'Max'} = $Cfg->{'track'}->{'ServoPanRight'} || "2000"; |
||
143 | $ServoPar[$ServoPan]{'Speed'} = $Cfg->{'track'}->{'ServoPanSpeed'} || "0"; |
||
144 | $ServoPar[$ServoPan]{'Accel'} = $Cfg->{'track'}->{'ServoPanAccel'} || "0"; |
||
145 | |||
146 | $ServoPar[$ServoTilt]{'Min'} = $Cfg->{'track'}->{'ServoTiltFront'} || "1000"; |
||
147 | $ServoPar[$ServoTilt]{'Mid'} = $Cfg->{'track'}->{'ServoTiltTop'} || "1500"; |
||
148 | $ServoPar[$ServoTilt]{'Max'} = $Cfg->{'track'}->{'ServoTiltBack'} || "2000"; |
||
149 | $ServoPar[$ServoTilt]{'Speed'} = $Cfg->{'track'}->{'ServoTiltSpeed'} || "0"; |
||
150 | $ServoPar[$ServoTilt]{'Accel'} = $Cfg->{'track'}->{'ServoTiltAccel'} || "0"; |
||
151 | |||
152 | my $ServoController = $Cfg->{'track'}->{'ServoController'} || "Pololu Micro Serial"; |
||
153 | |||
154 | # Debug |
||
155 | $MkTrack{'ServoPanLeft'} = $ServoPar[$ServoPan]{'Min'}; |
||
156 | $MkTrack{'ServoPanMiddle'} = $ServoPar[$ServoPan]{'Mid'}; |
||
157 | $MkTrack{'ServoPanRight'} = $ServoPar[$ServoPan]{'Max'}; |
||
158 | $MkTrack{'ServoPanSpeed'} = $ServoPar[$ServoPan]{'Speed'}; |
||
159 | $MkTrack{'ServoPanAccel'} = $ServoPar[$ServoPan]{'Accel'}; |
||
160 | |||
161 | $MkTrack{'ServoTiltLeft'} = $ServoPar[$ServoTilt]{'Min'}; |
||
162 | $MkTrack{'ServoTiltMiddle'} = $ServoPar[$ServoTilt]{'Mid'}; |
||
163 | $MkTrack{'ServoTiltRight'} = $ServoPar[$ServoTilt]{'Max'}; |
||
164 | $MkTrack{'ServoTiltSpeed'} = $ServoPar[$ServoTilt]{'Speed'}; |
||
165 | $MkTrack{'ServoTiltAccel'} = $ServoPar[$ServoTilt]{'Accel'}; |
||
166 | |||
167 | $MkTrack{'ServoController'} = $ServoController; |
||
168 | |||
169 | # Pan, Tilt in degrees for servo test |
||
170 | @ServoTest = ( [ 90, 0 ], |
||
171 | [ 180, 0 ], |
||
172 | [ 180, 90 ], |
||
173 | [ 90, 0 ], |
||
174 | [ 0, 0 ], |
||
175 | [ 0, 90 ], |
||
176 | [ 90, 0 ], |
||
177 | [ 90, 180 ], |
||
178 | [ 90, 0 ], ); |
||
179 | |||
180 | # |
||
181 | # Timer |
||
182 | # |
||
183 | |||
184 | sub SetTimer_ms() |
||
185 | { |
||
186 | return $SysTimerCount_ms + $_[0]; |
||
187 | } |
||
188 | |||
189 | sub CheckTimer_ms() |
||
190 | { |
||
191 | my $Diff = $_[0] - $SysTimerCount_ms; |
||
192 | return ($Diff <= 0); |
||
193 | } |
||
194 | |||
195 | # |
||
196 | # Servo |
||
197 | # |
||
198 | |||
199 | sub ServoInit() |
||
200 | { |
||
201 | # open COM-Port |
||
202 | undef $ServoPort; |
||
203 | |||
204 | if ( $ComPort =~ /^COM/i ) |
||
205 | { |
||
206 | $ComPort = "\\\\.\\" . $ComPort; # for Port > 9 required |
||
207 | } |
||
208 | |||
209 | if ( $^O =~ m/Win32/ ) |
||
210 | { |
||
211 | $ServoPort = Win32::SerialPort->new ($ComPort) || die "Error open $ComPort\n"; |
||
212 | } |
||
213 | else |
||
214 | { |
||
215 | $ServoPort = Device::SerialPort->new ($ComPort) || die "Error open $ComPort\n"; |
||
216 | } |
||
217 | |||
218 | if ( ! ($Cfg->{'track'}->{'PortSetSkip'} =~ /y/i) ) |
||
219 | { |
||
220 | # Set COM parameters, don't set for Bluetooth device |
||
221 | $ServoPort->baudrate(38400); |
||
222 | $ServoPort->parity("none"); |
||
223 | $ServoPort->databits(8); |
||
224 | $ServoPort->stopbits(1); |
||
225 | $ServoPort->handshake('none'); |
||
226 | $ServoPort->write_settings; |
||
227 | } |
||
228 | |||
229 | if ( $ServoController =~ /Pololu Micro Serial/i ) |
||
230 | { |
||
231 | # Byte 1: sync - Pololu Mode |
||
232 | # Byte 2: device |
||
233 | # Byte 3: command |
||
234 | # Byte 4: Servo num |
||
235 | |||
236 | # Byte 5: Set Speed 0, 1..127, 0=full speed |
||
237 | # Speed Pan/Tilt servo #0/#1 |
||
238 | my $Output = pack('C*', 0x80, 0x01, 0x01, $ServoPan, $ServoPar[$ServoPan]{'Speed'} ); |
||
239 | $ServoPort->write($Output); |
||
240 | my $Output = pack('C*', 0x80, 0x01, 0x01, $ServoTilt, $ServoPar[$ServoTilt]{'Speed'} ); |
||
241 | $ServoPort->write($Output); |
||
242 | |||
243 | # Acceleration not supported |
||
244 | } |
||
245 | |||
246 | elsif ( $ServoController =~ /Pololu Maestro/i ) |
||
247 | { |
||
248 | # Pololu compact protocoll |
||
249 | # Byte 1: Command |
||
250 | # Byte 2: device |
||
251 | # Byte 3: low bits 0..6 |
||
252 | # Byte 4: high bits 7..14 |
||
253 | |||
254 | # Speed |
||
255 | my $Speed = $ServoPar[$ServoPan]{'Speed'}; |
||
256 | my $Output = pack('C*', 0x87, $ServoPan, $Speed & 0x7f, ($Speed >> 7) & 0x7f ); |
||
257 | $ServoPort->write($Output); |
||
258 | |||
259 | my $Speed = $ServoPar[$ServoTilt]{'Speed'}; |
||
260 | my $Output = pack('C*', 0x87, $ServoTilt, $Speed & 0x7f, ($Speed >> 7) & 0x7f ); |
||
261 | $ServoPort->write($Output); |
||
262 | |||
263 | # Acceleration |
||
264 | my $Accel = $ServoPar[$ServoPan]{'Accel'}; |
||
265 | my $Output = pack('C*', 0x89, $ServoPan, $Accel & 0x7f, ($Accel >> 7) & 0x7f ); |
||
266 | $ServoPort->write($Output); |
||
267 | |||
268 | my $Accel = $ServoPar[$ServoTilt]{'Accel'}; |
||
269 | my $Output = pack('C*', 0x89, $ServoTilt, $Accel & 0x7f, ($Accel >> 7) & 0x7f ); |
||
270 | $ServoPort->write($Output); |
||
271 | } |
||
272 | |||
273 | @ServoStartTime = (0, 0, 0, 0, 0, 0, 0, 0); # Timestamp of last ServoMove() call |
||
274 | @ServoEndTime = (0, 0, 0, 0, 0, 0, 0, 0); # Timestamp of estimated arrival at end position |
||
275 | @ServoPos = (90, 90, 90, 90, 90, 90, 90, 90); # Current servo position 0..180 degree |
||
276 | } |
||
277 | |||
278 | |||
279 | sub ServoMove() |
||
280 | { |
||
281 | my ($Num, $Angel, $Time) = @_; |
||
282 | |||
283 | my $Overhead = 0; |
||
284 | |||
285 | if ( $Angel != $ServoPos[$Num] ) |
||
286 | { |
||
287 | if ( $Angel < 0) {$Angel = 0;} |
||
288 | if ( $Angel > 180) {$Angel = 180;} |
||
289 | |||
290 | my $Pos = $Angel - 90; # -90..0..90 |
||
291 | |||
292 | my $Min = $ServoPar[$Num]{'Min'}; |
||
293 | my $Mid = $ServoPar[$Num]{'Mid'}; |
||
294 | my $Max = $ServoPar[$Num]{'Max'}; |
||
295 | |||
296 | if ( $Pos >= 0 ) |
||
297 | { |
||
298 | $Pos = int ($Mid + $Pos / 90 * ($Max - $Mid) + 0.5); |
||
299 | } |
||
300 | else |
||
301 | { |
||
302 | $Pos = int ($Mid + $Pos / 90 * ($Mid - $Min) + 0.5); |
||
303 | } |
||
304 | |||
305 | if ( $ServoController =~ /Pololu Micro Serial/i ) |
||
306 | { |
||
307 | # output to COM port |
||
308 | # Byte 1: sync - Pololu Mode |
||
309 | # Byte 2: device |
||
310 | # Byte 3: command |
||
311 | # Byte 4: Servo num |
||
312 | # Byte 5: bits 7..14 in 0.5 us |
||
313 | # Byte 6: bits 0..6 in 0.5 us |
||
314 | |||
315 | $Pos *= 2; # 0.5 us resolution |
||
316 | |||
317 | my $Output = pack('C*', 0x80, 0x01, 0x04, $Num, ($Pos >> 7) & 0x7f, $Pos & 0x7f ); |
||
318 | $ServoPort->write($Output); |
||
319 | } |
||
320 | |||
321 | elsif ( $ServoController =~ /Pololu Maestro/i ) |
||
322 | { |
||
323 | # Pololu compact protocoll |
||
324 | # Byte 1: Command |
||
325 | # Byte 2: device |
||
326 | # Byte 3: low bits 0..6 in 0.25 us |
||
327 | # Byte 4: high bits 7..14 in 0.25 us |
||
328 | |||
329 | $Pos *= 4; # 0.25 us resolution |
||
330 | |||
331 | my $Output = pack('C*', 0x84, $Num, $Pos & 0x7f, ($Pos >> 7) & 0x7f ); |
||
332 | $ServoPort->write($Output); |
||
333 | } |
||
334 | |||
335 | $Overhead += $ServoConstPpm; # PPM protocol overhead |
||
336 | } |
||
337 | |||
338 | # set timer stuff for travel time predicion |
||
339 | my $LastAngel = $ServoPos[$Num]; |
||
340 | my $EstimatedTime = abs($Angel - $LastAngel) * $ServoSpeed[$Num] + $Overhead; |
||
341 | if ( $Time > 0 ) |
||
342 | { |
||
343 | # Parameter override |
||
344 | $EstimatedTime = $Time; |
||
345 | } |
||
346 | $ServoStartTime[$Num] = $SysTimerCount_ms; |
||
347 | $ServoEndTime[$Num] = $SysTimerCount_ms + $EstimatedTime; |
||
348 | $ServoPos[$Num] = $Angel; |
||
349 | |||
350 | return $ServoEndTime[$Num]; |
||
351 | } |
||
352 | |||
353 | |||
354 | # switch off servo |
||
355 | sub ServoRelax() |
||
356 | { |
||
357 | if ( defined $ServoPort ) |
||
358 | { |
||
359 | if ( $ServoController =~ /Pololu Micro Serial/i ) |
||
360 | { |
||
361 | |||
362 | # Byte 1: sync - Pololu Mode |
||
363 | # Byte 2: device |
||
364 | # Byte 3: command |
||
365 | # Byte 4: Servo num |
||
366 | |||
367 | # Byte 5: Set Parameter |
||
368 | # Bit 6: Servo on/off |
||
369 | # Bit 5: Direction |
||
370 | # Bit 4-0: Servo Range |
||
371 | # Set Pan/Tilt servo #0/#1 |
||
372 | my $Output = pack('C*', 0x80, 0x01, 0x00, $ServoPan, 0x00 | 15 ); |
||
373 | $ServoPort->write($Output); |
||
374 | my $Output = pack('C*', 0x80, 0x01, 0x00, $ServoTilt, 0x00 | 15 ); |
||
375 | $ServoPort->write($Output); |
||
376 | } |
||
377 | |||
378 | elsif ( $ServoController =~ /Pololu Maestro/i ) |
||
379 | { |
||
380 | # not supported |
||
381 | } |
||
382 | } |
||
383 | } |
||
384 | |||
385 | |||
386 | # Check, if servo has reached end position |
||
387 | sub ServoCheck() |
||
388 | { |
||
389 | my $Num = $_[0]; |
||
390 | return &CheckTimer_ms($ServoEndTime[$Num]); |
||
391 | } |
||
392 | |||
393 | |||
394 | sub ServoClose() |
||
395 | { |
||
396 | # close COM-Port |
||
397 | undef $ServoPort; |
||
398 | } |
||
399 | |||
400 | # |
||
401 | # Track it |
||
402 | # |
||
403 | |||
404 | sub TrackAntennaGps() |
||
405 | { |
||
406 | |||
407 | # initialize system-timer |
||
408 | $SysTimerCount_ms = 0; |
||
409 | $SysTimerError = 0; |
||
410 | ($t0_s, $t0_us) = gettimeofday; |
||
411 | |||
412 | # |
||
413 | # State maschine |
||
414 | # |
||
415 | |||
416 | my $State = "Idle"; |
||
417 | while (1) |
||
418 | { |
||
419 | |||
420 | $MkTrack{'State'} = $State; # export state |
||
421 | |||
422 | # |
||
423 | # Idle |
||
424 | # |
||
425 | if ( $State eq "Idle" ) |
||
426 | { |
||
427 | # nothing to do. Wait for commands in TrackQueue |
||
428 | } |
||
429 | |||
430 | # |
||
431 | # ColdStart |
||
432 | # |
||
433 | elsif ( $State eq "ColdStart" ) |
||
434 | { |
||
435 | &ServoInit(); |
||
436 | |||
437 | |||
438 | $ServoTestIndex = 0; |
||
439 | |||
440 | $State = "WaitGps"; |
||
441 | } |
||
442 | |||
443 | # |
||
444 | # Wait for GPS Home position and compass |
||
445 | # |
||
446 | elsif ( $State eq "WaitGps" ) |
||
447 | { |
||
448 | lock (%MkOsd); # until end of block |
||
449 | lock (%MkTrack); |
||
450 | |||
451 | if ( $MkOsd{'_Timestamp'} >= time-2 and |
||
452 | $MkOsd{'SatsInUse'} >= 6 ) |
||
453 | { |
||
454 | # gültige OSD daten vom MK und guter Satellitenempfang |
||
455 | |||
456 | # take GPS and compass from map definition or MK as antenna home-position |
||
457 | $MkTrack{'HomePos_Lon'} = $Map{'Track_Lon'} || $MkOsd{'HomePos_Lon'}; |
||
458 | $MkTrack{'HomePos_Lat'} = $Map{'Track_Lat'} || $MkOsd{'HomePos_Lat'}; |
||
459 | $MkTrack{'HomePos_Alt'} = $Map{'Track_Alt'} || $MkOsd{'HomePos_Alt'}; |
||
460 | $MkTrack{'CompassHeading'} = $Map{'Track_Bearing'} || $MkOsd{'CompassHeading'}; |
||
461 | |||
462 | $TrackTimer = &SetTimer_ms($TrackInterval); |
||
463 | |||
464 | $State = "InitServoTest"; |
||
465 | } |
||
466 | } |
||
467 | |||
468 | # |
||
469 | # Start servo test |
||
470 | # doesn't really make much sense, but looks cool:-) |
||
471 | # |
||
472 | |||
473 | elsif ( $State eq "InitServoTest") |
||
474 | { |
||
475 | if ( &ServoCheck ($ServoPan) and &ServoCheck ($ServoTilt) ) |
||
476 | { |
||
477 | |||
478 | my $PanPos = $ServoTest[$ServoTestIndex][0]; |
||
479 | my $TiltPos = $ServoTest[$ServoTestIndex][1]; |
||
480 | $ServoTestIndex ++; |
||
481 | |||
482 | if ( defined $PanPos and defined $TiltPos ) |
||
483 | { |
||
484 | my $Delay = 200; |
||
485 | my $LastPan = $ServoPos[$ServoPan]; |
||
486 | my $LastTilt = $ServoPos[$ServoTilt]; |
||
487 | |||
488 | my $ServoPanSpeed = $ServoPar[$ServoPan]{'Speed'}; |
||
489 | my $ServoTiltSpeed = $ServoPar[$ServoTilt]{'Speed'}; |
||
490 | |||
491 | if ( $ServoController =~ /Pololu Micro Serial/i ) |
||
492 | { |
||
493 | # 50 us/s |
||
494 | } |
||
495 | elsif ( $ServoController =~ /Pololu Maestro/i ) |
||
496 | { |
||
497 | # 25 us/s |
||
498 | $ServoPanSpeed /= 2; |
||
499 | $ServoTiltSpeed /= 2; |
||
500 | } |
||
501 | |||
502 | my $EstimatedPan = 1000; |
||
503 | if ( $ServoPanSpeed != 0 ) |
||
504 | { |
||
505 | # about 2ms/180degree pulse, 50us/s servo pulse change per unit |
||
506 | $EstimatedPan = abs($PanPos - $LastPan) * 0.002/180 / ($ServoPanSpeed * 0.000050) * 1000 + $Delay; |
||
507 | } |
||
508 | |||
509 | my $EstimatedTilt = 1000; |
||
510 | if ( $ServoTiltSpeed != 0 ) |
||
511 | { |
||
512 | # about 2ms/180degree pulse, 50us/s servo pulse change per unit |
||
513 | $EstimatedTilt = abs($TiltPos - $LastTilt) * 0.002/180 / ($ServoTiltSpeed * 0.000050) * 1000 + $Delay; |
||
514 | } |
||
515 | |||
516 | &ServoMove ($ServoPan, $PanPos, $EstimatedPan); # override travel time |
||
517 | &ServoMove ($ServoTilt, $TiltPos, $EstimatedTilt); # override travel time |
||
518 | } |
||
519 | else |
||
520 | { |
||
521 | # complete |
||
522 | $ServoTestIndex = 0; |
||
523 | $State = "TrackGps"; |
||
524 | } |
||
525 | } |
||
526 | } |
||
527 | |||
528 | # |
||
529 | # Servo test finisched |
||
530 | |||
531 | # |
||
532 | # GPS Fix Home position |
||
533 | # Track now |
||
534 | # |
||
535 | elsif ( $State eq "TrackGps" ) |
||
536 | { |
||
537 | if ( &CheckTimer_ms($TrackTimer) and &ServoCheck($ServoPan) ) |
||
538 | { |
||
539 | $TrackTimer = &SetTimer_ms($TrackInterval); # reload Timer |
||
540 | |||
541 | lock (%MkOsd); # until end of block |
||
542 | lock (%MkTrack); |
||
543 | |||
544 | if ( $MkOsd{'_Timestamp'} >= time -2 and |
||
545 | $MkOsd{'SatsInUse'} >= 4 ) |
||
546 | { |
||
547 | # valid OSD data from the MK and sufficient satellites |
||
548 | |||
549 | my $Track_Geo = Geo::Ellipsoid->new( 'units' => 'degrees', |
||
550 | 'distance_units' => 'meter', |
||
551 | 'ellipsoid' => 'WGS84', |
||
552 | 'longitude' => 1, # Symmetric: -pi..pi |
||
553 | ); |
||
554 | |||
555 | my ($Dist, $Bearing) = $Track_Geo->to($MkTrack{'HomePos_Lat'}, $MkTrack{'HomePos_Lon'}, |
||
556 | $MkOsd{'CurPos_Lat'}, $MkOsd{'CurPos_Lon'}); |
||
557 | my $Dir_h = $MkTrack{'CompassHeading'}; |
||
558 | my $Dir_c = $MkOsd{'CompassHeading'}; |
||
559 | |||
560 | if ( $Dist < 4 ) # meter |
||
561 | { |
||
562 | # Too close to Home-Position. Set antenna to middle position |
||
563 | $Bearing = $Dir_h; |
||
564 | } |
||
565 | |||
566 | $MkTrack{'Bearing'} = sprintf ("%d", $Bearing); |
||
567 | $MkTrack{'Dist'} = sprintf ("%d", $Dist); |
||
568 | $MkTrack{'CurPos_Lon'} = $MkOsd{'CurPos_Lon'}; |
||
569 | $MkTrack{'CurPos_Lat'} = $MkOsd{'CurPos_Lat'}; |
||
570 | $MkTrack{'CurPos_Alt'} = $MkOsd{'CurPos_Alt'}; |
||
571 | |||
572 | # antenna pan direction: 0..180 degree, centre = 90 |
||
573 | my $AngelPan = $Bearing - $Dir_h + 90; |
||
574 | $AngelPan = $AngelPan % 360; |
||
575 | |||
576 | # antenna tilt direction: 0..180 degree, centre is up, 0 is front |
||
577 | my $AngelTilt = rad2deg(atan2(($MkOsd{'CurPos_Alt'} - $MkTrack{'HomePos_Alt'}), $Dist)); |
||
578 | if ( $AngelTilt < 0 ) { $AngelTilt = 0; } |
||
579 | if ( $AngelTilt > 180 ) { $AngelTilt = 180; } |
||
580 | |||
581 | if ( $AngelPan >= 180 ) |
||
582 | { |
||
583 | # Flip Pan/Tilt |
||
584 | $AngelPan = $AngelPan - 180; |
||
585 | $AngelTilt = 180 - $AngelTilt; |
||
586 | } |
||
587 | |||
588 | $MkTrack{'AngelPan'} = $AngelPan; |
||
589 | $MkTrack{'AngelTilt'} = $AngelTilt; |
||
590 | |||
591 | &ServoMove ($ServoPan, $AngelPan); |
||
592 | &ServoMove ($ServoTilt, $AngelTilt); |
||
593 | |||
594 | # Timestamp, wann der Datensatz geschtieben wurde |
||
595 | $MkTrack{'_Timestamp'} = time; |
||
596 | } |
||
597 | } |
||
598 | } |
||
599 | |||
600 | else |
||
601 | { |
||
602 | # Restart |
||
603 | $State = "ColdStart"; |
||
604 | } |
||
605 | |||
606 | # |
||
607 | # check command queue |
||
608 | # |
||
609 | while ( $TrackQueue->pending() > 0 ) |
||
610 | { |
||
611 | my $Cmd = $TrackQueue->dequeue(1); |
||
612 | |||
613 | if ( $Cmd =~ /COLDSTART/i ) |
||
614 | { |
||
615 | $State = "ColdStart"; |
||
616 | } |
||
617 | |||
618 | if ( $Cmd =~ /IDLE/i ) |
||
619 | { |
||
620 | if ( defined $ServoPort ) |
||
621 | { |
||
622 | # move all Servo to neutral position |
||
623 | &ServoMove ($ServoPan, 90); |
||
624 | &ServoMove ($ServoTilt, 0); |
||
625 | |||
626 | sleep 1; |
||
627 | &ServoRelax(); # swith off servo |
||
628 | } |
||
629 | |||
630 | $State = "Idle"; |
||
631 | } |
||
632 | } |
||
633 | |||
634 | # |
||
635 | # update system-timer |
||
636 | # |
||
637 | ($t1_s, $t1_us) = gettimeofday; |
||
638 | $SysTimerSleep_us = ($t0_s - $t1_s) * 1000000 + $t0_us - $t1_us + $SysTimerCount_ms * $SysTimerResolution; |
||
639 | |||
640 | if ($SysTimerSleep_us > 0) |
||
641 | { |
||
642 | usleep ($SysTimerSleep_us); |
||
643 | } |
||
644 | else |
||
645 | { |
||
646 | $SysTimerError ++; |
||
647 | } |
||
648 | |||
649 | $SysTimerCount_ms ++; |
||
650 | } |
||
651 | } |
||
652 | |||
653 | |||
654 | # |
||
655 | # Main Program |
||
656 | # |
||
657 | |||
658 | if ( $0 =~ /track.pl$/i ) |
||
659 | { |
||
660 | # Program wurde direkt aufgerufen |
||
661 | |||
662 | # |
||
663 | # Commandline Parameter |
||
664 | # |
||
665 | my $TrackPort = $CmdLine{'-TrackPort'}; |
||
666 | if ( $TrackPort ne "" ) |
||
667 | { |
||
668 | $ComPort = $TrackPort; |
||
669 | } |
||
670 | |||
671 | my $MkPort = $CmdLine{'-MkPort'}; |
||
672 | if ( $MkPort ne "" ) |
||
673 | { |
||
674 | $Cfg->{'mkcomm'}->{'Port'} = $MkPort; |
||
675 | } |
||
676 | |||
677 | my $Pan = $CmdLine{'-ServoPan'}; |
||
678 | if ( $Pan ne "" ) |
||
679 | { |
||
680 | my ($Min, $Mid, $Max) = split ',', $Pan; |
||
681 | $ServoPar[$ServoPan]{'Min'} = $Min; |
||
682 | $ServoPar[$ServoPan]{'Mid'} = $Mid; |
||
683 | $ServoPar[$ServoPan]{'Max'} = $Max; |
||
684 | } |
||
685 | |||
686 | my $Tilt = $CmdLine{'-ServoTilt'}; |
||
687 | if ( $Tilt ne "" ) |
||
688 | { |
||
689 | my ($Min, $Mid, $Max) = split ',', $Tilt; |
||
690 | $ServoPar[$ServoTilt]{'Min'} = $Min; |
||
691 | $ServoPar[$ServoTilt]{'Mid'} = $Mid; |
||
692 | $ServoPar[$ServoTilt]{'Max'} = $Max; |
||
693 | } |
||
694 | |||
695 | my $PanSpeed = $CmdLine{'-PanSpeed'}; |
||
696 | if ( $PanSpeed ne "" ) |
||
697 | { |
||
698 | $ServoPar[$ServoPan]{'Speed'} = $PanSpeed; |
||
699 | } |
||
700 | |||
701 | my $TiltSpeed = $CmdLine{'-TiltSpeed'}; |
||
702 | if ( $TiltSpeed ne "" ) |
||
703 | { |
||
704 | $ServoPar[$ServoTilt]{'Speed'} = $TiltSpeed; |
||
705 | } |
||
706 | |||
707 | # Kommunikation zum MK herstellen |
||
708 | # Input: %MkOsd, %MkTarget, %MkNcDebug |
||
709 | # Ouput: Thread-Queue: $MkSendQueue |
||
710 | $mk_thr = threads->create (\&MkCommLoop) -> detach(); |
||
711 | |||
712 | $TrackQueue->enqueue("COLDSTART"); # start command |
||
713 | &TrackAntennaGps(); |
||
714 | |||
715 | # should never exit |
||
716 | } |
||
717 | |||
718 | 1; |
||
719 | |||
720 | __END__ |