Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: mack33 on July 08, 2009, 05:57:35 AM

Title: Motor position and speed control
Post by: mack33 on July 08, 2009, 05:57:35 AM
Okay, I've been researching various forms of controlling motors with some degree of precision. I've been trying to develop a circuit that won't use too much processing power of any processors that I may choose to include in my future robots. It is very important that i know how the circuit works. I wish to learn.

There are two aspects that I'm trying to control, the angle of the shaft (after gearbox) and the speed at which the motor tries to move.

My first thoughts were to create a circuit with a microprocessor at the center. A PWM output would be used to determine the speed. The following link shows a nice H-bridge driven by PWM: http://www.mcmanis.com/chuck/robotics/tutorial/h-bridge/bjt-circuit.html (http://www.mcmanis.com/chuck/robotics/tutorial/h-bridge/bjt-circuit.html)

I've always had trouble understanding transistor circuits, I can't figure out what the purpose of the 10k resistors are for. Could someone explain to me the purpose?

Of course, which direction used would have to be determined by another circuit. My first thought was to compare an analog output of the microprocessor to the position of a potentiometer.

With everything said, I really don't know if this is a good idea or a bad idea. Can someone give me input and direction.
Title: Re: Motor position and speed control
Post by: Soeren on July 09, 2009, 07:10:45 AM
Hi,

There are two aspects that I'm trying to control, the angle of the shaft (after gearbox) and the speed at which the motor tries to move.
Is this for continous rotation or limited to eg. 360°?


My first thoughts were to create a circuit with a microprocessor at the center.
Not a bad thought :)


A PWM output would be used to determine the speed. The following link shows a nice H-bridge driven by PWM: http://www.mcmanis.com/chuck/robotics/tutorial/h-bridge/bjt-circuit.html (http://www.mcmanis.com/chuck/robotics/tutorial/h-bridge/bjt-circuit.html)
Nice? Well, for simplicity perhaps, but it's not too efficient, using Darlingtons result in a power loss - perhaps your app. can bear it though, if you've got voltage to spare.


I've always had trouble understanding transistor circuits, I can't figure out what the purpose of the 10k resistors are for. Could someone explain to me the purpose?
They are simply pull ups (/downs) for the PNP (NPN) bases to make sure the transistors close in a proper way.


Of course, which direction used would have to be determined by another circuit. My first thought was to compare an analog output of the microprocessor to the position of a potentiometer.
Then you would have created the equivalent of an analog servo.


With everything said, I really don't know if this is a good idea or a bad idea. Can someone give me input and direction.
Oh, everything is far from said ;D
Start with the purpose for the circuit - a specification of what it has to do, how "well" (numbers - speed, force etc.) it has to do it and then the start is said ;)
Title: Re: Motor position and speed control
Post by: guru on July 09, 2009, 11:30:44 AM
I've just designed a h-bridge motor driver. In my case it's just an open loop speed controller. But the h-bridge was more complicated to build *to spec* than I expected, and it's probably my 5th h-bridge build. Other times I just built the circuit that I saw online or so. This time I really wanted to do it right.

I simulated my designs using Xspice and implemented on breadboards to verify. Using single drivers to pull high or low (in your case, the transistor side of an opto) just didnt work for me. In most cases, no matter how I tried to *tune* the resistors/caps I ended up having the transistor in the pseudo on/off range (the resistive range) and this would cause the mosfets to get very hot. Also, the switching was very slow. I went with totem-pole mosfet drivers and everything was all good!

Many optos are very slow, on the scale of 10e-5 or so (2000 to 40000 ns) like 4n35, check the datasheets. This will prevent you from doing any high frequency PWM, your max freq is probably around 1KHz. Above this you wont be able to achieve MAX ON, and the motor will stop at higher PWM values too.

I hooked up an FPGA development board I had on hand to my breadboard to control the 4 totem-pole inputs to my hbridge. (In my final design it will be a small/cheap CPLD.) The FPGA allowed me to implement my hbridge logic controller and have virtual instruments to see what is going on during the design phase.  One virtual instrument was the PWM timebase generator. I could choose anywhere from 1Hz to 50MHz. I also used optos between the FPGA and totem-poles and my max frequency before performance degraded was 10Kz. divided by 256 (my PWM precision) that equaled about 40 PWM periods per second. The mosfets switch in less than 100ns, so it has to be the optos causing this degredation with thier 2-40us switching time. The virtual instruments shown tie right into the physical fpga so I can control the PWM timebase, speed, brake and on/off for the motor and see the response in realtime. This was my first time playing with virtual instruments and I am HOOKED! I plan to do a tutorial on this sort of thing shortly.

Note: in your circuit you are using BJTs, I am using MOSFETs. I dont know for sure, but I would be suprised if the BJTs could handle 5amp like it says without requiring liquid cooling or something!!

