I think I can find a way to get a scope and power supply. I spent the morning coffee looking into PWM at 16 bits on an SX.
An SX28 would seem to provide a good learning environment. On the SX-48/52, I could easily save some effort by using the built-in timers. But I am beginning to see why to mention your RTOS. It can pull the pieces together in a very powerful stable way.
As it is....
I figure I need a 16 bit counter (that will take two 8 bit registers) running endlessly at 20Khz for the 20khz pulse of the PWM.
Then, I need two have two 8 bit registers (one for PW 'on condition' and one for PW 'off condition'
I also need an 8 bit 'Set up register" for setting the pulse in your 5% to 100% range of on. We could use 200 steps to get the math right. Or we could just have it run with 256 steps to have a little finer gradation. The least on value would be 3.9% or so.
And then I need to set up two input buttons for a plus 1 or a minus 1 to that Set up register to be adjusted from 0-255 or 0-200. Actually the 0-200 seems like I would never get full on, so I am going to say "Forget 200 steps for now", as percent math is not as important as staying with good function and binary math. 200 steps creates more work to get either down to the bottom or up to full on.
Also, the button will need a delay on the increment up or down or you will never get things set right. Let's say 1/4 of a second between changes in increment. That might seem slow for starters, but it I run a simulation in real time, I do want to see what is happening.
And soo....
If we are going to use the SX-28, are we going to move to that Forum and start a new thread or just endlessly continue here in the Sandbox.
Pjv has proposed something major. A tutorial on general electronics would have a sizeable audience. I would hope the focus would remain with the growing propeller community. He has said it would require a major commitment of his time. For this reason we cannot keep it to ourselves.
I even think a 'google' search would draw people in from outside the Parallax community. All micro's connect to electronics at some point. Consider a forum category, a download, an article, a webpage. A tutorial ->inside a thread<- would wander all over the place. I would bookmark this tutorial and perhaps print it out.
Propeller or SXes?
I've asked several times. I was just about to get into PWM on an SX-28 and go from there. Now you sound like you don't want that. Nonetheless, don't worry. I am a bit intrigued by how it could be done in 32-bit and on multiple cogs.
I do realize the SXes End of Life announcement came out about a year ago. But, I still work with them. "pjv" is a 'past master' at them and I would like to implement this with the RTOS. Also, the SX simulation tool really is very handy for getting into the details that Assembler presents. There are simply more software tools and a very, very good tutorial reference by Guenther.
And even then, we can go with the Prop...
But, WE must agree on what clock frequency this is all going to take place at. Going too high in the Prop just makes more work to bring it down.
I absolutely won't try to work on a collaborative project (SX or Propeller) where different people are working with a different fundamental clock rates. It is okay to work up a different method in code to get the same results, but trying to sort out the math between different clocks is absurd and bizarre.
The SXes have 'pre-scalers' that help out, but that really isn't a Prop feature. In the Prop we might still set up a counter (but maybe 32bit), then only use an 8 bit or 16bit portion for PWM for defining our duty-cycle. I am thinking that the 16bit portion should come in at the 20khz target.
-- Let's do a little binary math. That would have 4 bits of higher counts and 4 bits of lower.
20,000 x 2 x 2 x 2 x 2 = 320,000Khz for the 32-bit counter. That is still slow by Propeller standards.
Why use a 32 bit counter and then read only 16 bits?
With a 32bit counter easy to implement, that 16 bit could be slid left or right to change the PWM's duty-cycle to faster or slow. (By 'duty-cycle', I mean both the high and the low of the pulse as 1 cycle.) By having four bits up and four bits down, we have a 9 speed PWM generator. That could come in hand later. And if we decide that reading only 8 bits is necessary (which is more likely as we would have 256 speeds between 3.9% and 100%), the variable speed becomes greater at the low end.
BTW, I am still trying to figure out how to make the 5% to 100% a strict compliance rather than just dismiss it as inconvenient. On the SX without the adjustable speed it would have been easy by limiting the counter to 200 ticks in the high byte and not having it roll over. But with the Propeller, we may have to stay without adjustable speed if we want certainty of a 0.5 gradation.
Anyway, it is likely best to set aside the decimal math and start with building a good PWM that relies purely on binary math as a starting point.
It looks as though the Propeller Assembler takes on a very different approach to Assembly. It certainly is NOT going to easily transfer whatever we learn to PICs or AVRs. But it will open up the power and glory of 32-bit.
Rather than having so many physical registers to exploit and manage as RAM, each cog of the Propeller has a 'Stack' that gives a place for holding variables as 8bit, 16bit, or 32bit.
In other words, instead of combining two 8-bit registers into a 16-bit counter; we can chop up a 32-bit counter to fit our needs. It all becomes are very different approach. One is building up, the other is building down.
I read your comments, and had not really considered other followers when I preferred the SX over the Propeller, but now thinking about that again, the SX forum is pretty much toast, so a much bigger audience would follow it in the Propeller forum. So that's what I now think.
In order of preference (convenience really), a Propeller Prof. Dev. board, or Propeller Demo board, or Propeller Proto board will be required. And to make sense out of the tutorial explanations, an oscilloscope is essential, although there still is value in following the tutorial without one. And of course we need the logic level Nchannel power mosfets.... there are hundreds to choose from on the Digikey website.
Loopy, you are focusing way too much on the size of the Propeller counters.... they won't be used at all.
So if we are all agreed, I will break the subject of driving stepper motors into about half a dozen "lessons", and start a new thread for that.
StefanL38.... sorry that we have rather badly abused your thread.
@Pjv:
Yes, it is really a waste of your efforts to teach us this with a small audience.
Please advice on scope requirements. What is the minimal frequency and is their a trigger feature required?
Okay, Propeller it is. I have a Propeller Demo Board and a Propeller Proto Board available, but I suspect the Demo Board is simplest as it has direct USB hookup.
Looking into the SX literature, I did find three examples, but only one has the stable duty cycle. It really isn't much code and 8 bit resolution seems to apply.
Have you already started another thread in the Propeller Forum?
Basically a restatement of goals and a specific clock rate are all I need.
I was just about to settle on 4Mhz in the SX-28. Is the same good for the Propeller?
This is based on 4,000,000/200 = 20,000. So a 4Mhz Xtal seemed an easy way to get to 20Khz and to have exactly 5% as the bottom. (Though it seems that one cannot have 256 increments and 5% together without a lot of wasteful extra code nonsense. Some of Pjv's original spec may be contradictory.)
Or are we not concerned with holding exactly to 5% as our low. I am sure there may be other reasons for a fast speed - like eventual video output on another Cog. It certainly is a different approach and I may continue to do something with the SXes on my own.
For the Propeller, we already have two objects in ZIP files available for reference.
pwmAsm.spin
Unipolar_Stepper.spin
My first impression is that the PWM could be ANDed to the output pins of the Unipolar_Stepper, and you would have two Cogs running.
That may be a 'brute force' method and I need to study the code to see how much I understand. But the output pins can be shared and the PWM can pull low the high outputs at 20Khz while the steppers operate at an independent rate.
Unipolar_Stepper.Spin is my contribution to the obex. You can see Jon William's influence all through it for which I thanked him. I tweaked his code quite a bit so I could learn how it worked, modified it and then added some of my own insights. It's being downloaded so there must be a need for it. I gave my personal email address and haven't heard anything so I guess it's OK.
For all of the help you get here you have to give something back. I want to do it again.
Since I don't write in Spin, I do not frequent the OBEX, so I'm largely unaware of what that contains. Given that you (Lardom) already have sufficient knowledge to enter your own contribution, my efforts would largely be a duplication, and hence not a good use of my time.
So I think I will withdraw my offer of writing a stepper motor tutorial, and carry on with my other activities.
I'd be happy to help answer specific questions you might have.
NOTE - I have removed a substantial section of this posting that was in error.
@Pjv:
I really do value your input. If anything, you have made me look at why I haven't bought a scope and a good power supply. Without them, more sophisticated design isn't really possible.
You position seems reasonable, but I still may do all this in SXes, just to satisfy my own curiosity. And revisiting RTOS appeals to me as I couldn't find a use for it before.
Regarding the Propeller and SPIN doing this......
I looked into those two objects mentioned above in OBEX and together they seem to help create a PWM Unipolar Stepper application through combination. I still need to take a look at your PropRTOS code.
The nature of SPIN is OOP. And that means it appears easy to cobble together pieces to create something else, but that isn't the case here. Also, it means that Assembly Language is not nearly as close to the processor as it is in the SXes.
Further, the SPIN Assembly language is generally wrapped within SPIN OOP and that is very, very different from a stand-alone approach to Assembly Language. This thread has helped me to finally see the differences in a useful way. Tradition Assembly Language is used to start programing from scratch, but the Propeller never shows that firmware and has no reason to as the timing and access needs to be rigorously held to one way.
Some big differences are as follows:
1. The Propeller has two 32-bit counters on each COG, so there appears to be little or no reason to construct a counter in software (am I wrong?), unless one just wants to demonstrate how that might be done. (Nonetheless, it is an interesting way to investigate timing throughout the Propeller and how it reaches an individual COG.)
The PWM object I looked at uses one hardware counter and goes up to 80Khz. It really is hard to beat this object unless you want to output something else beside a square wave or want to use both counters to produce 2 PWMs on one Cog. There are other wave forms (sine, saw-tooth, etc.) to explore that may squeeze higher performance out a stepper.
2. The examples of PWM I have in SX are all driven by an ISR (Interrupt Service Routine), and the Propeller doesn't offer ISRs.
So that approach doesn't really lead very far, though I think the code does provide the core of what has to happen at an assembly language level.
3. The Unipolar Stepper object offers quite a bit, but the author admits it is lacking in two ways.
First is that it really isn't coded to be accessed from another Main object. That problem seems trivial and a very good exercise to learn good OOP format.
And second, there is no demonstration module. Again, it won't hurt to produce your own.
~~~~~~
So, it seems that we are nearing the end of this thread and exploring the topics it has brought up. Please feel free to PM me if you have questions that you don't want to post.
And thanks again Pjv. You've always been generous with your patience and knowledge.
Further conclusions.
To combine the output PWM and a Unipolar Stepper with a logical AND seems to best be done on ONE cog. Pjv's PropRTOS may be necessary to get the two working together. Though the Prop doesn't have ISRs, Pjv has found a powerful way around that issue.
Comments
An SX28 would seem to provide a good learning environment. On the SX-48/52, I could easily save some effort by using the built-in timers. But I am beginning to see why to mention your RTOS. It can pull the pieces together in a very powerful stable way.
As it is....
I figure I need a 16 bit counter (that will take two 8 bit registers) running endlessly at 20Khz for the 20khz pulse of the PWM.
Then, I need two have two 8 bit registers (one for PW 'on condition' and one for PW 'off condition'
I also need an 8 bit 'Set up register" for setting the pulse in your 5% to 100% range of on. We could use 200 steps to get the math right. Or we could just have it run with 256 steps to have a little finer gradation. The least on value would be 3.9% or so.
And then I need to set up two input buttons for a plus 1 or a minus 1 to that Set up register to be adjusted from 0-255 or 0-200. Actually the 0-200 seems like I would never get full on, so I am going to say "Forget 200 steps for now", as percent math is not as important as staying with good function and binary math. 200 steps creates more work to get either down to the bottom or up to full on.
Also, the button will need a delay on the increment up or down or you will never get things set right. Let's say 1/4 of a second between changes in increment. That might seem slow for starters, but it I run a simulation in real time, I do want to see what is happening.
And soo....
If we are going to use the SX-28, are we going to move to that Forum and start a new thread or just endlessly continue here in the Sandbox.
I even think a 'google' search would draw people in from outside the Parallax community. All micro's connect to electronics at some point. Consider a forum category, a download, an article, a webpage. A tutorial ->inside a thread<- would wander all over the place. I would bookmark this tutorial and perhaps print it out.
I've asked several times. I was just about to get into PWM on an SX-28 and go from there. Now you sound like you don't want that. Nonetheless, don't worry. I am a bit intrigued by how it could be done in 32-bit and on multiple cogs.
I do realize the SXes End of Life announcement came out about a year ago. But, I still work with them. "pjv" is a 'past master' at them and I would like to implement this with the RTOS. Also, the SX simulation tool really is very handy for getting into the details that Assembler presents. There are simply more software tools and a very, very good tutorial reference by Guenther.
And even then, we can go with the Prop...
But, WE must agree on what clock frequency this is all going to take place at. Going too high in the Prop just makes more work to bring it down.
I absolutely won't try to work on a collaborative project (SX or Propeller) where different people are working with a different fundamental clock rates. It is okay to work up a different method in code to get the same results, but trying to sort out the math between different clocks is absurd and bizarre.
The SXes have 'pre-scalers' that help out, but that really isn't a Prop feature. In the Prop we might still set up a counter (but maybe 32bit), then only use an 8 bit or 16bit portion for PWM for defining our duty-cycle. I am thinking that the 16bit portion should come in at the 20khz target.
-- Let's do a little binary math. That would have 4 bits of higher counts and 4 bits of lower.
20,000 x 2 x 2 x 2 x 2 = 320,000Khz for the 32-bit counter. That is still slow by Propeller standards.
Why use a 32 bit counter and then read only 16 bits?
With a 32bit counter easy to implement, that 16 bit could be slid left or right to change the PWM's duty-cycle to faster or slow. (By 'duty-cycle', I mean both the high and the low of the pulse as 1 cycle.) By having four bits up and four bits down, we have a 9 speed PWM generator. That could come in hand later. And if we decide that reading only 8 bits is necessary (which is more likely as we would have 256 speeds between 3.9% and 100%), the variable speed becomes greater at the low end.
BTW, I am still trying to figure out how to make the 5% to 100% a strict compliance rather than just dismiss it as inconvenient. On the SX without the adjustable speed it would have been easy by limiting the counter to 200 ticks in the high byte and not having it roll over. But with the Propeller, we may have to stay without adjustable speed if we want certainty of a 0.5 gradation.
Anyway, it is likely best to set aside the decimal math and start with building a good PWM that relies purely on binary math as a starting point.
It looks as though the Propeller Assembler takes on a very different approach to Assembly. It certainly is NOT going to easily transfer whatever we learn to PICs or AVRs. But it will open up the power and glory of 32-bit.
Rather than having so many physical registers to exploit and manage as RAM, each cog of the Propeller has a 'Stack' that gives a place for holding variables as 8bit, 16bit, or 32bit.
In other words, instead of combining two 8-bit registers into a 16-bit counter; we can chop up a 32-bit counter to fit our needs. It all becomes are very different approach. One is building up, the other is building down.
I need to thing more and get back to you all.
I read your comments, and had not really considered other followers when I preferred the SX over the Propeller, but now thinking about that again, the SX forum is pretty much toast, so a much bigger audience would follow it in the Propeller forum. So that's what I now think.
In order of preference (convenience really), a Propeller Prof. Dev. board, or Propeller Demo board, or Propeller Proto board will be required. And to make sense out of the tutorial explanations, an oscilloscope is essential, although there still is value in following the tutorial without one. And of course we need the logic level Nchannel power mosfets.... there are hundreds to choose from on the Digikey website.
Loopy, you are focusing way too much on the size of the Propeller counters.... they won't be used at all.
So if we are all agreed, I will break the subject of driving stepper motors into about half a dozen "lessons", and start a new thread for that.
StefanL38.... sorry that we have rather badly abused your thread.
Cheers,
Peter (pjv)
Yes, it is really a waste of your efforts to teach us this with a small audience.
Please advice on scope requirements. What is the minimal frequency and is their a trigger feature required?
Okay, Propeller it is. I have a Propeller Demo Board and a Propeller Proto Board available, but I suspect the Demo Board is simplest as it has direct USB hookup.
Looking into the SX literature, I did find three examples, but only one has the stable duty cycle. It really isn't much code and 8 bit resolution seems to apply.
Have you already started another thread in the Propeller Forum?
Basically a restatement of goals and a specific clock rate are all I need.
I was just about to settle on 4Mhz in the SX-28. Is the same good for the Propeller?
This is based on 4,000,000/200 = 20,000. So a 4Mhz Xtal seemed an easy way to get to 20Khz and to have exactly 5% as the bottom. (Though it seems that one cannot have 256 increments and 5% together without a lot of wasteful extra code nonsense. Some of Pjv's original spec may be contradictory.)
Or are we not concerned with holding exactly to 5% as our low. I am sure there may be other reasons for a fast speed - like eventual video output on another Cog. It certainly is a different approach and I may continue to do something with the SXes on my own.
pwmAsm.spin
Unipolar_Stepper.spin
My first impression is that the PWM could be ANDed to the output pins of the Unipolar_Stepper, and you would have two Cogs running.
That may be a 'brute force' method and I need to study the code to see how much I understand. But the output pins can be shared and the PWM can pull low the high outputs at 20Khz while the steppers operate at an independent rate.
For all of the help you get here you have to give something back. I want to do it again.
Since I don't write in Spin, I do not frequent the OBEX, so I'm largely unaware of what that contains. Given that you (Lardom) already have sufficient knowledge to enter your own contribution, my efforts would largely be a duplication, and hence not a good use of my time.
So I think I will withdraw my offer of writing a stepper motor tutorial, and carry on with my other activities.
I'd be happy to help answer specific questions you might have.
Cheers,
Peter (pjv)
@Pjv:
I really do value your input. If anything, you have made me look at why I haven't bought a scope and a good power supply. Without them, more sophisticated design isn't really possible.
You position seems reasonable, but I still may do all this in SXes, just to satisfy my own curiosity. And revisiting RTOS appeals to me as I couldn't find a use for it before.
Regarding the Propeller and SPIN doing this......
I looked into those two objects mentioned above in OBEX and together they seem to help create a PWM Unipolar Stepper application through combination. I still need to take a look at your PropRTOS code.
The nature of SPIN is OOP. And that means it appears easy to cobble together pieces to create something else, but that isn't the case here. Also, it means that Assembly Language is not nearly as close to the processor as it is in the SXes.
Further, the SPIN Assembly language is generally wrapped within SPIN OOP and that is very, very different from a stand-alone approach to Assembly Language. This thread has helped me to finally see the differences in a useful way. Tradition Assembly Language is used to start programing from scratch, but the Propeller never shows that firmware and has no reason to as the timing and access needs to be rigorously held to one way.
Some big differences are as follows:
1. The Propeller has two 32-bit counters on each COG, so there appears to be little or no reason to construct a counter in software (am I wrong?), unless one just wants to demonstrate how that might be done. (Nonetheless, it is an interesting way to investigate timing throughout the Propeller and how it reaches an individual COG.)
The PWM object I looked at uses one hardware counter and goes up to 80Khz. It really is hard to beat this object unless you want to output something else beside a square wave or want to use both counters to produce 2 PWMs on one Cog. There are other wave forms (sine, saw-tooth, etc.) to explore that may squeeze higher performance out a stepper.
2. The examples of PWM I have in SX are all driven by an ISR (Interrupt Service Routine), and the Propeller doesn't offer ISRs.
So that approach doesn't really lead very far, though I think the code does provide the core of what has to happen at an assembly language level.
3. The Unipolar Stepper object offers quite a bit, but the author admits it is lacking in two ways.
First is that it really isn't coded to be accessed from another Main object. That problem seems trivial and a very good exercise to learn good OOP format.
And second, there is no demonstration module. Again, it won't hurt to produce your own.
~~~~~~
So, it seems that we are nearing the end of this thread and exploring the topics it has brought up. Please feel free to PM me if you have questions that you don't want to post.
And thanks again Pjv. You've always been generous with your patience and knowledge.
To combine the output PWM and a Unipolar Stepper with a logical AND seems to best be done on ONE cog. Pjv's PropRTOS may be necessary to get the two working together. Though the Prop doesn't have ISRs, Pjv has found a powerful way around that issue.