cnc line algorithm on a 20X2

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.
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
'==================================================================
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.
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
Maybe someone has some tips for me or even a bit of code :)
Thank you!
 

Anobium

Senior Member
I just reread this thread. What progress have you made?

The process you require is called interpolation. The maths involved is somewhat of challenge but let discuss your progress and current thoughts.

Best regard.
 

picaxester

Senior Member
I haven't made much progress since that post. I'm kind of thinking about using an Arduino but I really wanted to use a PICAXE in this project.
I could just make the pc do the math and have the PICAXE act as a serial to parallel converter but I'm not sure if that's the best way though.
What do you think is the best method using a PICAXE?
 

inglewoodpete

Senior Member
Just glancing at your Freebasic source code, I'd say you'd have to use a maths coprocessor if you chose to use a PICAXE. As much as I love PICAXEs, I think there would be better options available for CNC calculations.
 

picaxester

Senior Member
Probably a dumb question but why can the Arduino work with floating point numbers and 32 bit numbers but the PICAXE can't? They're both 8 bit processors.
Don't get me wrong, I'm a PICAXE fan through and through :) Just wondering.
 

MartinM57

Moderator
From http://www.arduino.cc/en/Hacking/BuildProcess
with my additions...

< < A number of things have to happen for your Arduino code to get onto the Arduino board. First, the Arduino environment performs some small transformations to make sure that the code is correct C or C++ (two common programming languages). It then gets passed to a compiler (avr-gcc), which turns the human readable code into machine readable instructions (or object files). Then, your code gets combined with (linked against), the standard Arduino libraries that provide basic functions like digitalWrite() or Serial.print() and avr-gcc libraries such as floating point arithmetic/32 bit integer maths etc. The result is a single Intel hex file, which contains the specific bytes that need to be written to the program memory of the chip on the Arduino board. This file is then uploaded to the board: transmitted over the USB or serial connection via the bootloader already on the chip or with external programming hardware. > >

PICAXE Basic doesn't have any capabilities to include library code - Rev Ed talk about it occasionally and it may well be just round the corner, but it's not here yet.

Code:
#PICAXE 28X3
Dim i,j,k as float
i = 23454.34
j = 75344.89
k = i / j
sertxd ("result is ", k)
would be nice one day
 
Last edited:
Top