# CNC Dividing Head - PICAXE microcontroller



## cfellows (Dec 3, 2009)

After reading about Marv's plunge into the Basic Stamp world, I started doing some extensive research on microcontrollers. I've found a pretty good amount of information and some real good project examples on the PICAXE microcontroller. The most amazing bit is the their free (yes, it's really free) programming editor and simulator. It really works slick. It provides an environent where you can specify which microcontroller your using, then you can write your Basic code, and then run it in simulation mode. It shows the steps through the program and also provides a display of the microchip with the state of it's pins and the contents of the variables. You can click on the input pins to change their state and watch how your program handles these changes. This is a great way to get your feet wet with microcontrollers, write actual programs, and see how the microcontroller behaves, all at no cost.

Here is a picture of the editor with my dividing head program and the microcontroller showing pin states and variables:







Extensive documentation and information is available at the PICAXE website which is located in the UK. 

http://www.rev-ed.co.uk/picaxe/ 

I just bought a kit from this fellow http://www.phanderson.com/picaxe/index.html which includes everything you need to get started, including the interface to your PC for download the programs to your microcontroller and the power supply. Cost with shipping was about $30. I haven't received the kit yet and have been doing all my learning with the free editor / simulator.

This is my first foray into the microcontroller world. I was a programmer for many ears, but that was many years ago. I was able to pick this back up pretty quickly and I'm pleased with how fast I was able to get something running. 

There are several distributers located in the US and I just bought a kit from a


----------



## mklotz (Dec 3, 2009)

Chuck,

I hope you'll take the time to take a couple of snaps of the evaluation kit when you get it and then, after using it a while, write up your impression of it. I'm sure that I'm not the only one who would like to hear a hands-on evaluation from a trusted source.


----------



## cfellows (Dec 7, 2009)

This microcontroller stuff is kind of addicting. I've gotten sidetracked a little bit by another microcontroller system called Arduino. It's an open source microcontroller prototyping platform and there is ton's of stuff on the internet about it. Here's a link to their main website and a picture of the microcontroller board that costs about $30 at a lot of different electronics & Hobbyists websites.

http://www.arduino.cc/

Still waiting for my PICAXE circuit to arrive in the mail, but have been doing lots of playing with the PICAXE programming editor and simulator. Got my dividing head program pretty well debugged and working. This is fun! My old head is spinning with the possibilities.

Chuck


----------



## kuhncw (Dec 7, 2009)

Hi Chuck,

This is an interesting topic and I'm glad to see it pop up. I played with some PICBasic and hardware from Micro Engineering Labs, Inc. several years ago. I got sidetracked though and didn't keep working with it. 

You probably know this, but Nuts & Volts Magazine runs quite a few aricles on microcontrollers. Joe Pardue writes the Smiley's Workshop series and he is currently writing about the Arduino. www.smileymicros.com

Nuts and Volts also runs a column on the Picaxe.

Please keep us tuned in on your adventures with the micro controllers.

Regards,

Chuck Kuhn


----------



## black85vette (Dec 7, 2009)

Chuck; here is another Pic based project that may be interesting to you. It reads the digital caliper readout and then uses a Pic controller to output the data as an RS-232 stream. So, you can get the input into any PC based application.

This is another great use of Pic and Stamp based systems. Just use them as an interface to a PC and then use the power of the PC for serious applications.

http://www.compendiumarcana.com/caliper/


----------



## cfellows (Dec 7, 2009)

kuhncw  said:
			
		

> Hi Chuck,
> 
> This is an interesting topic and I'm glad to see it pop up. I played with some PICBasic and hardware from Micro Engineering Labs, Inc. several years ago. I got sidetracked though and didn't keep working with it.
> 
> ...



Hey Chuck, funny you should mention Nuts & Volts. I just subscribed to it a couple of months ago and was reading through my first copy last night. That's how I ran across the Arduino stuff. Interesting magazine. 

Chuck


----------



## cfellows (Dec 7, 2009)

black85vette  said:
			
		

> Chuck; here is another Pic based project that may be interesting to you. It reads the digital caliper readout and then uses a Pic controller to output the data as an RS-232 stream. So, you can get the input into any PC based application.
> 
> This is another great use of Pic and Stamp based systems. Just use them as an interface to a PC and then use the power of the PC for serious applications.
> 
> http://www.compendiumarcana.com/caliper/



Yeah, this is another area of interest. I really want a DRO for my milling machine but just can't bring myself to mortgage the house to buy one. Not to mention the grief I would get from the Chief Financial Officer!  : So, I will be following up on this project as soon as I get a little more comfortable with the microcontroller.

Chuck


----------



## LADmachining (Dec 7, 2009)

"Yeah, this is another area of interest. I really want a DRO for my milling machine but just can't bring myself to mortgage the house to buy one. Not to mention the grief I would get from the Chief Financial Officer!  Roll Eyes So, I will be following up on this project as soon as I get a little more comfortable with the microcontroller."

Chuck,

Are you aware of the Shumatech DRO?

www.shumatech.com

It is available as a kit that you build yourself, and either buy a pre-programmed microcontroller, or program your own.

I have built one, and find it invaluable. Much cheaper than a ready-built unit, with most of the functionality!

Regards,

Anthony


----------



## cfellows (Dec 7, 2009)

LADmachining  said:
			
		

> Are you aware of the Shumatech DRO?
> 
> www.shumatech.com
> 
> ...



Hi Anthony,

Yeah, I've seen their units and have thought long and hard about buying one. May wind up doing that since everything I've read about rolling-your-own DRO's isn't very positive. However, part of the adventure is the journey and if I have enough projects lined up, it will keep me motivated to learn about microcontrollers.

Chuck


----------



## Noitoen (Dec 7, 2009)

cfellows  said:
			
		

> The most amazing bit is the their free (yes, it's really free) programming editor and simulator.



But you still have to buy the Pics from them or not?

I like the the pic a lot and have made some useful projects but the programming language I use is assembly together with the Mplab from Microchip also free with a ICD2 programmer. It would be nice, sometimes, to program in basic. My main project is a constant pressure controller fo diesel irrigation pumps with automatic engine start and servo speed control and engine monitor/protection with a 18F452, 16x4 line LCD display, 16 key keyboard, stepper motor servo actuator (controlled by a 12f675), analog 250 psi pressure sensor and speed pick-up. The assembly program's listing is 120 A4 pages long.


----------



## cfellows (Dec 7, 2009)

Got my PICAXE development board from PH Anderson in the mail today. Plugged it in and it seemed to do everything as advertised out of the box. Of course, all that amounted to was a flashing red LED and solidly lit green one.

You can see the components in the picture. It has a large solderless bread-board with a PICAXE 18m microcontroller on it. The printed circuit board is a USB serial interface to the PC. Also included is the USB cable. The USB interface provides power to the development board. It also provides a serial link for downloading programs from the PC to the microcontroller and an uplink for displaying debugging data on the PC screen.






The PICAXE microcontrollers come in a variety of sizes, beginning with the 8 pin, 08M and going up to the 40 pin, 40x. Even the largest PICAXE is less than $10. My kit came with the 18m, which, as you might guess, has 18 pins. It has 14, 1 byte registers which also double as 2 byte, word sized registers. All math must be done in these registes. Sound familiar to you assembler programmers? There are also 40 bytes of memory for storing variables, constants, etc. And, finally, there is enough program memory for 255 lines of code. Or maybe it's 255 bytes for your code. My dividing head program consumed about 120 of the 255 available units, whatever they are.


The chip is programmed in a very simple BASIC language. Nothin fancy here, but easy and functional. As far as math goes, positive integers only. No decimals and no negative numbers. Try to go negative and you wind up with a very large positive number, 255 in the case of a byte and 65535 in the case of a word. Testing for < 0 doesn't produce an error but it doesn't work, either!

Chuck


----------



## cfellows (Dec 7, 2009)

Noitoen  said:
			
		

> But you still have to buy the Pics from them or not?
> 
> I like the the pic a lot and have made some useful projects but the programming language I use is assembly together with the Mplab from Microchip also free with a ICD2 programmer. It would be nice, sometimes, to program in basic. My main project is a constant pressure controller fo diesel irrigation pumps with automatic engine start and servo speed control and engine monitor/protection with a 18F452, 16x4 line LCD display, 16 key keyboard, stepper motor servo actuator (controlled by a 12f675), analog 250 psi pressure sensor and speed pick-up. The assembly program's listing is 120 A4 pages long.



You can buy the microcontroller chips from them or a number of other places on the internet. Several U.S. based companies sell them. The chips are cheap, all under $10 US and the supporting circuitry is minimal. However, the PICAXE is limited in the size of programs and data they will hold. For larger programs, the Arduino would be better suited. Or Microchip with external memory and supporting cuicuitry. But now you're getting complicated!  

By the way, the PICAXE's are made with microcontrollers from PIC. 

Chuck


----------



## Noitoen (Dec 7, 2009)

What I would like to know is, can you use "of the shelf" pics or do Picaxe pics come programmed with some kind of proprietary "firmware or bootloader"? I know where to get "normal" pics.


----------



## cfellows (Dec 7, 2009)

The PICAXE comes with proprietary firmware already loaded. This includes their Basic interpreter and their own bootstrap program.

Chuck


----------



## cfellows (Dec 7, 2009)

[email protected]! Now I've discovered the BasicX. Just like the Basic Stamp but way more powerful and cheaper. I'm so confused...

Chuck


----------



## cfellows (Dec 21, 2009)

I've selected a stepper motor, from my motley collection, that has voltage and current requirements in line with my controller. The stepper is from a telescope tracking system. It's a bipolar stepper, with 48 steps per motor revolution. It's geared at 30 : 1 to the output shaft giving an overall number of 1440 steps per output shaft revolution.

I was thinking that would give me very nice resolution on the output divisions, but now I'm running into some of the restrictions of microcontrollers. First off, calculations are performed using integer math, so no decimals. Second of all, I'm restricted to 16 bit registers so I can only deal with number up to 65536.

Too accommodate rounding issues, I was planning to calculate the rotor position each time like so:

 absolute position = division number * 1440 / number of divisions.

Example: If I have specified 49 divisions and I am on position 47, it looks like this:

      abs pos = 47 * 1440 = 67680 / 49 = 1381

Oops, my intermediate result of 67680 is larger than 65536... That's a Problem! So, for the moment, I've decided to use 360 steps per rotation and simply multiply the calculated steps by 4. This reduces my resolution but I think it will be OK. 

Anyone got any ideas how to fake the math with these little processors?

Chuck


----------



## joeby (Dec 22, 2009)

Chuck,

 I know squat about microcontrollers; but would it be possible to break down your movements into 45 degree increments for the calculations? Then 47 degrees would be 45 in one movement and 2 in the next. That would keep the step count under your limitation. It would likely result in an annoying pause in the movement of the rotary table, not good if you are interested in continuous 4 axis machining, but maybe acceptable for a positioning axis?

Basically thinking out loud.

Kevin


----------



## black85vette (Dec 22, 2009)

cfellows  said:
			
		

> Anyone got any ideas how to fake the math with these little processors? Chuck



Chuck, I have not worked with the PIC family but I have done some things with limited processors. Just a general idea, but sometimes you can set a variable or flag to a value that can be checked and used to branch in your program. Have the program check and when the register is going over 65536 set a flag = 1, yes, true or whatever and then reset the register to 0. From that point on when you do math check the flag to see if the register is its actual value or its value + 65536. Kind of a PITA but you work with what you have.  

Likely there is a much more elegant solution from a real programmer, but that is my first thought without knowing the PIC.


----------



## kvom (Dec 22, 2009)

> abs pos = 47 * 1440 = 67680 / 49 = 1381



If you calculate 47*144/49, then multiply by 10 you'll get 1380. That will be off by 1.

47*360/49 = 345; 345*4 = 1380. In this case it's off by 1, but could be off more depending on the factors.

Assume position 20 going to 49, then

20*1440 = 28800/49 = 587 (no overflow)

20*360 = 7200/49 = 146 * 4 = 584 (so less accurate by 3 divisions).

So an algorithm might look like this:

 abs_pos = curr_pos * 1440 / num_div;
 if (abs_pos < 0)
 {
  abs_pos = curr_pos *144/num_div;
  abs_pos = abs_pos * 10;
 }


----------



## zeeprogrammer (Dec 22, 2009)

I'm not familiar with your system. But here's some thoughts...

If you write in assembly you can use larger numbers. (I was surprised that you're limited to a 16 bit value). More difficult if you have to do any multiplication or division.

You can use two sets of variables...one to count revolutions and one to count the pulses in a revolution (like seconds, minutes, hours).

As a side note...the fact that integer math is used does not mean fractional math is not possible. Use a virtual decimal point. The more bits behind the virtual decimal point then the more resolution you have. For example, if we use an 8-bit number and let the virtual decimal point be 2 in from the right...then 4.5 would be represented by

00010010
000100.10
000100 is 4
.10 is 1/2

4.75 would be

00010011
000100.11
000100 is 4
.11 is 1/2 + 1/4

Moving the virtual decimal point left by one gets you 1/8
.101 is 1/2 + 1/8

All at the loss of range if you can't use more bytes.
(Basically you've multiplied everything by a number large enough to keep significant digits...then you divide by the number to get the final result).
This 'trick' is commonly used for speed in embedded systems when you need to use fractions but don't want to spend the money for hardware to support floating point.


----------



## shred (Dec 22, 2009)

You can also get an extra bit sometimes by using the carry flag, assuming this processor has one and shift/rotate instructions that go through it. Shift left through the carry bit gives you *2 with the most significant bit hanging out in the carry, and shift-right does the same thing for divide but with the least significant bit there.

There are also vast math routines available for tiny micros like this, but maybe more effort and difficulty than needed.

Aw rats, I just remembered you're using some higher level language to program these. I have no idea if they use the carry bit sensibly or give you access to shift commands.

Edit: is there some flag to let you know you've overflowed? Then you could just handle that case as two moves.


----------



## cfellows (Dec 22, 2009)

Don't know about the carry flag. Haven't gotten that far yet. My current ploy is to see if there is a way to do this without using big numbers. I looking at using the modulus function and trying to do something with remainders. In the end, I may just restrict it to 45 divisions or less. Got the capacity to handle that with no issues. However, it would be nice to go up to 120 or more, just to say I could!

Chuck


----------



## Noitoen (Dec 22, 2009)

Since there's always a maximum (practical) amount of divisions you will need, the best way to do it on a pic micro is to use a table, pre-calculated with the steps needed for each and use the program memory to store these values. They do this in the automotive industry's ECU's to avoid engine calculations "on the fly"


----------



## cfellows (Dec 22, 2009)

Noitoen  said:
			
		

> Since there's always a maximum (practical) amount of divisions you will need, the best way to do it on a pic micro is to use a table, pre-calculated with the steps needed for each and use the program memory to store these values. They do this in the automotive industry's ECU's to avoid engine calculations "on the fly"



That would be pretty good option if I had some memory to speak of. The little PICAXE only has 48 words (or bytes) of memory. I may be trying to do to much with too little. This is primarily a learning exercise for me, so maybe I should be content with up to 45 divisions. But, I'm always up for a challenge. I probably need someone like Marv to come along and say, "Sorry, Chuck, but the physics involved dictate that it can't be done!"  

Chuck


----------



## Noitoen (Dec 22, 2009)

I'm not talking about ordinary ram. Tables can be stored in program memory, but then I program in assembly and I don't know if Picaxe has instructions for it. Normal basic language has and usually it is listed as "data" at the end of the program. Some of the higher level languages also let you use assembly instructions so you could also implement this.


----------



## Kermit (Dec 22, 2009)

Look for a SET or DECLARE function. Something that lets you assign a value to a variable. 
Then just "decalre" a whole bunch of variables in the program for use in a matrix look up table.

Is that what you are getting at Noitoen?


----------



## Noitoen (Dec 22, 2009)

I've just looked at a Picaxe manual that says "on X1 and X2 parts 256 bytes of program memory can be "reserved" as a lookup table. See table/readtable in part 2 of the manual for details" (page 44)

www.rev-ed.co.uk/docs/picaxe_manual1.pdf


----------



## shred (Dec 22, 2009)

Does the PicAxe support modulo arithmetic?


----------



## cfellows (Dec 22, 2009)

Shred, yes, the picaxe basic language does have a modulo command. You can either use % or // as in 5 // 3 = 2. The PICAXE also always rounds up, so 3 / 5 = 1. It's a very simple basic and I expect each basic command translates to no more than 4 or 5 assembler commands. Also, there are no variables, only registers. You can assign a symbol name to a register so it looks like a variable, but you are limited to the bits that the register can hold. Basic commands like "Let B0 = B0 +1" or "Let B0 = B0 + B1" are valid. However, the following command, which combines more than 1 arithmetic operation, is not valid: "Let B0 = B1 + B2 / 3". You can't do an arithmetic command which requires an intermediate result. It's pretty unsophisticated.

On the positive side, it does have some pretty sophisticated input / output commands. There is Serial Read, Serial Write, ReadADC which reads an analog signal and converts it to digital, and a PWM output command which will output a Pulse Width Modified signal where you can specify the duty cycle for driving a variable speed DC motor.

Noitoen, if I understand your suggestion right, I would need a set of steps for each division I potentially wanted to use. For example, 47 divisions to a circle would require a table of 47 step values, 46 would require a separate table of 46 step values, 41 would require still another table with 41 step values, etc. What I'm aiming at doing, and it may not be within my grasp to do it, is be able to do any number of divisions from 1 (ok, 2) to 120


----------



## Noitoen (Dec 22, 2009)

I will think a while on the matter in particular and will come back to you tomorrow.

Just tell me what is your dividing head's gear ratio and stepper motor's steps per revolution.


----------



## cfellows (Dec 22, 2009)

Noiten, the stepper motor has 48 steps per revolution and the gear ratio is 30 to 1 giving an overall step count of 1440 per revolution.

Thx...
Chuck


----------



## Noitoen (Dec 23, 2009)

Ok, I've looked at the problem and came to the following conclusion: 

To work without decimal point, and since the resolution of you stepper motor is only 48 steps or 96 half steps, your maximum none accumulative precision or occasional offset error will be 0,25º or 0,125º using half steps. If you were using a 200 step motor this error would only be 0,06º or 0,03º in half steps.

The first thing you have to do is, create a routine with a counter that allows you to move your motor any amount of steps from 0 to 1440, thus when you say go to 720, the dividing head will move a half turn. If you say go to 0 then it goes back to the original position and so on. If you can accomplish this the rest will be easy.

Can PicAxe multiply and divide 16 bit numbers? Can you handle LCD displays and switches?

You wont need any tables, all you have to do is, define the number of divisions you want, start a separate counter from 1 to the desired division and call this the "work division", now multiply this number by 1440 and divide by the divisions. The result will be the "go to" number for the motor. This is better than assuming a fixed number for the division because the fractional error will not accumulate. 

If you don't understand what I mean, I'll explain it with an example.


----------



## mklotz (Dec 23, 2009)

Chuck,

I'm confused. Are you intending to drive the dividing table directly with the stepper output? On all the automated DHs I've seen, the stepper drives the worm of a normal rotary table so one has the added error reduction of the worm gear setup.

For example, suppose the RT has a typical 90:1 reduction worm gear setup. Then one rotation of the handwheel is 4 deg of table movement or 4*3600 = 14400 arc seconds.
With your stepper driving the handwheel, its 1440 steps would then yield a table movement of 10 arc seconds per stepper step.

With that sort of resolution, a lost step or two would be inconsequential - 10 arc seconds is a mighty small error.

If I've completely misunderstood (something at which I'm a master), just ignore this post.


----------



## Kermit (Dec 23, 2009)

Wouldn't the stepper and the rotary table drive have to be synced somehow at a primary 'zero' spot and then rezeroed again and again? Meaning the continued plus and minus operations and the backlash of the mechanical drive train in the rotary table, could combine and leave you several degrees 'off' at the end of executing a long program without doing it.


Would driving the rotary in only one direction keep the gear backlash from affecting the stepper count?  Would that depend on the accuracy of the gearing?

Lots of questions,
Kermit


----------



## Noitoen (Dec 23, 2009)

The way I understood is that he has a dividing head, small I suppose, with a 1:30 worm gear to be coupled to a 48 step motor giving a 0,25º resolution. If the drive is made the way I explained then there is no need for backlash compensation since the the motor can be moved anywhere to any "home" position and the the internal counter zeroed. Now the first move will take it to the first "machining" position, the second to the second and so on. Since its always moving in the same direction there is no need for backlash compensation. For a 6 side division the motor would move fro 0 to position 240 then 480, 720, 960, 1200 and finally 1440 (equals 0). No error in division here.

The first of the explanation was to make the system able to "go to" any desired position and not the function itself. Once his able to get the table to move to any possible position then the rest is easy.


----------



## cfellows (Dec 23, 2009)

Sorry for all the confusion. The stepper motor is actually a stepper gear motor with the gear train built in. The output shaft of the gearmotor has a 1 : 30 ratio with the motor. The motor, itself, has 48 steps per revolution. Thus, the output shaft has a resolution of 1440 : 1. This motor was used in a telescope tracking system. 1440 just happens to be the number of seconds in a 24 hour day... not that that has anything to do with this application. Backlash on this little gearmotor is neglible.

Marv, I'm not planning to drive a rotary table, but rather a dividing head. It will run in one direction, at least initially, or until my programming skills with the controller allow me to make it more sophisticated. My initial application is simply to index gear blanks for cutting teeth.

Again, this is an educational endeavor, so I'm mostly exploring the capabilities of the microcontroller, renewing my programming skills after probably 20 years of being idle, and improving my knowledge of stepper and controller electronics.

I do appreciate everyone's input. I'm certainly no math whiz!

Chuck


----------



## Noitoen (Dec 23, 2009)

In that case you get better precision and have to multiply 1440 by the dividing head's ratio.


----------



## shred (Dec 23, 2009)

I think the question becomes "where do you want the error?" All at the end of 360 or spread throughout the divisions?

I'd do something like StepsPerRotation MOD NeededDivisions. Get a zero and you're golden.  Any other value is the number of extra steps you have to sneak in somewhere along the way. There are various strategies on adding in extra steps here and there; spread them throughout the range, put them all at the end, every quarter, etc, but none of them are perfect.

Say 48 divisions-- 1440 MOD 48 = 0. Thus each division is exactly 1440 / 48 = 30 steps. 

Say 49 divisions. 1440 MOD 49 = 19. Each division is 1440 / 49 = 29 steps per division, but now you've got 19 steps to wedge in throughout the 360 degrees as desired. Stick them all at the end and the divisions will all except one be equally spaced, put them elsewhere and divisions won't be equally spaced, but there won't be one that's way out.


----------



## cfellows (Dec 24, 2009)

Shred, it's funny how when I get into math mode, I don't think about the physical side of things. For example, I had never thought of the remainder as the count of additional steps that need to be inserted in addition to the steps per division. I'll have to think about this angle for a bit and see if I can come up with something.

Chuck


----------



## mklotz (Dec 24, 2009)

Chuck,

Damn you! I couldn't get your problem out of my head all last night. While showering this morning I worked out a candidate scheme that's outlined below.

I haven't analyzed it exhaustively so it may be flawed but I'll offer it up for your consideration. Even if it's unworkable, it may trigger an ah-ha in your mind that leads to a workable scheme.

The basic idea is to compute the number of steps for one division, I, and then divide this number into its integer and fractional parts. These parts are stored separately in a table in the m/c and used to reconstruct the number of steps needed for the current division number, K.

In my example, I've used a scaling factor of 1000 to make the algorithm operation transparent. Since we need to divide by this factor in the m/c, it may make more sense to use a factor that's a power of two so division can be accomplished by shifting.

Workable or not, the scheme has a serious flaw in that it's limited to 65 divisions before you run into overflow problems. At the moment I can't think of any clever way around that. Maybe you'll have an insight.



Exterior to the m/c compute:
(numbers in braces {} are a numerical example}

-----------------------------------------------

N = number of divisions desired {49}

I = 1440/N = steps/division {1440/49 = 29.387...}

A = int_ {29}

B = int[1000*(I-A)] {int[1000*(29.387-29)] = 387}

-----------------------------------------------

Store A and B for each value of N in a table in the m/c

Now to calculate the number of steps in the m/c...


-----------------------------------------------

K = step number (1 <= K <= N) {47}

S = number of steps = K*A + K*B/1000 {S = 47*29 + 47*387/1000 = 1363 + 18189/1000
	= 1363 + 18 = 1381}

-----------------------------------------------

The value of B cannot exceed 999 and the maximum value of K is N so

L = K*B <= N*999

L cannot exceed 65535 to avoid overflow. This implies that the N < 65535/999 = 65 so this scheme would not work for N > 65. In gear cutting that could be a serious limitation.

_


----------



## shred (Dec 24, 2009)

You could do a couple more iterations of MOD and divide, sort of like a leap-year algorithm to figure out when to insert 'leap steps', but I'm thinking there ought to be a cleverer way. Marv may be on to something with his approach as well.. have to think on that.


----------



## mklotz (Dec 24, 2009)

To test my thinking, I wrote a brief program using 1024 rather than 1000 as the scale factor (sf). In the results below, each line is:

K K*I K*A K*B/sf S

where the variables are defined in my previous post.

Examining the output for N=49, you can see that it's always true that:

S = int[K*I]

as desired.

Unfortunately, there are cases (e.g. N=55) where there can be a one count error due to the rounding inherent in the finite size of the scale factor. While I haven't proved it, I'm fairly sure that that error can never exceed one count, however. Still, that's 0.25 deg which is too much for gear cutting. On top of that you've got the fact that truncating rather than rounding can introduce nearly another count of error bringing the total possible error to 0.5 deg.

You're welcome to a copy of the program code if you want it although it's probably just as easy to write it yourself.

I've got to go do Xmas stuff now...



```
N=49
I,A,B = 29.387755, 29, 397

0 0.000000 0 0 0
1 29.387755 29 0 29
2 58.775510 58 0 58
3 88.163265 87 1 88
4 117.551020 116 1 117
5 146.938776 145 1 146
6 176.326531 174 2 176
7 205.714286 203 2 205
8 235.102041 232 3 235
9 264.489796 261 3 264
10 293.877551 290 3 293
11 323.265306 319 4 323
12 352.653061 348 4 352
13 382.040816 377 5 382
14 411.428571 406 5 411
15 440.816327 435 5 440
16 470.204082 464 6 470
17 499.591837 493 6 499
18 528.979592 522 6 528
19 558.367347 551 7 558
20 587.755102 580 7 587
21 617.142857 609 8 617
22 646.530612 638 8 646
23 675.918367 667 8 675
24 705.306122 696 9 705
25 734.693878 725 9 734
26 764.081633 754 10 764
27 793.469388 783 10 793
28 822.857143 812 10 822
29 852.244898 841 11 852
30 881.632653 870 11 881
31 911.020408 899 12 911
32 940.408163 928 12 940
33 969.795918 957 12 969
34 999.183673 986 13 999
35 1028.571429 1015 13 1028
36 1057.959184 1044 13 1057
37 1087.346939 1073 14 1087
38 1116.734694 1102 14 1116
39 1146.122449 1131 15 1146
40 1175.510204 1160 15 1175
41 1204.897959 1189 15 1204
42 1234.285714 1218 16 1234
43 1263.673469 1247 16 1263
44 1293.061224 1276 17 1293
45 1322.448980 1305 17 1322
46 1351.836735 1334 17 1351
47 1381.224490 1363 18 1381
48 1410.612245 1392 18 1410
```


```
N = 55
I,A,B = 26.181818, 26, 186

0 0.000000 0 0 0
1 26.181818 26 0 26
2 52.363636 52 0 52
3 78.545455 78 0 78
4 104.727273 104 0 104
5 130.909091 130 0 130
6 157.090909 156 1 157
7 183.272727 182 1 183
8 209.454545 208 1 209
9 235.636364 234 1 235
10 261.818182 260 1 261
11 288.000000 286 1 287
error
12 314.181818 312 2 314
13 340.363636 338 2 340
14 366.545455 364 2 366
15 392.727273 390 2 392
16 418.909091 416 2 418
17 445.090909 442 3 445
18 471.272727 468 3 471
19 497.454545 494 3 497
20 523.636364 520 3 523
21 549.818182 546 3 549
22 576.000000 572 3 575
error
23 602.181818 598 4 602
24 628.363636 624 4 628
25 654.545455 650 4 654
26 680.727273 676 4 680
27 706.909091 702 4 706
28 733.090909 728 5 733
29 759.272727 754 5 759
30 785.454545 780 5 785
31 811.636364 806 5 811
32 837.818182 832 5 837
33 864.000000 858 5 863
error
34 890.181818 884 6 890
35 916.363636 910 6 916
36 942.545455 936 6 942
37 968.727273 962 6 968
38 994.909091 988 6 994
39 1021.090909 1014 7 1021
40 1047.272727 1040 7 1047
41 1073.454545 1066 7 1073
42 1099.636364 1092 7 1099
43 1125.818182 1118 7 1125
44 1152.000000 1144 7 1151
error
45 1178.181818 1170 8 1178
46 1204.363636 1196 8 1204
47 1230.545455 1222 8 1230
48 1256.727273 1248 8 1256
49 1282.909091 1274 8 1282
50 1309.090909 1300 9 1309
51 1335.272727 1326 9 1335
52 1361.454545 1352 9 1361
53 1387.636364 1378 9 1387
54 1413.818182 1404 9 1413
```


----------



## cfellows (Dec 24, 2009)

Thanks, Marv, I was hoping to pique your interest in this problem!  : To increase the resolution, I could half step the bipolar stepper. This would give me 2880 steps per revolution. If I was off 1 step one way or the other, this would reduce the error to 1/8th of a degree. Would that be accurate enough?

Chuck


----------



## Noitoen (Dec 25, 2009)

You all forgetting something here, the 1440 step will further be divided by the dividing head's reduction.

I have a small 1:50 worm gearbox that's waiting for a controller build for a 200 step motor, this would give a resolution of 10000 steps per rotation in whole steps and double that in half steps. That's 0,036/0,018º precision. I bet it would take some sophisticated measuring equipment to find that error in a part.


----------



## cfellows (Dec 25, 2009)

Noitoen  said:
			
		

> You all forgetting something here, the 1440 step will further be divided by the dividing head's reduction.
> 
> I have a small 1:50 worm gearbox that's waiting for a controller build for a 200 step motor, this would give a resolution of 10000 steps per rotation in whole steps and double that in half steps. That's 0,036/0,018º precision. I bet it would take some sophisticated measuring equipment to find that error in a part.



Not planning to use a dividing head with this setup. The shaft that holds the work will be driven directly with the stepper gearmotor output shaft, although I might use timing pulleys and a belt with some slight gear reduction, maybe another 2 or 3 :1.

Chuck


----------



## cfellows (Dec 25, 2009)

Marv, you came up with pretty much the same solution I was working toward yesterday before I got hauled off to the Christmas Festivities. I was multiplying the number of steps per division by 100, then dividing the remainder to get the decimal part of the steps per interval, the storing this "decimal" value in a different register. Hadn't worked out all the details yet on how to use that "decimal" number. Looks like your work will help me get there.

I'm probably losing sight of the fact that this is supposed to be a learning project to become familiar with microcontrollers, but I would like the final product to be useful!

Chuck


----------



## mklotz (Jan 4, 2010)

Bump.

Chuck,

What, if anything, is happening with your divider math?

While in the shower this morning (I do my best work there) I realized that the problem I had had before with the N=55 case could be solved simply by rounding, as opposed to truncating, to obtain the value of B. I tried it and it works. (Since B is computed external to the m/c - in a conventional computer - working with its extended decimal value is legitimate.)

The next step will be to write a program to test it against all values of N up to some rational maximum to see if it eliminates errors everywhere.


----------



## TroyO (Aug 13, 2010)

A few pages back someone mentioned an LCD Display... they have ones that can be written to serially, can be handy in a lot of of ways.

http://www.sparkfun.com/commerce/product_info.php?products_id=9395

There's others out there, but I figured I'd post one. It makes it dead easy to use with most microcontrollers. (Arduino fan myself... been hacking together a lightsaber driver.)

[ame]http://www.youtube.com/watch?v=YowVHboU-oQ&feature=channel[/ame]


----------

