picaxester
Senior Member
I'm trying to port this FreeBASIC code to a PICAXE 20X2. I'm having some problems, mostly floating point numbers and working around them.
The FreeBASIC post: http://www.freebasic.net/forum/viewtopic.php?p=126415#126415 p.s I'm Turd on the FB forum.
This is what I have right now. There's probably no point in posting it because it's a mess and doesn't work but here it is just so you know that I've tried lol.
Maybe someone has some tips for me or even a bit of code
Thank you!
The FreeBASIC post: http://www.freebasic.net/forum/viewtopic.php?p=126415#126415 p.s I'm Turd on the FB forum.
Code:
'==================================================================
' Triaxial stepper motors in a straight line via Parallel Port
'==================================================================
' feed rate and step rate are limited by variable stepTime
Dim Shared As Double stepTime = .01 ' safe time in seconds between steps
Dim Shared As Double waitUntil ' time when safe for next move
' these arrays map the step counter sequence to port bits.
Dim Shared As Integer Xgray( 0 To 3) => {0, 1, 3, 2} ' *1 , bits 0 and 1
Dim Shared As Integer Ygray( 0 To 3) => {0, 4, 12, 8} ' *4 , bits 2 and 3
Dim Shared As Integer Zgray( 0 To 3) => {0, 16, 48, 32} ' *16, bits 4 and 5
Const As Integer Parallel_Port = 888
'==================================================================
Sub steppers(Byval x As Integer, Byval y As Integer, Byval z As Integer)
' x, y and z are now rounded to integers
Do
Loop Until Timer > waitUntil ' when it will be safe to step
' convert x, y & z integers to stepper phase bit patterns
Dim As Integer BitPattern = Xgray(x And 3) Or Ygray(y And 3) Or Zgray(z And 3)
' uncomment the next line to output data to parallel port
' Out ParallelPort, BitPattern ' output gray codes to the stepper motors
Print Bin(BitPattern, 8) ' this line is demonstration code only
'--------------------------------------------------------------
waitUntil = Timer + stepTime ' minimum step time delay in seconds
End Sub
'==================================================================
Sub Move3D(_ ' 3D straight line move
Byref xold As Double,_
Byref yold As Double,_
Byref zold As Double,_
Byref xnew As Double,_
Byref ynew As Double,_
Byref znew As Double)
Dim As Double xdist, ydist, zdist
xdist = xnew - xold
ydist = ynew - yold
zdist = znew - zold
' find maximum distance
Dim As Double maxdist = Abs(xdist)
If maxdist < Abs(ydist) Then maxdist = Abs(ydist)
If maxdist < Abs(zdist) Then maxdist = Abs(zdist)
' signed step sizes, all are <= 1.0000
Dim As Double xstep, ystep, zstep
xstep = xdist / maxdist
ystep = ydist / maxdist
zstep = zdist / maxdist
' step along the line
For i As Integer = 1 To maxdist ' now draw the line
xold = xold + xstep
yold = yold + ystep
zold = zold + zstep
steppers(xold, yold, zold)
Next i
' eliminate any fp roundoff
xold = xnew
yold = ynew
zold = znew
steppers(xold, yold, zold)
End Sub
'==================================================================
' demo and test
Screen 19
Move3d (100, 100, 100, 116, 108, 101) ' short move to test x, y and z
'==================================================================
Sleep
'==================================================================
Code:
#no_data
#no_table
symbol xdist = w0
symbol ydist = w1
symbol zdist = w2
symbol xnew = w3
symbol ynew = w4
symbol znew = w5
symbol xold = w6
symbol yold = w7
symbol zold = w8
symbol maxdist = w9
symbol xstep = w10
symbol ystep = w11
symbol zstep = w12
symbol xstepth = w13
symbol ystepth = w14
symbol zstepth = w15
symbol xstepfp = b46
symbol ystepfp = b47
symbol zstepfp = b48
symbol xfp = w16
symbol yfp = w17
symbol zfp = w18
symbol xdir = b53
symbol ydir = b54
symbol zdir = b55
symbol bitpattern_a = b45
symbol bitpattern_b = b44
symbol xpattern = b43
symbol ypattern = b42
symbol zpattern = b41
symbol xlookup = b40
symbol ylookup = b39
symbol zlookup = b38
dirsb = %11111111
dirsc = %11111111
xnew = 200
xold = 0
ynew = 200
yold = 0
znew = 20
zold = 0
pause 2000
gosub move_3d
end
move_3d:
if xnew > xold then
xdist = xnew - xold
xdir = "+"
elseif xnew < xold then
xdist = xold - xnew
xdir = "-"
endif
if ynew > yold then
ydist = ynew - yold
ydir = "+"
elseif ynew < yold then
ydist = yold - ynew
ydir = "-"
endif
if znew > zold then
zdist = znew - zold
zdir = "+"
elseif znew < zold then
zdist = zold - znew
zdir = "-"
endif
maxdist = xdist
if ydist > maxdist then
maxdist = ydist
elseif zdist > maxdist then
maxdist = zdist
endif
xdist = xdist / 10
ydist = ydist / 10
zdist = zdist / 10
xstep = xdist * 10 / maxdist
ystep = ydist * 10 / maxdist
zstep = zdist * 10 / maxdist
xstepth = 9
ystepth = 9
zstepth = 9
xfp = 0
yfp = 0
zfp = 0
for b52 = 1 to maxdist
xfp = xfp + xstep
if xfp > xstepth then
if xdir = "+" then
xold = xold + 1
elseif xdir = "-" then
xold = xold - 1
endif
xstepth = xstepth + 10
endif
yfp = yfp + ystep
if yfp > ystepth then
if ydir = "+" then
yold = yold + 1
elseif ydir = "-" then
yold = yold - 1
endif
ystepth = ystepth + 10
endif
zfp = zfp + zstep
if zfp > zstepth then
if zdir = "+" then
zold = zold + 1
elseif zdir = "-" then
zold = zold - 1
endif
zstepth = zstepth + 10
endif
gosub steppers
next b52
b52 = 0
do
if xold < xnew then
xold = xold + 1
elseif xold > xnew then
xold = xold - 1
else
b52 = b52 + 1
endif
if yold < ynew then
yold = yold + 1
elseif yold > ynew then
yold = yold - 1
else
b52 = b52 + 1
endif
if zold < znew then
zold = zold + 1
elseif zold > znew then
zold = zold - 1
else
b52 = b52 + 1
endif
gosub steppers
loop while b52 < 2
return
steppers:
xlookup = xold and 3
ylookup = yold and 3
zlookup = zold and 3
lookup xlookup, (10, 6, 5, 9), xpattern
lookup ylookup, (160, 96, 80, 144), ypattern
lookup zlookup, (10, 6, 5, 9), zpattern
pinsb = xpattern + ypattern
pinsc = zpattern
pause 5
return
Thank you!