Arduino Forum

Using Arduino => Motors, Mechanics, and Power => Topic started by: Tom_G_2010 on Dec 04, 2012, 03:13 pm

Title: Stepper Position Sensing and Control
Post by: Tom_G_2010 on Dec 04, 2012, 03:13 pm
I am new to Arduino and to stepper motors, but I believe they are the solution to a part of my flight simulator project.  I need to simulate the magnetic compass that will be mounted on top of my instrument panel.  The motor needs very little torque since it will only be turning a small plastic disc, but precision as close to 1 degree as possible is important.  And, positional awareness, at least initializing it at start up, is critical.

My sim software will be able to feed the compass direction to the Arduino as a numeric value from 1 to 360 and I am finding a lot of code examples that seem  straight forward, IF, I can initialize the starting point when I boot up the sim.  That would be an ok solution.

I also started reading about steppers with position encoders built in and that got my attention since it might allow me to skip the initialization and simply drive the stepper to the correct position at any point in time.  However, I'm not finding any Arduino sketches or projects that leverage this position encoder functionality.

Any thoughts or direction on initial position set or use of a position encoder would be greatly appreciated!
Thanks!
Title: Re: Stepper Position Sensing and Control
Post by: permnoob on Dec 04, 2012, 04:45 pm
the lazy way is to just grab it and point it to north or some parked position at startup, and enjoy from there.  otherwise you need some reference location and a sensor/switch of some type
Title: Re: Stepper Position Sensing and Control
Post by: Tom_G_2010 on Dec 04, 2012, 04:53 pm

the lazy way is to just grab it and point it to north or some parked position at startup, and enjoy from there.  otherwise you need some reference location and a sensor/switch of some type


The compass will be in an enclosure so unfortunately that's not an option.  So I am looking for ideas on an initial position sensor or a maybe more ideal a way to take advantage of the position encoders that some steppers have.
Title: Re: Stepper Position Sensing and Control
Post by: drone on Dec 04, 2012, 06:00 pm
If you just need a starting reference point, consider attaching a hall effect sensor to the housing, and a small magnet to the dial, such that the magnet is directly over the hall effect sensor at 360'.  Then, you can just move in either direction until the magnet is directly over the sensor at startup.
Title: Re: Stepper Position Sensing and Control
Post by: kf2qd on Dec 04, 2012, 07:25 pm
Some type of switch, either mechanical or hall effect. When the Arduino starts it rotates the dial until it senses the switch and then slowly backs up until the switch breaks and calls that 0 degrees. then you just send the arduino a position and let it move to that position. you will need to code for the 0-360 degree transition and how to handle going from 5 degrees to 355 and from 355 to 5.

Unless you buy an expensive encoder you will still have to go through an initialization routine as most encoders are incremental instead of absolute.
Title: Re: Stepper Position Sensing and Control
Post by: retrolefty on Dec 04, 2012, 07:32 pm
Quote
I also started reading about steppers with position encoders built in and that got my attention since it might allow me to skip the initialization and simply drive the stepper to the correct position at any point in time.  However, I'm not finding any Arduino sketches or projects that leverage this position encoder functionality.


But there is a lot posted on using quadrature encoders which is just the input side of your stepper/encoder assembly. The problem with simple 2 channel encoders is that on power up there is no way for the encoder to tell you the specific starting point of the shaft, it just allows one to count steps up or down once you do command the stepper to move in one direction or the other. Often a design will have a optical gate at one extreme end of travel and your start-up code will move the sensor towards that direction until it detects that end of travel stop and thereafter you can just keep track of step counts to determine where you are in the full travel range.

Lefty
Title: Re: Stepper Position Sensing and Control
Post by: Tom_G_2010 on Dec 04, 2012, 07:38 pm
I understand how hall sensors work, but don't have any hands on experience with.  I have however worked with opitcal sensors.  I'll have to look at both and see which one I can get the most precision from.

Interesting about the stepper encoders not being absolute.  Kind of a bummer actually...  Does make a simple initialize function seem like the better way to go then.

