State Transition Diagram for Stepper Motor Control

Appendices: P03

Appendices:

State Transition Diagram Model for Stepper Motor Control

The state transition diagram shown in Fig. 1 graphically represents the states and transition conditions for the stepper motor control. States represented by S1 through S4 are full-step states. States represented by S0_5 through S3_5 are half-step states. Each state outputs a unique combination of four bits to the stepper motor, as specified by Table 1 in Project 3.

The next-state transitions that are between strictly full-step states and between strictly half-step states cause the stepper motor to rotate one full step. For example, a transition from state S1 to S2 causes the motor to rotate a full step. Likewise, a transition between S1_3 and S0_3 also causes the motor to move a full-step rotation. A transition between half-step and full-step states cause a half-step angular rotation. Such is the case for a transition from state S3_5 to S3.

Figure 1. FSM diagram for stepper motor step and half-step control. Each state is assigned an on-entry output defined by Table 1 (Project 3).

A transition is generated each time the FSM function is called. Each transition is identified by two parameters inside square brackets that are used to determine the step direction and mode. Only transitions shown on the state diagram will result in the stepper motor rotating in a predictable manner. Each state has four possible transitions to a new state. The transition path to the new state is controlled by the clockwise (CW) and counter-clockwise (CCW) direction controls as well as the full (F) or half (H) step mode. Whenever a new state is entered, it generates the output codes shown in Table 1.

To illustrate how state diagrams can be used to assist in the code development, consider the state diagram described by Fig. 2 that models using a momentary contact push button to implement a push-on / push off switch. There are two possible output conditions: the switch is either closed ('1') or open ('0'). The condition of the momentary contact push button input is either pressed ('1') or not pressed ('0'). This design requires four states labeled “SA” through “SD.” The initial state of the switch is identified by the transition from state “I.” Each state identified by a circle has a name and an output condition shown as a value below the state name. For example, the initial state, SA, has an output condition '0'. “B” represents the stable button condition (all button contact bounce has been eliminated). The state diagram shows that the switch output changes whenever the button makes a transition from the not pressed condition to the pressed condition.

Figure 2. Push-on / push-off button state diagram.

Listing 1 is the C code that implements the state diagram shown above using the “switch-case” program control structure. Line 13 defines the states using an enumeration declaration. The state variable, pb_state, and the switch output variable, sw, are both declares as static variables that retain the previously assigned value between successive calls to the pb_sm function. The variable, pb_state, provides the necessary state memory. The switch output variable, sw, is declared static for more efficient program execution by eliminating the need to set the variable each time the function is called.

The cases used in the switch-case control structure shown in Listing 1 have a one-to-one correlation with the states defined in Fig. 2. Each time the function is called, a transition will be taken to a next state. The next state transition is either to a different state or back to the current state. The conditional if statements in lines 19, 26, 32, and 40 control the transition to a different state. If no transitions conditions are met, the state remains unchanged. This represents the transition to the same state for the next state condition.

Listing 1. C code for implementation of the push-on / push-off button design.

/* pb_sm Function Description **********************************************
 * KEYWORDS:        push button, FSM, state machine
 * DESCRIPTION:     Implements a push-on / push-off switch action using a
 *                  simple momentary bounceless push button.
 * PARAMETER1:      int btn - button condition: 1 = pressed, 0 = relaxed
 * RETURN VALUE:    switch on/off output: 0 = open, 1 = closed.
 * NOTES:           This state implementation generates output on state exit
 *                  transitions.
 *
 * END DESCRIPTION *********************************************************/
int pb_sm(int btn)
{
   enum {SA=0, SB, SC, SD};    /* Declaration of states */
   static int pb_state = SA;   /* Push button state variable initialized to SA */
   static int sw = 0;          /* Switch output initialized to open */
   switch(pb_state)
   {
      case SA:
      if(btn)              /*Is button pressed? */
      {
         pb_state = SB;  /* Transition to next state */
         sw = 1;         /* Output change to closed (1) on state exit */
      }
      break;
      case SB:
      if(!btn)            /*Is button not pressed? */
      {
         pb_state = SC;  /* Transition to next state */
      }
      break;
      case SC:
      if(btn)              /*Is button pressed? */
      {
         pb_state = SD;  /* Transition to next state */
         sw = 0;         /* Output change to open (0) on state exit */
      }
      break;
      case SD:
      if(!btn)            /*Is button not pressed? */
      {
         pb_state = SA;  /* Transition to next state */
      }
      break;
   }
   return sw;                 /* Return switch output */
} /* End of pb_sm */

  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.