I have included some pics.

Virtual Instruments: (Top) ChannelA frequency counter shows the PWM timebase (should always equal "Actual Frequency" in instrument 2). ChannelB shows the PWM periods per second. (Middle) The PWM frequency generator, currently generating 10KHz. (Bottom) Custom instrument, the motor controller, has controls and buttons to turn on, set speed, direction and brake for the motor. Motor responds in real time. ABCD output shows signal lines to the h-bridge. (currently showing in the off-duty portion of PWM.) The graph simply shows the historical speed setting.
Title: Re: Motor position and speed control
Post by: mack33 on July 09, 2009, 06:53:38 PM
There are two aspects that I'm trying to control, the angle of the shaft (after gearbox) and the speed at which the motor tries to move.
Is this for continous rotation or limited to eg. 360°?
My intention is for the output shaft of the gearbox to have limited rotation. The motor shaft may need to rotate at a magnitude greater then the output shaft to create desired torque.

A PWM output would be used to determine the speed. The following link shows a nice H-bridge driven by PWM: http://www.mcmanis.com/chuck/robotics/tutorial/h-bridge/bjt-circuit.html (http://www.mcmanis.com/chuck/robotics/tutorial/h-bridge/bjt-circuit.html)
Nice? Well, for simplicity perhaps, but it's not too efficient, using Darlingtons result in a power loss - perhaps your app. can bear it though, if you've got voltage to spare.
I totally agree on both points. It's simple, so I do understand it. My intentions are to use these on, among other things, autonomous robots. Minimizing power loss is always beneficial. What would you suggest instead of Darlingtons?

Start with the purpose for the circuit - a specification of what it has to do, how "well" (numbers - speed, force etc.) it has to do it and then the start is said ;)

Well, I'm going to need a way to devise a process to create the specifications. I can always make guesses but there's no way to know if I'll get something that's appropriate.


I've just designed a h-bridge motor driver. In my case it's just an open loop speed controller. But the h-bridge was more complicated to build *to spec* than I expected, and it's probably my 5th h-bridge build. Other times I just built the circuit that I saw online or so. This time I really wanted to do it right.
Ya, it seems like the more one knows, the harder and more work one has to do. It's easy to buy a complete item from a manufacturer, but it's much harder to design and understand an equivalent product.
I've never used Xspice, I used to use multisim, much more professional, but it no longer work's. I may have to buy another version. Could you give me a link to the program that you used to analyze your circuits.

In most cases, no matter how I tried to *tune* the resistors/caps I ended up having the transistor in the pseudo on/off range (the resistive range) and this would cause the mosfets to get very hot.
I had concerns with the optos being slow. I thought the design had some merit because it looked like it had an extra level of isolation. Unfortunately, I don't know what affect the frequency of the pwm signal has on the motor. My understanding is the lower the duty cycle of the pwm, the lower the speed of the motor. I understand that the frequency has to be big enough not to osolate the speed of the motor. I don't know what quidlines dictate what's necessary and what's overkill.
Title: Re: Motor position and speed control
Post by: Soeren on July 09, 2009, 09:25:38 PM
Hi,

My intention is for the output shaft of the gearbox to have limited rotation. The motor shaft may need to rotate at a magnitude greater then the output shaft to create desired torque.
OK, then the feedback should be coupled to the gearbox output (would give a higher resolution if coupled to the motor axle of course, but the construction would be a huge step up).


I totally agree on both points. It's simple, so I do understand it. My intentions are to use these on, among other things, autonomous robots. Minimizing power loss is always beneficial. What would you suggest instead of Darlingtons?
MOSFETs and some well chosen ones for that matter - and lots of gate current.


Well, I'm going to need a way to devise a process to create the specifications. I can always make guesses but there's no way to know if I'll get something that's appropriate.
Easy now. It's as simple as eg. "I need to go at least 90° and do so in max. 2 seconds flat, 110° in 1.4 second would be great though".


In electronics design, you start by dreaming up something that you want done and _then_ you figure out a way to make it happen.
After getting your initial idea, it's much like carving a bunny out of a log... Just remove the wood that don't look like the bunny.
There's a bunny in every log, it just takes a trained eye to spot it and release it.
[Zen of Søren]


I've never used Xspice, I used to use multisim, much more professional, but it no longer work's. I may have to buy another version.
Save your doe, sims are notoriously error ridden, to the point of despair with some designs and trouble is, you don't know beforehand which, so you have to be able to simulate it in your own brain (or build it) to know if its output is OK and then you don't really need the sim.
I have seen grown men close to crying, killing and other unpleasant behaviour due to crappy sim outputs. Personally, I gave them up waaayyy back, when I got bad output on a confirmed good circuit and after modifying the circuit to get the sim to accept it, the circuit was in such bad shape that even a blind person could tell it would never work.
Sure, they're good for very simple circuits, but that's what we have brains for ;D

