kidi3
Full Member level 1
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
.cpp file
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.
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: