stepper motor unipolare in full step

Nandino

Member
Salve a tutti, ecco il mio programmino con il 08m2 per far funzionare un stepper motor in configurazione full step
start0: basso 4
avanti0: se pin3 = 1 allora gosub indietro0
alto 0
pausa 1
basso 0
pausa 1
basso 0
pausa 1
alto 0
pausa 1
goto avanti0

indietro0: basso 0
dietro0: se pin3 = 0 allora gosub start0
alto 4
pausa 1
basso 4
pausa 1
basso 4
pausa 1
alto 4
pausa 1
goto dietro0


start1: basso 2
avanti1: se pin3 = 1 allora gosub indietro1
alto 1
pausa 1
alto 1
pausa 1
basso 1
pausa 1
basso 1
pausa 1
goto avanti1

indietro1: basso 1
dietro1: se pin3 = 0 allora gosub start1
alto 2
pausa 1
alto 2
pausa 1
basso 2
pausa 1
basso 2
pausa 1
goto dietro1





start2: basso 1
avanti2: se pin3 = 1 allora gosub indietro2
basso 2
pausa 1
alto 2
pausa 1
alto 2
pausa 1
basso 2
pausa 1
goto avanti2

indietro2: basso 2
dietro2: se pin3 = 0 allora gosub start2
basso 1
pausa 1
alto 1
pausa 1
alto 1
pausa 1
basso 1
pausa 1
goto dietro2



start3: basso 0
avanti3: se pin3 = 1 allora gosub indietro3
basso 4
pausa 1
basso 4
pausa 1
alto 4
pausa 1
alto 4
pausa 1
goto avanti3

indietro3: basso 4
dietro3: se pin3 = 0 allora gosub start3
basso 0
pausa 1
basso 0
pausa 1
alto 0
pausa 1
alto 0
pausa 1
goto dietro3

programma lunghissimo che per funzionare funziona ma volevo sapere se c'è qualche altra alternativa meno macchinosa,
perche penso che anche se non si vede nel funzionamento del motore forse con il multitasking il silicio provoca dei ritardi .....
grazie per la cortesia un saluto a tutto siete fantastici
 

inglewoodpete

Senior Member
To assist forum members who do not understand Italian, I have translated your post into English, which may bring you more help.
Per assistere i membri del forum che non capiscono l'italiano, ho tradotto il tuo post in inglese, il che potrebbe darti ulteriore aiuto.

I see that you are using the gosub command but the (sub)routines that you call do not end with a return statement. This will cause problems for your PICAXE.
Vedo che stai usando il comando gosub ma le (sotto) routine che chiami non terminano con un'istruzione return. Ciò causerà problemi al tuo PICAXE.



Hello everyone, here is my little program with 08m2 to operate a stepper motor in full step configuration

Rich (BB code):
start0: Low 4
Forward0: If pin3 = 1 Then gosub Back0
High 0
Pause 1
Low 0
Pause 1
Low 0
Pause 1
High 0
Pause 1
goto Forward0

Back0: Low 0
Reverse0: If pin3 = 0 Then gosub start0
High 4
Pause 1
Low 4
Pause 1
Low 4
Pause 1
High 4
Pause 1
goto Reverse0

start1: Low 2
Forward1: If pin3 = 1 Then gosub Back1
High 1
Pause 1
High 1
Pause 1
Low 1
Pause 1
Low 1
Pause 1
goto Forward1

Back1: Low 1
Reverse1: If pin3 = 0 Then gosub start1
High 2
Pause 1
High 2
Pause 1
Low 2
Pause 1
Low 2
Pause 1
goto Reverse1

start2: Low 1
Forward2: If pin3 = 1 Then gosub Back2
Low 2
Pause 1
High 2
Pause 1
High 2
Pause 1
Low 2
Pause 1
goto Forward2

Back2: Low 2
Reverse2: If pin3 = 0 Then gosub start2
Low 1
Pause 1
High 1
Pause 1
High 1
Pause 1
Low 1
Pause 1
goto Reverse2

start3: Low 0
Forward3: If pin3 = 1 Then gosub Back3
Low 4
Pause 1
Low 4
Pause 1
High 4
Pause 1
High 4
Pause 1
goto Forward3

