The Make It Last Build Series Newsletter, Build #3, Dispatch #6
In this final issue of the newsletter for our third build, we'll cover the math and basic code needed to get your Drawbot moving around a coordinate system. After this, the rest is up to you!
Upload photos of your work to the MAKE Flickr pool (tag them: makeitlast) and you'll be eligible for a chance to win cool prizes, as detailed at the Make It Last landing page. Deadline for entries is 11:59 PST on Thursday, March 3, 2011.
Onward!
Shawn
back to top The Parameters
You'll need a few measured values to calculate a drawing path. The following parameters will vary from installation to installation, and the exact stepper motor you used.
- Diameter of the spool
- Number of steps per rotation (read from the motor)
- Dimensions of entire drawing area
To get the spool diameter, measure with tailor's tape or a strip of paper. The number of steps per rotation can be read from the motor's datasheet. To get the dimensions of the field, measure the width from one eye hook to the other and the height from one eyehook to the y coordinate of the home position. As previously discussed, the home position is defined as the bottom midpoint of the area: coordinate (w/2, h).
Attentive readers will note that the diameter of the spool is actually a function, not a constant; it is constantly growing and shrinking as the monofilament is wound and unwound. However, there are so many other sources of error in the setup (mostly mechanical) that I have never bothered to make this refinement. It would be fairly easy to make the radius a function of the string lengths, however.
back to top The Coordinate System
8-bit microcontrollers can do floating-point math, but not very accurately. In order to express all of the math using only integers, the drawing grid will be expressed in terms of steps. A 1x1 grid unit will be relative to the step length of the particular motor. The step length is the amount that the length of the string will increase or decrease with a one-step tug.
You can calculate the step length using the diameter of the spool and the step angle of the motor; for one revolution, the string will be lengthened or shortened by the circumference of the spool shaft. For example, if the motor has a .9 degree step angle (400 steps per rotation) and the diameter of the spool is 1 inch, the number of grid units per inch will be:
400 steps / (pi * d) = 142 steps per inch
All dimensions should be converted to grid units and rounded to the nearest integer when programming the Drawbot. The origin of the grid will be the upper left corner of the drawing field.
back to top Translating String Length to Coordinates
Because the marker will start from the same spot each time, we will know the starting length of string A and string B (in steps). As we turn each stepper motor, we simply add or subtract a step to that string length, depending on whether it is turning clockwise or counterclockwise. Looking at the diagram above, we know enough values to solve for the coordinate (x, y) given the length of A, B and the width and height of the drawing field. Using the Pythagorean Theorem:
a1= sqrt(pow(x, 2) + pow(y1, 2))
b1= sqrt(pow((w-x), 2) + pow(y, 2))
These are the equations to translate between a coordinate and the string lengths that correspond to that location.
back to top Moving to a Point
The simplest way to move from one point to another is to find the difference in the x and y directions and iterate, adding or subtracting steps until the final point is reached. The pseudocode for this operation would look like this:
moveTo x2, y2 from x1, y1:
calculate the desired final length of string A and string B from (x2, y2)
if (A2 > A1), set motor A to turn clockwise, otherwise set motor A to turn counterclockwise
if (B2 > B1), set motor A to turn counterclockwise, otherwise set motor A to turn clockwise
loop while ((A1 != A2) or (B1!=B2)):
{
if (A1!=A2), turn the stepper in the set direction
increment or decrement the step count
if (B1!=B2), turn the stepper in the set direction
increment or decrement the step count
}
However, this simple-minded moveto routine will only result in a straight line if the difference on both axes are the same, or if the difference on one axis is 0. Otherwise you'll end up with a curved approximation of a straight line as the pen will reach one correct axis coordinate before the other. It would seem easy to calculate the slope between the points and use that in the calculation, but we need to stick to integer math on our 8-bit microcontroller if we have any hope of doing the math accurately.
The solution is a time-tested method created for drawing lines on early raster displays called Bresenham's Algorithm. Bresenham's Algorithm is described in more detail on the Wikipedia page above.
back to top Setup and Calibration
Place the Sharpie in the binder clip (and the clip stabilizer, if you have one). The clip should hold the marker at a point about an inch away from the wall. You'll definitely have to fuss with it to get the mechanical properties worked out. The trick is to get the marker so it is perpendicular to the wall, and the strings parallel to the wall. The easiest way to get the drawbot to the home position is to wind the spools until it's at about (w/2, h). You'll find that running the drawbot is more art than science, and there's all sorts of error that propagates through the thing, from wall imperfections to inexact measurements. But the imperfections are part of what makes the final result interesting, too.
Once you've grabbed the sample drawing code, you should be ready to start modifying it and extending it to do all sorts of crazy algorithmic drawing. I've found that it is sometimes easier to model a drawing algorithm in Processing
first, rather than going through an elaborate debugging process in real time with the machine. The image at the top of this section is a mandala created in Processing that was "ported" to the Drawbot.
Post your finished projects to the MAKE Flickr pool (tagged: makeitlast). We can't wait to see what you come up with!
back to top Microchip Discount Code
Want to pick up a PIC programmer or development kit? As part of this series, Microchip is offering a 20% discount on the following development tools:
* PICkit3
* PICkit3 Debug Express
* ICD3 In-Circuit Debugger
* XLP 16-bit Development Board
* F1 Evaluation Platform
* F1 Evaluation Kit
If you've been thinking about getting started with PIC programming, this could be a great opportunity to get a good deal on a programmer. To request a discount code, send an email to makeitlast@makezine.com.
back to top |