Smart Pin DC Motor Control Demo
JonnyMac
Posts: 9,104
Look, Mom, no cogs! Easy-peasy DC motor control with smart pins.
Setup:
Use:
Note: Some files in archive require PNut v34n.
Setup:
pub start(a, b, en, enlevel, freq) | x '' Start dc motor driver '' -- a and b are motor control (pwm) pins '' -- en is the motor enable/coast pin (-1 if not used) '' -- enlevel is enable pin level (0 or 1) to activate h-bridge outputs '' -- freq is pwm frequency longmove(@mtra, @a, 3) ' copy pins enstate := (enlevel) ? 1 : 0 ' fix enable pin output level x := 100_0 << 16 ' set period to 100_0 units x |= 1 #> ((clkfreq / 100_0) / freq) <# $FFFF ' set timing of 1 unit pinstart(mtra, SP_PWM, x, 0) ' set mtra pin to pwm, duty = 0 pinstart(mtrb, SP_PWM, x, 0) ' set mtrb pin to pwm, duty = 0 set_enable(false)Edit: Added enable pin output level.
Use:
pub set_speed(speed) '' Set motor speed in 0.1% increments (-100_0 to 100_0) speed := -100_0 #> speed <# 100_0 ' limit range if (speed < 0) ' reverse wypin(mtra, 0) ' - a pin low wypin(mtrb, abs speed) ' - pwm on b pin elseif (speed == 0) ' stop wypin(mtra, 0) ' - both pins low wypin(mtrb, 0) else { speed > 0 } ' forward wypin(mtra, speed) ' - pwm on a pin wypin(mtrb, 0) ' - b pin low set_enable(true) ' h-bridge outputs activeEdit: Updated comments
Note: Some files in archive require PNut v34n.
Comments
Bill M.
Maybe it is just me, but IMO comments are short,
maybe terse for something so new.
Why can't Parallax use common terminology like
frequency in Khz, duty cycle, etc.
This is my first crack at this object, and I probably will bolster the comments when I can think of the right -- concise -- commentary.
Your turn. Please do what I did: sit at your desk, 'scope connected to the Propeller 2, then write, debug, and post an object demonstrating some new feature of the P2 in a ready-to-use form that others can put to use. I look forward to seeing your style -- I just may adopt some it if the comments are concise, useful, and meet my strict aesthetic requirements. I work in Hollywood where formatted documents (scripts, in particular) have very strict rules; I treat my program listings with the same seriousness.
Can't wait to see your thread.
I criticized this one code example, not your past work.
I assume the object was written for a L293 ic.
I noticed you used some of the same terminology that Parallax
used in the P2, Spin2 manuals. Parallax has used many (new to me)
terms or names to describe P2 pin functions.
I am criticizing your title and the comments to this piece of code,
"terse for something so new." I was referring to how you are setting
up the smart pins.
I understood your code comments, but not how you set-up the pins
other than you chose "Saw Tooth" mode and that you were modifying
the "_PPPPPPPPPPPPP_" portion of the smart pin register.
Bill M.
BTW: I have a jm_xxxx.spin collection, I use your code as a reference,
I purchased an online subscription to Nuts n Volts just so that I can
access to your SX and Propeller articles.
I've been slowly building up some constants for the WRPIN pin config instruction for a while now. Here's the complete smartpin mode list: There is only a few parameter constants defined for those smartpin modes. Lots more to do there.
And here's just some of the pin modes. The ones I've been using.
I'm confused. I'm writing P2 code -- why would I not use Parallax terms when it is Parallax documentation that others will use as their official reference?
You're unhappy with "Smart Pin DC Motor Control"? But, that's what I'm doing. It is likely that Chip will create a bunch of named constants that will make listings a little friendlier. If and when he does, I'll use them. If he doesn't I may adopt Evan's or create my own.
I told Chip that I found the "PWM Sawtooth" notation in the docs confusing -- I'm sure this will be addressed in official documentation. Unless it's a very peculiar set of circumstances, I don't use comments for why I do things, I just detail what I'm doing. I save the "Why I do it this way..." stuff for magazine articles and books. BTW, the motor code does not modify the PWM Sawtooth P field (output is true mode).
As I said, I will probably bolster my comments in the demo and the object -- I tend to do that as code is settled and not so transitional.
Thanks!
I started to edit your "Pin Mode" constants so that they
would fit nicely in a pdf.
Was there a file attached named jm_motor_testing.zip ? I can't see it anymore.
Please keep producing your examples and posting them on this forum.
Everything you have posted is 100% ok by me.
You are the best!
Kevin.
As I told Chip when we spoke yesterday, I'm doing this for myself and for those who might otherwise wait for examples in the final documentation. I've never claimed to be a great programmer, but I always succeed at that tasks I'm given, and my maniacal insistence on neat listings and style seem to resonate with people. I'm happy if code that I've written for myself can benefit others.
I think we were subjected to a forum glitch. When I first read your message I found the file missing, too. Since Chip released a new version of PNut in the early morning hours, and I added a feature to the motor driver, I went back and rebuilt the archive. When I returned to the forum the original file was there. It's been replaced with the latest.
Again, thanks for you kind words. Have fun with the P2 -- I certainly am (even if, at moments, not knowing all the details is frustrating).
Perhaps other postings of yours have lost there attachments. I have not checked.
I wish to suggest that you always attach the latest version to your most recent posting. Leave the past as is.
Also you might note in your posting, for example: "My version no. 2 is attached to this posting."
If future attachments disappear then it will be more noticeable. I believe the glitch really does exist.
Regards,
Kevin.
The glitch is real; forum administrators have had to deal with this from time-to-time.
Kevin.
I am on Chrome, and I see the zip attached to the first post fine, as well as your two attachments.
I can see @Publison's attachments.
- just after my lunch (I'm under UTC-3' time, thus, 5 hours ago), I've saw kg1's first post, telling us he couldn't find the attacment;
https://forums.parallax.com/discussion/comment/1491766/#Comment_1491766
- I was using my PC (W 8.1, Firefox: 74.0), so I did checked it, and the same problem happened with me: the attachment was not there; I did shut-down my system, and went to take a nap, for a while (too hot here, better spare some meningeal cells for the night-shift);
- Unfortunatelly, I didn't checked it thru my Android phone, before the long nap;
- I woke ~45m ago, did a look at the thread, using my phone (Android, Sansung browser (Chrome)), and there was three incarnations of the attachment, one at the first post, and the two Publison did attached to his own post;
- So I did woke-up my W 8.1 beast, and opened the forum, just to check how it looks like, from my W 8.1 (Firefox) POV: well there are three of them: Jon's at first post, and Publison's two, at his own post.
When clicking the URL link in this format, then Jon's attachment is missing:
http://forums.parallax.com/discussion/comment/1491796/#Comment_1491796
However... click the date-time stamp under @JonnyMac name at the top of the post, and the attachment appears.
That link uses a different structure:
http://forums.parallax.com/discussion/171321/smart-pin-dc-motor-control-demo
So the quick fix to make the attachments appear, is click the datetime link.
I will go add this issue to the "todo" list for the next forum update session.
I've never been able to edit any of my 16xxxx or 17xxxx posts. When I click on Save Comment, the edited first post is not saved and instead it is written to a new 14xxxxx post at the end of the discussion. This includes just changing thread title.
Firefox 3.6.28, Win98SE, can't upgrade to any later versions.
Can't you trashpick or otherwise obtain for free an old computer capable of running XP? Or, have you tried Linux?
I also want to chime in and assure you that your work here is really appreciated, and YES your coding style is very neat and it is fun to read your code. IMHO you are a great programmer, even if you do not claim to be one.
Actually I am teaching some kids out of our neighborhood to program and use some of your code as a example of how to to write readable code.
I think nobody questioned that chip is a great programmer, but reading his code is no fun at all. It is more torture. Single letter variables should be forbidden and flagged by the compiler, NAMING things is hard but absolutely needed.
Since I learned programming with the worst wordy language there is (COBOL) I had a hard time to learn to comment code once I switched to different languages. COBOL itself does not need comments much, file with 150,000 lines are common but the source is mostly plain English.
In my professional career as a programmer I learned the hard way that comments are needed to describe the intend of what is done, not how it is done. It is not just COBOL where I have to look thru decades old code, some programs are like children, you never can get really far away from them, they find you.
Since about 5 years I am rewriting some stuff I did in the 90's and some of my OWN code I wrote is a complete disaster to me. Sure at the time I wrote it, it was crystal clear to me what (and why) I did it, but now 30 years later? Not sure.
I found your comment about Hollywood scripts very interesting, I never thought about that but you seem to be right, there is something about formalizing some basic rules like Parallax tried with the (failed?) standard library approach.
Enjoy!
Mike
That is part of the reason I use your code examples, not mine.
My own programming style is quite horrible, thankfully I do not have co-workers right now, but when I had, I got yelled at some times, old habits do not leave you easy.
The current code base I am working on went from VB4 to VB6 then to C#, sometimes I generated code to get things done faster, sure I refactor things when I have to change stuff, but hell, there are monsters hiding in the code I can just attack after 10 hours of night work using the midnight oil keeping me running.
I do agree with you that the gold standard was uncool, but the basic idea is quite right, just not the way it was proposed.
Anyways, keep on the good work and get more jm_xxx stuff out here, it is badly needed.
Thanks,
Mike
I think that comes from other PWM modes, and historical misnomers...
The P1 mentions PWM, but that is actually PDM (Pulse Density Modulation), not PulseWidthModulation at all.
Other MCUs often have two modes of genuine PWM : A PWM sawtooth (Up counting only), and a PWM triangle (up/down counting)
The triangle mode is useful for where you need dead-bands across multiple PWM channels.