Back3: Low 4
Reverse3: If pin3 = 0 Then gosub start3
Low 0
Pause 1
Low 0
Pause 1
High 0
Pause 1
High 0
Pause 1
goto Reverse3
very long program that works to work but I wanted to know if there is any other less cumbersome alternative,
because I think that even if you can't see it in the functioning of the engine, perhaps with multitasking the silicon causes delays .....
thanks for the kindness a greeting to all you are fantastic
 

Nandino

Member
start0: Low 4
Forward0: If pin3 = 1 Then gosub Back0
High 0
Pause 1
Low 0
Pause 1
Low 0
Pause 1
High 0
Pause 1
Return

Back0: Low 0
Reverse0: If pin3 = 0 Then gosub start0
High 4
Pause 1
Low 4
Pause 1
Low 4
Pause 1
High 4
Pause 1
Return

Ecc......
What careless, this ok? thanks for the fix, so this is the only way to run the motor in full stepper? Isn't there a combination using the "toggle" or something?
Thanks for your kindness
 

Eng460

Well-known member
Hi Nandino, section 3 of the manuals interfacing, example 6 is a unipolar stepper motor, and uses a program similar to yours but using the “toggle“ command. That might help, especially if you have a copy of the manual in your language.

Also worth looking at the “toggle” command in section 2.

What project will you use the stepper motor for?

Eng460
 

Nandino

Member
The toggle command is not good for the unipolar command in full step mode, because it has a single sequence to power a single motor winding, in full step two opposite windings must be powered at the same time, all to have more torque!
I have no projects in progress but it could help me in the future
 

Eng460

Well-known member
Hi Nandino, I believe that is covered in the example by the circuit shown which uses a ULN2003 Darlington driver chip to provide the necessary two complimentary outputs. The code provided just provides single steps spaced by delays, but I assume by shortening the delay the behaviour could be tuned to your requirements.

Extra components I know. I wonder if as an alternative, the “let pinsA=... “ Which allows changing all pins in a port with one instruction could be used to avoid the Darlington driver providing you have enough available outputs on your Picaxe to do the job directly.

I have not tried it, just thinking....

Eng460
 

AllyCat

Senior Member
Hi,

Why make it so complex with multitasking (many Start:s) and/or Subroutines (slow). Also, the pulses will not be spaced uniformly because of the extra delay in the IF...GOSUB construction. Adapted from my suggestions in #3 of a recent thread , for example :
Code:
#picaxe 08m2
symbol A1 = c.0     ; Select as required
symbol A2 = c.1
symbol cB1 = c.2
symbol cB2 = c.4
symbol direction = pinc.3
symbol gapwid = 8          ; Adjust for speed
symbol gapwid1 = gapwid - 1
setfreq  m16                    ; Or as required
forwards:
  pause gapwid1  :  High A1 : Low A2     ; Enable coil A
  pause gapwid  :  High cB1 : Low cB2     ; Enable coil B
  pause gapwid  :  Low A1 : High A2     ; Reverse coil A
  pause gapwid  :  Low cB1 : High cB2     ; Reverse coil B
  if direction = 0 then forwards             ; Or fall into backwards
backwards:
  pause gapwid1  :  High A1 : Low A2     ; Enable coil A
  pause gapwid  :  Low cB1 : High cB2     ; Reverse coil B
  pause gapwid  :  Low A1 : High A2     ; Reverse coil A
  pause gapwid  :  High cB1 : Low cB2     ; Enable coil B
  if direction = 1 then backwards
  goto forwards
Or more compact versions are possible by using Variables (e.g. b0 - b3) to define the pins and/or TOGGLE commands and/or OUTPINS and XOR commands, etc. :

Cheers, Alan.
 

Nandino

Member
ciao allycat, ho provato a simulare il tuo programma ma le sequenze di accensioni non sono complementari, anzi se hai tempo prova a simulare il mio primo programma e così vedi anche tu cosa intendo, scusa se ti rompo con questa richiesta



ciao eng460: il circuito dell'esempio 6 ha un difetto che sono le resistenze di ritorno, quelle da 1k ... bruciano !!!!! anche se devo dire che il motore gira a pieno regime e sarebbe fantastico, il mio primo programma fa girare il motore a pieno regime ed è ok, ma come dice allycat è troppo complesso!
 

AllyCat

Senior Member
Hi,

