Thursday, April 21, 2011

answers to some code requests

some readers asked about the code.
Christopher, i am not sending it Gcode. I send it a single character followed by a carriage return.
The character is a digit 0 through 7. 0 means move up one step, 1 means up and to the right one step, 2 means move right one step, etc. everything is then calculated in a quickbasic program and sent through the serial port. now i have replaced the basic stamp board with the Arduino so now all the code is internal to it and it can run without the PC connected. i am having some problems with that setup though. when it gets toward the top of the board the steppers slip some steps and it gets lost as to where it really is. I think possibly because I am taking Vin and ground from the connector on the Arduino and the motors draw about 750 ma I am getting too much drop across that connection. I may change the driver from a unipolar design that grounds one end of the coil or the other while supplying power to the center to an H bridge driver that supplies power to one end and ground to the other and switches both to change the steps. That should supply the same amount of magnetic force while using half the current.
Here is the code to draw several pictures one after the other in quickbasic.

'WHITE BOARD PLOTTER TESTS
CLS
PI = 3.141593
GOSUB WBPSETUP


'SCREEN 12 '640 * 480 graphics



'OPEN "COM1:2400,N,8,1" FOR RANDOM AS #1
OPEN "COM1:2400,N,8,1,CD0,CS0,DS0,OP0,RS,TB2048,RB2048" FOR RANDOM AS #1
'OPEN "COM1:2400,N,8,1,RS,TB2048" FOR RANDOM AS #1


'GOTO FOURBALLO
'GOTO CESARO4


START:

HEADING = 0
CENTERX = 0
CENTERY = 0
LENGTH = 0
X = CENTERX
Y = CENTERY
FIRSTLOOP:
LENGTH = LENGTH + .3

HEADING = HEADING + 2.08

X = COS(HEADING) * LENGTH + X
Y = SIN(HEADING) * LENGTH + Y
GOSUB WBPMOVE
IF LENGTH < 18 GOTO FIRSTLOOP

FOURBALLO:
HEADING = 0
CENTERX = 0
CENTERY = 0
SIZE = 5
PHASES = 72

X = SIZE: Y = 0: GOSUB WBPMOVE
INPUT "ERASE THE BOARD AND PRESS ENTER "; N$

FOR HEADING = 0 TO (PHASES * 2 * PI) STEP (.02)
MOVCIRC = HEADING / PHASES
R = SIZE
R1 = COS(HEADING * 2)
R2 = SIN(HEADING + MOVCIRC)
R = R + R1 * R2 * SIZE
X = COS(HEADING) * R + CENTERX
Y = CENTERY + SIN(HEADING) * R
GOSUB WBPMOVE
NEXT
X = SIZE: Y = 0: GOSUB WBPMOVE
INPUT " PRESS ENTER "; N$

CESARO4:
CSIDE = 7.5
CSTAGE = 0
SIDE = CSIDE * .453 ^ CSTAGE
X = 0
Y = -(CSIDE / 3)
GOSUB WBPMOVE
INPUT "PRESS ENTER"; N$

FOR CSTAGE = 0 TO 7
SIDE = CSIDE * .453 ^ CSTAGE
FOR CUBE = 1 TO 4
STAGE = CSTAGE
GOSUB SPIKE
HEADING = HEADING + PI / 2
NEXT CUBE
HEADING = HEADING - PI / 2
NEXT CSTAGE
GOTO CESARODONE

SPIKE:
IF STAGE = 0 THEN GOSUB FORWARD: GOTO DONE
STAGE = STAGE - 1
GOSUB SPIKE
HEADING = HEADING + (84 / 180 * PI)
GOSUB SPIKE
HEADING = HEADING - (168 / 180 * PI)
GOSUB SPIKE
HEADING = HEADING + (84 / 180 * PI)
GOSUB SPIKE
STAGE = STAGE + 1

DONE:
RETURN

FORWARD:
X = X + SIDE * SIN(HEADING)
Y = Y + SIDE * COS(HEADING)
GOSUB WBPMOVE
RETURN

