3月接到一项开发任务:独自编写一套3D打印机的控制固件。对于FDM打印机,固件中最关键的控制对象是步进电机。我非常愿意向大家分享整个曲折的开发过程。
故事一开始,我要向Marlin项目致敬:以Marlin为代表的开源3D打印固件,孕育了桌面级3D打印的普及和繁荣;
这次开发任务不同于Marlin固件在于,在中断响应中动态规划步进电机的速度。路径规划器退化成一个更新末尾速度和管理队列的模块。这样做的理由来自于产品经理深信,采用32位控制器后,中断能够有效的处理速度的实时计算。我对这个观点报怀疑态度,不过这不影响我做这件事情。
算法实现轻松愉快
首先,利用arduino的串口用作后面的移植,我们可以很快实现逻辑。arduino的1.6.7版本甚至自带了串口绘图器:只要在代码中调用println函数输出速度变量,就能够直接绘制出速度曲线。初版代码如下(待验证):
#define ledPin 13
#define stepPin 8
#define dirPin 9
#define gratingPin 10
/*
* Each of the timers has a counter that is incremented on each tick of the timer's clock.
* CTC timer interrupts are triggered when the counter reaches a specified value stored in the compare match register.
*/
/*
* timer1 is 16 bit, which means it can store a maximum counter value of 65535.
* periods = prescaler*(1+31250)/16M=0.5s. when using, calculate OCR1A instead
*/
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(gratingPin,INPUT);
digitalWrite(dirPin, 1);
Serial.begin(9600);
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
int rpm=120;// 280 rpm should be the fastest
int MircoN=8;
/*
* FOR Microstepping is Full