yarf 0.1
Yet Another RepRap Firmware
Defines | Functions | Variables
src/movement/planner.c File Reference

The planner component translates movement g-code commands into blocks that represent a linear movement and places these blocks into a queue of blocks to be executed by the block handler component. More...

#include "planner.h"
#include "yarf.h"
#include "block.h"
#include "planner_queue.h"
#include "planner_lookahead.h"
#include "block_handler.h"
#include "advance.h"
#include "scheduling/realtime_timer.h"
#include "util/math.h"
#include "util/fixed_point.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <util/atomic.h>

Go to the source code of this file.

Defines

#define HOME_AXIS_CALIBRATION_MM_X   5
 The number of millimeters the machine will back up on its X axis, after reaching its X min endstop during the first stage of homing.
#define HOME_AXIS_CALIBRATION_MM_Y   5
 The number of millimeters the machine will back up on its Y axis, after reaching its Y min endstop during the first stage of homing.
#define HOME_AXIS_CALIBRATION_MM_Z   1
 The number of millimeters the machine will back up on its Z axis, after reaching its Z min endstop during the first stage of homing.
#define HOME_AXIS_CALIBRATION_SPEED_X   300
 The speed in mm/min at which the machine will move its X axis, during the second stage of homing.
#define HOME_AXIS_CALIBRATION_SPEED_Y   300
 The speed in mm/min at which the machine will move its Y axis, during the second stage of homing.
#define HOME_AXIS_CALIBRATION_SPEED_Z   50
 The speed in mm/min at which the machine will move its Z axis, during the second stage of homing.

Functions

static int32_t intersection_steps (speed_t entry_speed, speed_t exit_speed, float distance_mm, long nb_steps)
 Given that the machine starts a movement at a certain entry speed and that it needs to end this movement at a certain exit speed after a certain distance in millimeter and number of steps, this function returns the number of steps after which the machine must end accelerating and start decelerating.
static fxp16u16_t ticks_float_to_fxp16u16_safe (float ticks_fl)
 Returns the fixed point 16.16 representation of a (positive) floating point number, if the number is less than or equal to the half of the largest representable value in the fixed point representation.
static void initialize_block (block_t *b, steps_t steps_x, steps_t steps_y, steps_t steps_z, steps_t steps_e, speed_t plateau_speed, void(*collision_handler)(uint8_t))
 Initializes a number of basic parameters for a block, so it can be executed by the block handler.
static void linear_movement_rel (steps_t steps_x, steps_t steps_y, steps_t steps_z, steps_t steps_e, speed_t plateau_speed, void(*collision_handler)(uint8_t))
 Creates and initializes a block to perform a linear movement and places this block into the planning queue.
static void collision (uint8_t collisions)
 Function which is used as the collision handler for all normal movements.
static void homing_collision (uint8_t collisions)
 Function which is used as the collision handler for all homing movements.
void plan_init (void)
 Initializes the planner component.
steps_t plan_get_virtual_position (unsigned char axis)
 Returns the virtual position in number of steps from the origin for the specified axis.
void plan_set_virtual_position_rel (steps_t x, steps_t y, steps_t z, steps_t e)
 Sets the virtual position of all axes.
void plan_set_virtual_position_abs (steps_t x, steps_t y, steps_t z, steps_t e)
 Sets the virtual position of all axes.
void plan_calculate_trapezoid (block_t *b, speed_t entry_speed, speed_t exit_speed)
 Sets a number of block parameters in order to have the block accelerate from a given entry speed at the start of the block and to have it decelerate to a given exit speed at the end of it.
void plan_linear_movement_rel (steps_t steps_x, steps_t steps_y, steps_t steps_z, steps_t steps_e, speed_t plateau_speed)
 Adds a movement from the current virtual position to the specified position at the specified feed rate to the end of the planning queue.
void plan_linear_movement_abs (steps_t dest_x, steps_t dest_y, steps_t dest_z, steps_t dest_e, speed_t plateau_speed)
 Adds a movement from the current virtual position to the specified position at the specified feed rate to the end of the planning queue.
void plan_home_axes (bool x, bool y, bool z)
 Homes the specified axes.

Variables

static uint8_t axis_homed
 Flags indicating which axes have been homed since boot.
static steps_t virtual_position [NUM_AXES]
 The virtual position in steps of each axis.
static unsigned int next_id
 The identifier number to use for the next block.

Detailed Description

The planner component translates movement g-code commands into blocks that represent a linear movement and places these blocks into a queue of blocks to be executed by the block handler component.

Author:
Pieter Agten (pieter.agten@gmail.com)
Date:
23 sep 2011

Definition in file planner.c.


Function Documentation

static void collision ( uint8_t  collisions) [static]

Function which is used as the collision handler for all normal movements.

That is, movements that are not part of homing calibration.

Parameters:
collisionsa bit array of flags indicating which endstop was hit

Definition at line 353 of file planner.c.

static void homing_collision ( uint8_t  collisions) [static]

Function which is used as the collision handler for all homing movements.

Parameters:
collisionsa bit array of flags indicating which endstop was hit

