Shop OBEX P1 Docs P2 Docs Learn Events
Stepper Motors with Activity Board and H-Bridge - Page 3 — Parallax Forums

Stepper Motors with Activity Board and H-Bridge

13»

Comments

  • plau45plau45 Posts: 109
    edited 2017-06-29 16:31
    Can't get my images uploaded. :(
  • AribaAriba Posts: 2,682
    I don't see that you change direction in your code.

    I would try it with a code like this:
    #include "simpletools.h"
    
    // Pins
    #define DIRPIN      14
    #define STEPPIN     15
    #define LEFTBUTTON  0
    #define RIGHTBUTTON 1
    
    // Vars
    int moveLeft  = 0;
    int moveRight = 0;
    
    int main() {
      low(STEPPIN);
      while(1) {
        moveLeft  = input(LEFTBUTTON);     //expects buttons are active high..
        moveRight = input(RIGHTBUTTON);    //..otherwise use: input(<pin>) ^ 1;
        if(moveLeft || moveRight) {
           set_output(DIRPIN, moveLeft);   //use moveRight if the direction is wrong
           pulse_out(STEPPIN, 5);          //5us pulse
        }
        pause(3);                          //3ms wait, defines speed
      }
      return 0;
    }
    
    I have it not tested with hardware, but it compiles in SimpleIDE.

    Andy
  • You could also try this
    #include "simpletools.h"
    
    int main()
    {
    	// Set the desired number of steps
    	uint32_t required_steps = 40000;
    
    	// Set the desired direction of rotation.
    	// This variable can only be set to a
    	// value of 0 or 1.  A value of 0 will
    	// turn the motor either clockwise or
    	// counter-clockwise, dependant upon how the
    	// motor coils are attached to the stepper
    	//driver, and a value of 1 will turn it in
    	//the opposite direction of 0.
    	uint8_t direction_of_rotation = 0;
    
    	// Establish the IO pin being used to communicate
    	// with the direction pin of the stepper driver.
    	uint8_t direction_pin = 14;
    
    	// Establish the IO pin being used to communicate
    	// with the step pin of the stepper driver.
    	uint8_t step_pin = 15;
    
    	// Depending upon inertia of the motor and/or
    	// the load attached to the motor, you should be
    	// able to vary the running speed of the motor
    	// by adjusting this variable.  If the duration
    	// is too short, the motor will either wine, vibrate,
    	// and not move, or it will miss steps during runtime,
    	// and if the duration is too long, it will take
    	// forever to get from point A to point B,  You will
    	// have to experiment with this variable, depending
    	// upon your motor, load, desired speed, etc...	
    	uint32_t overcome_inertia = CLKFREQ / 5714;
    
    	// This variable must meet the required pulse width
    	// of the stepper driver IC being used.  Check the
    	// IC manufacturers specifications for the required pulse width.
    	uint32_t high_pulse_width = CLKFREQ / 500000;  // 160 // 2uS
    
    	// Set the clockwise pin.
    	uint8_t RotateClockwise = 0;
    
    	// Set the counter-clockwise pin.
    	uint8_t RotateCounterClockwise = 1;
    
    	// Set DIRA as an output.
    	DIRA |= 1 << direction_pin;
    
    	// Set up the CTRMODE of Counter A for NCO/PWM single-ended.
    	CTRA = (4 << 26) | step_pin;
    
    	// Set the value to be added to PHSA with every clock cycle.
    	FRQA = 1;
    
    	// Set DIRA as an output.
    	DIRA |= 1 << step_pin;
    
    	while(1)
    	{
    		if(get_state(RotateClockwise) == 1)
    		{
    			OUTA &= ~(1 << direction_pin);
    
    			while(get_state(RotateClockwise) == 1)
    			{  
    				// At this location, drive the step pin high
    				// for the required high pulse width
    				PHSA = -high_pulse_width;
    
    				// Wait for a specified period of time before sending another
    				// high pulse to the step pin.
    				waitcnt(overcome_inertia + CNT);
    			}
    		}   
    
    		if(get_state(RotateCounterClockwise) == 1)
    		{
    			OUTA |= 1 << direction_pin;
    
    			while(get_state(RotateCounterClockwise) == 1)
    			{
    				// At this location, drive the step pin high
    				// for the required high pulse width
    				PHSA = -high_pulse_width;
    
    				// Wait for a specified period of time before sending another
    				// high pulse to the step pin.
    				waitcnt(overcome_inertia + CNT);
    			}    
    		}
    	}  
    }
    
  • And perhaps this might also work for you, not sure
    #include "simpletools.h"
    
    int main()
    {
    	// Establish the IO pin going to the pushbutton used to
    	// rotate the stepper motor in a clockwise direction.
    	uint8_t rotate_motor_clockwise = 0;
    
    	// Establish the IO pin going to the pushbutton used to
    	// rotate the stepper motor in a counter-clockwise direction.
    	uint8_t rotate_motor_counter_clockwise = 1;
    
    	// Establish the IO pin being used to communicate
    	// with the direction pin of the stepper driver.
    	uint8_t direction_pin = 14;
    
    	// Establish the IO pin being used to communicate
    	// with the step pin of the stepper driver.
    	uint8_t step_pin = 15;
    
    	// Depending upon inertia of the motor and/or
    	// the load attached to the motor, you should be
    	// able to vary the running speed of the motor
    	// by adjusting this variable.  If the duration
    	// is too short, the motor will either wine, vibrate,
    	// and not move, or it will miss steps during runtime,
    	// and if the duration is too long, it will take
    	// forever to get from point A to point B,  You will
    	// have to experiment with this variable, depending
    	// upon your motor, load, desired speed, etc...	
    	uint32_t overcome_inertia = CLKFREQ / 5714;
    
    	// This variable must meet the required pulse width
    	// of the stepper driver IC being used.  Check the
    	// IC manufacturers specifications for the required pulse width.
    	uint32_t high_pulse_width = CLKFREQ / 500000;  // 160 // 2uS
    
    	// Set up the CTRMODE of Counter A for NCO/PWM single-ended.
    	CTRA = (4 << 26) | step_pin;
    
    	// Set the value to be added to PHSA with every clock cycle.
    	FRQA = 1;
    
    	// Set the Propeller pin going to the stepper driver
    	// step pin as an output.
    	set_direction(step_pin, 1);
    
    	// Set the Propeller pin going to the stepper driver
    	// direction pin as an output.
    	set_direction(direction_pin, 1);
    
    	while(1)
    	{
    		if(get_state(rotate_motor_clockwise) == 1)
    		{
    			set_output(direction_pin, 0);
    
    			while(get_state(rotate_motor_clockwise) == 1)
    			{  
    				// At this location, drive the step pin high
    				// for the required high pulse width
    				PHSA = -high_pulse_width;
    
    				// Wait for a specified period of time before sending another
    				// high pulse to the step pin.
    				waitcnt(overcome_inertia + CNT);
    			}
    		}   
    
    		if(get_state(rotate_motor_counter_clockwise) == 1)
    		{
    			set_output(direction_pin, 1);
    
    			while(get_state(rotate_motor_counter_clockwise) == 1)
    			{
    				// At this location, drive the step pin high
    				// for the required high pulse width
    				PHSA = -high_pulse_width;
    
    				// Wait for a specified period of time before sending another
    				// high pulse to the step pin.
    				waitcnt(overcome_inertia + CNT);
    			}    
    		}
    	}  
    }
    
  • Ariba,
    To change the direction of the motor using the code I uploaded by stopping the program editing it and changing the direction variable to either 1 or 0 depending on the value it was set to previously. I will try this code. Oh and in the application of this motor I will not be editing the program every time I want to get the direction changed. It will be determined by push buttons. Thanks I will see if this works.
  • Okay. So I have now tried all three bits of code you guys provided and none of them actually work with turning in the opposite direction. I'm going to try a different driver seeing as I literally have 50 of them and another stepper and see if this works.
    P.S. I have 50 of them because amazon decided to pack 10 packs of 5 instead of 1 pack of 5 haha.
  • Okay so here's an update on my problem. I decided to step to see if the pin for direction was actually getting signal so I decided to run the same program twice but the second time all I did was remove the connection from my microcontroller to the driver. Now this resulted in, again, the same direction of rotation leading me to believe that some how the program or the board is not properly transmitting the high signal. I am going to try doing the test again but instead of using the microcontroller and running high I will use a separate battery to give the direction pin its high state. Hopefully this works because that would mean that the program is most likely at fault.
  • That was the issue! YES! I finally figured it out. So I guess I have a few options that may work. I could use a digital switch to toggle it that way, figure out where the program is messing up, or manually switch it every time I wish to switch directions. I'd say choice 2 would be the best.
  • I got it working. Thanks guys for your help and patience through out this process. It means a lot to have such a great group of individuals willing to help a newbie lol.
  • plau45

    I would like to now who's driver is the BEST!
  • Well, the one that I got working first was yours actually, however, it had a few modifications and also used parts of the other driver as well. So to preserve good terms with everyone I'll call it a tie lol, but your code was the basis for the fix.
  • idbruceidbruce Posts: 6,197
    edited 2017-07-14 20:07
    plau45

    Glad it all worked out for you eventually.

    Although I have never tried it, for your particular application with pushbuttons, I did like the way Andy used:
    pulse_out(STEPPIN, 5);          //5us pulse
    

    And I would imagine that is the portion of the other driver that you used, however I am unaware of any limitations associated with the use of that function. I notice he set it for 5uS, but if I remember correctly, you don't need 5uS, so you may be able to get a little more speed by reducing that number.

    It is worth noting that when I started working with stepper drivers, way back when, with the assistance from savvy forum members, I got much better results with counters, as compared to drivers using waitcnt in Spin.

    Good luck with your project.
  • Thanks a lot guys.
Sign In or Register to comment.