Any suggestions on motors?  Most are either 1.8 or 0.9 degree steps so I wasn't sure about accuracy on a compass display.  Can they be held between steps to get at least close 1 degree accuracy or should I be looking at a some of the more expensive 0.36 steppers?
Title: Re: Stepper Position Sensing and Control
Post by: retrolefty on Dec 04, 2012, 07:48 pm
Quote
Interesting about the stepper encoders not being absolute.  Kind of a bummer actually...  Does make a simple initialize function seem like the better way to go then.


Well there are industrial absolute encoders available and many would be no problem to wire up to an arduino, but their price can be pretty much a sticker shock. The fact that with one external sensor and one quadrature encoder, you can emulate a absolute encoder function in software, pretty much tells you which way you should proceed. As far as optical Vs magnetic encoders that is pretty much internal details of the encoder that doesn't much concern one, it's just a matter of the electrical interface method and getting the steps per revolution resolutions you require for your application. You might note that most encoders are specified as having X number of 'steps' per shaft rotation, but with standard software encoder decoding methods you can turn that into detection of X or 2X or 4X steps per shaft rotation. So a 100 SPR rated encoder you can resolve 100 or 200 or 400 counts per revolution just by the software decoding method used to decode the A and B encoder signals.

Lefty
Title: Re: Stepper Position Sensing and Control
Post by: Erni on Dec 04, 2012, 11:39 pm
Did you consider a 360 degree servo ?
Title: Re: Stepper Position Sensing and Control
Post by: Tom_G_2010 on Dec 05, 2012, 02:01 pm

Did you consider a 360 degree servo ?