CESARODONE:
X = 0: Y = 0: GOSUB WBPMOVE

CLOSE
END

WBPMOVE:

WBPX = X
WBPY = Y
WBPOLDX = WBPXNOW
WBPOLDY = WBPYNOW
WBPXDELTA = WBPX - WBPXNOW
WBPYDELTA = WBPY - WBPYNOW
IF ABS(WBPXDELTA) < (WBPSTEPSIZE) AND ABS(WBPYDELTA) < (WBPSTEPSIZE) THEN GOTO WBPMOVEDONE

WBPXDIR = SGN(WBPXDELTA)
WBPYDIR = SGN(WBPYDELTA)

IF ABS(WBPYDELTA) > ABS(WBPXDELTA) THEN GOTO WBPYBIGGER

WBPXBIGGER:
FOR WBPX = WBPXNOW TO X STEP (WBPXDIR * .01)
WBPY = Y - ((X - WBPX) / WBPXDELTA) * WBPYDELTA
GOSUB WBPTRYSTEPPING
NEXT WBPX
GOTO WBPXFINISH

WBPXFINISH:
GOSUB WBPTRYSTEPPING
IF ABS(X - WBPXNOW) > WBPSTEPSIZE OR ABS(Y - WBPYNOW) > WBPSTEPSIZE THEN GOTO WBPXFINISH
GOTO WBPMOVEDONE

WBPYBIGGER:
FOR WBPY = WBPYNOW TO Y STEP (WBPYDIR * .01)
WBPX = X - ((Y - WBPY) / WBPYDELTA) * WBPXDELTA
GOSUB WBPTRYSTEPPING
NEXT WBPY

WBPYFINISH:
GOSUB WBPTRYSTEPPING
IF ABS(X - WBPXNOW) > WBPSTEPSIZE OR ABS(Y - WBPYNOW) > WBPSTEPSIZE THEN GOTO WBPYFINISH
GOTO WBPMOVEDONE

WBPTRYSTEPPING:
'try stepping in each direction and see which way gets closer to x,y

'TRY STEPPING 0
WBPLEFTTRY = WBPLEFT - WBPSTEPSIZE
WBPRIGHTTRY = WBPRIGHT - WBPSTEPSIZE
GOSUB WBPTRYDIST
WBPTRY(0) = SQR((WBPXDIFF * WBPXDIFF) + (WBPYDIFF * WBPYDIFF))

'TRY STEPPING 1
WBPLEFTTRY = WBPLEFT
WBPRIGHTTRY = WBPRIGHT - WBPSTEPSIZE
GOSUB WBPTRYDIST
WBPTRY(1) = SQR((WBPXDIFF * WBPXDIFF) + (WBPYDIFF * WBPYDIFF))

'TRY STEPPING 2
WBPLEFTTRY = WBPLEFT + WBPSTEPSIZE
WBPRIGHTTRY = WBPRIGHT - WBPSTEPSIZE
GOSUB WBPTRYDIST
WBPTRY(2) = SQR((WBPXDIFF * WBPXDIFF) + (WBPYDIFF * WBPYDIFF))

'TRY STEPPING 3
WBPLEFTTRY = WBPLEFT + WBPSTEPSIZE
WBPRIGHTTRY = WBPRIGHT
GOSUB WBPTRYDIST
WBPTRY(3) = SQR((WBPXDIFF * WBPXDIFF) + (WBPYDIFF * WBPYDIFF))

'TRY STEPPING 4
WBPLEFTTRY = WBPLEFT + WBPSTEPSIZE
WBPRIGHTTRY = WBPRIGHT + WBPSTEPSIZE
GOSUB WBPTRYDIST
WBPTRY(4) = SQR((WBPXDIFF * WBPXDIFF) + (WBPYDIFF * WBPYDIFF))

'TRY STEPPING 5
WBPLEFTTRY = WBPLEFT
WBPRIGHTTRY = WBPRIGHT + WBPSTEPSIZE
GOSUB WBPTRYDIST
WBPTRY(5) = SQR((WBPXDIFF * WBPXDIFF) + (WBPYDIFF * WBPYDIFF))

