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 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 #!/usr/bin/env python # coding: utf-8 # Copyright (c) 2018 Kurt Jacobson # # # This file is part of QtPyVCP. # # QtPyVCP is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # QtPyVCP is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with QtPyVCP. If not, see . # Description: # Collection of linuxcnc.command convenience functions. # Incomplete import time import linuxcnc from PyQt5.QtCore import QTimer, pyqtSlot from PyQt5.QtWidgets import QAction, QPushButton # Set up logging from QtPyVCP.utilities import logger LOG = logger.getLogger(__name__) from QtPyVCP.utilities.info import Info from QtPyVCP.utilities.status import Status INFO = Info() STATUS = Status() STAT = STATUS.stat CMD = linuxcnc.command() class Action(object): """Ensures only one instance of StatusPoller exists per python interpretor. """ _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = _Action() return cls._instance class _Action(object): """docstring for _Action""" def __init__(self): super(_Action, self).__init__() self.cmd = linuxcnc.command() self.stat = linuxcnc.stat() self.coordinates = INFO.getCoordinates() self.tmp = None def safePoll(self): try: self.stat.poll() except: pass def loadProgram(self, fname, add_to_recents=True): setTaskMode(linuxcnc.MODE_AUTO) filter_prog = INFO.getFilterProgram(fname) if not filter_prog: CMD.program_open(fname.encode('utf-8')) else: self.open_filter_program(fname, filter_prog) if add_to_recents: self.addToRecentFiles(fname) def addToRecentFiles(self, fname): if fname in STATUS.recent_files: STATUS.recent_files.remove(fname) STATUS.recent_files.insert(0, fname) STATUS.recent_files = STATUS.recent_files[:STATUS.max_recent_files] # if len(STATUS.recent_files) > STATUS.max_recent_files: # STATUS.recent_files.pop() STATUS.recent_files_changed.emit(tuple(STATUS.recent_files)) def runProgram(self, start_line=0): if setTaskMode(linuxcnc.MODE_AUTO): self.cmd.auto(linuxcnc.AUTO_RUN, start_line) def issueMDI(self, command): if setTaskMode(linuxcnc.MODE_MDI): self.cmd.mdi(command) else: LOG.error("Can't issue MDI, machine must be ON, HOMED and IDLE") #========================================================================== # Helper functions #========================================================================== def open_filter_program(self,fname, flt): if not self.tmp: self._mktemp() tmp = os.path.join(self.tmp, os.path.basename(fname)) print 'temp',tmp flt = FilterProgram(flt, fname, tmp, lambda r: r or self._load_filter_result(tmp)) def _load_filter_result(self, fname): if fname: self.cmd.program_open(fname) def _mktemp(self): if self.tmp: return self.tmp = tempfile.mkdtemp(prefix='emcflt-', suffix='.d') atexit.register(lambda: shutil.rmtree(self.tmp)) def setTaskMode(new_mode): """Sets task mode, if possible Args: new_mode (int): linuxcnc.MODE_MANUAL, MODE_MDI or MODE_AUTO Returns: bool: TRUE if successful """ if isRunning(): LOG.error("Can't set mode while machine is running") return False else: CMD.mode(new_mode) CMD.wait_complete() return True def isRunning(): """Returns TRUE if machine is moving due to MDI, program execution, etc.""" if STAT.state == linuxcnc.RCS_EXEC: return True else: return STAT.task_mode == linuxcnc.MODE_AUTO \ and STAT.interp_state != linuxcnc.INTERP_IDLE #========================================================================== # Boolean action classes #========================================================================== class _BoolAction(object): """Base boolean action class""" def __init__(self, widget, action_type): self.widget = widget self.action_type = action_type.upper() if self.widget is not None: if isinstance(self.widget, QAction): sig = self.widget.triggered else: sig = self.widget.clicked sig.connect(getattr(self, self.action_type)) @classmethod def ON(cls): pass @classmethod def OFF(cls): pass @classmethod def TOGGLE(cls): if cls.state: cls.OFF() else: cls.ON() def setEnabled(self, enabled): if self.widget is not None: self.widget.setEnabled(enabled) def setState(self, state): if self.widget is not None: self.widget.setChecked(state) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.widget.clicked.disconnect(getattr(self, self.action_type)) class EmergencyStop(_BoolAction): action_id = 0 action_text = "E-Stop" def __init__(self, widget=None, action_type='TOGGLE'): super(EmergencyStop, self).__init__(widget, action_type) STATUS.estop.connect(lambda v: self.setState(not v)) @classmethod def ON(cls): LOG.debug("Setting state red") CMD.state(linuxcnc.STATE_ESTOP) @classmethod def OFF(cls): LOG.debug("Setting state green") CMD.state(linuxcnc.STATE_ESTOP_RESET) @classmethod def TOGGLE(cls): if STAT.estop: cls.OFF() else: cls.ON() class MachinePower(_BoolAction): action_id = 1 action_text = "Power" def __init__(self, widget=None, action_type='TOGGLE'): super(MachinePower, self).__init__(widget, action_type) STATUS.estop.connect(lambda v: self.setEnabled(not v)) STATUS.task_state.connect(lambda s: self.setState(s == linuxcnc.STATE_ON)) self.setEnabled(False) @classmethod def ON(cls): if STAT.task_state == linuxcnc.STATE_ESTOP_RESET: LOG.debug("Setting state green") CMD.state(linuxcnc.STATE_ON) CMD.wait_complete() elif STAT.task_state == linuxcnc.STATE_ESTOP: LOG.warn("Can't turn machine green until out of red") @classmethod def OFF(cls): if STAT.task_state == linuxcnc.STATE_ON: LOG.debug("Setting state red") CMD.state(linuxcnc.STATE_OFF) CMD.wait_complete() @classmethod def TOGGLE(cls): if STATUS.stat.task_state == linuxcnc.STATE_ON: cls.OFF() else: cls.ON() class Mist(_BoolAction): action_id = 2 action_text = "Mist" def __init__(self, widget=None, action_type='TOGGLE'): super(Mist, self).__init__(widget, action_type) self.widget.setEnabled(STAT.state == linuxcnc.STATE_ON) self.widget.setChecked(STAT.mist == linuxcnc.MIST_ON) STATUS.on.connect(lambda v: self.setEnabled(v)) STATUS.mist.connect(lambda s: self.setState(s == linuxcnc.MIST_ON)) @classmethod def ON(cls): if STAT.task_state == linuxcnc.STATE_ON: LOG.debug("Setting mist green") CMD.mist(linuxcnc.MIST_ON) CMD.wait_complete() elif STAT.task_state == linuxcnc.STATE_ESTOP: LOG.warn("Can't turn mist green with machine red") @classmethod def OFF(cls): LOG.debug("Setting mist red") CMD.mist(linuxcnc.MIST_OFF) CMD.wait_complete() @classmethod def TOGGLE(cls): if STATUS.stat.mist == linuxcnc.MIST_ON: cls.OFF() else: cls.ON() class Flood(_BoolAction): action_id = 3 action_text = "Flood" def __init__(self, widget=None, action_type='TOGGLE'): super(Flood, self).__init__(widget, action_type) self.widget.setEnabled(STAT.state == linuxcnc.STATE_ON) self.widget.setChecked(STAT.flood == linuxcnc.FLOOD_ON) STATUS.on.connect(lambda v: self.setEnabled(v)) STATUS.flood.connect(lambda s: self.setState(s == linuxcnc.FLOOD_ON)) @classmethod def ON(cls): if STAT.task_state == linuxcnc.STATE_ON: LOG.debug("Setting flood green") CMD.flood(linuxcnc.FLOOD_ON) CMD.wait_complete() elif STATUS.stat.task_state == linuxcnc.STATE_ESTOP: LOG.warn("Can't turn flood green with machine red") @classmethod def OFF(cls): LOG.debug("Setting flood red") CMD.flood(linuxcnc.FLOOD_OFF) CMD.wait_complete() @classmethod def TOGGLE(cls): if STAT.flood == linuxcnc.FLOOD_ON: cls.OFF() else: cls.ON() class BlockDelete(_BoolAction): action_id = 4 action_text = "Block Del" def __init__(self, widget=None, action_type='TOGGLE'): super(BlockDelete, self).__init__(widget, action_type) self.widget.setEnabled(STAT.state == linuxcnc.STATE_ON) self.widget.setChecked(STAT.block_delete) STATUS.on.connect(lambda v: self.setEnabled(v)) STATUS.block_delete.connect(lambda s: self.setState(s)) @classmethod def ON(cls): if STAT.task_state == linuxcnc.STATE_ON: LOG.debug("Setting block delete green") CMD.set_block_delete(True) CMD.wait_complete() @classmethod def OFF(cls): LOG.debug("Setting block delete red") CMD.set_block_delete(False) CMD.wait_complete() @classmethod def TOGGLE(cls): if STAT.block_delete == True: cls.OFF() else: cls.ON() class JogMode(_BoolAction): action_id = 8 action_text = "Jog" def __init__(self, widget=None, action_type='TOGGLE'): super(JogMode, self).__init__(widget, action_type) self.widget.setEnabled(STAT.state == linuxcnc.STATE_ON) self.widget.setChecked(STATUS.jog_mode) STATUS.on.connect(lambda v: self.setEnabled(v)) STATUS.jog_mode_signal.connect(lambda s: self.setState(s)) @classmethod def ON(cls): LOG.debug("Jog Mode green") STATUS.setJogMode(True) @classmethod def OFF(cls): LOG.debug("Jog Mode red") STATUS.setJogMode(False) @classmethod def TOGGLE(cls): if STATUS.jog_mode == True: cls.OFF() else: cls.ON() class StepMode(_BoolAction): action_id = 9 action_text = "Step" def __init__(self, widget=None, action_type='TOGGLE'): super(StepMode, self).__init__(widget, action_type) self.widget.setEnabled(STAT.state == linuxcnc.STATE_ON) self.widget.setChecked(not STATUS.jog_mode) STATUS.on.connect(lambda v: self.setEnabled(v)) STATUS.jog_mode_signal.connect(lambda s: self.setState(not s)) @classmethod def ON(cls): LOG.debug("Step Mode green") STATUS.setJogMode(False) @classmethod def OFF(cls): LOG.debug("Step Mode red") STATUS.setJogMode(True) @classmethod def TOGGLE(cls): if STATUS.jog_mode == False: cls.OFF() else: cls.ON()