Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Why is better to ramp up a stepper motor, rather than PWM?

Status
Not open for further replies.
Ahh.. sorry my bad.

I wrote an implementation with the use a timer1 library i found on the arduino page https://code.google.com/archive/p/arduino-timerone/downloads.

Could any on you maybe comment on whether it would work or not.

.h file

Code:
#ifndef stepper_motor_h
#define stepper_motor_h

#include "Arduino.h"
#include <TimerOne.h>

#define step_pin  13
#define dir_pin   12
#define en_pin    11
#define max_step  200

 class stepper_motor
{
  public:
    stepper_motor();
    void step_pwm();
    static void callback();
    TimerOne timer1;
  private:
    //uint32_t period;
    bool alive_bool;    
    //int step_count;
    bool position_bool;
};


#endif

.cpp file

Code:
#include "stepper_motor.h"

int step_count = 0;
uint32_t period = 40825UL << 16; // 20412,5 us fixed point arithmetic time = 0

stepper_motor::stepper_motor()
{
  //pinMode(BUILTIN_LED,OUTPUT);
  pinMode(step_pin, OUTPUT);
  pinMode(dir_pin, OUTPUT);
  pinMode(en_pin, OUTPUT);
  alive_bool = true;
  position_bool = false;
  step_count = 0;

}

static void stepper_motor::callback()
{
  if (step_count < 240)
  {
    uint32_t new_period = sqrt(2 * step_count / 4.8);
    period -= new_period; 
    step_count++;
  }
  else
  {
    return;
  }
}


void stepper_motor::step_pwm()
{

  digitalWrite(en_pin, HIGH);

  delay(0.005);

  digitalWrite(dir_pin, HIGH);
  //LOW  - Move towards the sensor
  //HIGH -  Move away from the sensor

  delay(0.005);
  
  timer1.initialize(period);
  timer1.attachInterrupt(callback);
  timer1.pwm(step_pin, 512);

  
}

So how does it work?

I decided for just testing purposes, I would have the ramp to take 10 second, and it should start at 0 hz and end at 48 khz.
The ramp can be descriped as f(t) = at , t = time, a = slope 4.8 khz.

by integrating f(t) I get this function F(t) which tells me how many cycles has been completed given a time.
ex. if F = 1 means that one cycle has completed, and the t = provides me the information of how much time it took for this cycle to complete.

By reordering things t(F) i get i function that tells me period of each new period. The period will be decreasing since they are based on the ramp up function, and by decreasing the period i also increase the frequency.. Does it make sense? would this work with the implementation i've made.
 
Last edited:

A good programmer writes a lot of comments because the morning after the night before he himself will forget what he wanted the piece of code to do...

You name variables as per their roles in the program; I do not understand what is the sensor and what HIGH and LOW has to do with them...

You made a few functions that perform step up or step down and call them from somewhere...

Use hooks liberally...

Use your main program to call and check the functions regularly...

(I am out of touch with programming for a long time)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top