Trying to undrestand controling a servo motor
T Chap
Posts: 4,223
I am currently using a servo motor in a system that is a Nema 23 size motor with built in gearbox, and seperate controller. The controller is very convenient in that it has a PC software that can make changes instantly. The controller has 16 positions accessed by the 4 bit input, plus other features such as Program Finished that gos high when positions are reached. One complete system as described is running over $800 in small quantities, but it much less than others that are basically identical. The controller has programmable speed, destination(distance relative to pulses), acceleration, deceleration.
I recently implimented the P40 Stamp to replace all logic control, and now am considering a second stamp or SX to replace the $400 controller mentioned above. With no prior experience with writing code for a motor and encoder, this is what I assume, but need to clarify:
1. The microprocessor first "homes" to put a reference 0 in a variable to keep track of where it is, and after that it how many pulses to calculate to get to any destination.
2. The micro puts out a pulse to move the motor, at a specified length on and off between pulses
3. For incorporationg accel, decel, the spaces between the pulses are lengthened or shortened over time
4. The encoder input to the micro looks for a pulse following every output pulse, and if it is missing, then some action can take place, i.e. stop, apply more torque, etc.
First I'd like to find a resource for getting these concepts down, then I'd like to get a bi-directional servo and start learning how to make it move at various sppeds and accels to fixed destinations. Is the Stamp or SX suited for this use, and is this something that can be easily done with a bit of learning?
OT How complex is it to create a standalone app to program the Stamp or SX to make changes in position, speeds, etc?
Thanks for pointing me to any place to get this info.
I recently implimented the P40 Stamp to replace all logic control, and now am considering a second stamp or SX to replace the $400 controller mentioned above. With no prior experience with writing code for a motor and encoder, this is what I assume, but need to clarify:
1. The microprocessor first "homes" to put a reference 0 in a variable to keep track of where it is, and after that it how many pulses to calculate to get to any destination.
2. The micro puts out a pulse to move the motor, at a specified length on and off between pulses
3. For incorporationg accel, decel, the spaces between the pulses are lengthened or shortened over time
4. The encoder input to the micro looks for a pulse following every output pulse, and if it is missing, then some action can take place, i.e. stop, apply more torque, etc.
First I'd like to find a resource for getting these concepts down, then I'd like to get a bi-directional servo and start learning how to make it move at various sppeds and accels to fixed destinations. Is the Stamp or SX suited for this use, and is this something that can be easily done with a bit of learning?
OT How complex is it to create a standalone app to program the Stamp or SX to make changes in position, speeds, etc?
Thanks for pointing me to any place to get this info.
Comments
servo control drivers usually use either an analog CV(control voltage) signal, or a step\direction based logic approach. the later would probably be much easier for you to accurately control. Also, servos come in several types, the easiest for you is a brushed DC servo. There are also brushless servos and other tyes, these are much more difficult to control without extensive driver hardware design(or so i understand)..servo motors are often very similar(or identical) to a DC brushed motor. Higher performance servos are usually designed for more accurate work - better bearings, special coiul designs, etc... i dont know all the details.. the best things to find out to get more help are:
1)the motors power requirements(ac\dc brush.. or(uh oh) brushless)
2)how the drives are controlled (CV or TTL)
3)the number of counts on the encoder. if you take it apart BE carefull. they can be costly and fragile. attempt to look for a ID on the case. HEDS-xxxx possibly.
4)your accuracey requirements.. unlike a stepper which is fairly 'mechanical', a servo has some room for error. infact, error will become your pal when writing the control algorithms..
the higher your encoder count the more difficult using a stamp to count the pulses at reasonable speed becomes...
in addition you should research 'PID Control' - A type of error-correction process control which is often used for servo control (as well as many other processes.. checkout the parallax industrial book.. its a 'free' pdf. it is very informative. i printed it and just recently ordered all the components to try it out.
this sort of closed loop control CAN be done. ive got another thread asking about help for my stepper motor based linear positioning. if your drivers take step\direction signals, you are very lucky.
a good place to checkout is www.cnczone.com - look at the 'opensource hardware' forums, and other electronics forums.. people there KNOW about positioning control. lots of hobbyists and tinkereres, but also some real gurus... www.geckodrive.com provides a $125ish commercial solution that would make your project much easier.
Good luck! I would be interested to see your progress. If i knew more about stamps i would try to give you more specific information... in fact, i really can write quite a primer on this, but i would strongy suggest going to more authoratative sources... a simple google search will pull up ample engaging material.. if you cannot find anything, i can find you some bookmarked links..
cheers.
Robert
the nuts n volts articles to look for are entitled 'a stamp high power h-bridge driver' (or thereabouts) and details the HIP chip. the paralax book is an exploration of closed loop control.. the problem you will encounter though is that real accurate closed loop control of a nema-23 servo takes quite a bit of tuning and some reasonable circuit design skills...
take it in parts.. in fact, i am doing that for a different closed loop control project..
you will possibly end up with these components:
an LM339 or other comparator conditioning the encoder signal, a HIP44 type chip doing current regulation and other power related tasks, and your stamp.. counting pulses on the one hand, and outputting a PWM signal to control the HIP on the other...
In fact, correct me if i am wrong, but this is impossible, at least on a BS2.
There are a few servo controllers listed at Parallax, maybe they would be a good starting point for learning some fundamentals?
I can't fnd the Hip chip you talked about.
The first question is whether a Basic Stamp is suitable for an application like this. In general, I'd say yes. Some of the more demanding examples of a servo control would require greater bandwidth than a Stamp can provide. For those, one of the SX units or the new Propeller would be more suitable.
The second is the complexity of developing the control circuits and the application code. The circuits you can find on the Net if your background will let you recognize the good from the bad, and it sounds like it might. The application code is tougher. You definitely should play with a few servos first to recognize the issues. Like a lot of Stamp applications, this is "real time" code, which means you not only have to get a mathematically correct answer, but you have to get it within the time constraints imposed by physical events you're trying to control. Moreover, you don't want to add an additional Basic Stamp for each aspect, so you have to learn how to write code that divides attention between multiple activities. The trick there is learning how to prioritize, but also how to recognize changes in priority. For example, you might be driving three circuits with a nominal time loop, but have to respond quickly when one of them reaches a limit condition.
Microprocessors specifically intended for dedicated control usually employ interrupts. That is, an input signal that causes the microprocessor to stop what it's doing, keep a record so it can come back to that 'context' later, and divert attention to a different bit of code altogether until a return-from-interrupt is executed. You can't fake that in software, and implementing it in a processor adds several expensive assets to the chip. Basic Stamps don't do interrupts. So you can't implement an event driven design in the strict sense. But you can come close if the job isn't time critical. Time critical is relative. Depends on how fast you can do things.
The BS2P and presumably the later Stamp models provide 'polling' support. This is not interrupts, but a way to tell the interpreter that if a particular pin changes state the Stamp should jump to a particular bank of code. No context is saved though, so you have to be a fairly experienced programmer already before attempting this. For example, the code executing when the pin goes high will be abandoned abruptly. (Or at least that is my reading of the manual. Haven't tried this yet.) You must be able to write code that will tolerate this and permit you to examine the state of the data at some later point to pick up processing. I could see using this technique to stop my code updating the LCD display while I go handle a critical change of state on a servo. Because when I return, I can start updating the display from scratch. I can clear screen and ignore whatever portion of the job I had finished before that pin changed state. Polling is suitable for many cases, but writing the code is non-trivial in those where it is really needed. (If that makes sense.) I mean that it isn't a substitute for interrupts if you're trying to manage half a dozen time critical sequences in the same Stamp.
Without a lot of programming experience, that leaves the fairly simple process of adding up all the code you have to run in a particular time interval, seeing how long it takes on a particular Stamp and then buying a model fast enough for your job. That is, one that will execute the code for *all* of your tasks in the same time interval without jumping around from one to the other. You run the code for Job A, like grab a count of incoming pulses from a pulse counter circuit (since you can't tie up the stamp doing that mindless job when you have other things to do) and decide whether to increase or decrease the impulse to a servo. Having finished that, go to Job B. Maybe do the same thing for a second servo, or check battery status and see if you need to do anything about that. And then go on to Job C. Something like updating status lights. You get the idea. If you need to do all those things ten times a second, it will take one model. Maybe a BS2P. If you can settle for five times a second, you can use a BS2. If you need fifteen times a second, budget for a BS2PX.
For my second robot, I had seven servos in motion concurrently using that one-job-at-at-time technique, with a small dispatcher routine giving more time to·fast moving servos and less to the slow ones.·But I was having fun finding out how many coding techniques from serious real-time programming I could find a way to implement on a BS2. It wasn't easy and I probably wouldn't do it myself for a production product. I'd use a couple of Stamps instead and coordinate them. I was just testing the limits of the stamp and flexing old muscles gone pretty stale. Fun, in other words.
You should probably think in terms of two or at most three things at once that have to be matched to outside events, and limit yourself to slow motion background work for anything else the Stamp is doing at the same time. Things like checking a battery or updating a display are what I mean by background work.
Within those constraints, it is certainly within the range of a Stamp to control a servo in pretty sophisticated ways. And within the reasonable learning curve of a first timer.
Oh yes, that maybe-a-third-question that occurred to me. Make sure that servo isn't doing something that requires regulatory approvals on the controller itself. Lots of military applications, most medical ones, and so forth. I don't know any technical reason you couldn't use a Stamp for something like that, but I do know the total cost is going to make the economic benefits of using a Stamp irrelevant in the project plan, so you might as well start with a less general purpose product. A whole family of electronic products exists that are designed by experts to deal with servo issues using custom circuits. Buying that expertise is the cheapest way to get regulatory-approved products.
But for anything not critical, lay on MacDuff. You'll have fun and learn a lot about servo control using a Stamp. More of both than you would just buying a black box. Someone mentioned a sprinkler control system recently. Maybe in another forum here. That's a perfect application for a Stamp. At least in computer terms. It might teach you a lot more than you ever wanted to know about corrosion issues in electronics. But every project teaches me something I didn't know the week before. That's at least half the fun.<g>
To accurately move a motor, your stamp must ALWAYS count EVERY pulse - otherwise all you have is a speedometer. To move the motor, your stamp must CONSTANTLY be sending a PWM signal to 1 of 2 pins, or pulling them both up for a brake action... This is why the HIP chip is so usefull - it handles the currents and stuff when braking, accelerating, working, etc... much easier than trying to put together the opamp and sense resistor circuitry usually used for current 'chopping'.
Here are some links:
HIP4081A Datasheet[noparse]:http:[/noparse]//www.intersil.com/data/an/an9405.pdf
N n V's Article about HIP and BS2: http://www.parallax.com/dl/docs/cols/nv/vol2/col/nv52.pdf
PID Article: http://www.engin.umich.edu/group/ctm/PID/PID.html
Forum psot about BS2 and PID: http://forums.parallax.com/forums/default.aspx?f=6&m=66982
PID for Servos article: http://www.ctc-control.com/customer/elearning/servotut/pid.asp
encoder circuitry: http://lims.mech.northwestern.edu/~design/mechatronics/2000/Team24/circuit.html
very interesting thread on a DIY servo controller: http://www.cnczone.com/forums/showthread.php?t=14217
i recently had a small DC motor, 64CPR encoder, and a half h-bridge made of a tip120 and tip125 hooked up. i couldnt get PWM to control the motor at all, let alone in closed loop.. it was either jerky on\off, or not enough current to turn the motor, or full speed... i also had real trouble getting an accurate encoder count while the motor was spinning.. but i am extremely inexperienced programmer.. i used a comparator to prepare the signal..
I think most PID algorithms deal with floating point math, and i also think you have to further define your project goals to get better advice..
Well, no. Not the right job for a Stamp. Buy a couple of deputy chips for those jobs and let the Stamp boss things.
I need to control just motor with moderate precision(down to .01 increments is good), the ability to progam externally(a PC app) the speeds, distances, accels, etc. I am sure there are products that exist for the purpose, just finding the right one is key.
I will dig through all the above mentioned links, maybe get some of the parts and breadboard them to get a taste for the application. In short, it doesn't look simple, it would take a major effor to pull it off from scratch based on the above.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap 4-digit LED display with driver IC·www.hc4led.com
Low power SD Data Logger www.sddatalogger.com
"I reject your reality, and substitute my own." Mythbusters
·
people seem to be using 18f series PIC micros for these tasks. another option is this UHU product. (http://www.uhu-servo.de/)
for something like $8 you get a programmed IC that does ALL the hard work. you build the circuitry.. also you get great support from various people who seem dedicated to the UHU project.
It is a real shame the code isnt released!
This UHU chip has a serial port you use to configure the thing with a terminal program, drives FET based output stage, and accepts ste;p\direction signals..
this means that you first program the chip with your encoder info and other factors that determine resolution, and control characteristics (accel, decel, max speed, etc),then you pulse a clock line to increment 1 graduation, and another ttl line sets direction. the closed loop control is kept entirely seperate from your stamp. the stamp still has to provide the ttl pulses. for a BS2, even this can prove a challenge.. not to mention the little tricks needed to simulate floating point math... (say you want to move 1.625inches).
if you were willing to move to a stepper motor project, i can help you alot. you couldpiggyback my project:
a 2-axis, stepper driver and controller with 4x20LCD, 16key interface, etc..
so far i am still building little modules and learnign the ropes(just built a 4x4 key matrix.. waiting for shift register ICs from digikey, built 2 simple unipolar stepper boards, etc)
Post Edited (RGuyser) : 6/6/2006 9:55:40 PM GMT
the thing is, as soon as i feel more confident, i will switch to picbasic.. ive been gathering the resources ($$$ for software, a programmer board, etc)
the benefits are many.. i still like parallax stamps though. ive bought a few. will probably get a propeller chip in the next few months.. waiting for more projects to be detailed..
For now the off the shelkf controller is great, but I'd like to move towards having the whole thing self contained, even if it were several modules stuck together.
It is a sure thing that using a stepper is much easier, but you tell me, if you are moving a 125 lb object that costs $1000 to replace if broken(not counting the labor which would be expensive plus shipping--could be thousands total) would you trust a stepper?
steppers are used to move $100,000 experiments around. who knows the ungodly sums of money trusted to steppers. it is a misconception to beleive that a stepper system is inferior to a servo system. it depends on all the various factors. steppers have some serious benefits for some applications. for instance, low speed, high torque positioning. to make a servo move an object slowly with full torque requires lots of pulleys or down gearing, at least as far as i know. another issue is cost. steppers are not usually closed loop, but they are much easier to control(from an electronics and software POV). encoders designed for accurate numerical control, and not just tachometer or coarse positioning are often as much as $500 each(!). for just 1 encoder! another issue is that steppers can be driven with as few components as 4 big resistors, 4 transistors, mosfets, or similar, and a microcontroller... a servo is always going to require more components.
i used to use a 4'x4' cnc gantry machine at industrial design school. it was a $14,000 machine, and used 3 big steppers to move it's $3000 spindle...
the benefits of servo control do exsist - for one, a proper servo system usually has much higher resolution than a stepper system. a servo system probably will create less noise, and is probably less susceptable to emf and other outside electrical noise (this is because they are closed loop). servo motors also provide accuracy over a wider speed range, and offer much faster acceleration and max speeds. at high speeds steppers get weaker, servos get stronger...
i am building a $2000ish CNC device using steppers.. i have no hesitations about this. my machines 'system' accuracy(if not mechanical reality) should be .001 with a resolution of something like .0000625... this, of course, means nothing if the overall machine is not sturdy and accurate...
how complex does your control software need to be? what is your application? i ask because it is possible the stuff i am currently writing is close and i could modify it for you for a small fee, or give you the source for your own experiments..
incidently, i am not trying to build a cnc control system, just a 2-axis stepper position controller with a simple LCD GUI and a total cost around $100.00 to drive unipolar stepper motors upto 10amps.. (easly expanded with different output circuitry). with a 10:1 reduction, and 2x halfstepping stepper control, this gives a system resolution of 1inch / (200 steps/rev * 2 * 10) = .00025. i was going to get a simple controller with an absolute and relative mode working.
the VB app is for a vacuum former i am building, you can see a 3D drawing on my website.. the stamp is in charge of 2 stepper motors, a 4-zone heater, an e-stop switch, several thermistors, and some motion limiting switches.. the VB app reads the various sensor readings from the stamp, provides PID control of the heaters, and other such functions..
see attachment.
check some of my stuff... www.robertguyser.com
Post Edited (RGuyser) : 6/7/2006 12:51:45 AM GMT
At the moment I am in metal working mode. Only withing the past two weeks have I gotten into processors, and just incorprated a BS2P40 as the controller. With no programming experience, I wrote the program in about 4 days, got some great tips from a few people here and went through and chopped it down and made it "pretty". Now I'll slowly convert it to SX as it is mostly IF Then stuff anyways.
I certainly would like to discuss the VB app, plus the stepper stuff further, I am assuming you could easily adapt something to run one motor at low speeds, then build a slick looking user interface in VB.
I always heard that the encoders add a lot of noise, I know on this Max benchtop mill machine I have, it has the high pitched sound that drives people nuts. They call it a "closed loop" system, not sure if that technically defines servo or not. Maybe there a middle ground, a stepper with encoders that knows where it is and if it is missing pulses?
that was a great read Thanks for the insight!
that vacuum former software screen was an old version. the current beta has a simple sequencer that lets you make an automated cycle.
perhaps you would like to email me your requirements, while i am not an expert in programming, i know a fair bit about this specific topic of motion control. i could possibly help your project if you have commercial aims. i do not know a uninitiated programmer from 'over seas' would be able to efficiently understand your goals..
it is quite possible to program a VB front end for a stamp based motion control system. the PC does the difficult work..
i cannot do servos unless there is additional servo positioning hardware purchased.
feel free to email me offline.. robert at robertguyser dot com
Checking a couple of them on the net, NEMA 23's seem to draw an amp and a half, maybe two at worst, so we're not talking about a lot of muscle. That should simplify the control problem, because you aren't going to be dealing with a lot·of kinetic energy and the dynamics of the system will be moderate. It isn't hard to isolate a unit operating at those power levels and frequencies, so you shouldn't have any special RF noise unless the machine the motor is on creates noise we haven't discussed.
RGuyser has mentioned the wide variation in which motors are open loop and which are closed. I've never worked much with open loop situations and can't say much about the algorithms for that purpose. But obviously you can close the loop on any motor using the stamp. If it is fast enough to keep up with the dynamics of the application. Actually, now that I say that, I think most of the Parallax robots use open loop motion control in the example code. But I'm sure that's·because it's easier to teach young people as·a first programming exercise. Even when I built a toddler, the first thing I did was add ways to read the position of the 'limbs' and program it as a closed loop system, although $10 hobby servos are very much open loop. And pretty non-linear in their response under load as well. (It was worth the effort though. I taught it to dance the Hokey Pokey and got a laugh from my wife when she was recovering from a severe stroke and badly needed cheering. No hands, but I added a waist, neck·and head so it could bow when it was done. Cute little bugger.)
I don't think the lack of floating point is going to be an issue. First, in an application with this limited bandwidth it's fairly easy to do your calculations on a PC in design phase and then reduce your control algorithms to fixed-point form for the control code. Table-driven solutions are very useful and amazingly fast.·Second, to the extent you need a moderate amount of fixed point, even in real time, you can do that on the Stamp. Tracy Allen has discussed this on his website. I think it was Tracy, it's been a couple of years.
In any case, if I can help, let me know.