'TRY STEPPING 6
WBPLEFTTRY = WBPLEFT - WBPSTEPSIZE
WBPRIGHTTRY = WBPRIGHT + WBPSTEPSIZE
GOSUB WBPTRYDIST
WBPTRY(6) = SQR((WBPXDIFF * WBPXDIFF) + (WBPYDIFF * WBPYDIFF))

'TRY STEPPING 7
WBPLEFTTRY = WBPLEFT - WBPSTEPSIZE
WBPRIGHTTRY = WBPRIGHT
GOSUB WBPTRYDIST
WBPTRY(7) = SQR((WBPXDIFF * WBPXDIFF) + (WBPYDIFF * WBPYDIFF))

WBPTEMP = 0
IF WBPTRY(1) < WBPTRY(WBPTEMP) THEN WBPTEMP = 1
IF WBPTRY(2) < WBPTRY(WBPTEMP) THEN WBPTEMP = 2
IF WBPTRY(3) < WBPTRY(WBPTEMP) THEN WBPTEMP = 3
IF WBPTRY(4) < WBPTRY(WBPTEMP) THEN WBPTEMP = 4
IF WBPTRY(5) < WBPTRY(WBPTEMP) THEN WBPTEMP = 5
IF WBPTRY(6) < WBPTRY(WBPTEMP) THEN WBPTEMP = 6
IF WBPTRY(7) < WBPTRY(WBPTEMP) THEN WBPTEMP = 7
ON (WBPTEMP + 1) GOSUB WBP0, WBP1, WBP2, WBP3, WBP4, WBP5, WBP6, WBP7
FOR WBPZ = 1 TO 6000: NEXT

'CALCULATE WHERE THE PEN IS NOW
WBPS = (WBPLEFT + WBPRIGHT + WBPBASE) / 2
WBPAREA = SQR(WBPS * (WBPS - WBPLEFT) * (WBPS - WBPRIGHT) * (WBPS - WBPBASE))
'VERTICAL DISTANCE FROM SPOOLS DOWN TO THE PEN IS
WBPHEIGHT = (WBPAREA * 2) / WBPBASE
WBPWIDTH = SQR((WBPLEFT * WBPLEFT) - (WBPHEIGHT * WBPHEIGHT))

WBPXNOW = WBPWIDTH - WBPCENTERWIDTH
WBPYNOW = WBPCENTERHEIGHT - WBPHEIGHT

RETURN

GOTO WBPMOVE

WBPTRYDIST:
WBPTRYS = (WBPLEFTTRY + WBPRIGHTTRY + WBPBASE) / 2
WBPTRYAREA = SQR(WBPTRYS * (WBPTRYS - WBPLEFTTRY) * (WBPTRYS - WBPRIGHTTRY) * (WBPTRYS - WBPBASE))
'VERTICAL DISTANCE FROM SPOOLS DOWN TO THE PEN IS
WBPTRYHEIGHT = (WBPTRYAREA * 2) / WBPBASE
WBPTRYWIDTH = SQR((WBPLEFTTRY * WBPLEFTTRY) - (WBPTRYHEIGHT * WBPTRYHEIGHT))

WBPXTRY = WBPTRYWIDTH - WBPCENTERWIDTH
WBPYTRY = WBPCENTERHEIGHT - WBPTRYHEIGHT
WBPXDIFF = WBPX - WBPXTRY
WBPYDIFF = WBPY - WBPYTRY

RETURN

WBPMOVEDONE:

'THE SEMIPERIMETER IS THE DISTANCE BETWEEN THE SPOOLS PLUS
'THE LENGTH OF EACH STRING DIVIDED BY TWO
'THE AREA OF THE TRIANGLE IS THE SQUARE ROOT OF
'THE SEMIPERIMETER TIMES THE DIFFERENCE BETWEEN THE
'SEMIPERIMETER AND EACH SIDE
'WBPLEFT = LENGTH OF LEFT STRING
'WBPRIGHT = LENGTH OF RIGHT STRING
'WBPBASE = DISTANCE BETWEEN SPOOLS