I have and am ordering one to test for other applications, but have been told that absent the position feedback (which none of the 360 servos I've found have) it won't work for this application.  Am I mistaken?
Title: Re: Stepper Position Sensing and Control
Post by: Tom_G_2010 on Dec 05, 2012, 02:16 pm

Quote
Interesting about the stepper encoders not being absolute.  Kind of a bummer actually...  Does make a simple initialize function seem like the better way to go then.


Well there are industrial absolute encoders available and many would be no problem to wire up to an arduino, but their price can be pretty much a sticker shock. The fact that with one external sensor and one quadrature encoder, you can emulate a absolute encoder function in software, pretty much tells you which way you should proceed. As far as optical Vs magnetic encoders that is pretty much internal details of the encoder that doesn't much concern one, it's just a matter of the electrical interface method and getting the steps per revolution resolutions you require for your application. You might note that most encoders are specified as having X number of 'steps' per shaft rotation, but with standard software encoder decoding methods you can turn that into detection of X or 2X or 4X steps per shaft rotation. So a 100 SPR rated encoder you can resolve 100 or 200 or 400 counts per revolution just by the software decoding method used to decode the A and B encoder signals.

Lefty


Funny you should mention sticker shock.  What got me started down this road was just that, sticker shock when I looked at purchasing a prefabricated simulated aircraft compass and found they cost between $500 and $700 US.  That said, the lower the cost the better, but I suspect there are many options that would put me well below that mark.

I wonder if you might know of any such steppers absolute encoders that you could point me to as an example?  That would allow me to access the cost/benefit as compared to likely less costly options.

Thanks!
Title: Re: Stepper Position Sensing and Control
Post by: ash901226 on Dec 05, 2012, 04:15 pm
if you want to always know where the arrow always point at, you have only one choice that is to use an absolute encoder. however it is expensive, and even if you do it yourself, i cant say how cheap can you get. however i think since it is just to show direction you could get away with maybe  a switch/reed switch/hall effect sensor or opto switch. that should be your 0 position and use a quadrature encoder to detect the angle of you if you use a dc motor , if you use a stepper motor then you could count the number of step. i i just remember what if you use a sail winch servo use for rc sail boat. that have a 700 ++ rotation.
Title: Re: Stepper Position Sensing and Control
Post by: Erni on Dec 05, 2012, 04:44 pm
Quote
I have and am ordering one to test for other applications, but have been told that absent the position feedback (which none of the 360 servos I've found have) it won't work for this application.  Am I mistaken?


Try to look for winch servo (they are used in model sailboats to control the sail).
They can rotate 360 degrees ore more and and they have the same position control as "normal" servos .

example:

http://www.pololu.com/catalog/product/522
Title: Re: Stepper Position Sensing and Control
Post by: kf2qd on Dec 05, 2012, 07:50 pm
A servo won't do what you want. Isn't a sail winch just a continuous rotation with a winch attached?

You could go half steps which would give you 400 positions, you could use a gear train and come up with more positions.

The hardest part is how do you handle the moves across 0 degrees. Not a major issue, just depends on how big the steps are that you would be sending to the stepper motor.

Another thought - Power up the compass and assume that is 0 degrees. then have a mechanical cam, push a button and the compass is then zeroed. You could also sense when the compas has beeen zeroed, delay a bit and then move it to the proper heading.

If the Arduino that is controlling the compass is recieving just degree numbers and moving the dial to position you should be able to keep the position correct for quite a while, no load, low speeds mean the stepper should have no problem keeping position without feedback.

How much total rotation do you think you would have in one "flight"? If your number for compass position were a long you could do a lot of 360's before you ever hit overflow.

Will your flight sim be sending position info in degrees? or degree step?
Title: Re: Stepper Position Sensing and Control
Post by: Tom_G_2010 on Dec 05, 2012, 10:01 pm

Try to look for winch servo (they are used in model sailboats to control the sail).
They can rotate 360 degrees ore more and and they have the same position control as "normal" servos .


I just looked at the winch servos and because they are still limited in the number of rotations they will not work for this application.  On a long multi leg flight in bad weather with heavy traffic it is possible to be stuck in holding patterns (oval track loops) at length so any limit on the number of turns would not be realistic.


A servo won't do what you want. Isn't a sail winch just a continuous rotation with a winch attached?

You could go half steps which would give you 400 positions, you could use a gear train and come up with more positions.

The hardest part is how do you handle the moves across 0 degrees. Not a major issue, just depends on how big the steps are that you would be sending to the stepper motor.

Another thought - Power up the compass and assume that is 0 degrees. then have a mechanical cam, push a button and the compass is then zeroed. You could also sense when the compas has beeen zeroed, delay a bit and then move it to the proper heading.

If the Arduino that is controlling the compass is recieving just degree numbers and moving the dial to position you should be able to keep the position correct for quite a while, no load, low speeds mean the stepper should have no problem keeping position without feedback.

How much total rotation do you think you would have in one "flight"? If your number for compass position were a long you could do a lot of 360's before you ever hit overflow.

Will your flight sim be sending position info in degrees? or degree step?


I did find one servo for about $80 us that had 0.337 degree steps so about 3 steps to a degree.  A bit pricey but not out of reach.

I need to double check the interface app but I believe I am getting heading info as integers from 1 to 360.  Having given this more thought, absent absolute position feedback, I am leaning towards an initialization routine using an optical sensor to bring the compass disc to zero and then read the initial aircraft heading and step the motor x steps to align with the actual heading value.  From there I can keep track of the last heading value read and compare it to each subsequent value to step the motor as needed. I may also add a sync button so I can trigger the initialization at any time should errors start to accumulate and throw off the heading.  I don't think crossing zero will be an issue as I can work out the math/logic programmatically.
Title: Re: Stepper Position Sensing and Control
Post by: wildbill on Dec 06, 2012, 02:09 pm
Check this link: http://g33k.blogspot.com/2010/11/playing-with-stepper-motor-i-ordered-5.html (http://g33k.blogspot.com/2010/11/playing-with-stepper-motor-i-ordered-5.html). The video there shows a $5 stepper with a reduction gear that takes thousands of steps for a full rotation. Might be worth a look for a $75 saving.
Title: Re: Stepper Position Sensing and Control
Post by: Tom_G_2010 on Dec 06, 2012, 03:02 pm

Check this link: http://g33k.blogspot.com/2010/11/playing-with-stepper-motor-i-ordered-5.html (http://g33k.blogspot.com/2010/11/playing-with-stepper-motor-i-ordered-5.html). The video there shows a $5 stepper with a reduction gear that takes thousands of steps for a full rotation. Might be worth a look for a $75 saving.


WOW!!!  Inexpensive, smooth, precise!!  Perfect!

I think I'll drop a hint to the wife so that Santa has some stocking stuffer ideas...

Thanks!
Title: Re: Stepper Position Sensing and Control
Post by: wildbill on Dec 06, 2012, 03:49 pm
The only caveat with that one is whether it will move the needle fast enough for your needs. If it doesn't, there must be others that aren't reduced so radically.
Title: Re: Stepper Position Sensing and Control
Post by: ronpbaker on Jan 31, 2013, 04:08 pm
I do not know if you found you solution or not but I always prefer to use stepper motors over servos if at all possible. Here is how I would overcome the issue you have.

Using a stepper motor in this manner has two drawbacks.  When fist applying power to your instrument the needle is pointing to where you last turned it off and begins working from that point and after prolonged use it can start to miss a step or two making it inaccurate.

The feature you are looking for in a stepper motor is called "Closed Loop", this is typically found in servos but is available in higher end stepper motors. This translates in to expensive. But then again the closed loop only solves the prolonged "real time" accuracy issue. What you truly need is a HOME function.

When you first apply power to your stepper motor your controller will run a series of codes making the steeper motor turn until it hits a predefined degree. There is several ways to locate this but I have found using a bipolar motor (has a drive shaft coming out both sides) with an Optical Slotted Switch works best. You can use a reed switch to do the same thing. Point in fact there is dozens of ways to achieve this but we will stick with the (Optical Slotted Switches) because once you understand how this works you can translate this into any sensor you are familiar with.

The Optical Slotted Switche can be set 2 ways, when the field of light is broken or when the field of light is opened. I use it then the field of light is opened to identify home.  Simply put a disk (like a pulley) on the back shaft of the stepper motor and place a tiny hole on the outer edge. As the stepper motor spins the solid parts of the disk prevent the light from registering on the optical slotted switch. But, once the hole passes in between, the optical slotted switch light goes thru and the micr-ocontroller reads this and understand this is home.

Now that you have a "Home" point you can start your stepper motor out in exactly the same place every time at power up. The accuracy issue is a small one, lets say you are building a gauge for something that will run for weeks on end. In this event I would code my micro-controller to do a "self diagnosis" once every 24 hours.What it would do is simply go until it hits home then advance back to the appropriate reading. This event would take milliseconds and just look like a twitch on the needle to the human eye.

OTHER ISSUE STATED ON THIS POST!!
People have mention gear boxes on your stepper motor. These are useful for accuracy and torque but not needed to control the speed of the motor. Your (pwm)pulse width modulation does that for you already. Gearing does slow it down but that is a side affect of it's intended use. Stepper motors have a maximum RPM but you can slow it down with your coding to resemble grass growing.

***NOTE***
If you want to have the motor point to 360 positions with an accuracy of  +/- .01 degrees then you need a 5/1 gear box for the 1.8 stepper. I would not recommend using a .9 stepper because the math doesn't work for your case. These gear boxes are readily available, just make sure you get one for your shaft diameter.

I built one of these for DCS and recall that only certain points on the gauge were Compass symbols N,S,W,E with 2 definitive headings between each symbol for a total of 8 numbers and the in between numbers were hash marked and a I did not need a gear box, just some creative coding.

So, if you printed all 360 marks on your gauge how are you able to read them and how big is this gauge?

4-8 wire stepper motors. The smaller the number of wires your stepper motors has the less accurate they will be. Do not let that make you run out and buy the 8 wire motors because I built a CNC for etching SMD PCB's (smd = cell phone like tiny circuit boards) and I only use the 4 wire function. I bought the 8 wire stepper motors but bridged the additional wires essentially making them 4. so 4 wire is extremely accurate and you would need a very specific need for the 8 wire. As it happened the distributor that sold me my NEMA 23 425oz steppers only had 8 wire. The number of wires does affect the speed as well tho, My nema 23 425oz will run max 600rpm on 4 wires and about 400rpm on 8. But the only time I use the MAX RPM is for the homing feature so does this really matter?   For a CNC application, YES! / For a dash gauge, HELL NO!

1.8 and .9 degree stepper motors. Here is how this works. 360 degrees is a full circle, so if you took that and divided it by 1.8 you get 200 and if you did it by .9 you would get 400. That is the number of pulses you would need to send to the stepper to get it to turn one complete turn. I have never needed a .9 stepper motor. 200 individually controlled positions has always been more than enough. The most detailed gauge I have ever made was for a motorcycle. A car needs maybe 4-9 thousand rpm's but the motorcycle goes all the way up to 20, 000. But we can easily do the math here and see that you need exactly 200 points of movement to achieve this. I could have done it with way less but used all the points BECAUSE I CAN.

425oz?   OZ, this is a unit applied to stepper motors. This identifies the holding torque of a particular motor. Basically stepper motors do not turn freely and require a bit of pressure to get them to move to the next position. Get a large stepper motor and try to turn that shaft with your fingers and you will most likely not be able to. So, when building things with stepper motors you have to consider the weight of the objects your motor will be turning. Gauges do not really matter much here, the needles are insignificant. The same type of stepper motors you would use for your gauge are capable of turning web cams. I find these on Ebay for a couple bucks (shipping included) and they come with a drive board.


optical slotted switch - you should youtube these. some great videos available from the Arduino fans.
http://www.google.com/imgres?imgurl=http://www.futurlec.com/Pictures/OptoSwitch.jpg&imgrefurl=http://www.futurlec.com/LEDOptoSwitch.shtml&h=150&w=200&sz=24&tbnid=CJJu_8JdOQCcMM:&tbnh=95&tbnw=127&zoom=1&usg=__nxthd8CdQjDnV4Mprjywdpnue6M=&docid=XP3vVsIbs8LDcM&sa=X&ei=IX0KUeOfAbC10AGD4oGgCw&ved=0CGUQ9QEwBQ&dur=335
Title: Re: Stepper Position Sensing and Control
Post by: ronpbaker on Jan 31, 2013, 05:36 pm
I would like to point out that if I were to build another flight sim, I would simply go to the junk yard and salvage old gauges from junk cars. You can reverse engineer these things to suit your needs and only spend maybe a couple bucks. Free if the junk yard doesn't check your tool box completely.   :smiley-eek-blue:

I filled a 5 gallon bucket once with gauges and switches of all sorts and the guy charged me like 10 bucks. Just stay away from classic junk yards. Find one in your area that cares more about volume of scrap metal and they could care less about those damn parts.
Title: Re: Stepper Position Sensing and Control
Post by: Tom_G_2010 on Feb 01, 2013, 03:31 pm
Thanks for all the input.  Several weeks back I ordered a 0.9 stepper motor, a Seeed motor shield, and an optical sensor.  I have the basic code started and I'm fabricating a heading wheel to mount on it that will also trip the optical sensor at a calibration point.

The wiring of the motor is basic enough and I have that taken care of.  Likewise the wiring of the optical sensor.  The basic code to run the motor is easy enough as is the code to do the calibration.  I have almost everything either working on my test bench with one exception.

That exception being the continuous rotation past North in either direction.  Not a show stopper, but I just haven't come up with a satisfactory way to deal with it yet.  The flight sim software feeds me a compass bearing in the form of 1 to 360 degrees.  I am reading the changes and doing the simple math to move the stepper from prior read to current read by the calculated number of steps.  In most cases the step is no more that 1 or 2 degrees in either direction, but on occasion either do to high rates of turn or program lag the steps can be slightly bigger.

So I am trying to come up with the best way to deal with crossing North where the prior read might be 359 and the current read is 001, or in the other direction, the prior read might be 002 and the current read 360.  I need the first to calculate +2 and the second to calculate -2.

Thoughts?

As for gauge or part sources, I have purchased several parts from bone yards, but not gauges.  My sim is somewhat generic and will emulate about 6 different aircraft.  As such the variation in operating ranges needed on the gauges makes hardware based gauges somewhat problematic unless I can come up with easily interchangeable face plates.  As such I am only looking at mechanical gauge builds for a very limited portion of the sim, the Wet Compass being one of them.

Thanks!
Tom G.
Title: Re: Stepper Position Sensing and Control
Post by: nguyentangdai on Jun 01, 2018, 05:08 pm
help for me write 1 góc control code and position of stepper 4 and a servo
Title: Re: Stepper Position Sensing and Control
Post by: polymorph on Jun 01, 2018, 08:14 pm
You need to start your own thread.

This is at the top of every subforum.

How to use this forum (http://forum.arduino.cc/index.php/topic,148850.0.html)