MadModder

Gallery, Projects and General => Project Logs => Topic started by: raynerd on December 13, 2009, 04:42:03 PM

Title: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 13, 2009, 04:42:03 PM
Hi Guys

After the sucess and enjoyment of converting my Vertex rotary table to CNC, I thought I`d jump back onto the X-axis conversion. I did try this some time ago and it failed so I didn`t post anything but now looking back, the two shafts were not aligned, only a fraction out but it made the axis twist. Since then and during my rotab conversion, I learnt about Oldham couplings. So a fresh start on the build, a new connecting rod but the same motor mount. To be honest, the setup is going to be the "easy" bit, since it is a stepper motor I want the maximum control that I can get from it but I don`t want to be messing with computers and Mach3. This is where a little microprocessor, some coding and an a little circuit is going to come in which will run all this. Similar to what is running my rotab but I`m going to attempt to make this one !! I`m going to be making a start on that on Wednesday night so I`ll post updates then. As far as the rest of the setup goes, I`m now just waiting the Oldham couplings to joing the two shafts:

Stepper motor and motor mount fitted to the rear end of the X axis, great thing about this is that the handle stays on and no resistance in the motor when it is off means that it maintains full manual use.
(http://www.raynerd.co.uk/images/xaxis1.JPG)

This is the part that connects the lead screw of the x-axis to the motor shaft. The thin end will attach to the oldham coupling
(http://www.raynerd.co.uk/images/xaxis2.JPG)

This is looking into the end of the fat end. The bar going through the bore locks into the notch at the end of the lead screw and engages it.
(http://www.raynerd.co.uk/images/xaxis3.JPG)


Both parts held into position - the gap is the space for the Oldham coupling and spacer to sit.
(http://www.raynerd.co.uk/images/xaxis5.JPG)

More updates soon.... Oldham coupling, 0.25" to 6mm and spacer are on their way



Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: websterz on December 13, 2009, 04:51:45 PM
I must admit, I had never heard of an Oldham coupling before. Brilliant little piece of engineering, filed away in the dark recesses of my brain for later use.  :thumbup:
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 14, 2009, 03:02:43 AM
Webby, to be honest if you can engineer your part for a perfect fit then there is no problem using a straight link, however it does allow about 1mm of misalignment and so I need one!
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: John Rudd on December 14, 2009, 07:11:34 AM
Chris,

What size stepper motor are you using please?
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 14, 2009, 12:44:55 PM
Hi John

I`m using some stepper motors that came with a lathe I aquired a while ago. They are 270 oz/in (190 Ncm ??) wired bipolar, 3.75V and 2.8A. Not huge but they should be well enough for this conversion and at slow speeds, the one on my rotary table can drive through a cut so I should be OK. I had to buy a new driver so I purchased a Arc Euro 4.2A driver so if I do need to upgrade the motors the motor will still be OK.

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 16, 2009, 06:33:02 PM
Well it has gone slower than I thought but I spent most of my night with a tutor learning to programme in C. We then used a development board to programme the chip with the compiled code and tested our software.

The current function is as follows:

4 Buttons:  Left, Right, Stop, Limit Sets
Variable Resistor/Pot - Speed
2 LEDS - Motor Running, Set Limits

Functions:
Left - jogs motor left
Right - jogs motor right
Stop - kills motor
Speed - Variable Resistor pot varies speed
Limits - Turning the circuit on from a hard reset sets the position to Zero. Press 'limit' button, light comes on. Move left & right until at position. Press 'mem' light starts to flash. Press left or right to set that limit, light goes out and the user can then press left and right to move between these limits.


So this was all coded in, complied and programmed onto the chip. A test board looks like this:

