打印
[STM32]

3D打印运动控制系统源码分享

[复制链接]
1773|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 一路向北lm 于 2018-4-3 11:03 编辑

开源的3D打印系统(marlin)主要是基于8位arduino控制板板卡。无法支持彩色触摸屏。为此本人花了为时3个月的时间,将marlin从arduino移植到STM32。并命名为Dlion。
3D打印运动系统的核心主要是步进电机驱动子系统,是由中断响应函数实现的。如果是恒定速度的步进电机驱动,实现就和这句话一样简单。不过对于3D打印机系统,x,y轴的运动往往速度变化非常频繁:不仅在每次更新位置的速度不同,而且每一段位移的速度也需要经历加速,恒速和减速阶段。这是由机械系统的惯性特征决定的:如果不同动作之间的速度衔接不好,会对电路系统造成强大的电流冲击。特别是3D打印过程,这种速度的变化每次打印任务都数以万计,这就意味着电路寿命将大打折扣。
步进电机驱动子系统系统的速度衔接,基于leibRamp Algorithm,这是一个支撑步进电机速度和控制器计数器频率关系的算法理论,由IBM的工程师于1994年发表并于2004年在控制器内实现。这里算法实现的关键在于路径规划器(planner)。路径规划器的设计意味着,程序在执行步进电机的动作之前,就已经计算好了整个过程的速度曲线。后面就只是Stepper模块准确地执行。在机器层面,这样的设计减少了中断响应函数中的运算量,这对于单片机来说非常友好。同时3D打印机的机械运动相比控制器的16M主频来说要慢很多,路径规划器相比直接驱动,增加了一个运动缓存。这样就能够有效的利用控制器的高频率,里面蕴藏着“空间换取时间”的思想。
在代码层面,planner的本质在于对于一个FIFO的管理。使用C的结构体指针数据结构能够非常优雅的实现这个缓存的创建和管理:
planner.h:
typedef struct {
  // Fields used by the bresenham algorithm for tracing the line
  long steps_x, steps_y, steps_z, steps_e;  // Step count along each axis
  unsigned long step_event_count;           // The number of step events required to complete this block
  long accelerate_until;                    // The index of the step event on which to stop acceleration
  long decelerate_after;                    // The index of the step event on which to start decelerating
  long acceleration_rate;                   // The acceleration rate used for acceleration calculation
  unsigned char direction_bits;             // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
  float nominal_speed;                               // The nominal speed for this block in mm/sec
  float entry_speed;                                 // Entry speed at previous-current junction in mm/sec
  float max_entry_speed;                             // Maximum allowable junction entry speed in mm/sec
  float millimeters;                                 // The total travel of this block in mm
  float acceleration;                                // acceleration mm/sec^2
  unsigned char recalculate_flag;                    // Planner flag to recalculate trapezoids on entry junction
  unsigned char nominal_length_flag;                 // Planner flag for nominal speed always reached

  // Settings for the trapezoid generator
  unsigned long nominal_rate;                        // The nominal step rate for this block in step_events/sec
  unsigned long initial_rate;                        // The jerk-adjusted step rate at start of block  
  unsigned long final_rate;                          // The minimal rate at exit
  unsigned long acceleration_st;                     // acceleration steps/sec^2
  unsigned long fan_speed;
  #ifdef BARICUDA
  unsigned long valve_pressure;
  unsigned long e_to_p_pressure;
  #endif
  volatile char busy;
} block_t;leibRamp Algorithm

block_t block_buffer[BLOCK_BUFFER_SIZE];            // A ring buffer for motion instfructions
volatile unsigned char block_buffer_head;           // Index of the next block to be pushed
volatile unsigned char block_buffer_tail;
volatile 关键字确保了队列头和队列尾被不同函数访问过程中,编译器不会因为优化和丢失更改行为。block_t类型的指针可以方便的方位结构体内任何元素。在后面的planner规划动作plan_buffer_line()中,代码可以用非常优雅的结构体指针来完成。


     每当3D打印机解析到位移指令的时候,plan_buffer_line()函数就被调用。在里面新的block_t首先被创建,并且排入队列的队尾;然后执行calculate_trapezoid_for_block(),计算新的block_t的关键速度节点及其对应的step数目;接着更新队列里面所有block_t的连接速度:之前队尾的block_t的收尾速度和相关速度节点会被更新。最后调用st_wake_up()保证stepper执行的中断打开。


    而在%steppper中,ISR函数负责在主循环之外,执行队列里可能存在的所有block_t。在ISR中,首先由plan_get_current_block()读取队列首的block_t,然后按照结构成员的step数,调用STEP_ADDSTEP_IF_COUNTER两个宏来执行x,y,z三轴的运动。ISR每执行一次,三路各发出一个脉冲,并通过lamp ramp算法更新,根据下一个速度值来更新OCR1A寄存器来设定下一次中断响应的周期。
消化一段代码的最有效方法是对其移植或者重写。换言之,仅仅是走马观花的浏览一遍,除非自己曾经编写过类似程序,很难能透彻的领会固件代码的精髓。所以这里我决定把它分享出来给大家!

Dlion-开源固件源码.rar (171 Bytes)

相关帖子

沙发
xumch| | 2018-12-3 15:22 | 只看该作者
感谢,高手,有没有兴趣帮我做款打印机?有偿服务。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

4

帖子

1

粉丝