Definition at line 386 of file planner.c.

static void initialize_block ( block_t b,
steps_t  steps_x,
steps_t  steps_y,
steps_t  steps_z,
steps_t  steps_e,
speed_t  plateau_speed,
void(*)(uint8_t)  collision_handler 
) [inline, static]

Initializes a number of basic parameters for a block, so it can be executed by the block handler.

Parameters:
ba pointer to the block to initialize
steps_xthe number of steps the block needs to take in the x direction (can be negative)
steps_ythe number of steps the block needs to take in the y direction (can be negative)
steps_zthe number of steps the block needs to take in the z direction (can be negative)
steps_ethe number of steps the block needs to take in the e direction (can be negative)
plateau_speedmaximum speed of the block in mm/min
collision_handlera pointer to a function to be called when one of the endstop is hit during execution of the block

After calling this method, the block is ready to be placed into the planning queue, to be executed by the block handler. The entry and exit speed of the block are set very conservatively to the minimum of the given plateau speed and the configured START_SPEED. These speeds can be increased by running the lookahead component after placing the block into the planning queue.

Definition at line 223 of file planner.c.

static int32_t intersection_steps ( speed_t  entry_speed,
speed_t  exit_speed,
float  distance_mm,
long  nb_steps 
) [inline, static]

Given that the machine starts a movement at a certain entry speed and that it needs to end this movement at a certain exit speed after a certain distance in millimeter and number of steps, this function returns the number of steps after which the machine must end accelerating and start decelerating.

This function can be used to compute the intersection point between the acceleration slope and the deceleration slop for the case where the block trapezoid has no plateau:

             speed ^
                   |       +
                   |      /:\
                   |     / : \
                   |    /  :  + <-- exit speed
     entry speed --|-> +   :  |
                   |   |   :  |
                   |   +---|--+
                   |       +------> intersection time
                   |____________________________
                                         time -->

 This function returns the nb of steps travelled at the intersection time. The
 easiest way to calculate this, is by using the classic formulas for uniform
 acceleration:
  V(t) = Vi + a*t
  s(t) = (Vi + Vf)*t/2
 Where Vi is the entry speed in mm/min, 
       Vf is the exit speed in mm/min,
        s is the displacement in mm and
        t is the time passed

 From the second equation, we get:
  t(s) = 2*s/(Vi+Vf)
 So
  V(s) = Vi + 2*a*s/(Vi+Vf)

 We can use this formula directly for the acceleration curve:
  Va(s) = Vi + 2*a/(Vi+Vf) * s

 And by shifting and mirroring it, we can also use it for the deceleration
 curve:
  Vd(s) = Vf + 2*a(Vi+Vf) * (l - s)
 Where l is the length in mm of the entire movement

 Now we can just solve for the intersection of Va and Vd, which gives us (after
 performing some substitutions and simplifications):
  s = (Vf² - Vi²)/(4*a) + l/2

 All that's left is to convert s (which is in mm) to the number of linear steps.
 This can simply be done by multiplying by the average number of steps per mm
 of the movement:
  result = s * b/l
         = b * [(Vf² - Vi²)/(4*a*l) + 1/2]
         = b * [(Vf + Vi)(Vf - Vi)/(4*a*l) + 1/2]
 With b the total number of steps of the entire movement.

Definition at line 165 of file planner.c.

static void linear_movement_rel ( steps_t  steps_x,
steps_t  steps_y,
steps_t  steps_z,
steps_t  steps_e,
speed_t  plateau_speed,
void(*)(uint8_t)  collision_handler 
) [static]

Creates and initializes a block to perform a linear movement and places this block into the planning queue.

The linear movement is specified by giving the number of steps to move in each direction. This function blocks if the planning queue is full.

Parameters:
steps_xthe number of steps to take in the x direction (can be negative)
steps_ythe number of steps to take in the y direction (can be negative)
steps_zthe number of steps to take in the z direction (can be negative)
steps_ethe number of steps to take in the e direction (can be negative)
plateau_speedmaximum speed of the block in mm/min
collision_handlera pointer to a function to be called when one of the endstop is hit during execution of the block

If the lookahead component is enabled, it is called immediately after adding the new block to the queue, in order to optimize the block's entry speed and to reduce the jerkiness of the transitions between blocks. The machine's virtual position is updated to the position it will be in after this movement has been executed.

Definition at line 316 of file planner.c.

void plan_calculate_trapezoid ( block_t b,
speed_t  entry_speed,
speed_t  exit_speed 
)

Sets a number of block parameters in order to have the block accelerate from a given entry speed at the start of the block and to have it decelerate to a given exit speed at the end of it.

These parameters are used by the block handler during execution of the block.

Parameters:
ba pointer to the block for which to calculate the parameters
entry_speedthe entry speed of the block in mm/min
exit_speedthe exit speed of the block in mm/min

Definition at line 431 of file planner.c.

steps_t plan_get_virtual_position ( unsigned char  axis)

Returns the virtual position in number of steps from the origin for the specified axis.

The virtual position is the position the printer will be at when all pending blocks in the planning queue have been executed.

