|
Post by alexfish on Nov 22, 2023 23:19:52 GMT 1
Hi All
Think I posted some problem with comparing floating point a while ago
Here on raspy pi this bit of code is from some stepper motor drivers the code is in doubles since it looks correct in a Meter Length
' 1000 mm 1mtr CONST MAX_STEP = 200000 CONST MOTOR_STEP = 0.005 LOCAL ptr TYPE double
DECLARE motor_step[MAX_STEP+1] TYPE double
FOR t = 0 TO MAX_STEP motor_step[t] = ptr INCR ptr,MOTOR_STEP NEXT '============================================================= FUNCTION GET_STEPS(double Dist) TYPE int LOCAL res TYPE double FOR t = 0 TO MAX_STEP res = motor_step[t] IF res >= Dist THEN PRINT motor_step[t] PRINT motor_step[t-1] res = motor_step[t] RETURN t END IF NEXT
END FUNCTION
PRINT motor_step[MAX_STEP] LOCAL MY_DIST TYPE double MY_DIST = 9.6903 tstep = GET_STEPS(MY_DIST) PRINT "tstep " ,tstep
if change all the double to float then on meter length IE:: 1000 mm then get 1000.82 mm
Food for thought including the bits to play with
Added That could course a seg fault on an array if dividing the step(pos(dim)) this 0.82 mm is 217 step more
BR Alex
|
|
|
Post by alexfish on Nov 24, 2023 0:12:26 GMT 1
Here have used strings
If need the value Simple : FOO = VAL(X)
Anyway : can get pretty close to the mark in terms of a stepper motor steps to distance
BR Alex
the Bits
' 1000 mm 1mtr OPTION PARSE FALSE CONST MAX_STEP = 200000 CONST MOTOR_STEP = 0.005 LOCAL ptr TYPE double DECLARE motor_step$[MAX_STEP+1] TYPE char*
REM ============================================ SUB SET_UP()
FOR t = 0 TO MAX_STEP LOCAL gx[20] TYPE static char sprintf(gx,"%.3f",ptr) motor_step$[t] = gx INCR ptr,MOTOR_STEP NEXT
END SUB REM ============================================ FUNCTION GET_STEPS(double Dist) TYPE int
LOCAL res TYPE double LOCAL pos LOCAL gx[20] TYPE static char LOCAL res$ sprintf(gx,"%.3f",Dist) res$ = gx PRINT "Look Up ", res$ PRINT "Steps " , Dist / MOTOR_STEP pos = Dist / MOTOR_STEP PRINT "Distance " , Dist PRINT "Nearest Dist ",motor_step$[pos] RETURN pos END FUNCTION REM ============================================ SET_UP LOCAL rp TYPE double FOR rp = 1 TO 100 st = GET_STEPS(rp+ 0.486) PRINT "Steps ",st PRINT "==========================" NEXT
IMPROVED
' 1000 mm 1mtr OPTION PARSE FALSE CONST MAX_STEP = 200000 CONST MOTOR_STEP = 0.005 LOCAL ptr TYPE double DECLARE motor_step$[MAX_STEP+1] TYPE char*
REM ============================================ SUB SET_UP()
FOR t = 0 TO MAX_STEP LOCAL gx[20] TYPE static char sprintf(gx,"%.3f",ptr) motor_step$[t] = gx INCR ptr,MOTOR_STEP NEXT
END SUB REM ============================================ FUNCTION GET_STEPS(double Dist) TYPE int
LOCAL res TYPE double LOCAL pos LOCAL gx[20] TYPE static char LOCAL res$ sprintf(gx,"%.3f",Dist) res$ = gx PRINT "Look Up ", res$ PRINT "Steps " , Dist / MOTOR_STEP pos = Dist / MOTOR_STEP PRINT "Distance " , Dist IF res$ = motor_step$[pos] THEN PRINT "Nearest Dist 1 ",motor_step$[pos] RETURN pos ELSE res$ = motor_step$[pos+1] PRINT "Nearest Dist 2 ",motor_step$[pos+1] RETURN pos+1 END IF END FUNCTION REM ============================================ SET_UP LOCAL rp TYPE double FOR rp = 1 TO 50 STEP 0.3 st = GET_STEPS(rp) PRINT "Steps ",st PRINT "==========================" NEXT
|
|
|
Post by alexfish on Nov 24, 2023 11:57:29 GMT 1
Conclusion
Here on pi math wise to real world use double
my case on raspy pi
Final bit
' 1000 mm 1mtr OPTION PARSE FALSE CONST MAX_STEP = 200000 CONST MOTOR_STEP = 0.005
REM ============================================
FUNCTION GET_STEPS(double Dist) TYPE int LOCAL t LOCAL res$ LOCAL val TYPE double PRINT "Lookup " , Dist LOCAL gx[20] TYPE static char sprintf(gx,"%.3f",Dist) res$=gx val=VAL(res$) t = val/MOTOR_STEP PRINT "Nearest " , val RETURN t END FUNCTION REM ============================================
st= GET_STEPS( 0.3144) PRINT "Steps ",st st= GET_STEPS( 0.3145) PRINT "Steps ",st
Can Now spot the difference using float
' 1000 mm 1mtr OPTION PARSE FALSE CONST MAX_STEP = 200000 CONST MOTOR_STEP = 0.005
REM ============================================
FUNCTION GET_STEPS(float Dist) TYPE int LOCAL t LOCAL res$ LOCAL val TYPE float PRINT "Lookup " , Dist LOCAL gx[20] TYPE static char sprintf(gx,"%.3f",Dist) res$=gx val=VAL(res$) t = val/MOTOR_STEP PRINT "Nearest " , val RETURN t END FUNCTION REM ============================================
st= GET_STEPS( 0.3144) PRINT "Steps ",st st= GET_STEPS( 0.3145) PRINT "Steps ",st
Results
double
Lookup 0.3144 Nearest 0.314 Steps 62 Lookup 0.3145 Nearest 0.315 Steps 63 float
Lookup 0.3144 Nearest 0.314 Steps 62 Lookup 0.3145 Nearest 0.315 Steps 62
in the float function test
st= GET_STEPS( 0.3149) results stay same at 62 steps
Lookup 0.3144 Nearest 0.314 Steps 62 Lookup 0.3149 Nearest 0.315 Steps 62 Have Fun BR Alex
|
|
|
Post by alexfish on Nov 25, 2023 14:53:15 GMT 1
Put the final solution to test in hug gcoder Now have Quadrants in alignment Same For Circle modified G3 + the start of and end ARC 90deg G3("[-]" , "#FF0000",0,90,0,0,300,200) START OF 90 deg ARC G0 X150.000 Y0.000 G3 X148.691 Y0.004 G3 X147.382 Y0.015 G3 X146.073 Y0.034 G3 X144.765 Y0.061 G3 X143.457 Y0.095 G3 X142.150 Y0.137 G3 X140.843 Y0.187 G3 X139.537 Y0.244 G3 X138.231 Y0.308 G3 X136.927 Y0.381 G3 X135.623 Y0.460 G3 X134.321 Y0.548 G3 X133.020 Y0.643 G3 X131.720 Y0.745 G3 X130.421 Y0.856 G3 X129.124 Y0.973 G3 X127.829 Y1.098
END OF 90 deg ARC G3 X0.365 Y93.024 G3 X0.280 Y93.895 G3 X0.206 Y94.766 G3 X0.143 Y95.638 G3 X0.091 Y96.510 G3 X0.051 Y97.382 G3 X0.023 Y98.255 G3 X0.006 Y99.127 G3 X0.000 Y100.000
Alex the picture shows Finnese in the stepping in HUG3.5.3 can apply same in drawing : therefore gets rid of the chunky pixel look Attachments:
|
|