How to setup counter in code
-
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
- sets up the variables to collect the triggers
- 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. - 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
-
@oliof thank you
-
@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
-
@martin7404 a simple typo.
-
@oliof One more thing
I have to define enstops in config.g or not -
@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)
-
@oliof thank you , this is what i meant to define them like triggers
-
-
@oliof HI,
I am finaly setup and as far I am not good in meta commands here it isM32 "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
-
@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.
-
@oliof
Finaly get it working
Here it isvar 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
-
@oliof thank you
-
@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.