I presume your program is written for an Italian version of the Program Editor, but it is too complex for me to bother to translate it. However, I now see that you require code for a UNIPOLAR not BIPOLAR stepper. You don't appear to have identified the "A" and "B" coil connections, so I have changed the Symbols and if the drive outputs are "Active High" then I believe the equivalent version would be:
Code:
#picaxe 08m2
symbol A1 = c.0     ; Select as required
symbol A2 = c.2     ; *swapped
symbol cB1 = c.1   ; *swapped
symbol cB2 = c.4
symbol direction = pinc.3
symbol gapwid = 8          ; Adjust for speed
symbol gapwid1 = gapwid - 1
setfreq  m16                    ; Or as required
forwards:
  pause gapwid1  :  Low A2 : High A1      ; Enable coil A+
  pause gapwid  :   Low cB2 : High cB1     ; Enable coil B+
  pause gapwid  :  Low A1 : High A2     ; Enable coil A-
  pause gapwid  :  Low cB1 : High cB2     ; Enable coil B-
  if direction = 0 then forwards             ; Or fall into backwards
backwards:
  pause gapwid1 :  Low A2 : High A1     ; Enable coil A+
  pause gapwid   :  Low cB1 : High cB2     ; Enable coil B-
  pause gapwid  :  Low A1 : High A2     ; Enable coil A-
  pause gapwid   :   Low cB2 : High cB1     ; Enable coil B+
  if direction = 1 then backwards
  goto forwards
Or you can reduce the power consumption by introducing another "Pause" between each pair of "Low" and "High" commands and reducing the first Pause correspondingly.

Cheers, Alan.
 

Nandino

Member
hello eng460: I apologize if I have not always translated my words now I will rewrite you translated, the circuit of example 6 has a defect which are the return resistors, the 1k ones ... they burn !!!!! although i have to say the engine runs at full throttle and that would be great, my first program runs the engine at full throttle and it's ok but as allycat says it's too complex!
 

Eng460

Well-known member
Hi Nandino, thank you for translating. Google translate on an iPad is beyond my computing skills. No need for apologies. It is my loss that I only have one language. It adds to the richness of the forum to have people participating from all around the world with a common interest.

Interesting that those return resistors are burning, or even warm. There is obviously something about the circuit that I don’t understand as I thought they should be at signal level. Interesting!

I wonder if others can give more explanation.

Eng460
 

AllyCat

Senior Member
Hi,

No, the 1k resistors should not get hot or "burn". With 5 volts the power is v2 / R = 25 / 1000 = 25 mW. Even quite small resistors are rated at 250 mW so should not get noticibly warm. However, a problem with the hardware (inverter) method (in example 6) is that always one "A" coil and one "B" coil must be active; the motor can NOT be switched OFF, so it might get quite (or very) hot. In fact, due to the inductance of the coils, the power can be higher when the stepper is NOT moving than when it rotates !

Thus it is better to have one output pin for each coil (so that all the coils can be switched OFF) but there is also a potential problem with the TOGGLE command: To switch the coils needs for example TOGGLE A1 : TOGGLE A2 , which would be equivalent to alternatively HIGH A1 : LOW A2 and then LOW A1 : HIGH A2 . Even using PICaxe's Multi-Tasking (many Starts) there is a time delay at each separate instruction, so there must be a time when BOTH A coils are active at the same time (which will use double the current, but the magnetic fields cancel to zero). This is not a problem with direct HIGH and LOW commands (because we can always arrange to switch OFF one coil before switching the other ON), and with a BiPolar stepper, switching both ends of the coil to the same voltage level switches it OFF.

All these problems can be solved by using the PINS command, but everything needs to be configured correctly. The basic core of the program could be (untested), but the code in post #11 should be easier to understand and work correctly:
Code:
DIRS = %10111   ; Coil pins = B2 , 0 , A2 , B1 , A1
PINS = %00011   ; Start Forwards  .. or :
;  PINS = %00110   ; Start Backwards
FOR b0 = 0 TO 9
  PAUSE 7
  OUTPINS = OUTPINS XOR %00101
  PAUSE 8
  OUTPINS = OUTPINS XOR %10010
NEXT
OUTPINS = 0     ; Stop
Cheers, Alan.
 

Nandino

Member
You are very kind to say the least I hope to be able to try all your advice one of these evenings, especially the latest example of Alan and in any case thanks for the support, see you soon Luca
 

Nandino

Member
Alan .... Fantastic, I tried to simulate your two programs .... perfect, but now I have to put them on the picaxe and let's see the engine how it behaves and how much it warms up, during the week I try and let you know .... Thanks thank you thank you
 

Nandino

Member
#picaxe 08m2
DIRS = %10111 ; Coil pins = B2 , 0 , A2 , B1 , A1
symbol direction = pinc.3
PINS = %00011 ; Start Forwards

forwards:
PAUSE 7
OUTPINS = OUTPINS XOR %00101
PAUSE 8
OUTPINS = OUTPINS XOR %10010
if direction = 0 then forwards
PINS = %00110 ; Start Backwards
backwards:
PAUSE 7
OUTPINS = OUTPINS XOR %00101
PAUSE 8
OUTPINS = OUTPINS XOR %10010
if direction = 1 then backwards
PINS = %00011 ; Start Forwards
goto forwards

hi Alan here I am again, I made a mix of your two programs that you gave me, what do you say ... could there be some problem if I do this?. in simulation everything is ok, and then I wanted to ask you if you explain this to me ....
OUTPINS = OUTPINS XOR% 00101
what is XOR?
and because the first pause put 7 and the next one put 8
why not all two equal?
and if I wanted to put the engine at a standstill for 3 seconds between one change of direction and the other, what should I do?
thank you very much for your time that you dedicate to me thanks
Luca
 

premelec

Senior Member
Exclusive or XOR, is a logical operator which results true when either of the operands are true (one is true and the other one is false) but both are not true and both are not false.
 

AllyCat

Senior Member
Hi,

The XOR effectively TOGGLES all bits in the first variable which are set in the second. So var XOR %00101 toggles bits 0 and 2 (A1 and A2), reversing the current. Note that {OUT}PINS is a short-cut, you might normally write b0 = b0 XOR %00101 : PINSC = b0 .

The Test and Jump (IF .. THEN GOTO) itself takes some time, approximately 1.2 ms (@ 4 MHz), so I subtracted 1 ms from the normal delay. I also showed a standstill (Stop) in my code (OUTPINS = 0) , just follow that with a PAUSE 3000 before setting the new direction.

Cheers, Alan.

ADDED: Yes, you can "mix" the instructions in the program in many ways, but personally I refer the program in #11 because it has a better structure. Also, you can then add delays to turn one of the coils (at a time) OFF, and I am considering more complex arrangements where one might use TRI-STATE, DAC or PWM outputs to save some pins and/or power and to "hold" a position, or perhaps "microstep", etc..
 
Last edited:

Eng460

Well-known member
Hi Premelec, that is the definition that I learned so many years ago, but that seems only to yield a simple true/false or 1/0 answer. But there must be more to it. In section 2 on variable mathematics, the operation is described as a “bit wise XOR”. I have to guess that means when performed on two binary numbers, the operation requires performing the XOR on the all the corresponding bits of the two numbers and so results in a new binary number, not a simple true (1) or false (0) outcome. Is that correct? I have always wondered if that was the meaning, but had no idea what it would be used for, so this is indeed a very interesting thread.

So the next question becomes where did that mystery number Alan used (with the original pins to get the required result) come from? We actually know the initial output number and the final number. Can we use these to find the mystery number?

I thought nothing ventured, nothing gained and took a wild guess. I wrote down the first and final numbers and performed the bitwise XOR on them and like magic, the mystery number appears.

So my question changes again! What is the logic behind choosing XOR? Why not one of the seven other logical operations listed in section 2 under the title of variable mathematics? And the obviously what would the others be used for?

It’s a bit obtuse when written out with words, so here is the first program step

%00011 ;pins
%00101 ;mystery number
————-
%00110, the result of bitwise XOR

Now the reverse -
%00011 ;pins
%00110 ;required result
————-
%00101, the result of bitwise XOR, the mystery number.

So I feel I have learned something. Just puzzling over when those other bitwise logical operations might come in handy.

I see Alan has replied while I have been doing all that and answered some of my questions by explaining the timing of the pauses and detailing the shortcut. That is very helpful, thank you. So now only my question about why XOR was appropriate in this case and when would the other bitwise logical operations be used still applies.


Eng460
 

premelec