I really fear a future where everyone trusts their sims and when things goes haywire, they just don't know what to do, as they've allways let the sim do the work, so hasn't really learned the trade.

A couple of decades ago, the term "Drag&Drop coders" was coined (usually connected with very sloppy coding) - let's not get into Drag&Drop engineering - where's the fun in that ;)



Unfortunately, I don't know what affect the frequency of the pwm signal has on the motor. My understanding is the lower the duty cycle of the pwm, the lower the speed of the motor. I understand that the frequency has to be big enough not to osolate the speed of the motor. I don't know what quidlines dictate what's necessary and what's overkill.
DC-motors are (unfortunately) best driven in the lower audio range of a few hundred Hertz to around 3kHz (some up to ~6 kHz or so) - the larger the motor, the lower the PWM frequency.
It's best to experiment in that range, to learn what's best with the specific motor you're using, as they all have different sweet spots, due to inductance, resistance, mechanical mass, number of poles etc.

Set the frequency too high and you don't have PWM at the motor terminals, just a varying DC, due to the inductance of the motor.
The difference is quite pronounced with a low duty cycle, as the pulse modulated signal gives full power in each pulse, while the averaged low DC will result in low torque.

A 'scope is really nice to have when playing around with this.
Title: Re: Motor position and speed control
Post by: mack33 on July 10, 2009, 12:47:55 AM
I think messing around and getting my hands dirty will be my next step.

Maybe I'll try the circuit in the diagram above. Could the poster tell me the value of VA?

I have a craftsman 82089. Far from the best instrument, but hopefully it will do. I geuss I need to buy a singal generator that can produce a pwm that I can manually change. How exactly would I test a motor for it's sweet spot? What would I be looking for?
Title: Re: Motor position and speed control
Post by: guru on July 11, 2009, 03:48:29 PM
Soren: I agree, sims are notoriously error ridden. Really, they can at best "be only as good as the models you use". I don't trust it until I breadboard it. I think of it like a second opinion. I also only sim smaller circuits of my overall design like you said, then I bring it all together.

For others, I also think that as long as you know where sims fall short it can still be a helpful tool. It helped me figure out why the h-bridge I got off the internet just wouldn't work. It told me that driving a mosfet gate's high and low transistions with upper and lower transistors would achieve much better timing and performance. If my name was Soeren I would have known all this instintively ;) (you've been a great help) but it took the sim and breadboarding to really see it like I do know.

Having said that, my sim is saying that my mosfet driver should work with a 3.3v signal and sink only 5ma, thus it should work as driven by my fpga pins in LVCMOS33 io standard in 24ma mode. However, on my breadboard, no go, the transistor base drives the voltage down to 0.5v. (I cant switch the IOB to LVTTL33 standard because of other signals on the IOB.) The XC9572XL CPLD I am using should drive it fine but I am going to try using a small FET on the low side of the driver, I feel this will work better than the 2n2222.

Back to your example of a known good circuit not working in sim...you must have had bad models involved do you think? Lot's of models given by manufacturers are really only an approximation of the the true behaviour and lots of physical or manufacturing effects are not included.

Mack33: As I stated earlier, I used virtual instruments in the FPGA as a frequency generator. I could choose the PWM frequency in real time. I found 80Hz a good smooth value for the motor I was using, about a 1amp geared motor. Any lower than 40Hz and the motor started to experience vibration, much higher than 80Hz and the motor started to lose torque as Soeren explained, and audible noise began. If you can setup a method like I did of being able to change the timing in real time while operating the motor the right period would be pretty clear.

Title: Re: Motor position and speed control
Post by: guru on July 11, 2009, 05:56:08 PM
Having said that, my sim is saying that my mosfet driver should work with a 3.3v signal and sink only 5ma, thus it should work as driven by my fpga pins in LVCMOS33 io standard in 24ma mode. However, on my breadboard, no go, the transistor base drives the voltage down to 0.5v. (I cant switch the IOB to LVTTL33 standard because of other signals on the IOB.) The XC9572XL CPLD I am using should drive it fine but I am going to try using a small FET on the low side of the driver, I feel this will work better than the 2n2222.

Stupid mistake. I forgot that the opto inverts the input when configured open-collector, so as I was only testing with one opto bypassed I was shorting the supply between two mosfets. (I only powered the hbridge momentarily during testing so I didnt notice what I was doing right away.) I inverted the fpga pin and voila! Looks like my spice sim was right afterall. Just goes to show you can't trust me! My sim isn't talking to me now. lol