WBPS = (WBPLEFT + WBPRIGHT + WBPBASE) / 2
WBPAREA = SQR(WBPS * (WBPS - WBPLEFT) * (WBPS - WBPRIGHT) * (WBPS - WBPBASE))
'VERTICAL DISTANCE FROM SPOOLS DOWN TO THE PEN IS
WBPHEIGHT = (WBPAREA * 2) / WBPBASE
WBPWIDTH = SQR((WBPLEFT * WBPLEFT) - (WBPHEIGHT * WBPHEIGHT))

WBPXNOW = WBPWIDTH - WBPCENTERWIDTH
WBPYNOW = WBPCENTERHEIGHT - WBPHEIGHT
'FOR WBPZ = 1 TO 10000: NEXT

RETURN

WBP0:
PRINT #1, 0
WBPLEFT = WBPLEFT - (WBPSTEPSIZE)
WBPRIGHT = WBPRIGHT - (WBPSTEPSIZE)
RETURN

WBP1:
PRINT #1, 1
WBPRIGHT = WBPRIGHT - (WBPSTEPSIZE)
RETURN

WBP2:
PRINT #1, 2
WBPLEFT = WBPLEFT + (WBPSTEPSIZE)
WBPRIGHT = WBPRIGHT - (WBPSTEPSIZE)
RETURN

WBP3:
PRINT #1, 3
WBPLEFT = WBPLEFT + (WBPSTEPSIZE)
RETURN

WBP4:
PRINT #1, 4
WBPLEFT = WBPLEFT + (WBPSTEPSIZE)
WBPRIGHT = WBPRIGHT + (WBPSTEPSIZE)
RETURN

WBP5:
PRINT #1, 5
WBPRIGHT = WBPRIGHT + (WBPSTEPSIZE)
RETURN

WBP6:
PRINT #1, 6
WBPLEFT = WBPLEFT - (WBPSTEPSIZE)
WBPRIGHT = WBPRIGHT + (WBPSTEPSIZE)
RETURN

WBP7:
PRINT #1, 7
WBPLEFT = WBPLEFT - (WBPSTEPSIZE)
RETURN


WBPSETUP:

'48 STEPS IS ONE TURN OF THE MOTOR
'20 TURNS IS ABOUT TWO FEET
'468 STEPS IS ABOUT 12 INCHES, 39 STEPS PER INCH
'THE WHITE PART OF THE BOARD IS 34.5 INCHES WIDE * 22.5 INCHES TALL
'THE CENTER OF THE SPOOLS ARE 33.875 INCHES APART
'THE CENTER OF THE SPOOLS ARE ABOUT .75 INCHES ABOVE THE BOARD
'IF BOTH STRINGS ARE 24 INCHES LONG
'THE PEN IS ABOUT 16.5 INCHES BELOW THE TOP OF THE BOARD
'THE SEMIPERIMETER IS THE DISTANCE BETWEEN THE SPOOLS PLUS
'THE LENGTH OF EACH STRING DIVIDED BY TWO
'THE AREA OF THE TRIANGLE IS THE SQUARE ROOT OF
'THE SEMIPERIMETER TIMES THE DIFFERENCE BETWEEN THE
'SEMIPERIMETER AND EACH SIDE
'WBPLEFT = LENGTH OF LEFT STRING
'WBPRIGHT = LENGTH OF RIGHT STRING
'WBPBASE = DISTANCE BETWEEN SPOOLS
'WBPS=(WBPLEFT + WBPRIGHT + WBPBASE)/2
'WBPAREA = SQR(WBPS*(WBPS-WBPLEFT)*(WBPS-WBPRIGHT)*(WBPS-WBPBASE))
'VERTICAL DISTANCE FROM SPOOLS DOWN TO THE PEN IS
'WBPHEIGHT = (WBPAREA*2)/WBPBASE
'WBPBASE = 33.875
'50.5 INCHES BETWEEN THE CUPHOOKS
'32 INCHES FROM THE LEFT HOOK TO THE CENTER OF THE BOARD

