Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    How to setup counter in code

    Scheduled Pinned Locked Moved
    Gcode meta commands
    2
    13
    650
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • martin7404undefined
      martin7404
      last edited by

      Hi,
      all I am building test rig and I would like to use duet wifi .
      The rig has to use 1 output ( optocoupler ) to actuate 24 solenoid of pneumatic valve that engage 8 pneumatic cylinders, that press 8 switches, then I have to read the 8 switches (endstop inputs) and count them. The cycle has to run every second.
      After 1 million cycles I will check the reliability of each switch. I.e. how many times each switch did close\open
      I am planning to use meta-commands in a loop for both output and inputs.
      The problem is that I am not familiar in-depth with meta programing.
      Any help or advice will be greatly apreciated

      Muldex IDEX Duet2+Duex5
      Custom CoreXY 600x400 Hemera , Duet3+Toolboard+1HCL closed loop
      Sapphire Pro with Duet2, with closed-loop motors
      custom high temp E3D tool changer with Duet2+Duex

      oliofundefined 1 Reply Last reply Reply Quote 0
      • oliofundefined
        oliof @martin7404
        last edited by oliof

        The loop can be done with the built-in iterations keyword.
        You can count your endstops by looking at the object model and incrementing a variable if it's triggered, doing nothing if it's not.

        A very simple, untested skeleton script to help you start out could look like this.

        It

        1. sets up the variables to collect the triggers
        2. runs one million times and each run
          a. does your thing
          b. for each endstop, checks if it is triggered and increases the appropriate variable
          c. waits 1000 milliseconds.
        3. after running one million time, appends the results to a file called counts.

        You will likely need to adjust this code to actually work and do your thing.

        var endstop0_triggers = 0
        var endstop1_triggers = 0
        [...] ; expand for all eight triggers
        
        while iterations <1000000
          do_your_thing ; replace with gcode commands to fire your solenoids
          if sensors.endstops.0.triggered
           endstop0_triggers = endstop0_triggers+1
          if sensors.endstops.1.triggered
               endstop1_triggers = endstop1_triggers+1
          [...] ; expand for all eight triggers
          G4 P1000 ; this adds a 1 second pause, if you want to measure every second, measure your cycle time and reduce the pause accordingly
        
        echo >>counts endstop0_triggers
        echo >>counts  endstop1_triggers
        [...] ; expand for all eight triggers
                
        

        <>RatRig V-Minion Fly Super5Pro RRF<> V-Core 3.1 IDEX k*****r <> RatRig V-Minion SKR 2 Marlin<>

        martin7404undefined 2 Replies Last reply Reply Quote 1
        • martin7404undefined
          martin7404 @oliof
          last edited by

          @oliof thank you

          Muldex IDEX Duet2+Duex5
          Custom CoreXY 600x400 Hemera , Duet3+Toolboard+1HCL closed loop
          Sapphire Pro with Duet2, with closed-loop motors
          custom high temp E3D tool changer with Duet2+Duex

          1 Reply Last reply Reply Quote 0
          • martin7404undefined
            martin7404 @oliof
            last edited by

            @oliof said in How to setup counter in code:

            var endstop0_triggers = 0 var endstop1_triggers = 1

            why enstop1 triger value starts from 1 and not 0

            Muldex IDEX Duet2+Duex5
            Custom CoreXY 600x400 Hemera , Duet3+Toolboard+1HCL closed loop
            Sapphire Pro with Duet2, with closed-loop motors
            custom high temp E3D tool changer with Duet2+Duex

            oliofundefined 1 Reply Last reply Reply Quote 0
            • oliofundefined
              oliof @martin7404
              last edited by

              @martin7404 a simple typo.

              <>RatRig V-Minion Fly Super5Pro RRF<> V-Core 3.1 IDEX k*****r <> RatRig V-Minion SKR 2 Marlin<>

              martin7404undefined 1 Reply Last reply Reply Quote 0
              • martin7404undefined
                martin7404 @oliof
                last edited by

                @oliof One more thing
                I have to define enstops in config.g or not

                Muldex IDEX Duet2+Duex5
                Custom CoreXY 600x400 Hemera , Duet3+Toolboard+1HCL closed loop
                Sapphire Pro with Duet2, with closed-loop motors
                custom high temp E3D tool changer with Duet2+Duex

                oliofundefined 1 Reply Last reply Reply Quote 0
                • oliofundefined
                  oliof @martin7404
                  last edited by

                  @martin7404 you can define them wherever you want, config.g is the normal place. They need to be defined before you use them of course.

                  Another option would be to define them as triggers and have trigger2 to trigger9 macros that write to a file whenever they are actuated (triggers 0 and 1 are special cases)

                  <>RatRig V-Minion Fly Super5Pro RRF<> V-Core 3.1 IDEX k*****r <> RatRig V-Minion SKR 2 Marlin<>

                  martin7404undefined 2 Replies Last reply Reply Quote 1
                  • martin7404undefined
                    martin7404 @oliof
                    last edited by

                    @oliof thank you , this is what i meant to define them like triggers

                    Muldex IDEX Duet2+Duex5
                    Custom CoreXY 600x400 Hemera , Duet3+Toolboard+1HCL closed loop
                    Sapphire Pro with Duet2, with closed-loop motors
                    custom high temp E3D tool changer with Duet2+Duex

                    1 Reply Last reply Reply Quote 0
                    • Phaedruxundefined Phaedrux moved this topic from Using Duet Controllers
                    • martin7404undefined
                      martin7404 @oliof
                      last edited by martin7404

                      @oliof HI,
                      I am finaly setup and as far I am not good in meta commands here it is

                      M32 "0:/gcodes/test.g"
                      File 0:/gcodes/test.g selected for printing
                      Error: Bad command: endstop0_triggers = endstop0_triggers+1
                      Error: in GCode file line 16 column 6: meta command: missing array index
                      Cancelled printing file 0:/gcodes/test.g, print time was 0h 0m
                      

                      I did checke the spelling 3 times no luck

                      Muldex IDEX Duet2+Duex5
                      Custom CoreXY 600x400 Hemera , Duet3+Toolboard+1HCL closed loop
                      Sapphire Pro with Duet2, with closed-loop motors
                      custom high temp E3D tool changer with Duet2+Duex

                      oliofundefined 1 Reply Last reply Reply Quote 0
                      • oliofundefined
                        oliof @martin7404
                        last edited by

                        @martin7404 my mistake, replace

                             endstop0_triggers = endstop0_triggers+1
                        

                        with

                           set endstop0_triggers = {endstop0_triggers+1}
                        

                        and see if that works. Same for the other equivalent lines.

                        <>RatRig V-Minion Fly Super5Pro RRF<> V-Core 3.1 IDEX k*****r <> RatRig V-Minion SKR 2 Marlin<>

                        martin7404undefined 2 Replies Last reply Reply Quote 1
                        • martin7404undefined
                          martin7404 @oliof
                          last edited by

                          @oliof
                          Finaly get it working
                          Here it is

                          var endstop0_triggers=0
                          var endstop1_triggers = 0
                          var endstop2_triggers = 0
                          var endstop3_triggers = 0
                          var endstop4_triggers = 0
                          var endstop5_triggers = 0
                          var endstop6_triggers = 0
                          var endstop7_triggers = 0;
                          
                           while iterations <30
                            M106 S1.0
                            if sensors.endstops[0].triggered
                              set var.endstop0_triggers = {var.endstop0_triggers+1} ;
                            ; if sensors.endstops[1].triggered
                             ;    endstop1_triggers = {endstop1_triggers+1} ;
                            G4 P400
                           M106 S0
                            G4 P500 ; this adds a 1 second pause, if you want to measure every second, measure your cycle time and reduce the pause accordingly
                          echo var.endstop0_triggers
                          echo >>"sys/counts" var.endstop0_triggers
                          

                          Muldex IDEX Duet2+Duex5
                          Custom CoreXY 600x400 Hemera , Duet3+Toolboard+1HCL closed loop
                          Sapphire Pro with Duet2, with closed-loop motors
                          custom high temp E3D tool changer with Duet2+Duex

                          1 Reply Last reply Reply Quote 0
                          • martin7404undefined
                            martin7404 @oliof
                            last edited by

                            @oliof thank you

                            Muldex IDEX Duet2+Duex5
                            Custom CoreXY 600x400 Hemera , Duet3+Toolboard+1HCL closed loop
                            Sapphire Pro with Duet2, with closed-loop motors
                            custom high temp E3D tool changer with Duet2+Duex

                            oliofundefined 1 Reply Last reply Reply Quote 0
                            • oliofundefined
                              oliof @martin7404
                              last edited by

                              @martin7404 your current version only counts the first endstop. You'll need to duplicate line 13/14 and adjust for endstop1-endstop8 in there to count the others too.

                              <>RatRig V-Minion Fly Super5Pro RRF<> V-Core 3.1 IDEX k*****r <> RatRig V-Minion SKR 2 Marlin<>

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post
                              Unless otherwise noted, all forum content is licensed under CC-BY-SA