Parameters:
axisthe axis for which to get the virtual position. Valid values are X_AXIS, Y_AXIS, Z_AXIS and E_AXIS.
Returns:
The virtual position in number of steps for the specified axis.

Definition at line 406 of file planner.c.

void plan_home_axes ( bool  x,
bool  y,
bool  z 
)

Homes the specified axes.

Homing an axis means moving it in the negative direction until it reaches its min endstop.

Parameters:
xboolean indicating whether or not to home the x axis
yboolean indicating whether or not to home the y axis
zboolean indicating whether or not to home the z axis

Definition at line 505 of file planner.c.

void plan_linear_movement_abs ( steps_t  dest_x,
steps_t  dest_y,
steps_t  dest_z,
steps_t  dest_e,
speed_t  plateau_speed 
)

Adds a movement from the current virtual position to the specified position at the specified feed rate to the end of the planning queue.

The position arguments are interpreted as absolute positions in numbers of steps from the origin.

Parameters:
dest_xthe destination position of the x axis, in steps from the origin, must be within the bounds of the axis
dest_ythe destination position of the y axis, in steps from the origin, must be within the bounds of the axis
dest_zthe destination position of the z axis, in steps from the origin, must be within the bounds of the axis
dest_ethe destination position of the e axis, in steps from the origin
plateau_speedmaximum speed of the block in mm/min

If any of the X, Y or Z axes is not homed when calling this movement, that axis is homed before making the movement.

If any of the destination parameters is out of bounds, the movement is not added to the planning queue (it is silently dropped).

If the lookahead component is enabled, it is called immediately after adding the new block to the queue, in order to optimize the block's entry speed and to reduce the jerkiness of the transitions between blocks. The machine's virtual position is updated to the position it will be in after this movement has been executed.

Definition at line 491 of file planner.c.

void plan_linear_movement_rel ( steps_t  steps_x,
steps_t  steps_y,
steps_t  steps_z,
steps_t  steps_e,
speed_t  plateau_speed 
)

Adds a movement from the current virtual position to the specified position at the specified feed rate to the end of the planning queue.

The position arguments are interpreted as offsets in numbers of steps from the current virtual position.

Parameters:
steps_xthe number of steps to take in the x direction (can be negative), must not make the machine go out of bounds
steps_ythe number of steps to take in the y direction (can be negative), must not make the machine go out of bounds
steps_zthe number of steps to take in the z direction (can be negative), must not make the machine go out of bounds
steps_ethe number of steps to take in the e direction (can be negative)
plateau_speedmaximum speed of the block in mm/min

If any of the X, Y or Z axes is not homed when calling this movement, that axis is homed before making the movement.

If any of the destination parameters would cause the machine to go out of bounds, the movement is not added to the planning queue (it is silently dropped).

If the lookahead component is enabled, it is called immediately after adding the new block to the queue, in order to optimize the block's entry speed and to reduce the jerkiness of the transitions between blocks. The machine's virtual position is updated to the position it will be in after this movement has been executed.

Definition at line 468 of file planner.c.

void plan_set_virtual_position_abs ( steps_t  x,
steps_t  y,
steps_t  z,
steps_t  e 
)

Sets the virtual position of all axes.

The arguments are interpreted as absolute positions in numbers of steps from the origin.

Parameters:
xthe new virtual x position in number of steps
ythe new virtual x position in number of steps
zthe new virtual x position in number of steps
ethe new virtual x position in number of steps

Definition at line 421 of file planner.c.

void plan_set_virtual_position_rel ( steps_t  x,
steps_t  y,
steps_t  z,
steps_t  e 
)

Sets the virtual position of all axes.

The arguments are interpreted as offsets in numbers of steps relative to the current virtual position.

Parameters:
xthe number of steps to add to the virtual x position
ythe number of steps to add to the virtual y position
zthe number of steps to add to the virtual z position
ethe number of steps to add to the virtual e position

Definition at line 412 of file planner.c.

static fxp16u16_t ticks_float_to_fxp16u16_safe ( float  ticks_fl) [inline, static]

Returns the fixed point 16.16 representation of a (positive) floating point number, if the number is less than or equal to the half of the largest representable value in the fixed point representation.

If it is larger than half of the maximum value, the half of the maximum value is returned.

Parameters:
ticks_flthe floating point number to convert
Returns:
The fixed point 16.16 value that is closest to the value of the given floating point number, but is less than or equal to the half of the largest representable fixed point number.

This method is used to convert floating point timer ticks into a fixed point 16.16 representation, which can safely be multiplied by 2, without causing an overflow. This is because the block handler needs to multiply the current timer tick value by two in order to calculate the new timer tick value.

Definition at line 188 of file planner.c.


Variable Documentation

unsigned int next_id [static]

The identifier number to use for the next block.

The identifier numbers are only used for debugging purposes.

Definition at line 100 of file planner.c.

steps_t virtual_position[NUM_AXES] [static]

The virtual position in steps of each axis.

The printer will actually be at this position when all pending blocks in the planning queue have been executed.

Definition at line 93 of file planner.c.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines