SHARE
TWEET

Z change detection

bearer Jun 5th, 2019 (edited) 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4.  
  5. # Usefull refrenecs:
  6. # https://www.perltutorial.org/perl-write-to-file/
  7. # https://perl101.org/regexes.html
  8. # https://www.regular-expressions.info/floatingpoint.html
  9.  
  10. my $src = 'input.gcode';
  11. my $des = 'output.gcode';
  12. my $oldZ = 0;
  13. # open source file for reading
  14. open(SRC,'<',$src) or die $!;
  15.  
  16. # open destination file for writing
  17. open(DES,'>',$des) or die $!;
  18.  
  19. print("copying content from $src to $des\n");
  20.  
  21. while(<SRC>){
  22.   #match for Z value in lines starting with  G0, G1, G2, G3 /i-nsensitive to case
  23.   if ($_ =~ /^g[0123].*z([-+]?[0-9]*\.?[0-9]+)/i ) {
  24.     print "Z value detected: $1";
  25.     if ($1 > $oldZ){
  26.       #incrase Z, make cut
  27.       print " - increase, inserting cut code";
  28.       print DES "M280 P1 S90 ;tell servo to make cut to 90 deg\n";
  29.       print DES "G4 P2000 ;wait 2000ms for cut to finish\n";
  30.       print DES "M280 P1 S0 ;tell servo to return to 0 deg\n";
  31.       print DES "G4 P2000 ;wait 2000ms for servo to return\n";
  32.       $oldZ = $1;
  33.       }
  34.     if ($1 < $oldZ) {
  35.       #decrease Z ,store new Z
  36.       print " - decrease, do nothing";
  37.       $oldZ = $1;
  38.       }
  39.     print "\n"; #newline
  40.     }
  41.    #output original g-code
  42.    print DES $_;
  43. }
  44.  
  45. # always close the filehandles
  46. close(SRC);
  47. close(DES);
  48.  
  49. print "File content copied successfully!\n";
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top