Senior Member
Thanks for bringing that up.... bitwise it is... which is clearer when writ in binary rather than hex or decimal... as to all those other clever section 2 functions hippy and Alan have a mastery and understanding I never will though I occasionally use one or two. I guess it is just a lot of practice... you are on the right track there!
 

AllyCat

Senior Member
Hi,

Yes "bitwise" means working in parallel on all the bits in the byte at the same time, or actually you can do all 16 bits in a Word. You can (also) change each bit in the byte using the bitx variables in bytes b0 - b3, or work with the "weight" of the bit(s) in any byte or word. For example: %01000000 OR 17 is the same as %01000000 OR %00010001 and becomes %01010001 .

The main reason for using XOR is "because it works". Most of the logical operators work with two binary values so there are 4 possibilities (i.e. 00 , 01 , 10 . 11) and 4 possible "outcomes" (i.e. 0 or 1). Here, we want a selective TOGGLE, i.e. to INVERT (only) the bits in the first word that are selected by "1" bits in the second. Note actually the first and second bytes can be swapped and still produce the same result. For inversion, of the 4 possible input combinations, two of the outcomes should be "0" and two should be "1", but the majority of Logic operations produce a "1" (or a "0") for 1 or 3 of the combinations.

Most of this is described in Manual 2, "Mathematics - Variables", but there are actually more than 7 Logical operators; I think PICaxe supports: AND , NAND , ANDNOT (which is not the same), OR , NOR , ORNOT , XOR , XNOR and XORNOT , and NOT with a single variable. You could make a full "Truth Table" but I'll just start you off:
Code:
A   B     AND  NAND  ANDNOT  XOR
0   0      0    1      0      0
0   1      0    1      1      1
1   0      0    1      0      1
1   1      1    0      0      0
          ---  ---    ---    ---
Total  =   1    3      1      2    bits set
Interestingly, NAND and similar use more program bytes than ANDNOT, etc. There are also the dedicated characters for many of those (e.g. & , | , ^ , / and &/ etc.) but personally I prefer the "plain text" version to hieroglyphics. ;)

EDIT: I didn't fully read Eng460's questions before this post, so to add a little more detail: The bitwise operations are particularly useful for "masking" operations, for example testing for a condition on one or a group of bits within a byte or port. For example you can force any byte (not just the bit-addressable b0 - b3) ODD by using var = var OR %00000001 or EVEN with var = var AND %11111110 . The XOR is particularly useful for testing or creating a change in a group of bits, for example to swap an ASCII character between Upper Case (Capitals) and Lower Case you can use char = char XOR %00100000 (noting the weight of that "1" is 32 and "a" - "A" = 97 - 65 = 32).

Finally, it's interesting that any ("combinatorial") logic function can be created from only NAND gates (Latches may be more complex). A NOT is created just by linking the two (or all) inputs, and two (or more) NANDs feeding into the inputs of another NAND give an AND - OR function (e.g. inputs (A AND B) OR (C AND D) ). Thus an EXCLUSIVE - OR can be created from 5 x NAND gates, for example (A AND (NOT B) ) OR (B AND (NOT A) ) . But of course brackets are not available in PICaxe Basic !


Note that I have also added a paragraph to my post #19, but now perhaps we should give Nandino his thread back. :)

Cheers, Alan.
 
Last edited:

Nandino

Member
it was a fantastic lesson to say the least, to tell the truth I miss the steps, the example programs to understand the functions and nand etc .... but in my case the XOR came right to my brain .....
now I put this threed in suspension for a few days because I have to physically put everything into practice and then we will hear from you and I will post my progress,
I greet you all and thank you all for your help
Luca
 

Eng460

Well-known member
Thanks so much Alan, you have gone a long way to filling in the vast gaps in my understanding of how those logic functions can be used. I am sure that I am not the only one who will be reading your explanation many times before I feel I really have it, but I can now see the possibilities of those function.

Instead of just a few lines of code that solve the specific problem, you have explained how they work, so the knowledge is so much more useful. (And I suggest right on topic.)

I found the seven plus XOR operations in section 2, but NOT is only described on the pages for X1 and X2 parts. Does it apply on the M2 parts as well? And I where would we find the XORNOT description?

Thanks Premelec, you can see why I needed a little more than the definition. But so many doors now open.

Now back to Nandino. Though I must keep an eye out for one of those stepper motors in my box of salvaged parts.

Eng460
 
Top