Spin and PWM; Abandoned
bbrien
Posts: 561
I am giving up on propeller C and going back to Spin. cant get serial to work so I shall use 4 outputs from one "flip' to the inputs of a second mirror the pulses on the outputs of a second 'Flip'. First question is what is "parse". Second question ,I need a pwm pulse around 50 Hz with a duty cycle of 1.5 to 2.5 ms. How do I set the number of "ticks".
Comments
Your pulse frequency and pulse width is close to the band of common hobby servos (50Hz and 1mS to 2mS with 1.5mS mid-band)
There are a large number of SPIN and C programs on OBEX and elsewhere for hobby servo control, so you can modify one of those for your requirements.
To get fixed-frequency PWM you need to run your code in a loop; this is where waitcnt is your friend. For example: I generally think of PWM having a duty cycle expressed as a percentage of the period; your case seems more servo-like. Is that the case? More details would be helpful. Each cog has two counters than can create precision pulses, but in your case if the pulse is will never occupy the entire period, you can use one counter and stack them.
If you provide more details I can knock up a bit of code to help.
If that's the case, this code works (I checked with a 'scope). As this is a specialty thing, there's no need to create an external object file -- you can use what I call an "embedded object." They're not always as flexible as standard objects (e.g., which IO pins to use), they're easy to manage because they're right in your main code (and can take advantage of that).
Note that the pulse outputs are cascaded: you get one pulse every 5ms; this gives you the 20ms period which is 50Hz.
(2) sw2 = Motor2 and direction 2-0.
(3) sw3 = Motor1 and direction 1-1.
(4) sw4 = Motor2 and direction 2-1.
In my program, I have a constant called CLK_FREQ which saves the program a little time (clkfreq is a system variable that has to be retrieved; using a constant is always faster). Repeating myself (from the first post), this loop will run at 50Hz IMO, it's better to use a calculated constant instead of a magic number like 100000; this allows you to change to clock frequency and still get the same results. Look at the timing section in my demo program -- there's some good stuff there that I liberated from other forum members.
@JonnyMac, In your case, not sure how much stuff is liberated vs. repatriated.
phsa:=PulseWith
period:=period +Cycle_time
waitcnt(period) but got an error. is there a way to do this or do I have to write this for each instance. There are 5 times I need to do this.
Let's say you have an input that, when active, you want your PWM to run at 25% speed. When that input is low, you want the PWM off (0%).
I suggest you approach it like this The setup method is where you'd configure all your IO and instantiate objects.
Easy, right? And clean and maintainable, too. I've attached my dual motor PWM object. It's simple code that has been used in several motor control applications for my clients.
Tip: Avoid the use a magic numbers where possible. In your code you would have to change the high-time ticks if you decide to change your clock speed.
Edit: Newer version of driver attached to post below.
I still don't understand your end goal. This is the biggest problem for those seeking help (like my programming clients). You're getting fractured help here because your descriptions have been fractured. This is a tricky thing about being human: we have this thing were we assume others understand what we understand. It happens to us all. Maybe answering these questions will help other forum members help you with your program.
-- How many inputs do you have?
-- How does the state of each input affect the program?
-- What are their interactions?
-- How many outputs do you have?
-- How do the outputs behave in relation to the state of the program?
I have been doing product development for more than 30 years -- describing the "product" is the hardest part of the process. With some of my clients who are producing commercial products that I'm designing circuits and code for, I tell them to write the manual first. I have two patents for a sprinkler controller that I created for Toro and found back then that the easiest way to sell the concept to the company (before I asked the VP to write a $250K check) was to create a user manual that conveyed what the controller did, and how it interacted with the customer. I am proud to say that the product is still in production nearly 25 years later (updated, of course, but still follows my design rules).
I'll conclude this long post with a tip: Experiment with coding outside your project. Connect some LEDs and buttons to a Propeller and just play. We often get tense about a project, so put it aside for a minute so that you can experiment for fun and education. Taking the pressure off of your experimenting often makes that time more fruitful. I experiment every day. It pays off.
rdirPin(18) = high
dmotorPin (set_pwm(16,1,0))
ddirPin(15) =low
This is only an example of the case statements.
It appears that pins 17 and 18 are the speed and direction pins for one motor, and 16 and 15 are for the second. Is that the case? If yes, I suggest that you code like pros do and use named constants.
If you used the dual motor object I shared with you, it would be instantiated like this: ...and now all the pin manipulations are abstracted for you -- just set each motor speed/direction (negative speed is reverse).
Break the program into small pieces. Start by naming you IO pins; not doing this is foolish, and makes a program more difficult to read and understand. Using well-chosen pin names helps document the behavior of the program (it becomes self commenting). If you have to change your wiring later, this is the only section the needs updating; the names will flow through the program.
Next I suggest you create a routine than scans the buttons and gives you a direction state. The way you've written your pseudo-code suggests that only one button can be active at a time, hence this routine returns the direction of the first pressed button. Now you can put it in your main loop. This assumes the use of my dual motor control object. This is not the complete listing but it is the major parts, along with the code and objects I've shared in this thread. Start with the template I provided as it will help you keep organized.
Thanks;
Hmmm... sounds like you need about 10 minutes and the Propeller manual (PDF is linked in the Help menu). FWIW... have a look at www.JonyJib.com. Every one of their products that uses a DC motor uses that driver. Since I am meeting with them today about a couple new projects I touched up the comments in my dual motors object -- maybe this version will be helpful. It's attached.
I don't know what you want anyone to correct -- you haven't posted any Spin code, only pseudo-code that is meaningful to you, but has me (and maybe others) scratching my head. I've done my best to help, but you push back on every suggestion. I really don't know what else to do.
Why are you running at 5MHz?