Main Menu

stepper motor math

Started by Rx7man, May 09, 2015, 07:31:24 PM

Rx7man

I was looking for a neat solution to figuring out what outputs you need for stepping a motor.. Some code I found used a Case / Switch statement and assigned each pinout for each case... surely it works but it took up a couple pages and was messy, so I thought there must be a nice way of doing it.. here's what I came up with.

Step numbers are in halfsteps, increment by 2 if you want full steps.. yes, it's VB


            output.PhaseA = (stepnumber + 1) Mod 8 < 3
            output.PhaseB = (stepnumber + 5) Mod 8 < 3
            output.PhaseC = (stepnumber + 7) Mod 8 < 3
            output.PhaseD = (stepnumber + 3) Mod 8 < 3


4 lines of code that are nicely justified sure looks nicer to me.

When using the halfstep mode, bear in mind that you can have both coils operating at once.. make sure you can dissipate that heat if you're going to stop energized to hold..

'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

I was doing some testing...

Stepnumber MUST be positive for the logic listed above to work...
Stepnumber SHOULD be an UNSIGNED integer.. the modulus function is MUCH faster unsigned!
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

Yeah, unless you actually need negative numbers go unsigned.

Also if you know variables won't go over specific sizes, bytes are of course faster than ints of course faster than double/floats.
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

I did a few tests for speed.. this is what came up with the fastest iteration time.. 30,000 loops per second.. of course nothing else was running.

byte mod = stepNumber%8;
 
  digitalWrite(pinA, mod <=2);
  digitalWrite(pinB, mod >=2 && mod<=4);
  digitalWrite(pinC, mod >=4 && mod <=6);
  digitalWrite(pinD, mod >=6 || mod ==0);
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

I'm also going to try it on the PWM pins with analogwrite.. preliminary tests show it runs at about 1500 ms for 30,000 loops.. but it might be handy to keep the heat in the motor down to a minimum.. we'll have to do some test runs and see if it's really going to be an issue anyhow before worrying about it too much.
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

Riiight, direct PWM may not be the way to go, but some type of control timer.

The one posted uses millis which are in ms, so if 1ms is too slow you may need a direct int pin / timer library.
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

It may not be, but it is also what we've got available to us quite readily... More ideally you'd use one of those LM2907 injector driver chips for true current limiting..

I will just be running it every loop and not timing it... if I need to do timing I'll determine that by how far I let it move... I can also force it into halfsteps to slow it.
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

Rx7man

Well.. I think I might offload the stepper driving to a seperate board...
Take a look at this one, I think it has ample power, SPI based, and you can just tell it how many steps to move, it has stall detection, current limiting, etc, and it isn't shield based so I can mount it wherever I want and not tie up space on the arduino.. or outputs.. perhaps you should consider it if you want to do an electronic AFC control too. 
Of all the stepper drivers I've seen it's the most suited.
SmartLynx Bipolar Stepper Motor Controller - RobotShop
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

Looks pretty bad ass. I'm not sure I'm going to put my AFC back on until its time to smog my truck again... I love rolling it on people that are jerks and just let them have the coal.
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

You can always put a "rollcoal" mode in where it just sets your AFC to wide open... perhaps leave your VGT wide open too so it just doesn't spool!
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles

hakcenter

Quote from: Rx7man on May 26, 2015, 09:59:42 PM
You can always put a "rollcoal" mode in where it just sets your AFC to wide open... perhaps leave your VGT wide open too so it just doesn't spool!

Ya that's the cruise button right now, haha! bwuuuuuuuuaaaaaaaaaaaaaaaaaaa. Ya probably wouldn't be a bad idea to have valet mode incase I get stopped because my air filter is obviously clogged *rolls eyes*.

The other day this jack ass, cuts in front of someone who was let in from a parking lot onto the street. Yes, just cut right in front of them just edged their way in even thou the parking lot driver was let in by the car in front of me.

So I had noticed it was a brand new FRS with the windows down. I wondered if they smoked ? So I rolled up, gave them a dirty look he snickered at me.. rolled up a few more feet, and watched my exhaust flow through the drivers window and out the passenger, and just romped it on him LOLOLOLOL
TS2009 Deḇarim 8:2
"And you shall remember that יהוה your Elohim led you all the way these forty years in the wilderness, to humble you, prove you, to know what is in your heart, whether you guard His commands or not.

Rx7man

daily driving I don't like rolling coal, and I've tuned my truck so it really doesn't do much more than haze under normal circumstances.. though if I just mash the throttle from a dead stop it will repaint things... I especially do it to dogs that like chasing my truck.. one in particular (german shepherd) would chase every vehicle that came around the bend on the dirt road.. After a year of gagging on coal and getting peppered with rocks as I drift my truck around that corner, he comes up to the edge of the road, sees it's me, and ducks away... *snickers*
'94 dually,  67/67 HE351VE, NV5600, ~600hp
'93 ECLB 47RH, new toy truck, H pump project, 1000hp goal, 300K miles
93 XCLB auto, bone stock, 350K miles
93 XCLB 5spd, bone stock, 100K miles