Instrument/PD11042.pm untested

filter: Statementstmt 0.0% 0 / 347 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 551 CC 43 SCAR 42.0
CC
43
Cov
0%
init_limits
156.0
move
132.0
read_motorinitdata
56.0
CRAP 66.7
1 package Lab::Instrument::PD11042;
2 $Lab::Instrument::PD11042::VERSION = '3.899';
3 #ABSTRACT: Trinamic PD-110-42 low-cost 42mm stepper motor
4  
5 use v5.20;
6  
7 use strict;
8 use Time::HiRes qw/usleep/, qw/time/;
9 use Lab::Instrument;
10 use Config;
11  
12 # todo:
13 # * test with VISA_RS232
14 # * integrate the limits etc into the config hash
15 # * hash call parameters
16 # * path for ini / log file???
17 # ...
18  
19 # Opcodes of all TMCL commands that can be used in direct mode
20 use constant {
21 TMCL_ROR => 1,
22 TMCL_ROL => 2,
23 TMCL_MST => 3,
24 TMCL_MVP => 4,
25 TMCL_SAP => 5,
26 TMCL_GAP => 6,
27 TMCL_STAP => 7,
28 TMCL_RSAP => 8,
29 TMCL_SGP => 9,
30 TMCL_GGP => 10,
31 TMCL_STGP => 11,
32 TMCL_RSGP => 12,
33 TMCL_RFS => 13,
34 TMCL_SIO => 14,
35 TMCL_GIO => 15,
36 TMCL_SCO => 30,
37 TMCL_GCO => 31,
38 TMCL_CCO => 32
39  
40 # Options for MVP commandds
41 ,
42 MVP_ABS => 0,
43 MVP_REL => 1,
44 MVP_COORD => 2
45  
46 # Options for RFS command
47 ,
48 RFS_START => 0,
49 RFS_STOP => 1,
50 RFS_STATUS => 2
51  
52 # Result codes for GetResult
53 ,
54 TMCL_RESULT_OK => 0,
55 TMCL_RESULT_NOT_READY => 1,
56 TMCL_RESULT_CHECKSUM_ERROR => 2
57  
58 # axis paramter for SAP and GAP
59 ,
60 target_position => 0,
61 actual_position => 1,
62 target_speed => 2,
63 actual_speed => 3,
64 maximum_positioning_speed => 4,
65 maximum_acceleration => 5,
66 absolut_max_current => 6,
67 standby_current => 7,
68 minimum_speed => 130,
69 ramp_mode => 138,
70 microstep_resolution => 140,
71 ramp_divisor => 153,
72 puls_divisor => 154,
73 freewheeling => 204,
74 stall_detection_threshold => 211,
75 power_down_dely => 214
76  
77 # global parameters
78 , user_defined => 2
79  
80 };
81  
82 our %limits;
83 our $RESOLUTION = 32;
84 our $GETRIEBEMULTIPLIKATOR = 43;
85  
86 our @ISA = ("Lab::Instrument");
87  
88 our %fields = (
89 supported_connections => [ 'VISA', 'VISA_RS232', 'RS232', 'DEBUG' ],
90 connection_settings => {
91 baudrate => 9600,
92 databits => 8,
93 stopbits => 1,
94 parity => 'none',
95 handshake => 'none',
96 termchar => '\r',
97 timeout => 2,
98 },
99  
100 device_settings => {
101 read_default => 'device',
102 pos_mode => 'ABS',
103 speed_max => 180,
104 upper_limit => 180,
105 lower_limit => -180,
106 inipath => $Config{sitelib} . "/Lab/Instrument/PD11042.ini",
107 logpath => $Config{sitelib} . "/Lab/Instrument/PD11042.log",
108  
109 },
110  
111 device_cache => {
112 position => undef,
113 target => undef,
114 },
115  
116 device_cache_order => ['id'],
117 );
118  
119 sub new {
120 my $proto = shift;
121 my $class = ref($proto) || $proto;
122 my $self = $class->SUPER::new(@_);
123 $self->${ \( __PACKAGE__ . '::_construct' ) }(__PACKAGE__);
124  
125 # my $status;
126 # termchar is imho disabled by default
127 # $status=Lab::VISA::viSetAttribute($self->{vi}->{config}->{RS232}->{vi}->{instr}, $Lab::VISA::VI_ATTR_TERMCHAR_EN, $Lab::VISA::VI_FALSE);
128 # if ($status != $Lab::VISA::VI_SUCCESS) { Lab::Exception::CorruptParameter->throw( error => "Error while setting termchar enabled: $status");}
129 # not sure what the echo setting by default is, let's test
130 # $self->{vi}->{config}->{RS232_Echo} = 'OFF';
131  
132 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, maximum_positioning_speed, 10);
133 # print $result."\n";
134 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, ramp_mode, 0);
135 # print $result."\n";
136 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, microstep_resolution, 5);
137 # print $result."\n";
138 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, absolut_max_current, 745);
139 # print $result."\n";
140 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, standby_current, 1400);
141 # print $result."\n";
142 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, stall_detection_threshold, 2040);
143 # print $result."\n";
144 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, power_down_dely, 0);
145 # print $result."\n";
146 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, ramp_divisor, 7);
147 # print $result."\n";
148 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, puls_divisor, 3);
149 # print $result."\n";
150 # my ($result, $errcode) = $self->exec_cmd(TMCL_GAP, freewheeling, 100);
151 # print $result."\n";
152 # exit;
153  
154 my ( $result, $errcode );
155  
156 # set initial motor parameters (reference point, speed, microstep resolution, etc.
157 # 1.) speed:
158 $limits{'maximum_positioning_speed'} = 120;
159 ( $result, $errcode ) = $self->exec_cmd( TMCL_SAP, target_speed, 0 );
160 ( $result, $errcode )
161 = $self->exec_cmd( TMCL_SAP, maximum_positioning_speed,
162 $self->steps2angle( $limits{'maximum_positioning_speed'} / 2.4 )
163 ); #HIER ANSETZEN!!!
164 ( $result, $errcode ) = $self->exec_cmd( TMCL_SAP, ramp_mode, 2 );
165  
166 # 2.) microstep resolution:
167 # 0 = full step (don't use),
168 # 1 = half step (don't use),
169 # 2 = 4 microsteps,
170 # 3 = 8 microsteps,
171 # 4 = 16 microsteps,
172 # 5 = 32 microsteps,
173 # 6 = 64 microsteps
174 $RESOLUTION = 32;
175 ( $result, $errcode )
176 = $self->exec_cmd( TMCL_SAP, microstep_resolution, 5 );
177  
178 # 3.) motor current settings:
179 ( $result, $errcode )
180 = $self->exec_cmd( TMCL_SAP, absolut_max_current, 745 ); # 469 mA
181 ( $result, $errcode )
182 = $self->exec_cmd( TMCL_SAP, standby_current, 1400 ); # 117 mA
183 ( $result, $errcode )
184 = $self->exec_cmd( TMCL_SAP, stall_detection_threshold, 2040 );
185 ( $result, $errcode ) = $self->exec_cmd( TMCL_SAP, power_down_dely, 0 );
186 ( $result, $errcode ) = $self->exec_cmd( TMCL_SAP, ramp_divisor, 7 );
187 ( $result, $errcode ) = $self->exec_cmd( TMCL_SAP, puls_divisor, 3 );
188 ( $result, $errcode ) = $self->exec_cmd( TMCL_SAP, freewheeling, 100 );
189  
190 # ($result, $errcode) = $self->exec_cmd(TMCL_SAP, actual_position, $self->angle2steps(-330));
191 # print $self->get_position()."\n";;
192 # exit;
193  
194 # 4.) restore position
195 #$self->exec_cmd(TMCL_RSGP, 0, 0, 2);
196 #my ($result, $errcode) = $self->exec_cmd(TMCL_GGP, 0, 0, 2);
197 #my ($result2, $errcode) = $self->exec_cmd(TMCL_SAP, target_position, $self->angle2steps(83.59));
198 #my ($result2, $errcode) = $self->exec_cmd(TMCL_SAP, actual_position, $self->angle2steps(83.59));
199 #exit;
200  
201 # 5.) set some reference point and limits for motor movements:
202 $self->init_limits();
203  
204 return $self;
205 }
206  
207 # Execute command: exec_cmd(cmd, type, value, [motor])
208 sub exec_cmd {
209 my $self = shift;
210  
211 my $addr = 1;
212 my $cmd = shift;
213 my $type = shift;
214 my $value = shift;
215 my $motor = shift;
216 if ( not defined $motor ) {
217 $motor = 0;
218 }
219  
220 if ( $value < 0 ) {
221 $value = 4244897281 + $value;
222 }
223  
224 my $v4 = int( $value / 255**3 );
225 $v4 = ( $v4 > 255 ) ? 255 : $v4;
226 $value -= ( 255**3 ) * $v4;
227 my $v3 = int( $value / 255**2 );
228 $v3 = ( $v3 > 255 ) ? 255 : $v3;
229 $value -= ( 255**2 ) * $v3;
230 my $v2 = int( $value / 255 );
231 $v2 = ( $v2 > 255 ) ? 255 : $v2;
232 $value -= 255 * $v2;
233  
234 my $checksum
235 = ( $addr + $cmd + $type + $motor + $v4 + $v3 + $v2 + $value ) % 256;
236  
237 my $query = pack(
238 "C9", $addr, $cmd, $type, $motor, $v4, $v3, $v2, $value,
239 $checksum
240 );
241 my $result = 0;
242 my $errcode = 0;
243 my $i = 0;
244  
245 while ( $errcode != 100 && $i < 10 ) {
246  
247 #print $addr."-".$cmd."-".$type."-".$motor."-".$v4."-".$v3."-".$v2."-".$value."-".$checksum." (".$i.")\n";
248 $self->write($query);
249 ( $result, $errcode ) = $self->get_reply();
250 $i++;
251 }
252  
253 return $result, $errcode;
254 }
255  
256 sub get_reply {
257 my $self = shift;
258 my @result;
259  
260 foreach ( 1 .. 9 ) {
261 my $a = $self->connection()->BrutalRead( read_length => 1 );
262 push( @result, unpack( "C", $a ) );
263 }
264  
265 my $value
266 = ( 255**3 ) * $result[4]
267 + ( 255**2 ) * $result[5]
268 + 255 * $result[6]
269 + $result[7];
270 if ( $value > 2122448640 ) {
271 $value = -( 4244897281 - $value );
272 }
273  
274 return $value, $result[2];
275 }
276  
277 sub move {
278 my $self = shift;
279  
280 # Parameter, old style: ABS/REL, angle, speed
281  
282 my ( $position, $speed, $mode )
283 = $self->_check_args( \@_, [ 'position', 'speed', 'mode' ] );
284  
285 # TODO:
286 # if (ref($mode) eq 'HASH') ...
287 # 'mode', 'angle', 'speed'
288  
289 if ( not defined $mode ) {
290 $mode = $self->device_settings()->{pos_mode};
291 }
292 if ( not $mode =~ /ABS|abs|REL|rel/ ) {
293 Lab::Exception::CorruptParameter->throw( error =>
294 "unexpected value for <MODE> in sub move. expected values are ABS and REL."
295 );
296 }
297 if ( not defined $speed ) {
298 $speed = $self->device_settings()->{speed_max};
299 }
300 if ( not defined $position ) {
301 Lab::Exception::CorruptParameter->throw(
302 error => $self->get_id() . ": No target given in sub move! " );
303 }
304 elsif (
305 not $position =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ )
306 {
307 Lab::Exception::CorruptParameter->throw( error => $self->get_id()
308 . ": Illegal Value given for POSITION in sub move!" );
309 }
310  
311 # this sets the upper limit for the positioning speed:
312 $speed = abs($speed);
313 if ( $speed > $self->device_settings()->{speed_max} ) {
314 print new Lab::Exception::CorruptParameter( error =>
315 "Warning in sub move: <SPEED> = $speed is too high. Reduce <SPEED> to its maximum value defined by internal limit settings of "
316 . $self->device_settings()->{speed_max} );
317 $speed = $self->device_settings()->{speed_max};
318 }
319  
320 $speed = $speed / 2.4;
321 my ( $result, $errcode )
322 = $self->exec_cmd( TMCL_SAP, maximum_positioning_speed, $speed );
323  
324 # Moving in ABS or REL mode:
325 my $CP = $self->get_position();
326 if ( $mode eq "ABS"
327 or $mode eq "abs"
328 or $mode eq "ABSOLUTE"
329 or $mode eq "absolute" ) {
330 if ( $position < $self->device_settings()->{lower_limit}
331 or $position > $self->device_settings()->{upper_limit} ) {
332 Lab::Exception::CorruptParameter->throw( error =>
333 "unexpected value for NEW POSITION in sub move. Expected values are between $limits{'LOWER'} ... $limits{'UPPER'}"
334 );
335 }
336 $self->device_cache()->{target} = $position;
337 $self->_save_motorlog( $CP, $position );
338 $self->save_motorinitdata();
339 $self->exec_cmd( TMCL_MVP, MVP_ABS, $self->angle2steps($position) );
340 }
341 elsif ($mode eq "REL"
342 or $mode eq "rel"
343 or $mode eq "RELATIVE"
344 or $mode eq "relative" ) {
345 if ( $CP + $position < $self->device_settings()->{lower_limit}
346 or $CP + $position > $self->device_settings()->{upper_limit} ) {
347 Lab::Exception::CorruptParameter->throw( error =>
348 "ERROR in sub move.Can't execute move; TARGET POSITION ("
349 . ( $CP + $position )
350 . ") is out of valid limits ("
351 . $limits{'LOWER'} . " ... "
352 . $limits{'UPPER'}
353 . ")" );
354 }
355  
356 $self->device_cache()->{target} = $CP + $position;
357 $self->_save_motorlog( $CP, $CP + $position );
358 $self->save_motorinitdata();
359  
360 $self->exec_cmd( TMCL_MVP, MVP_REL, $self->angle2steps($position) );
361 }
362 return 1;
363 }
364  
365 sub active {
366 my $self = shift;
367  
368 my ( $result, $errcode ) = $self->exec_cmd( TMCL_GAP, actual_speed, 0 );
369 $self->get_position();
370 return abs($result);
371  
372 }
373  
374 sub wait {
375 my $self = shift;
376 my $flag = 1;
377 local $| = 1;
378  
379 while ( $self->active() ) {
380 if ( $flag <= 1.1 and $flag >= 0.9 ) {
381 printf(
382 "%s is sweeping (%06.2f)\r",
383 $self->get_id(), $self->device_cache()->{position}
384 );
385 }
386 elsif ( $flag <= 0 ) {
387 printf(
388 "%s is (%06.2f)\r",
389 $self->get_id(), $self->device_cache()->{position}
390 );
391 $flag = 2;
392 }
393 $flag -= 0.5;
394 usleep(5e3);
395 }
396  
397 print "\t\t\t\t\t\t\t\t\t\r";
398 $| = 0;
399  
400 return 1;
401  
402 }
403  
404 sub abort {
405 my $self = shift;
406 $self->exec_cmd( TMCL_MST, 0, 0 );
407 print "Motor stoped at " . $self->get_position() . "\n";
408 return;
409 }
410  
411 sub init_limits {
412 my $self = shift;
413 my $lowerlimit;
414 my $upperlimit;
415  
416 if ( $self->read_motorinitdata() ) {
417 while (1) {
418 print
419 "Motor-Init data found. Do you want to keep the reference point and the limits? (y/n) ";
420 my $input = <>;
421 chomp $input;
422 if ( $input =~ /YES|yes|Y|y/ ) {
423 return 1;
424 }
425 elsif ( $input =~ /NO|no|N|n/ ) {
426 my ( $result, $errcode )
427 = $self->exec_cmd( TMCL_MST, 0, 0 )
428 ; # Motor stop, to prevent unexpected motor activity
429 ( $result, $errcode )
430 = $self->exec_cmd( TMCL_SAP, target_position, 0 );
431 ( $result, $errcode )
432 = $self->exec_cmd( TMCL_SAP, actual_position, 0 );
433 $self->device_settings->{lower_limit} = -360;
434 $self->device_settings->{upper_limit} = 360;
435 last;
436 }
437 }
438 }
439  
440 print "\n\n";
441 print "----------------------------------------------\n";
442 print "----------- Init Motor PDx-110-42 ------------\n";
443 print "----------------------------------------------\n";
444 print "\n";
445 print
446 "This procedure will help you to initialize the Motor PDx-110-42 correctly.\n\n";
447 print "Steps to go:\n";
448 print " 1.) Define the REFERENCE POINT.\n";
449 print " 2.) Define the LOWER and UPPER LIMITS for rotation.\n";
450 print " 3.) Confirm the LOWER and UPPER LIMITS.\n";
451 print "\n\n";
452  
453 print "----------------------------\n";
454 print "1.) Define the REFERENCE POINT:\n\n";
455 print "--> Move the motor position to the REFERENCE POINT.\n";
456 print "--> Enter an angle between -180 ... +180 deg.\n";
457 print
458 "--> Repeat until you have reached the position you want to define as the REFERENCE POINT.\n";
459 print
460 "--> Enter 'REF' to confirm the actual position as the REFERENCE POINT.\n\n";
461  
462 while (1) {
463 print "MOVE: ";
464 my $value = <STDIN>;
465 chomp $value;
466 if ( $value eq "REF" or $value eq "ref" ) {
467  
468 # set actual position as reference point Zero
469 $self->device_cache()->{position} = 0; # for testing only
470 my ( $result, $errcode )
471 = $self->exec_cmd( TMCL_MST, 0, 0 )
472 ; # Motor stop, to prevent unexpected motor activity
473 ( $result, $errcode )
474 = $self->exec_cmd( TMCL_SAP, target_position, 0 );
475 ( $result, $errcode )
476 = $self->exec_cmd( TMCL_SAP, actual_position, 0 );
477 last;
478 }
479 elsif ( $value =~ /^[+-]?\d+$/ and $value >= -180 and $value <= 180 )
480 {
481 $self->move( $value, { mode => 'REL' } );
482 $self->wait();
483 }
484 else {
485 print
486 "Please move the motor position to the REFERENCE POINT. Enter an angle between -188° ... +180°.\n";
487 }
488 }
489  
490 print "----------------------------\n";
491 print "2.) Define the LOWER and UPPER LIMITS for rotation:\n\n";
492 print "--> Enter LOWER LIMIT\n";
493 print "--> Enter UPPER LIMIT\n\n";
494  
495 while (1) {
496 print "LOWER LIMIT: ";
497 my $value = <STDIN>;
498 chomp $value;
499 $lowerlimit = $value;
500 $self->device_settings()->{lower_limit} = $lowerlimit;
501 print "UPPER LIMIT: ";
502 $value = <STDIN>;
503 chomp $value;
504 $upperlimit = $value;
505 $self->device_settings()->{upper_limit} = $upperlimit;
506  
507 if ( $lowerlimit < $upperlimit ) {
508 last;
509 }
510 else {
511 print "LOWER LIMIT >= UPPER LIMIT. Try again!\n";
512 }
513 }
514  
515 print "----------------------------\n";
516 print "3.) Confirm the LOWER and UPPER LIMITS:\n\n";
517 print "--> Motor will move to LOWER LIMIT in steps of 10 deg\n";
518 print "--> Motor will move to UPPER LIMIT in steps of 10 deg\n";
519 print
520 "--> Confirm each step with ENTER or type <STOP> to take the actual position as the limit value. \n\n";
521  
522 print "Moving to LOWER LIMIT ...\n";
523 while (1) {
524 print "MOVE +/-10: Please press <ENTER> to confirm.";
525 my $input = <STDIN>;
526 chomp $input;
527 if ( $input =~ /stop|STOP/ ) {
528 $lowerlimit = $self->get_position();
529 last;
530 }
531 if ( abs( $self->get_position() - $lowerlimit ) >= 10 ) {
532 if ( $lowerlimit <= 0 ) {
533 $self->move( -10, { mode => 'REL' } );
534 $self->wait();
535  
536 }
537 else {
538 $self->move( 10, { mode => 'REL' } );
539 $self->wait();
540 }
541 }
542 else {
543 $self->move( $lowerlimit, { mode => 'ABS' } );
544 $self->wait();
545 last;
546 }
547  
548 }
549 print "Reached LOWER LIMIT\n";
550 print "Please confirm the position of the LOWER LIMIT: ";
551 <STDIN>;
552 $self->device_settings()->{'lower_limit'} = $lowerlimit;
553 print "\n\n";
554 print "Moving to REFERENCE POINT ... \n";
555 print $self->move( 0, { mode => 'ABS' } ) . "\n";
556 $self->wait();
557 print "Moving to UPPER LIMIT ...\n";
558  
559 while (1) {
560 print "MOVE +/-10: Please press <ENTER> to confirm.";
561 my $input = <STDIN>;
562 chomp $input;
563 if ( $input =~ /stop|STOP/ ) {
564 $upperlimit = $self->get_position();
565 last;
566 }
567 if ( abs( $upperlimit - $self->get_position() ) >= 10 ) {
568 $self->move( 10, { mode => 'REL' } );
569 $self->wait();
570 }
571 else {
572 $self->move( $upperlimit, { mode => 'ABS' } );
573 $self->wait();
574  
575 last;
576 }
577  
578 }
579 print "Reached UPPER LIMIT\n";
580 print "Please confirm the position of the UPPER LIMIT: ";
581 <STDIN>;
582 $self->device_settings()->{'upper_limit'} = $upperlimit;
583 print "\n\n";
584 $self->save_motorinitdata();
585  
586 print "moving to the reference point.\n";
587 $self->move( 0, { mode => 'ABS' } );
588  
589 $self->wait();
590 print "------------------------------------------------------\n";
591 print "------------ Motor PDx-110-42 initialized ------------\n";
592 print "------------------------------------------------------\n";
593 print "\n\n";
594  
595 }
596  
597 sub _set_REF {
598 my $self = shift;
599 my $new_ref = shift;
600  
601 my $old_ref = $self->get_position();
602  
603 open( my $handle, ">>", $self->device_settings()->{logpath} )
604 or print "cant open logfile\n";
605  
606 print "Set actual position from $old_ref to $new_ref\n";
607 my ( $result, $errcode ) = $self->exec_cmd( TMCL_SAP, target_position,
608 $self->angle2steps($new_ref)
609 );
610 ( $result, $errcode ) = $self->exec_cmd( TMCL_SAP, actual_position,
611 $self->angle2steps($new_ref)
612 );
613 print {$handle} ( my_timestamp() )
614 . "\t set new REF: $old_ref -> $new_ref \n";
615 close($handle);
616  
617 # this writes the new position to the ini-file.
618 $self->get_position();
619  
620 return;
621 }
622  
623 sub steps2angle {
624 my $self = shift;
625 my $steps = shift;
626 my $angle = $steps / ( 200 * $RESOLUTION / 360 ) / $GETRIEBEMULTIPLIKATOR;
627 return $angle;
628 }
629  
630 sub angle2steps {
631 my $self = shift;
632 my $angle = shift;
633 my $steps = $angle * ( 200 * $RESOLUTION / 360 ) * $GETRIEBEMULTIPLIKATOR;
634 return sprintf( "%0.f", $steps );
635  
636 }
637  
638 sub get_value {
639 my $self = shift;
640 my ( $read_mode, $tail ) = $self->_check_args( \@_, ['read_mode'] );
641  
642 return $self->get_position( { read_mode => $read_mode } );
643 }
644  
645 sub get_position {
646 my $self = shift;
647 my ($read_mode) = $self->_check_args( \@_, ['read_mode'] );
648  
649 if ( not defined $read_mode
650 or not $read_mode =~ /device|cache|request|fetch/ ) {
651 $read_mode = $self->device_settings()->{read_default};
652 }
653  
654 if ( $read_mode eq 'cache'
655 and defined $self->{'device_cache'}->{'position'} ) {
656 return $self->{'device_cache'}->{'position'};
657 }
658  
659 my ( $result, $errcode )
660 = $self->exec_cmd( TMCL_GAP, actual_position, 0 );
661  
662 # store position in Global-Parameter 0:
663 $self->exec_cmd( TMCL_SGP, 0, $result, 2 );
664 $self->exec_cmd( TMCL_STGP, 0, 0, 2 );
665  
666 $self->device_cache()->{position} = $self->steps2angle($result);
667 $self->save_motorinitdata();
668 $self->{value} = $self->device_cache()->{position};
669 return $self->device_cache()->{position};
670  
671 }
672  
673 sub save_motorinitdata {
674 my $self = shift;
675  
676 open( DUMP, ">" . $self->device_settings()->{'inipath'} )
677 ; #open for write, overwrite
678 print DUMP "POSITION: " . $self->device_cache()->{position} . "\n";
679 print DUMP "TARGET: " . $self->device_cache()->{target} . "\n";
680 print DUMP "SPEED_MAX: " . $self->device_settings()->{speed_max} . "\n";
681 print DUMP "UPPER_LIMIT: "
682 . $self->device_settings()->{upper_limit} . "\n";
683 print DUMP "LOWER_LIMIT: "
684 . $self->device_settings()->{lower_limit} . "\n";
685 print DUMP "TIMESTAMP: " . time() . "\n";
686 close(DUMP);
687 }
688  
689 sub _save_motorlog {
690 my $self = shift;
691 my ( $init_pos, $end_pos )
692 = $self->_check_args( \@_, [ 'init_pos', 'end_pos' ] );
693  
694 open( DUMP, ">>" . $self->device_settings()->{'logpath'} )
695 ; #open for write, overwrite
696  
697 print DUMP ( my_timestamp() ) . "\t move: $init_pos -> $end_pos \n";
698  
699 close(DUMP);
700 }
701  
702 sub read_motorinitdata {
703 my $self = shift;
704  
705 if ( not open( DUMP, "<" . $self->device_settings()->{'inipath'} ) ) {
706 return 0;
707 }
708 while (<DUMP>) {
709 chomp($_);
710 my @line = split( /: /, $_ );
711 if ( $line[0] eq 'POSITION' ) {
712 $self->device_cache()->{position} = $line[1];
713 }
714 elsif ( $line[0] eq 'TARGET' ) {
715 $self->device_cache()->{target} = $line[1];
716 }
717 elsif ( $line[0] eq 'SPEED_MAX' ) {
718 $self->device_settings()->{speed_max} = $line[1];
719 }
720 elsif ( $line[0] eq 'UPPER_LIMIT' ) {
721 $self->device_settings()->{upper_limit} = $line[1];
722 }
723 elsif ( $line[0] eq 'LOWER_LIMIT' ) {
724 $self->device_settings()->{lower_limit} = $line[1];
725 }
726  
727 }
728  
729 print "\nread MOTOR-INIT-DATA\n";
730 print "--------------------\n";
731 print "POSITION: " . $self->device_cache()->{position} . "\n";
732 print "TARGET: " . $self->device_cache()->{target} . "\n";
733 print "SPEED_MAX: " . $self->device_settings()->{speed_max} . "\n";
734 print "UPPER_LIMIT: " . $self->device_settings()->{upper_limit} . "\n";
735 print "LOWER_LIMIT: " . $self->device_settings()->{lower_limit} . "\n";
736  
737 print "--------------------\n";
738 return 1;
739 }
740  
741 sub my_timestamp {
742  
743 my (
744 $Sekunden, $Minuten, $Stunden, $Monatstag, $Monat,
745 $Jahr, $Wochentag, $Jahrestag, $Sommerzeit
746 ) = localtime(time);
747  
748 $Monat += 1;
749 $Jahrestag += 1;
750 $Monat = $Monat < 10 ? $Monat = "0" . $Monat : $Monat;
751 $Monatstag = $Monatstag < 10 ? $Monatstag = "0" . $Monatstag : $Monatstag;
752 $Stunden = $Stunden < 10 ? $Stunden = "0" . $Stunden : $Stunden;
753 $Minuten = $Minuten < 10 ? $Minuten = "0" . $Minuten : $Minuten;
754 $Sekunden = $Sekunden < 10 ? $Sekunden = "0" . $Sekunden : $Sekunden;
755 $Jahr += 1900;
756  
757 return "$Stunden:$Minuten:$Sekunden $Monatstag.$Monat.$Jahr\n";
758  
759 }
760  
761 1;
762  
763 __END__