WBPSTEPSIZE = 11.125 / 500


PRINT "POSITION THE PEN AT THE CENTER OF THE BOARD."
INPUT "PRESS ENTER"; N$

INPUT "ENTER THE LENGTH OF THE LEFT STRING "; WBPLEFT
'WBPLEFT = 28.5
INPUT "ENTER THE LENGTH OF THE RIGHT STRING "; WBPRIGHT
'WBPRIGHT = 27.75
'INPUT "ENTER THE DISTANCE BETWEEN SPOOL CENTERS "; WBPBASE
'WBPBASE = 33.875
WBPBASE = 50.5
'INPUT "ENTER THE HEIGHT OF THE WHITE AREA "; WBPTALL
WBPTALL = 22.5
'INPUT "ENTER THE WIDTH OF THE WHITE AREA "; WBPWIDE
WBPWIDE = 34.5
WBPS = (WBPLEFT + WBPRIGHT + WBPBASE) / 2
WBPAREA = SQR(WBPS * (WBPS - WBPLEFT) * (WBPS - WBPRIGHT) * (WBPS - WBPBASE))
WBPHEIGHT = (WBPAREA * 2) / WBPBASE
WBPCENTERHEIGHT = WBPHEIGHT
PRINT "WBPCENTERHEIGHT="; WBPCENTERHEIGHT
WBPCENTERWIDTH = SQR((WBPLEFT * WBPLEFT) - (WBPCENTERHEIGHT * WBPCENTERHEIGHT))

PRINT "WBPCENTERWIDTH="; WBPCENTERWIDTH

RETURN

Wednesday, April 13, 2011

Arduino

Last week I converted the doodler to use an Arduino.
There is a bit of a learning curve to converting my programming from quickbasic to the Arduino language which is a form of C.
It runs a lot faster this way but I think it is too fast. I will add some delay and see if it works better. I displayed it at a homeschool talent night and got some good comments. They allowed dads to bring projects too.

doodler formulas

Here are some of the formulas I use to calculate where to draw.

doodler formulas

I need to figure out where the pen is.
I keep track of how long each string is with each step it takes.
I also know the distance between the cuphooks the strings go over.
That defines a triangle.
There is a neat formula for calculating the area of a triangle.
Add up all three sides, that is the perimeter. Half of that is the semi-perimeter.
Find the difference between each side and the semi-perimeter.
Multiply those three differences times each other and times the semi-perimeter.
Take the square root of that and you have the area.
Once you have the area you can figure out the height of the triangle, (area*2)/base=height.

WBPLEFT is the length of the left string.
WBPRIGHT is the length of the right string.
WBPBASE is the distance between the cuphooks.
WBPS is the semi-perimeter.
WBPS = (WBPLEFT + WBPRIGHT + WBPBASE) / 2
WBPAREA = SQR(WBPS * (WBPS - WBPLEFT) * (WBPS - WBPRIGHT) * (WBPS - WBPBASE))
WBPHEIGHT is the distance down from the cuphooks to the pen.
WBPHEIGHT = (WBPAREA * 2) / WBPBASE
WBPWIDTH is the distance from the left hook to the pen.
WBPWIDTH = SQR((WBPLEFT * WBPLEFT) - (WBPHEIGHT * WBPHEIGHT))
The center of my board is 0,0 so this calculates X and Y for where the pen is.
Units is inches.
WBPXNOW = WBPWIDTH - WBPCENTERWIDTH
WBPYNOW = WBPCENTERHEIGHT - WBPHEIGHT
WBPCENTERWIDTH is the distance from the left hook to the center of the board.
WBPHEIGHT is the distance from the hooks down to the center of the board.

Tuesday, April 12, 2011

some one elses idea

here is a link to a similar idea someone is doing with spray paint.
http://geekphysical.blogspot.com/2011/02/giant-printer.html

Friday, April 1, 2011