(http://www.mikroe.com/en/tools/easypic5/gallery/easypic5_550_3.jpg)

So basically ALL the buttons are on there and the pins for the outputs were connected to the driver and then stepper motor. We run this programme and it worked a treat so it is totally possible to make a circuit for this which is scarily simple! In all that board all we were using was obviously the PIC, 2 LEDS and 4 press buttons, a POT and about 6 resistors! That is it - maybe a few connectors to make connecting easier but not necessary! However I enjoyed this and although this works I want to understand more and learn how to move this forward further. Àlthough he was happy to programme a pic for me I want to see this project through from start to finish, especially since I just used the circuit in my Rotarytable CNC conversion, I wanted to have a part in all of this.

So for me, my next step is to get a simple development board (the one in the picture was like what we used but far too complex for what I need) probably about £40. Now it would be £20 for a PIC programming circuit alone so I may as well spend the extra and get the development function as well. Then I`ll acutally use the test board to actually run my controller on the mill and then when I`m happy, see exactly what is being used and put together the circuit required to run the controller.

Sorry, no pictures, there is nothing to take! I`m quite happy to provide the HEX. files if anyone has a use for them.

Chris

Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: NickG on December 17, 2009, 06:29:20 AM
Very interesting the CNC stuff Chris. Have you put ball screws into your mill then? That circuit looks complex will be watching your updates.

Nick
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 17, 2009, 10:06:05 AM
Nick - I`ve cranked down the acme's anti-backlash nut hard, it is stiff to turn manual but the motor is fine with it. I totally appreciate it won`t be good enough, or unlikely to be good enough for a CNC control (although there are some excellent setups on cnczone with acne screws!) but for the time being the acme screws will get me going. Remember, this is an eventual CNC conversion, right now my priority is to setup as an X-axis controller, build the circuit and get it in use. I can then spend more time in developing it into a fully blown CNC independent controller. My intention is to even have a USB port which you can stick into the controller and boot up a G-code and exicute the file!

The amazing thing about these PICs is that you can write the code and using a devlopment board immediately load the program and test it! Last night we literally had a fully working motor running and we kept updating as we added extra code and extra functions. The code that runs my X axis power feed will eventually be embedded within the code that runs the CNC controller - just extra buttons, LEDs and such needed on the circuit.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 17, 2009, 10:22:49 AM
Nick - that circuit remember is the development board not the circuit of the controller!

It looks complex and remember I`m a noob at this too but in actual fact it is quite simple. If you look at the PIC (or actually there are a few different PIC craddles) all the controller does is connect every (well nearly) pin on the pic to an LED, a push switch, a connector, a pot, an LCD etc....   The point is, that each pin is connected to one of each so that when you programe the PIC you tell the PIC what is connected to it and that is totally your choice! So basically the first programme we made last night was like this:

PortB pin1 an input
PortB pin2 an output

If
PortBPin1= 1     // if the button is pressed, i.e 1
PortBpin2 is 1   // the LED is on i.e 1
else               // otherwise if it is not pressed     
Port B pin 2 is 0   // the LED is off  i.e 0

Infact it was pretty much as simple as that! You then pressed the button connected to PortBpin1 and the LED connected to PortBpin2 came on!  And because one of everything is connected to each pin, you can do whatever you like on each pin!
A code almost identical could be used to turn the motor on, instead of the LED have a motor coming on (1) but you`d just need to set the pulse to high and then low for the step. Also it would only go one way, so you`d need another button to alter between 1 (left) or 0 (right).

I know I`m probably going a bit over the top here in my explanation but it is even easier to read than what I wrote above if you use Define statements to define the ports:

Define (button) = portBpin1
Define (LED) = portBpin2

Then use the code like above but in terms of making sense to us, it should help:

If
button= 1     // if the button is pressed, i.e 1
LED is 1   // the LED is on i.e 1
else               // otherwise if it is not pressed     
LED  is 0   // the LED is off  i.e 0

 At that would work exactly the same but it makes more sense to us and is easier to read when defining the ports. I`ve picked all this up in one night, don`t get me wrong it can get bloody complex but it is not too bad to pick up.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: NickG on December 17, 2009, 10:32:12 AM
Sounds good. My dad was a technology teacher and had a denford mircromill and lathe in his classroom - I always intended to go and have a play with it. They hardly ever used it as were a little scared of it / wasn't really in the curriculum so all they used it for was making signs, milling letters out of plastic I think. Don't think they ever used the lathe. I should have put an offer in before he left! Very small but pretty neat bits of kit. I think he said the software was basic and seemed very limited but am sure you could get other software or if you got the right controllers etc could have made them really good.

I've just skipped over your info below a bit but it won't go in at the moment with square eyes, so will have to read properly later!

Nick
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: tumutbound on December 17, 2009, 04:57:49 PM
I'm looking to put a power feed on my X2 and was going to use a motor from an old cordless drill.
Having read your thread, I might have to dig through the stepper motors to see if I have anything that will suit.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 18, 2009, 03:28:07 AM
Tumutbound - if  I manage to get this controller running you are welcome to have the code, pic or circuit to run it!

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 19, 2009, 02:25:37 PM
Well I got a bit further today. Managed to get hold of a Development board, load the PIC and test the circuit with an osciloscope. Here is the setup:

(http://www.raynerd.co.uk/wp-content/upLoads/devboard1.jpg)

EasyPIC5 dev board
(http://www.raynerd.co.uk/wp-content/upLoads/devboard2.jpg)

Osc, shows us when we are pulsing and is easier to test and calculate from than actually hooking up a motor. Although a motor in its place would run!
(http://www.raynerd.co.uk/wp-content/upLoads/devboard3.jpg)


Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: NickG on December 20, 2009, 03:24:21 AM
Chris,

That's really good stuff there, very interesting - think it'd take a lot to explain that in writing, a video was worth 1,000,000 words in this case! Did you loan the development board then? How much would one like that set you back? And what software is required to drive it?

Cheers,

Nick
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: spuddevans on December 20, 2009, 04:11:35 AM
How much would one like that set you back? And what software is required to drive it?

Funnily enough, I have been looking into those particular development boards recently. The latest version can be found here (http://www.mcustore.com/acatalog/EasyPIC6_Development_Board.html)

You can use different programming languages like C, Basic and Pascal, or you can learn the PIC specific coding. There are complete starter development packs that come with your choice of programming language and a few other accessories Clicky (http://www.mcustore.com/acatalog/Starter_Packs.html)

The manufacturers site holds a lot more detail about the different development boards, as well as a load of downloadable pdf's of user manuals, projects and schematics. Linky (http://www.mikroe.com/en/tools/)


Hope this helps,

Tim
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 20, 2009, 04:54:16 AM
Hi, Tim has answered the question but yes, I did loan the board. I wanted it over Christmas otherwise things come to a halt. This board is $130 dollars I think but from the UK distributor is £95 - it is the EasyPIC5. The software can be downloaded on evaluation - google "MikroC Pro download" - it is the evalutation version but does not restrict features, just the size of the code which is way big enough to get more than going. Infact I bet my final code will still fit within the limits!

I`m just setting off to Maplin now, they do this board:

http://www.maplin.co.uk/Module.aspx?ModuleNo=37192

It is a cheap development board with an onboard programmer! At this price, I`m actually going to USE this board for the actual final circuit. It is a decent size and with the programmer onboard it means that the chip can be updated in situ whenever needed without removing the chip from the controller. OK, it is serial and not USB but good enough for what I want. Infact, its great just to have the components needed at this price and adapt the circuit to suit. I`ll probably pull some of the circuit onto a custom board for anything extra needed. This does not support pic18XXX 40 pin pics but to be honest, they are overkill for anything I`ll be able to do.


I`ll post later when I have it home. Tim, is this an interest of yours? - now I know who to call on when I need help !! lol
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: John Rudd on December 20, 2009, 04:59:17 AM
Chris, I use that board too....

Its cheap cheerful and works...I also have a pickit2 but havent gotten round to using it yet...

Too many projects and not enough toy room time... :doh:
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: spuddevans on December 20, 2009, 08:38:06 AM
Tim, is this an interest of yours? - now I know who to call on when I need help !! lol

Well it is an interest that I am looking into, I have yet to actually DO anything with PIC's yet. I am looking to get the easypic6 development board, but will just be getting by with the demo version of the mikroBasic PRO compiler.

My initial interest is with learning to use PICs in decoding digital train signals on my dad's LGB layout. I am also keen to see your development of a CNC controller as I plan a full CNC conversion for my X2 mill. I have a spare PC for it, but would also like to have a simple method of controlling it without having to wait for windoze to load.

So feel free to call on me, but I am only just starting on this PIC journey myself so you probably know more about it than I do.


Tim
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: NickG on December 20, 2009, 08:40:44 AM
Thanks Tim / Chris / John,

At the Maplin price it might be worth a dabble - I've always been interested in electronics since a young age too but it was only a couple of years back I realised what was available for DIY these days. Somebody at our club had done the rotary table like Chris and he also built a flash steam boiler that I think it controlled by a pic. Monitors gas, water etc and applies more or less to gas and feed pump through solenoids etc and a car windscreen wiper motor.

Amazing stuff really. Keep up the good work Chris.

Nick
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: cfellows on December 20, 2009, 11:32:28 AM
Very nice, informative thread.  Appreciate the time and effort you're spending to document this for us.  I've been playing with the PICAXE microcontroller which is a hybrid version of a PIC.  It has a built in bootloader and basic interpreter which makes learning quite a bit easier, cheaper, and simpler although I suspect it is also more limited, at least in it's basic configuration.  I've been working on an electronic dividing head, more as an exercise than anything else, although I do expect it to have a practical application.

Chuck
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 20, 2009, 12:45:22 PM
Hi Chuck - it was a dividing head or at least a conversion of a rotary table to indexing head that inspired me to go down this route. This is my thread on the indexer here:

http://madmodder.net/index.php?topic=2242.0

I have to say, it has to be one of the most brilliant concepts I have seen and the electronics probably cost about £30 max. I was so impressed but disappointed that I didn`t actually know what was happening, that I decided to start looking into it. I thought this X-axis controller would be a good project.

Just curious, what are you guys planning on using to code the pic? I`ve decided to go down the C route as this is the way my mate knows how to programme and how he does it but I do believe BASIC or Assembly language are more appropriate as the first to learn.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: spuddevans on December 20, 2009, 01:27:01 PM
Just curious, what are you guys planning on using to code the pic? I`ve decided to go down the C route as this is the way my mate knows how to programme and how he does it but I do believe BASIC or Assembly language are more appropriate as the first to learn.

I've kinda decided on using Basic as many many years ago I used to mess around with basic programming on an old Amstrad CPC464, so that is what I am more familiar with. I am thinking about trying my hand at Assembly route as well as it is the cheapest ( ie free ) and apparantly it results in smaller code for the same results.

I havent fully decided, I think I'll have to do a little more research on it to see if it worth the time/effort learning a new language ( assembler ) verses the money for a Basic compiler  :scratch: :coffee:

Tim
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 20, 2009, 01:35:03 PM
I`m being encouraged to use C as it is the industry standard but apparently BASIC and especially assembly code give the writer a better understanding of what it going on inside the PIC. I know quite a few choose one of those first and then move to C.

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 21, 2009, 03:11:00 AM
I was going to say this is offtopic, which it is a little but this is my first code wrote entirely by me from scratch. The intention is that it is a set of traffic lights, Road1 and Road2.

Road1 is always green

Unless:

Sensor at road2 is triggered, road1 lights go red, road2 lights go green



Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Darren on December 21, 2009, 05:34:42 AM
I see you're having fun with that Chris, that's what it's all about  :thumbup:
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: John Rudd on December 21, 2009, 07:29:48 AM
I see you're having fun with that Chris, that's what it's all about  :thumbup:


Where's all the swarf?..... :lol:
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 21, 2009, 01:21:36 PM
Sure am having fun Darren. I`m going to attempt to run the stepper motor over Christmas. I`ve just picked up the programmer/development board from maplin a few hours ago so my next step is to build that up, encase it and then use it to run the stepper.

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: No1_sonuk on December 23, 2009, 08:08:55 AM
I`m being encouraged to use C as it is the industry standard but apparently BASIC and especially assembly code give the writer a better understanding of what it going on inside the PIC. I know quite a few choose one of those first and then move to C.
All the high level languages like BASIC and C will give some unneeded code when compared to assembler code.  They also can give varying time-frames for some commands, making assembler the prime choice for timing-critical functions.

I learned PIC assembler for some RC servo projects initially, which require reasonably accurate timing, and have just recently dabbled with C.  THE biggest advantages of C or BASIC are that they are easier to port from one device to another if you need more (or less) facilities, and you don't have to write complicated maths routines yourself (try working out how to divide in assembler and you'll know what I mean).
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 27, 2009, 04:32:11 AM
I`ve not had much shop time over the past few days with the run up to Christmas and family stuff but I have had a little time most evening to continue with developing my understanding of coding. Now I know you may think this is off topic but infact I realise I was nieve to say that I could just build this controller right from the start. I needed to start with something easier. So none of this is off topic, I`m learning how to code so that I can develop the code on my X-axis controller and not only that, an easier alternative project will also allow me to actually build a circuit and integrate a PIC start to finish. The traffic lights posted a few days ago were too basic and useless so I started a code for an electronic dice. I`ve posted all the code at the bottom for you if anyone is interested. Sadly I left my camera at my mother-in-laws so my only pictures have been taken with my phone so bad quality but I just wanted to show you what I`d been up to.

Basically there are two counts going on both going 1 to 6. The first is a delayed count and is being displayed while the button is pressed. The second is a fast count (1 to 6 a few hundred thousand times a second) and this is displayed when the button is released. Since the fast count is literally so fast it becomes random as it is impossible to release the button in sync with an outcome of your choice. I believe it could be hundreds of times slower and it would still be impossible. For all intent and purposes it is random. I initially choose a square of 3 x 3 leds to output the number on the dev board led clusters in a pattern like a dice:

1.      2.    3.     4.     5.       6.
XXX  1XX  1XX  1 X1  1X1   1X1
X1X  XXX  X1X  XXX   X1X  1X1
XXX  XX1  XX1  1X1   1X1   1X1

It worked great but I didn`t take any pictures. The video is exactly the same but the LEDs are in a row but will need to be arranged and the same config or display will be obtained. The reason I`ve changed it was that the first pic I used was a 40 pin with 4 ports. To use the square config of the leds on the dev board I needed to use the three consecutive pins on each port - A to C. The pic I`m atually going to put into this board (the 40 pin is too big and too good for the job - it would be wasted!) is an old PIC16F627 which only had two ports. To be honest, it is better the way I`m doing it anyway as it allows me to have all the LEDs on one port it just means that I have to display them as a line on the dev board because of their position.

So here is the vid:



I now need to make the circuit which will end in a bit of machining with making the case!

Here is my “paint” circuit diagram!

(http://www.raynerd.co.uk/images/leddicejpg.GIF)

And the start of the LED cluster:

(http://www.raynerd.co.uk/images/dice1.jpg)

The pic socket, a resistor for each led and two 5k resistors in series as I needed 10k resistance for my pull up for the switch. You will also notice the switch is a tilt switch so will be activated when you shake the dice!

(http://www.raynerd.co.uk/images/dice3.jpg)

Since I`ve put the pic on a socket it will allow me to remove it and update the code. There are a few other things I want it to do, for example, it would be good for the shaking effect when the button is pressed to last a set time even when the button is released and slow down as it gets nearer to displaying your number...that sort of stuff. We`ll see... then I`ll need to machine the case and at last we may see some swarf.


The code for anyone interested:

Quote
// define which pins our buttons are on
#define rick PORTB.b7


// led array - binary portB state for each number zero to 6.
char ledB[7] = {0b00000000, 0b00001000, 0b01000001, 0b01001001, 0b01010101, 0b01011101, 0b01110111};
char dice1;    // index into array Leds for the "real" dice throw
char dice2;   // index into array Leds for the shake slow dice throw while (button)
int loop;       // defines an int called loop for misc use  

void main()
{

    // set all the pins on PORT B to 'off' before we start
    PORTB = 0;
    // set the tri-state buffer. Pins set to '1' are input, '0' are output. In this case PortB bits 0-6 as output for leds and bit 7 as input for button
    TRISB = 0b10000000;
    
    // set all the pins on PORT B to 'off' before we start
    PORTB = 0;
//============================================================
//=================code proper starts=========================
//============================================================
// two dice throws are actually calculated - dice proper and a dice throw to display while shaking
// calculate dice

dice1 = 1;           // dice1 is set to 1
dice2 = 1;           // dice2 is set to 1
while(1)             // loops this entire code forever as the condition is always 1
{
while (rick)         // while button is pressed
 {
      // ==== This is dice2 the "shaking" display ======
      loop++;                // loop is set as an int and can therefore store numbers upto a few thousand so will loop through starting from 1 and adding + 1
      if (loop > 6000)     //  when loop goes above 6000 loop is set to zero and therefore comes out of the while statement (this creates a count delay)
      {
      loop = 0;                        
      dice2++;              // add 1 to dice2
      if (dice2 > 6)        // cycles dice between 1 and 6 (if it goes over 6 it returns back to 1)
      dice2 = 1;            // as above. reset to 1.
      PORTB = ledB[dice2];  // BUT the display shows the led array for the number it is on in each loop, hence the counting shaking effect
      }
      // ==== This is dice1 the actual throw and is running in the background very fast===
      dice1++;          // dice1 + 1
      if (dice1 > 6)  // when dice gets to 6 take it back to 1
      dice1 = 1;
  }
  // Now out of the rick loop so when the button is  not pressed it displays the results of dice1
PORTB = ledB[dice1];
}
}
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: spuddevans on December 27, 2009, 01:29:09 PM
That's looking good Chris :thumbup: From everything I've been reading, everyone seems to reccomend starting with a simple project like you are doing, but to have the "big" project to work towards.

As for it being or not being  :offtopic: It is your thread, you take it where you see fit. In my view this is not off topic at all, like you say this is part of the build up to getting the CNC controller done.


Tim
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 27, 2009, 06:37:24 PM
Hi Tim, glad you understand ...

I managed to put together the circuit for it and I have bought a few kits from maplin over the years and this is the first that has ever worked and it was built from scratch!

I spent about two hours pulling my hair out before I got it running. If you go back to my diagram there is a pin labeled MCLR and since I have not turned this off in the code it meant that I needed to pull it high for the pic to run! I simply jumped it with a perminant live and now the circuit works great.

I just want to add a slow acting delay so that the shaking effect slows down and comes to a stop rather than stopping so abruptly as it currently does.

Hope you like it. Just a bit of swarf to make now for a case and then maybe back on to the x-axis controller!

Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: andyf on December 27, 2009, 07:17:33 PM
OK Chris, that's got playing Monopoly sorted - no more hoisting Auntie up to look for the die down the side of her cushion.

Seriously though, I'm watching this closely. Apart from fiddling around with CMOS adders to get the PLLs in old CB sets on to the amateur 10m band, my experience with digital can be summed up as 0. The fact that you seem to be starting from a fairly basic level is teaching me more about microcontrollers as applied to engineering than I would learn from trying to adapt ideas from some "Run Your Whole Home Using Just One PIC" book. Thanks.

Andy
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 28, 2009, 04:59:23 AM
Cheers Andy. It all started with the indexer that my mate made for me and then I figured I fancied having a go. To be honest, it didn`t really start making any sense until I borrowed the dev board and could code and then immediately send it to the chip and see if it actually did what I wanted it to. Think the first thing I did was hold an led lit for 5 secs each time you pressed a button. It is supprising how fast it comes together, it is totally logical and the commands are quite straight forward using MikroC.

Thanks for looking and commenting, Andy.

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on December 30, 2009, 06:32:16 PM
I have finally finished the code and the circuit. I have also put in a little button that switches it into a "weighted dice" mode which makes the dice weighted to 6. I think I`m getting into this and into the way of thinking. It basically counts 1 to 6 and then looks at an array of LED options (1-6) and displays the relevant number. To get it weighted, I simply made it count from 1 to 8 with 7 and 8 also being the arrangement for a six throw. Obviously, there is then more chance of the six being thrown as it is in the array 3 times. It was only a bit of fun really and I just wanted to see if I could do it.
I`ve built the cicuit and it looks like a dogs dinner (I really nead to use strip board on make my own PCB (another project!)) but it works!

I just need to encase it now. Has anyone any suggestions? I was thinking an aluminium case but I don`t know how you could lock it together but with a release so that you can change the batteries or even press the button. I think I`ll hide the weighted button inside the case as it was only fun and probably won`t ever be used.

Now we are talking swarf, there must be some opinions?

Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: No1_sonuk on January 04, 2010, 07:53:36 AM
I once spent 2 hours trying to work out why one of my PIC projects wouldn't work.  I eventually realised I'd not switched over the analogue inputs to digital.  I'd not used an 8-pin PIC with analogue inputs before, and didn't realise they defaulted to analogue...  :scratch:


WRT the box: Maplin, and others, sell plastic cases with battery compartments.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: spuddevans on January 04, 2010, 12:28:40 PM
That's looking good Chris  :wave:

I just need to encase it now. Has anyone any suggestions? I was thinking an aluminium case but I don`t know how you could lock it together

You could hollow out a cube of Ali with holes drilled for the led's, then use some of those really strong magnets to hold the base onto the hollowed out section. The whole thing would look like a large dice ( die? ) but would have no visable fixings.

Just a thought.


Tim
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 04, 2010, 01:01:04 PM
No1-sonuk - lol, tell me about it!! It took me ages to figure out what to do to get them off. I found I had to type:

// All ports digital
ADCON1 = 0x0F;
CMCON = 0x07;

and that works perfectly but if I`m being honest, I haven`t a clue what it means. I didn`t even find it on the data sheet, I googed it!!! This is for my PIC18F4520 which I`m using on a current project but on the PIC16F627 I was using on the dice project shown in this thread obviously didn`t need it, I presume it doesn`t have any ADC?

I wouldn be interested to see your projects if your willing to share them. What code do you program in?

SpudEvans - yea I thought of that, I think it would look best but at present my circuit design is too bulky and wouldn`t fit in a decent size block. I think maybe I`ll redo the circuit on strip board and try and slim it down a little.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: John Rudd on January 04, 2010, 01:13:15 PM
Chris,

The F627 doesnt have adc capability, it has 2 in built analog comparators with inputs on pins 1,2,17 18 and needs the cmcon function to turn off the comparators...

If you look at the Microchip data sheet pg 63, cmcon=111(Binary) turns off the comparator function, your function above says 7 ( I guess the Radix used is Decimal or Hex)
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 05, 2010, 03:37:32 AM
Hi John,

That explain a lot - thank you!!!

I didn`t know that but it is clearer now when reading the datasheet after you have said, that would explain why the dice would not work correctly when I put the button on PortA.b0 or b1   - they are ports 17 and 18 and as you said, I need to turn the analog comparators off. If I being honest, I don`t even know what the analogue comparators do - I`ll have to have a google.

Thanks for the info. Regarding the code I posted in my last message - ADCON1 = 0x0F; CMCON = 0x07; I had to write that to get the 18f4520 to work. I`m sorry, I`m looking at the data sheet but I can`t see where I get that from (I googled it and someone mentioned it to another noob on a forum)? I know it is going to turn the pins to digital but can`t see where the code if from. Any advice appreciated.
Just curious John, what sort of stuff have you done with PICs?
Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: No1_sonuk on January 05, 2010, 07:32:22 AM
"0x07" is the convention used for hexadecimal numbers.

Most of my stuff has been in assembler, but I've recently dipped my toe into C.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: John Rudd on January 05, 2010, 11:50:50 AM
Just curious John, what sort of stuff have you done with PICs?
Chris


Chris, Hex code isnt something new to me, I used it lots for receiving encrypted tv during the D2MAC days...
As for pics, I've built a number of pic based projects from magazines, infra red control for one...Most recent project which still isnt complete is a tacho for my 9*20 lathe...based on a 16F628A and a 2 line lcd display..

I'll post up photos ( nearly said pics.... :D ) when its done..
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 05, 2010, 05:06:30 PM
Hi John, I look forward to seeing the tacho - I`d be interested in making one so please post plenty of photos and details - obviously unless it is a commercial idea.

I`m going to post up again shortly. I`ve just spent this evening making a little ball and cup game! I can`t help myself. I love these things!!

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 05, 2010, 05:52:49 PM
This is totally pointless (not like the dice wasn`t!!) but again I`ve learnt a lot from it, specifically using functions.

It is based on the old Ball and Cup game - you know the one where you have to select which cup the ball is under. Basically, there are three buttons and the first row of leds lights to show your selection. The top row then starts to count and then stops on the computer selection (or under which cup the ball actually was!). If you match the leds, you win and the leds flash. If you don`t you lose and the leds come on solid and then reset.



My issue is that I have used a lot of messy delay code in my "void CupDisplay" function. Anyone know a better way to do it?

OHHH and OK before anyone says --- that is it now, back on task with the X-axis controller!!!!!

Quote
// define which pins our buttons are on
#define button1 PORTA.b0
#define button2 PORTA.b1
#define button3 PORTA.b2
#define YourLED1 PORTB.b0
#define YourLED2 PORTB.b1
#define YourLED3 PORTB.b2

// 3 buttons 1,2,3 linked to YourLED1, YourLED2, YourLED3
// When button is pressed corresponding LED lights and computer counts CupLED1-3
// CupLED1-3 lights.
// if YourLED = CupLED  you win - flashing. Game Resets
// else...you loose. Game Resets
char buttonPressed;   // a variable that saves our button press
char cup;
char LED[4] = {0b00000000, 0b00000001, 0b00000010, 0b00000100};

void cupDisplay ()
{
  PORTC = 0b00000001;
  delay_ms(300);
  PORTC = 0b00000010;
  delay_ms(300);
  PORTC = 0b00000100;
  delay_ms(400);
  PORTC = 0b00000010;
  delay_ms(400);
  PORTC = 0b00000001;
  delay_ms(400);
  PORTC = 0b00000010;
  delay_ms(500);
  PORTC = 0b00000100;
  delay_ms(500);
  PORTC = 0b00000010;
  delay_ms(500);
  PORTC = 0b00000001;
  delay_ms(650);
  PORTC = 0b00000010;
  delay_ms(750);
  PORTC = LED[cup];
  delay_ms(1000);
 if (buttonPressed == cup)
 {
 PORTC = 0b00000111;
 PORTB = 0b00000111;
 delay_ms(250);
 PORTC = 0b00000000;
 PORTB = 0b00000000;
 delay_ms(250);
 PORTC = 0b00000111;
 PORTB = 0b00000111;
 delay_ms(250);
 PORTC = 0b00000000;
 PORTB = 0b00000000;
 delay_ms(250);
 PORTC = 0b00000111;
 PORTB = 0b00000111;
 delay_ms(250);
 PORTC = 0b00000000;
 PORTB = 0b00000000;
 delay_ms(250);
 PORTC = 0b00000111;
 PORTB = 0b00000111;
 delay_ms(250);
 PORTC = 0b00000000;
 PORTB = 0b00000000;
 delay_ms(250);
 }
 else
 {
 PORTB = 0b00000111;
 PORTC = 0b00000111;
 delay_ms(2000);
 }
 }

char ReadButtons ()
{
if (button1) return 1;
if (button2) return 2;
if (button3) return 3;
return 0;
}

void main()
{
    char count;             // misc use

    int adc;                // somewhere to put the ADC we read

    // PIC's have lots of hardware - in this case in order to use PORTB
    // we have to turn off the ADC's
// All ports digital
ADCON1 = 0x0F;
CMCON = 0x07;
    // set all the pins on PORT B to 'off' before we start
    PORTA = 0;
    PORTB = 0;
    PORTC = 0;
    // set the tri-state buffer. Pins set to '1' are input, '0' are output
    // we've done this in binary to make it easier to see
    // each number is 1 pin, right hand is pin 0, left is pin 7
    TRISA = 0b11111111;
    TRISB = 0b00000000;
    TRISC = 0b00000000;
   
// ============= Code Proper Starts ========

while (1)    // loop forever
{
while (!(buttonPressed = ReadButtons() ));

cup = 1;
while (ReadButtons())     // counts while buttons are pressed
      {
      cup++;
      if (cup > 3)
      cup = 1;
      }

if (buttonPressed == 1)
{
YourLED1 = 1;
cupDisplay ();
}

if (buttonPressed ==2)
{
YourLED2= 1;
cupDisplay ();
}

if (buttonPressed ==3)
{
YourLED3= 1;
cupDisplay ();
}
PORTA = 0;
PORTB = 0;
PORTC = 0;
}
}
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bernd on January 05, 2010, 06:12:48 PM
 :offtopic: Hey Chris, what ever happened to the clock you were building?  :offtopic:  :poke:

Bernd
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 05, 2010, 06:17:21 PM
lol, it is warmer up here in the living room programming these pics on my PC than in the workshop. I actually can`t open the door to the shop at the moment with all this snow!!!

Naaa, not really the excuse, if I`d wanted to I`d have carried on. I`ve just got a bit of a bee in my bonnet about these PICs at the moment, I find them amazing. My clock is my life ambition, it will be done there is no fear of that!
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 08, 2010, 05:52:26 AM
Just another step towards the xaxis contoller and I know probably not that interesting. I`ve actually started it properly now but I wanted to include an LCD so I thought it would be best to first try and implement an lcd on a current code.
Here is my electronic ball and cup game with an lcd.... I`m certainly building up my understanding especially considering I`ve know nothing about either coding or electronics/circuits.



Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: NickG on January 08, 2010, 12:27:06 PM
Chris, impressive stuff this.  :clap: It's good that you can be doing this preparatory work indoors too.  :smart: then  :dremel:  :thumbup:
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 08, 2010, 06:42:11 PM
Believe it or not, I am now getting down to the task in hand. Can I ask for a few ideas that you would think would be useful for an x-axis controller unit.
Just to recap, as I appreciate this thread has lost a little focus -

The controller is to drive the X-axis of the X2 milling machine.

The current inputs and outputs:

button left
button right
button stop
button memory
LCD display
MotorRun LED
Memory LED

Function:
On power-up, the indexer is auto set to position "0".
The motor can be jogged left and right using the buttons
If memory is pressed, followed by "left" or "right" that sets a limit at that side. Once two limits have been set, this allows you to simply move between limits (I expect progressing the feed on either the z or y axis).

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

That is where I am up to now. Anyone any suggestions as to the sort of functionallity or other features it would be useful to have?

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: No1_sonuk on January 08, 2010, 08:54:48 PM
Variable traverse speed? Not necessarily constantly variable, but at least presettable.  That could be on a +/- basis, set up several presets, or both.


Also maybe a button/function to make it auto-traverse from one limit to the other and stop, or go back.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: spuddevans on January 09, 2010, 04:06:23 AM
Having a rapid traverse could be helpful for covering larger spans.


Tim
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: kvom on January 09, 2010, 09:31:33 AM
Quote
My issue is that I have used a lot of messy delay code in my "void CupDisplay" function. Anyone know a better way to do it?

I recoded your program to make it a bit less "messy";  Hope this helps.  Unfortunately the website messes up the formatting a bit.

Quote
// define which pins our buttons are on
#define button1 PORTA.b0
#define button2 PORTA.b1
#define button3 PORTA.b2
#define YourLED1 PORTB.b0
#define YourLED2 PORTB.b1
#define YourLED3 PORTB.b2

// 3 buttons 1,2,3 linked to YourLED1, YourLED2, YourLED3
// When button is pressed corresponding LED lights and computer counts CupLED1-3
// CupLED1-3 lights.
// if YourLED = CupLED  you win - flashing. Game Resets
// else...you loose. Game Resets
char buttonPressed;   // a variable that saves our button press
char cup;
char LED[4] = {0b00000000, 0b00000001, 0b00000010, 0b00000100};
char portvals[10] = {1,2,4,2,1,2,4,2,1,2};
int delays[10] = {300,300,400,400,400,500,500,500,650,750};
void cupDisplay ()
{
  for (int i=0; i<10; i++)
    {
      PORTC = portvals;
      delay_ms = delays;
    }
  PORTC = LED[cup];
  delay_ms(1000);
  if (buttonPressed == cup)
    {
      for (int i=0; i<4; i++)
   {
     PORTB = PORTC = 7;
     delay_ms(250);
     PORTB = PORTC = 0;
     delay_ms(250);
   }
    }
  else
    {
      PORTB = PORTC = 7;
      delay_ms(2000);
    }
}

char ReadButtons ()
{
  if (button1)
    return 1;
  if (button2)
    return 2;
  if (button3)
    return 3;
  return 0;
}

void main()
{
    int adc;                // somewhere to put the ADC we read

    // PIC's have lots of hardware - in this case in order to use PORTB
    // we have to turn off the ADC's
    // All ports digital
    ADCON1 = 0x0F;
    CMCON = 0x07;
    // set all the pins on PORT B to 'off' before we start
    PORTA = PORTB = PORTC = 0;
    // set the tri-state buffer. Pins set to '1' are input, '0' are output
    // we've done this in binary to make it easier to see
    // each number is 1 pin, right hand is pin 0, left is pin 7
    TRISA = 0b11111111;
    TRISB = 0b00000000;
    TRISC = 0b00000000;
   
    // ============= Code Proper Starts ========

    while (1)    // loop forever
      {
   while ((buttonPressed = ReadButtons()) == 0);

   cup = 1;
   while (ReadButtons() > 0)     // counts while buttons are pressed
     {
       cup++;
       if (cup > 3)
         cup = 1;
     }

   switch (buttonPressed}
   {
   case 1:
       YourLED1 = 1;
       break;
   case 2:
       YourLED2= 1;
       break;
   case 3:
       YourLED3= 1;
   }
   cupDisplay ();
   PORTA = PORTB = PORTC = 0;
      }
}
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 09, 2010, 01:04:45 PM
Thanks for your suggestions, I have the speed running off a variable resistor/pot so it is basically just a twist knob to alter the speed at any point.

I like the idea of auto traverse with one button press from one side to the other and then perhaps wait for a button pressed for the return. I doubt without a multi axis cnc you would wish for it to traverse from one side to the other continuously? Maybe - and I bet it would be easy to code in the function. Also, regarding Tims suggestion of rapid traverse, I could include a button which sets an auto fast speed with a one press it returns it back to the other limit and then auto knocks off, returning back to the variable speed set by the pot for the cut.

Any more suggestions welcome!

Kvom - I really appreciate the time taken for you to run through my code. I have actually done some bits of it already myself. I had removed the duplicated "if" statements at the bottom of my code and replaced it with the "switch" statement as you have. I also managed to have a go at a loop for the delay and display sequence. I knew I should be using the for statement and have seen through googling that the use of i for a variable is common for a count. I couldn`t get it to work properly with the delays because I hadn`t put them in a delay array as you have. I totally see now why it was not working and your corrections have really helped me understand how I should have done that, thanks!

Could you or anyone else explain to me or better still show me how I could change the following code for my x-axis controller so that the actual motor stepping sequence will be carried out on an interrupt? At the present time, the motor stepping sequence ("stepMotor") is a global function and as such the PIC is not scanning anything else while carrying out this function. I believe if I put the stepMotor sequence as an interupt, it will both allow the controller unit to be functional (i.e to scan for other user input operations, stop, or "do something else!") while the motor is running and also, I believe interupts are a better timing sequence and will allow sharper control of the pulse timing. I am not to sure, therefore, if the motorStep sequence is moved to an interupt whether the use of a delay as a speed control will be changed?? Or something to do with the interupt itself being used for timing. I`m all a bit muddled up...

Chris

Quote
// Function:
// Press 'mem', light comes on.
// Move left & right until at position.
// Press 'mem' light starts to flash.
// Press left or right to set that limit, light goes out and unit works as before.
// SHOULD THE MEMORY SETTING BE CHANGED TO WORK THE FOLLOWING?????
//A button that toggles free move or limited.
//at any point in either mode you can simply press the 'memory' button followed
//by left or right to set the limit.

// define which pins our buttons are on
#define right PORTC.b0
#define left PORTC.b1
#define stop PORTC.b2
#define memory PORTC.b3

// define which pins our motor is connected to
#define memoryLed PORTC.b4
#define motorLed PORTC.b5
#define step PORTC.b6
#define direction PORTC.b7

// LCD display config.
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections


long tablePos = 0;
long limitLeft = -10000;
long limitRight = 10000;
int motorRun = 0;

// mode == 0 : running
// mode == 1 : setting limits
char mode = 0;

//===========================================
// this function moves the motor 1 step
// only moving if it can and also updates the table position

void stepMotor()
{
    if (!mode)                  // we only take note of the limits in mode 0 (otherwise we're setting them)
    {
    // if going right then check right limit - if greater than limit, motorRun = 0, stops motor.
        if (direction)
        {
            if (tablePos >= limitRight)
            {
                motorRun = 0;
                return;
            }
        }
        else
        {
            if (tablePos <= limitLeft)
            {
                motorRun = 0;
                return;
            }
        }
    }

    // these 3 lines step the motor
    step = 1;       // set the step line high
    delay_us(1);    // wait 1 micro second
    step = 0;       // and low again - the motor will now move one step

    // update the table position
    if (direction)
        tablePos++;
    else
        tablePos--;
}

//===========================================
// when the PIC is first switched on it will start to run at "main"

void main()
{
    char motorSpeed = 3;    // 3 = fast, 255 = slow
    char count;             // misc use
    int adc;                // somewhere to put the ADC we read

    // set ADC's
    ADCON0 = 1;         // enable ADC
    ADCON1 = 0b1110;    // enable ADC only for ADC 0

    // set all the pins on PORT B to 'off' before we start
    PORTC = 0;

    // set the tri-state buffer. Pins set to '1' are input, '0' are output
    // we've done this in binary to make it easier to see
    // each number is 1 pin, right hand is pin 0, left is pin 7
    TRISC = 0b00001111;
    TRISB = 0;
    TRISA = 0b11111111;

    while(1)
    {
        if (left) // is left button pressed?
        {  // if we get in here button must be pressed
            direction = 0;  //set direction to 0
            motorRun = 1; // start the motor
        }

        //------------------------------
        // check for right button
        if (right) // right button pressed
        {
            direction = 1;   // set direction to 1
            motorRun = 1; // start the motor
        }

        //------------------------------
        // check for stopping the motor
        if (stop)   // stop button pressed
            motorRun = 0; // stop the motor

        //------------------------------
        // handle setting the limits.
        //------------------------------
        // is memory button pressed?
        if (memory)
        {
            // wait for it to be released
            while (memory);

            // if in 'run' mode, change to 'limit set' mode
            if (!mode)
                mode = 1;
            else
            {
                // in limit set mode - wait for left or right to be pressed
                while (!left && !right) // if neither are pressed then stay here
                {
                    // be clever here and flash the memory light to show we're waiting
                    delay_ms(2);
                    count++;            // count is 8 bits (char) - when it gets to 255, the next time it increments it goes to zero
                    if (count > 128)    // 5ms * 128 is a bit over  1/4 seconds - so light should flash about 2hz
                        memoryLed = 0;
                    else
                        memoryLed = 1;
                }

                // depending on which button was pressed set the appropriate limit
                if (left)
                    limitLeft = tablePos;
                if (right)
                    limitRight = tablePos;

                // wait for left/right to be released and then we're done
                while (left || right);

                // we're finished so go back to 'run' mode
                mode = 0;
            }
        }

        //------------------------------
        // if in 'limit set' mode then light the memory LED
        if (mode == 1)
            memoryLed = 1;
        else
            memoryLed = 0;

        //------------------------------
        // step the motor
        if (motorRun)   // are we moving the motor?
        {
            motorLed = 1;
            stepMotor();

            // read the ADC and set the speed (delay)
            adc = ADC_Read(0);
            adc = adc >> 2;         // divide by 4
            if (adc < 3)
                adc = 3;
            for(count = 0; count < 10; count++)
                Delay_Cyc(adc);
        }
        else
            motorLed = 0;
        }
}
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: spuddevans on January 09, 2010, 01:15:14 PM
I could include a button which sets an auto fast speed with a one press it returns it back to the other limit and then auto knocks off, returning back to the variable speed set by the pot for the cut.

Why not code it so that the fast-traverse-button activates the fast traverse until a direction button is pressed and then released, ie you press the fast traverse button, press and hold the relevent direction button, then when the button is released then fast traverse function automatically is cancelled, returning the speed to the previously set speed via the pot.

You could code it along the lines of the fast traverse button sets a flag to "1" and then the direction button could test that flag to see if it is set, if it is then it would traverse at the high speed, and then when the direction button is released it would clear the flag thereby cancelling the fast traverse.


Tim
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: BobWarfield on January 09, 2010, 01:20:41 PM
That's the beauty of it being a computer: you can make it do anything.

I imagine it is not all that much more costly than a regular power feed, yet it has the potential to be a lot more powerful.  Just by changing software and adding a few switches you can provide a variety of functions:

-  Fast jog
-  Slow jog with potentiometer to control speed
-  Speed readout in IPM so you can adjust to match a recommended feed.  Turn the potentiometer until you get the number
-  Or, provide a keypad to enter a feedrate
-  Push a button to set a stop.  Now the stepper will always stop after that many step pulses and you have effectively got a software stop on your feed
-  Interface a handwheel-style pulse generator
-  Simple DRO function.  Push a button to zero, and then the readout will tell you how far you've moved.

The list goes on and on.  It would not be hard to add a couple of circuits so that you can take the step/dir right out of a Mach3 breakout board.  Now you have the ability to either run the g-code via Mach or run as a manual with power feeds.


I've said for a long time the cost of fitting CNC is about the cost of power feeds and a nice DRO.

Cheers,

BW
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 09, 2010, 03:52:53 PM
Tim, excellent idea. In C it will be even easier than that:

if (left)
{
     if (fastTraverse)
     motorSpeed = **TopSpeed;
     else
     motorSpeed = **ACD_PotSpeed
Rest of move code.....
}

OK - over simplified but it won`t be far off.


Bob - your totally right and thanks for the nice ideas.

Regards
Chris

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: kvom on January 09, 2010, 06:35:56 PM
WRT controlling the motor in an interrupt:

Putting a delay in an interrupt routine is poor programming practice.  Interrupts should perform limited functions and return quickly.  Assuming that delay_us() is a system routine, it is almost certainly itself setting a timer interrupt and suspending execution pending that timer.

If I read your code correctly, the outer while loop continuously checks for a button push and then continues with the new or current motor movement.  In any case, your code is not checking for 2 or 3 buttons pressed simultaneously.  In addition to correcting this, you may need to "debounce" the buttons, since a button may remain "pressed" for a short time it's released by a finger.  The code could get a little messy to do this, so the likely easiest solution is to default to motor stop when more than one button is pressed.  Something like this:


   int button= 0;
   if (left)
     button += 1;
   if (right)
     button += 2;
   if (stop) || (button > 2)
     motorRun = 0;
   direction = (button == 1 ? 0 : 1);
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 10, 2010, 08:19:42 AM
Kvom, I have tried to put the step on an interupt triggered by the clock timer. I totally agree with what you say regarding the delay but apparently since the delay_ms is 1 microsecond, apparently that is about 4 lines of assembler so no time at all. However, I appareciate what you say about the bad practice so I believe I could set the step high at the start of the interupt, then "do some more code" - say the limit stuff and then set the step low. Would that not have the same effect of a super short delay as it runs the code between the step high and the step low?

This code allows the user no control of when the interupt occurs and therefore the stepper pulses. All speed control has been lost now in this re-worked code. By using the ADC or even presets to alter the timer count between triggering the interupt it should be able to control the speed.

If you can change and improve my code then please do.... I don`t really know how to move forward from here....

also the current limit setting idea is too complex, it would much better if it worked like this:

//A button that toggles free move or limited.
//at any point in either mode you can simply press the 'memory' button followed
//by left or right to set the limit.

here is the code...the current limit setting function described at the start:

Quote
// Function:
// Press 'mem', light comes on.
// Move left & right until at position.
// Press 'mem' light starts to flash.
// Press left or right to set that limit, light goes out and unit works as before.

// define which pins our buttons are on
#define right PORTC.b0
#define left PORTC.b1
#define stop PORTBC.b2
#define memory PORTC.b3

// define which pins our motor is connected to
#define memoryLed PORTC.b4
#define motorLed PORTC.b5
#define step PORTC.b6
#define direction PORTC.b7

// LCD display config.
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections


long tablePos = 0;
long limitLeft = -10000;
long limitRight = 10000;
int motorRun = 0;

// mode == 0 : running
// mode == 1 : setting limits
char mode = 0;

void interupt()
{
    TMR0IF_bit = 0;    // clear TMR0IF
    TMR0L  = 96;       // reset counter
    
    if (motorRun)      // interupt only steps motor if motorRun is true.
    {
    if (!mode)         // we only take note of the limits in mode 0 (otherwise we're setting them)
    {
    // if going right then check right limit - if greater than limit, motorRun = 0, stops motor.
        if (direction)
        {
            if (tablePos >= limitRight)
            {
                motorRun = 0;
                return;
            }
        }
        else
        {
            if (tablePos <= limitLeft)
            {
                motorRun = 0;
                return;
            }
            }
            }
    
    // these 3 lines step the motor
    step = 1;       // set the step line high
    delay_us(1);    // wait 1 micro second
    step = 0;       // and low again - the motor will now move one step
      // update the table position
    if (direction)
        tablePos++;
    else
        tablePos--;
 }
 }
 
//===========================================
// when the PIC is first switched on it will start to run at "main"

void main()
{
    char motorSpeed = 3;    // 3 = fast, 255 = slow
    char count;             // misc use
    int adc;                // somewhere to put the ADC we read

    T0CON  = 0xC4;       // Set TMR0 in 8bit mode, assign prescaler to TMR0
    // set ADC's
    ADCON0 = 1;         // enable ADC
    ADCON1 = 0b1110;    // enable ADC only for ADC 0

    // set all the pins on PORT B to 'off' before we start
    PORTC = 0;

    // set the tri-state buffer. Pins set to '1' are input, '0' are output
    // we've done this in binary to make it easier to see
    // each number is 1 pin, right hand is pin 0, left is pin 7
    TRISC = 0b00001111;
    TRISB = 0;
    TRISA = 0b11111111;

    while(1)
    {
        if (left) // is left button pressed?
        {  // if we get in here button must be pressed
            direction = 0;  //set direction to 0
            motorRun = 1; // start the motor
        }

        //------------------------------
        // check for right button
        if (right) // right button pressed
        {
            direction = 1;   // set direction to 1
            motorRun = 1; // start the motor
        }

        //------------------------------
        // check for stopping the motor
        if (stop)   // stop button pressed
            motorRun = 0; // stop the motor

        //------------------------------
        // handle setting the limits.
        //------------------------------
        // is memory button pressed?
        if (memory)
        {
            // wait for it to be released
            while (memory);

            // if in 'run' mode, change to 'limit set' mode
            if (!mode)
                mode = 1;
            else
            {
                // in limit set mode - wait for left or right to be pressed
                while (!left && !right) // if neither are pressed then stay here
                {
                    // be clever here and flash the memory light to show we're waiting
                    delay_ms(2);
                    count++;            // count is 8 bits (char) - when it gets to 255, the next time it increments it goes to zero
                    if (count > 128)    // 5ms * 128 is a bit over  1/4 seconds - so light should flash about 2hz
                        memoryLed = 0;
                    else
                        memoryLed = 1;
                }

                // depending on which button was pressed set the appropriate limit
                if (left)
                    limitLeft = tablePos;
                if (right)
                    limitRight = tablePos;

                // wait for left/right to be released and then we're done
                while (left || right);

                // we're finished so go back to 'run' mode
                mode = 0;
            }
        }

        //------------------------------
        // if in 'limit set' mode then light the memory LED
        if (mode == 1)
            memoryLed = 1;
        else
            memoryLed = 0;

        //------------------------------
        // step the motor
        if (motorRun)   // are we moving the motor?
        {
            motorLed = 1;
            //INITIATES interupt!
      TMR0L  = 96;         // Timer0 initial value
      INTCON = 0xA0;       // Enable TMRO interrupt

            // read the ADC and set the speed (delay)
            adc = 512;                   // MyADC needs replacing with  ADC_Read(0);
            adc = adc >> 2;         // divide by 4
            if (adc < 3)
                adc = 3;
            for(count = 0; count < 10; count++)
                Delay_Cyc(adc);
        }
        else
            motorLed = 0;
        }
}
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: kvom on January 10, 2010, 09:46:22 AM
Some thoughts:

From the above, I am assuming that the 1 microsecond delay is necessary to create the proper length pulse to drive the motor.  If so, that is the primary requirement for the program and hence should be in the mainline of the program.  There is no need for any explicit interrupts as the only asynchronous events are button pushes, which are comparatively rare and not needing precision.

So I would use the prior code with the following modifications:

1) Ensure that only one button is pressed at a time

2) Stop the motor any time a button is pressed, and restart only when a good left or right button is pressed.  In cases where the motor is stepping in one direction and the opposite button is pressed, I would treat that as a stop and require two presses to reverse direction.

3) When the memory button is pressed, a second press or stop should turn off the LED without setting either limit.

4) You need to check that the right limit is not set less than the left limit and vice-versa.

Another way to think of this type of problem is to regard it as an "automaton".  An automaton is a machine that has a number of clearly defined states and all the valid transitions between them.  You can draw a diagram where the states are labeled circles and the transitions are directional arrows connecting them.

You might have the following states:

stopped
motor right
motor left
memory
memory left
memory right

The transition events are:

buttons
limits

Then a good way to structure the code is to hold the state in a variable and test it in the main loop via a switch statement.  In each case of the switch, you check for valid transition events, and if found change the state.

For complex situations, automata are a better way to organize the code as opposed to complex nested loops and ifs.  It's easy to locate the code for a given state and modify it without affecting other states, and in addition defining additional states is easier.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 10, 2010, 02:22:00 PM
Kvom, These are not my words, these are actually notes I made from someone else giving me advice:

If I want my stepper motor to run at 3000 steps per second, I can`t start it at that or it will stall. I need to accelerate it to that speed and then once there I need to make sure the train of pulses are at the right frequency. If my code can not guarante that frequency I`ll have to run it slower and won`t be able to use the top speed which has already been discussed on here, will be useful for a fast return! The step pulse can be very small - just a microsecond or so  but the important bit is the distance between pulses. For example 1000 steps per second requires a time of 1/1000 of a second between pulses, 3000 requires 1/3000 th of a second etc and since time = 1 / frequency or frequency = 1 / time to run my stepper at 3khz you need a time of 1/3000th of a second between the pulses and since we know how fast a timer runs on the PIC we can calculate a suitable number to put in it so that it'll reach zero (trigger the interupt) in 1/3000th of a second. If we then set up an interrupt on that timer then 1/3000th of a second later, exactly, we'll get an interrupt and in there can step the motor. Since this is an interupt in our main loop we can do other stuff - write position to an lcd, scan the keypad, flash leds or whatever we want and we don't have to worry that the stepper is stepping correctly - the interrupt will do it for us!

I hope that makes sense...that is my understanding of it mainly taken word for word from an explanation I was given. I appreciate your advice and would quite like to hear your response!

Regarding your other points, I will certainly be using some of those ideas.

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: kvom on January 10, 2010, 05:42:21 PM
That makes sense to use the interrupt in that case.  So if you set the interrupt at 3Khz, you need to define two additional things:

1) Accelleration time
2) Max speed

Assume it takes 100ms to accellerate to max speed, or 0-3Khz.   One way to do this is to vary the interrupt rate.  Assume 10ms intervals.  For the first interval you set the interrupt rate at 300 Hz and generate a step at each interrupt.  Then increase the rate to 60Hz until the motor is at full speed.  You can define an automaton state of 'accellerating' to handle the rate change.  Obviously you can make the accelleration smoother if you want.

Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: No1_sonuk on January 10, 2010, 07:39:48 PM
Remember you have a limited number of stack levels.  A hardware interrupt will use one of those, so you have to ensure that you don't use more than the total available minus 1 for the main loop and the interrupt routine combined.  So if you have the normal 8 levels available, don't use more than 7 total for the main loop and the interrupt service routine.

EDIT:-
BTW, the "messy" C instructions is one reason why I'd probably code at least some of this sort of thing in Assembler.  That guarantees the timing is as accurate as the processor can make it.  That is, however, a personal choice, and due mainly to the fact that I've programmed PICs almost exclusively in Assembler for several years.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 11, 2010, 05:33:51 AM
I`ve got a bit stuck again with this. I have a blurb at the start.

My issue now is that if I want to reset a limit past the previous limit, I can`t get there!! I need to ignore limits while in "limit setting mode" or in terms of the code, while (memory).  I could do that by setting something like;

Quote
while (memory)
{
 mode = 1,
Rest of limit setting code....
}

and then in the interupt pulse section

Quote
while (!mode)        // while not in mode
{
rest of code checking limits // check limits then move onto pulse
}
pulse.       // if we are in mode, it will just pulse and not check limits.

However now my issue is that if I did something like the above code, I need to use left and right to jog the motor to position while ignoring the limits. I then need to set limits. So how do I set limits as I am now using the left right to jog. Does this make sense. I could add another button that removed limits at any point but ideally I wanted to keep it simple by using just the memory button and left right to set limits. Maybe the design of this is not possible. Any thoughts...

updated code below:
Quote
/* X-axis Stepper Controller
The controllers primary functions are to produce a pulse train with a
step pulse of approximately 1 microsecond delay to the "step" pin and define
the "dircection" pin  1/0 to set the motor direction. Limits can be set in both
directions.

Functions:
left and right jog the motor until limit is reached.
To set limits, jog to tableposition of required limit. Hold memory button while
pressing the left or right  to set the corresponding limit. If memoryLED lights,
error and may need correcting - limitLeft >= limitRight

Development:
1.Variable Speed Control via ADC.
2.Rapid Return - pressing (new) button while holding left or right sets max speed
until limit is reached.
3.Limit to Limit - jogs motor between limits continually.
4.Limit to Limit; rapid return - as above but user sets one direction at top speed

Code Development:
1.Ensure that only one button is pressed at a time
2.In cases where the motor is stepping in one direction and the opposite button
is pressed, treat as a stop and require two presses to reverse direction
*/

// define which pins our buttons are on
#define right PORTC.b0
#define left PORTC.b1
#define stop PORTC.b2
#define memory PORTC.b3

// define which pins our motor is connected to
#define memoryLed PORTC.b4
#define motorLed PORTC.b5
#define step PORTC.b6
#define direction PORTC.b7

// LCD display config.
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections


long tablePos = 0;
long limitLeft = -100000;
long limitRight = 100000;
int motorRun = 0;

void interupt()    // step motor interupt - Only steps if motorRun is set.
{
    TMR0IF_bit = 0;    // clear TMR0IF
    TMR0L  = 96;       // reset counter

    if (motorRun)      // interupt only steps motor if motorRun is set in main.
    {

    // if going right then check right limit - if greater than limit, motorRun = 0, stops motor.
        if (direction)
        {
            if (tablePos >= limitRight)
            {
                motorRun = 0;
                return;
            }
        }
        else
        {
            if (tablePos <= limitLeft)
            {
                motorRun = 0;
                return;
            }
            }

    // these 3 lines step the motor
    step = 1;       // set the step line high
    delay_us(1);    // wait 1 micro second
    step = 0;       // and low again - the motor will now move one step
      // update the table position
    if (direction)
        tablePos++;
    else
        tablePos--;
    }
 }
 
//===========================================
// when the PIC is first switched on it will start to run at "main"

void main()
{
    char motorSpeed = 3;    // 3 = fast, 255 = slow
    char count;             // misc use
    int adc;                // somewhere to put the ADC we read

    // Set TMR0 in 8bit mode, assign prescaler to TMR0
    T0CON  = 0xC4;

    // set ADC's
    ADCON0 = 1;         // enable ADC
    ADCON1 = 0b1110;    // enable ADC only for ADC 0

    // set all the pins on PORT C to 'off' before we start
    PORTC = 0;

    // set the tri-state buffer. Pins set to '1' are input, '0' are output
    // we've done this in binary to make it easier to see
    // each number is 1 pin, right hand is pin 0, left is pin 7
    TRISC = 0b00001111;
    TRISB = 0;
    TRISA = 0b11111111;

    while(1)
    {
    //Sets interupt - triggers into step interupt but if motorRun is not set, motor does not run.
    TMR0L  = 96;         // Timer0 initial value
    INTCON = 0xA0;       // Enable TMRO interrupt

        // Checks left and right buttons - sets direction and motorRun
        if (left)
        {
            direction = 0;
            motorRun = 1;
        }
        if (right)
        {
            direction = 1;
            motorRun = 1;
        }
        // Stop button
        if (stop)
        motorRun = 0; // stop the motor

        // -- ----------------Limit Setting ------------------------
        // -- hold down memory and press left or right to set limit.
        // ---------------------------------------------------------
        while (memory)        // loops here while waiting for limit to be set.
        {
                // set limit left or right
                if (left)
                    limitLeft = tablePos;
                if (right)
                    limitRight = tablePos;
                   
                // wait for left/right to be released
                while (left || right);
        }
       
        // Checks limitLeft is not >= to limitRight
        //  memeryLed = 1 indicated error to user
        if (limitLeft >= limitRight)
        memoryLed = 1;      // COULD I ALSO DO SOMETHING ELSE HERE? maybe reset limit after a delay?
        else
        memoryLed = 0;

        //------------------------------
        // step the motor
        if (motorRun)   // are we moving the motor?
        {
            motorLed = 1;

            /* OLD ADC SPEED CONTROL NOT IN USE
            read the ADC and set the speed (delay)
            adc = 512;                   // MyADC needs replacing with  ADC_Read(0);
            adc = adc >> 2;         // divide by 4
            if (adc < 3)
                adc = 3;
            for(count = 0; count < 10; count++)
            Delay_Cyc(adc); */
        }
        else
            motorLed = 0;
        }
}
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: kvom on January 11, 2010, 07:57:20 AM
You could do limit resetting by memory-stop sequence.  Using the automaton model each button press is a 2-part state.  When you detect a press you enter a state waiting for a release, at which point you enter the state signalled by that button.  Here's some pseudo code:

  switch (state)
  {
     case stop:
       if (memory)  // memory button pressed
       {
          state = memory1;
          break;
        }

      case memory1:
        if (!memory)  // button released
        {
          state = memory2;
          break;
        }

      case memory2:
        // turn on LED if not already on
        // check for next keypress: memory, left, right, stop
        break;

    }
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: No1_sonuk on January 11, 2010, 08:11:57 AM
WRT setting limits:
You could do it so that you hold the memory button and both jog buttons together (like the MS CTRL-ALT-DEL combination) to disable the limits, then do it again to enable the limits after setting them as you already have set up.
If you don't reset the limit numbers when they're disabled, that also gives you the option to temporarily disable the limits at any time.

Having said that, a separate switch for limit enable/disable would make the code FAR simpler.  Even easier if it's a toggle switch.  Using a toggle switch would mean you'd only need to check the state of the switch when one of the limits is reached, rather than having to continuously monitor a pushbutton and store the result in a flag variable.  It also gives a physical indication of its status.

WRT this:
Quote
4.Limit to Limit; rapid return - as above but user sets one direction at top speed
Could be a bad idea if you're cutting - backlash, tool flex, etc. could mean you would cut on the way back.  On a lathe, that might make a shiny-smooth surface going one way, only to cut a shallow "thread" on the way back!  If it stopped and waited for the user to press a button, that'd give the option to move the work or cutter before the rapid traverse.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 18, 2010, 06:07:21 PM
A bit of an update. I`ve been doing some updates to my mill at the moment but I`m hoping I should have some time over the next week to get a first simple prototype built. Quite a bit to do yet.



Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on January 24, 2010, 06:32:30 PM
Getting there... the oldham coupling is now in place and motor fully mounted. I`m just waiting for a driver and then I can have a trial. Mean while I`ll keep updating the code...

(http://www.raynerd.co.uk/images/shuma2.JPG)
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on August 31, 2010, 07:45:26 PM
Not been touched since Jan 29th !!!

Well....I just got around to having a code that works to an extent. It needs lots of tidying but it works!!



Cheers Kwackers for the help!!!  :headbang: :ddb: :ddb:

Next job is to tidy the code and then build it on its own circuit.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: spuddevans on September 01, 2010, 03:33:49 AM
Well done Chris  :ddb: :ddb: :ddb: :clap: :clap: :clap:



Tim
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on September 09, 2010, 08:13:08 AM
Finally finished it!  :headbang:




Very pleased with it. There is a lot that could be improved but it will stay as it is certainly for a few months while I get on with other things.

Basic functions & instructions for use:

Central Green LED - flashes to show controller in ON
Left Blue LED - lights when motor is moving
Right Red LED - Lights when in memory mode when we can move beyond limits and programme them

Button Left-Green = Table moves left
Button Right-Green = Table move right
Press Red-Stop = Table Stops
Press Speed = speed scrolls between 0-9 (preset at 5 on PIC startup)

Limits are preset at startup to +10,000 and -10,000 steps,
To Set Limits:
Hold both Black buttons, RightLimitSet and LeftLimitSet together and release (the ones next to each green button) - controller goes into memory mode.
In memory mode movement is not restricted by limits. At any current table positon, either, left memory or right,memory button alone can be pressed to set the associated limit.

Moving between limits::
Hold LeftGreen AND LeftMemory and release - table moves to the left, hits the left limit and moves between the limits continuously.
Hold RightGreen and RightMemory and release - table moves to the right, hits the right limits and moves between the limits continuously.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 10, 2010, 04:49:51 AM
Hi Chris

No-one seems to have replied yet.. not very polite of 'em. No matter ... I'm impressed     :bow:

Been faffing with PICs for some months now, with MPASM..

Keep writing code that works, then find some code examples on the web to do exactly the same thing, but with about 1/10th of the instructions.  :doh:

Seem to recall you did this with a C compiler. Which did you use ?.

Need to get a grip of C I think.

BTW. What thickness of steel will the LED cut ??  :lol:
Not a blue one is it? I've got some that chuck out no end of light, even at 10 mA. Especially if you look directly at them. Had to shut them down to < 3mA ... even then too bright axially ..
 
Dave BC
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: slowcoach on September 10, 2010, 06:34:11 AM
Very impressive, good job  :bow: :bow:

Rob  :thumbup:
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: spuddevans on September 10, 2010, 07:39:44 AM
Well done Chris  :clap: :clap: :clap: A nice and tidy unit.


Tim
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on September 10, 2010, 03:48:14 PM
Cheers Guys....yep, it is working well. I had it running earlier today and am quite please with it but I`m actually going to have another hour and reprogramme the chip. I`ve been looking at the data sheets and think I can swap the 16f887 for 18f4520 using the same board and circuit and then use the 18f to convert the current "step" position to a measurement, inch and mm.

BlueChip - I was advised to use C code when I was first introduced to PICs. I wouldn`t change it one bit - C seems excellent! I spoke to a bloke who was into coding in Assembler a few months back, quite a random meeting but and he pratted around for ages showing me some assembler code and man, the number of lines of code he had to do for a simple programe! I could have done it in literally a couple of lines. OK - I don`t have a clue what is happening as much as he does inside the PIC itself but I suppose you have to decide if that matters. To be honest, the few times that I will want a PIC, I`m interested in the function I get from it, not how it is running. I know this is ignorant, but for the time I can afford to invest in it, its good enough for what I want. Great thing about C, is it is pretty much just English!

I use MikroC compiler for my code:
http://www.mikroe.com/eng/products/view/411/mikroc-for-pic/


It is great because I`ve also got Mikroelectronica`s EasyPIC 6 dev board. That being said, you can just use MikroC to generate your hex and then blow it to your chip any way you like.

Regarding those blue LEDs, yes it doubles as a lazer cutter. I`ve managed to put a hole in the ceiling above the controller.!

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Rob.Wilson on September 10, 2010, 04:15:41 PM
Very cool Chris  :bow: :clap: :bow: :clap: :bow: :clap: :dremel:


Rob  :thumbup:
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 11, 2010, 04:47:18 AM
Hi Chris

Tks for the info.

Couldn't work out just what they wanted from your link   :scratch:

As I have the 'lite' C18 with the MPLAB, I might have a go with that.
I doubt if I need it to be optimised for the bit of code I do.

I used to fool around with the 16F84 years ago, used ASM with that, as there was nowt else at the time as far as I'm aware. So, just plugged away with ASM when I returned a few months ago. I find it simple but tedious ..

Have fun ...

Dave BC





 
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: kwackers on September 11, 2010, 05:18:28 AM
There's no point writing a project in assembler, that's for masochists. (and I've spend decades writing assembler for almost every known processor going)

Write your project in C. Then if it turns out to be too slow optimise it - usually rethinking your method is plenty, failing that have a look at the assembler the C compiler is kicking out and rewrite the slow parts then link them back (as assembler) into your C project (or inline using the asm keywords). Finally, PIC's are cheap! So just use a faster one!

PIC16's aren't particularly good targets for C compilers so you can generally optimise code for them quite well, 18's are much better targets and by the time you get to the 32's you'd struggle to beat a good C compilers optimisations anyway so would be better off optimising by changing the method.

Where the Mikro stuff scores over things like the lite version of the compiler that comes with MPLAB is the fairly large set of libraries. Need an LCD? Just make the library call, GLCD? Ditto. They provide libraries and example code for everything from pressing a button, through USB all the way to having a wireless network and serving web pages.
To be fair to use these libraries you'll probably run out of space on the free version and will need to buy the full version.
The downside is it's not the best compiler when it comes to producing optimised code, but as I mentioned above there are ways...
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 11, 2010, 06:46:29 AM


Write your project in C.



Hi Kwackers

I don't know C, so it's a bit difficult to write it in C ...

I have never yet found any 'tutorial' or whatever that actually assumes you know nothing about it.

One has the usual 'flash a LED' stuff, gives some example code, but does not explain all of the damn symbols / directives etc.

Some can be inferred, others I've not a clue ...  :bang:

Void ??

Braces ??

// appears to be comment ?

Gave up  :(

Apologies to Chris for hi-jacking his thread  ..

Dave BC



Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: kwackers on September 11, 2010, 08:00:16 AM
If you really want to give it a go send me a PM with your questions and I'll answer them.

But some simple things to help:-

your program starts in the function 'main' so:

void main()

this means you have a function that returns no result (hence the void)

You could return an integer - eg

int fred() 

would be a function called fred that returns an integer (note that main never returns in a PIC so there's no point in it returning anything).

the braces simply collect code together so:

void main()
{
    // you're right this is a comment - until the next line

   // everything between the braces is part of main

   {
      // you can open as many braces as you like - it can help to keep bits together
      // but remember the closing ones
      // and indenting them makes the code look nice....
   }
}


Most directives have a '#' in front of them
the most common one is a string substitution, e.g.
#define A_VAR 23

whenever A_VAR appears then 23 is substituted, handy for defining registers and stuff you might want to change later.

Another is the #include directive, this simply takes the filename after it and inserts it into the file (as though you'd cut and paste the whole thing in)
It's most common use is for header files - usually a file full of #defines that are appropriate for the PIC you're using.

Hope that's of some help.

p.s Chris doesn't mind - I get the same questions off him all the time    :)
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on September 11, 2010, 08:07:19 AM
Bluechip - your not hijacking my thread...all good discussion is worth while! Kwackers taught me what I know ... he knows what he is talking about for sure!!

Download mikroC from the link I sent, it is the top download - MikroC compiler.

Kwacks is right with regards to the library - it is bloody fanstastic! Take me for example, a dumb arse when it comes to PICs: So OK, I can write a few lines of "if, else, while"...bla but I wouldn`t have a clue how to get the LCD to run. Well you just call on the LCD library, look at the example and change it suit your needs. If you scout about on the MikroC website there is a download containing examples for the 16F887 and if you open these up with MikroC you can pretty much see how things are working. So like you said there is an LCD example, a flashing LED, GLCD, TMR1, TRM0 example etc. Loads of them, they are really useful.

With regards to learning,... don`t give up it really is fairly easy. I`m sure Kwackers will quickly pick me up if I start spouting rubish but here is a simple code:

// <<-- these are comments so I`ll comment through it

void main()      // this is where the programme starts
{

// This is the PIC setup specifically for the 16F887

  ANSEL  = 0;            // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;          // Disable comparators
  C2ON_bit = 0;

  TRISA.b0 = 0x00;          // Bit 0 on PORTA - TRIS sets pins as output   TRIS 0XFF would be input
  PORTA.b0 = 0x00;          // initiates it at 0
  TRISA.b1 = 0xFF;          // Bit 1 on PORTA is an INPUT - lets say a button
  PORTA.b1 = 0x00;          // initiates it at 0
 
  while (1)                 // while (Statement) is true, it does everything between the braces -
                           //  1 is always true so this is an endless loop.
  {
     if (PORTA.b1)           // if we press the button
     {                      // do everything between these brackets
       PORTA = 0xFF;        // Turn ON LEDs on PORTA
       Delay_ms(1000);      // 1 second delay
       PORTA.b0 = 0x00;        // Turn OFF LEDs on PORTA
     } 
  }
}             

So this code is going to turn on the LED for 1 second each time you press the button.

Nice thing about C is you can make it a little more simple by using define - same code using define:


#define LED1 PORTA.b0   
#define button PORTA.b1

void main()      // this is where the programme starts
{

// This is the PIC setup specifically for the 16F887

  ANSEL  = 0;            // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;          // Disable comparators
  C2ON_bit = 0;

  TRISA.b0 = 0x00;          // Bit 0 on PORTA - TRIS sets pins as output   TRIS 0XFF would be input
  PORTA.b0 = 0x00;          // initiates it at 0
  TRISA.b1 = 0xFF;          // Bit 1 on PORTA is an INPUT - lets say a button
  PORTA.b1 = 0x00;          // initiates it at 0
 
  while (1)                 // while (Statement) is true, it does everything between the braces -
                            //  1 is always true so this is an endless loop.
  {
     if (button)            // if we press the button
     {                      // do everything between these brackets
       LED1 = 0xFF;         // Turn ON LED
       Delay_ms(1000);      // 1 second delay
       LED1 = 0x00;         // Turn OFF LED
     } 
  }
}


Well I hope I`ve not mucked up somewhere but if I can do it, I`m sure you certainly can!
Chris

Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on September 11, 2010, 08:08:33 AM
:) we posted at the same time and after that effort didn`t want to delete my post. Ignore mine :D :wave:
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: AdeV on September 11, 2010, 09:43:08 AM
String comparisions (strcmp) are the bits that always get me in C; that, and pointers. Or dereferenced pointers.... I mean, what IS a dereferenced pointer, and why would one desire it?

FWIW, I've been programming in BASIC (in various incarnations - Sinclair SuperBASIC, BBC BASIC, GWBASIC, Visual Basic versions 3-6, VB.NET 1.0 & 1.1) for the last 24 years or so, so it's not like I can't program.... yet I've never ever had to (explicitly) work with a pointer...

Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: kwackers on September 11, 2010, 10:39:46 AM
I'm not sure why you have an issue with string comparisons although they do require pointers.

So quick pointer post.

A pointer is just an address to computer memory. If your machine has 64k of memory then it's a number from 0 to 64k.
Everything you type is in memory.

So in basic if you type:  let A$ = "hello world";
Then the string "hello world" sits in memory somewhere and A$ is probably internally a pointer to it.
(excuse my basic, it's 30 years since I last wrote any).

So say you look through memory and find your string at address 27000, then A$ would be the number 27000.

In C you do the same like this

char A[] = "hello world";

The variable A is a pointer to a type of 'char' (which is 8 bits per character - i.e. a string) by putting the []'s after it we're telling it that it's a pointer to an array of characters.
The string is placed into memory and the variable A is made equal to it's address.

Being a pointer to an array we can index using the brackets, so A[4] would be the letter 'o' (0 to length of string).

Normally you'll see pointers declared with asterisks like this:
char *B = A;
This declares a pointer to types of char named B and makes it equal to A

Therefore B[4] is also equal to 'o'...

Now because the pointer is not the string - it's the address of the string, if we look at it we'll just get the number (in our example we decided it was 27000).

But by using the []'s and looking at the 4th element in the examples above we're 'de-referencing' the pointer (and adding 4). De-referencing is simply the obtaining of the contents of the memory at the location pointed to by the pointer.

Without brackets we can dereference by using a '*' in front.

so *A is the same as A[0],  *(A+4) is A[4] etc etc (brackets used to make sure the addition happens first).

The main reason to dereference is to do pointer arithmetic and then look at what's in the address you calculated. Pointers can point to any type you have or can define (using structures etc).

If this makes sense then back to string compares.

char A[] = "my string";  // this string is 9 characters long - including the zero
char B[] = "my strang";

// pass the two pointers to our strings to strcmp
if (strcmp(A, B) == 0)
{
   // strings are equal
}

strcmp expects two pointers, in this case we created two pointers A & B that already point to some strings in memory.
the C language says strcmp returns zero if the strings are the same, so we use the 'is equals' operator '==' to check if its equal to zero.
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 11, 2010, 11:12:40 AM

 ( 1 )  Download mikroC from the link I sent, it is the top download - MikroC compiler.

 ( 2 )  Take me for example, a dumb arse when it comes to PICs:



Hi Chris

( 1 )  Done ...

( 2 ) All very well, but what about we poor souls whose evolution has not reached that exalted stage ?   :scratch:

Thanks for the offer of help Kwackers ... will take you up on that  :thumbup:

I think this would be better in a separate thread ? Wonder how many others are interested in C PIC compilers ?? Gotta be some, world appears to be awash with nutters at the moment ...

So .. having pulled out a 16F877 LED flasher wotsit .. and ...

Q1  How do I change that hideous 'green text on a black background' screen. I can do it on MPASM, ( when I eventually found how to ).

Dave BC
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on September 11, 2010, 12:44:28 PM

Q1  How do I change that hideous 'green text on a black background' screen. I can do it on MPASM, ( when I eventually found how to ).



Green text on a black background in MikroC ?? - mine starts up with the theme Whidbey in the Styles Toolbar
: View --> Toolbars --> Styles Toolbar     and select whidbey or another that isn`t green!     

Does that solve the issue? I`m wondering if we are talking about different things as I can`t see any view styles with black and green!
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 11, 2010, 01:59:33 PM

Hi Chris

I give the Icon a wallop, things happen, and I land up with this ..


(http://i359.photobucket.com/albums/oo31/Bluechip_mech/mikroC.jpg)

If I do the New Project screen, as per the getting started .pdf it does not work for me. I made a folder on C:\ and called a new project Prog_1

Clicked OK after fiiling in the bits it required. Refused to save it.

So, I got no new project screen. Dunno what that looks like. Must be better ... :scratch:

Dave




Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on September 11, 2010, 02:12:47 PM
Ewww...yes, that isn`t too pretty. Don`t let it put you off lol

Have you tried :

Tools --> options --> Colours       and then change the colours or the scheme?

I`m running V3.2 MikroC Pro and didn`t change anything from how it originally came, viewing wise.

Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 11, 2010, 02:34:16 PM

Chris

It appears to be stuffed anyway. Have found how to alter the screen but it's a no go for some reason.

If I try to apply the new screen, it says 'cannot write to etc.etc. some file.

Same as for the new project ....  :scratch:

This is version v8.2 wossat then ?

Officially abandoned for now. De-installed  .. back to ASM   :bang:

Dave
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on September 11, 2010, 06:37:02 PM
Noooooooooooo !!!!!!

Don`t do that ... try a fresh install because something doesn`t seem right!

Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 12, 2010, 03:37:17 AM
Chris

Did that. Still no joy ...  :(

If you look at the link you posted, one of the .pdf's to download is one for a licence key.

The text infers, to me anyway, that if you restrict the code to some 2k ( IIRC ) you don't need one.

I think maybe you do.

I had a PCB layout prog. about  2 years or so ago. You still had to e-mail the the varmints for a key to get the 'Freeware' version to ekkle.
Needn't have bothered. Was hoping it was a bit more intuitive than Eagle .. if anything it was worse..   :doh:

Anyway, off to uChip, grabbed C18, kicked it into shape, pinched a bit code off the web, compiled, stuffed into a 'F819. And off it went  :D

QED.

I might have another go at mikroC in a bit, when it's too gruesome to be outside.

Dave BC



Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: raynerd on September 12, 2010, 05:13:52 AM
Yes, your right about the 2k code limit BUT the software is identical, you just add a licence key and it removes the limit. Well I`m sorry it didn`t work for you, I`m still not entirely sure why.
Chris
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 12, 2010, 05:29:05 AM
Yes, your right about the 2k code limit BUT the software is identical, you just add a licence key and it removes the limit. Well I`m sorry it didn`t work for you, I`m still not entirely sure why.
Chris

Not sure it is identical. I come up with version 8.2, not the same as yours ..

No matter, the uChip C18 works fine, or so it seems.
So, I'll go with that.

Not sure whether C18 has the 'macro' gizmos yours has, but I don't have any need for them anyway at this time ..

Onward and Upward ... and back to getting about 2 tons of tomatos off. Bugger. Why do they all ripen at once? Most irresponsible of 'em.

Dave BC
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: DMIOM on September 12, 2010, 06:08:06 AM
...... C compiler. Which did you use ?. ................

Dave (BC)

FWIW I've had to use a variety of C compilers - I use C and C++ for PCs, and Paradigm C++ for embedded x86 (TERN) boards. When we started using PICs (app 2004) I bought the IDE from Forest Electronics (Fored) (http://www.fored.co.uk) as, at that time, it seemed to offer the best value combination for dipping our toes into the PIC world.

The Fored "Wiz-C" system (IIRC, originally branded "PIC-C" / "Pixie" but changed possibly due to copyright issues) has an IDE with an ANSI compiler, a text editor (albeit with some quirky keystrokes), a graphical building blocks editor where you drag & drop elements (which I don't use), in-circuit debugging, and, most importantly for us, a simulator.  Whilst there is no native version control system, I use the same Subversion/Tortoise which I use across other development toolsets as well.

The Fored system has done OK for us, once I got used to it; the code it produces is good, tight and reliable; and we have developed a number of systems which both I and my clients are happy with.

Fored do provide updates and bug fixes (although the Yahoo group can be a quicker resource); but the biggest problem for Fored, as a small development outfit, seems to be keeping up with the perenially evolving PIC family (both in terms of processor definitions and also adding new support libraries) - this may not be an issue in a hobby environment but can be in a commercial one.

Was it the right thing to buy in 2004? yes, even in hindsight, I still think so.

Would I buy it again now if I was starting over again with relatively little PIC experience? quite possibly.

Would I buy Fored now for serious, open-ended commercial development? knowing what I do now, almost certainly not.

Am I going to stick with Fored? .... that's a harder question.  We have a certain investment in Fored, something in capital terms but far more in terms of time and developed code.  However, since starting with Fored, we have also built up a significant Proteus ISIS/ARES system.  We already use the Proteus mixed-signal simulation - but Proteus also have MPLAB integration and a PIC simulator.  If, for example, we move to using dsPIC (which is not supported by Fored) we will have to move to a new IDE and compiler, and in fact last winter I started a low-priority project to assess the implications of migrating to either Microchip's own, or HiTech C compiler.

Dave (IOM)
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 12, 2010, 08:41:19 AM
Hi Dave (IOM)

That looks interesting. £50 is not a killer is it, if you need the full Monty? Good link  :thumbup:

Having got C18 up and running, I'll stick with that for now. Even when the Optimizer doins times out, it's very likely much more than I'll need.

IOM ? Is that Okells still going? Very effective at erasing brain cells IIRC  :lol:

Dave BC



 

Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: z3t4 on September 22, 2010, 11:51:58 AM
I have never yet found any 'tutorial' or whatever that actually assumes you know nothing about it.
Dave BC


Hi Dave

Speaking as another who's interested in messing with PICs but (in my case) almost entirely devoid of any skill or knowledge, I'm finding the Gooligum tutorials (http://www.gooligum.com.au/tut_baseline_C.html) really helpful. Apols if you've seen these already.

John
Title: Re: X2 X-axis Stepper motor Power Feed - (possible CNC conversion?)
Post by: Bluechip on September 22, 2010, 01:12:43 PM
Hi John

Have seen that one. Some useful bits on it. I'm on MPLAB C18 at the moment, all I could get to download and perform.
Quite a lot of tutorial sites seem to use Hi-Tech C, CCS,  or some other compiler. Never too sure whether code works on C18 or not. Usually yes, but there are exceptions it seems ...

Not done too much lately, waiting for the weather to close in .. not long to Crimbo now  :D  :D ..

Pratting about with uChips seems more appropriate when it's too miserable to be out.  ::)

Dave BC