4 * Copyright 2004, 2008 Develer S.r.l. (http://www.develer.com/)
8 * \brief Compute, save and load ramps for stepper motors.
10 * The acceleration ramp is used to properly accelerate a stepper motor. The main
11 * entry point is the function ramp_evaluate(), which must be called at every step
12 * of the motor: it gets as input the time elapsed since the stepper started
13 * accelerating, and returns the time to wait before sending the next step. A pseudo
14 * usage pattern is as follows:
20 * float delta = ramp_evaluate(&my_ramp, time);
27 * A similar pattern can be used to decelerate (it is sufficient to move the total
28 * time backward, such as "time -= delta").
30 * The ramp can be configured with ramp_setup(), providing it with the minimum and
31 * maximum operating frequency of the motor, and the total acceleration time in
32 * milliseconds (that is, the time that will be needed to accelerate from the
33 * minimum frequency to the maximum frequency).
35 * Both a very precise floating point and a very fast fixed point implementation
36 * of the ramp evaluation are provided. The fixed point is hand-optimized assembly
37 * for DSP56000 (but a portable C version of it can be easily written, see the
38 * comments in the code).
41 * \author Simone Zinanni <s.zinanni@develer.com>
42 * \author Giovanni Bajo <rasky@develer.com>
43 * \author Daniele Basile <asterix@develer.com>
45 * $WIZ$ module_name = "ramp"
46 * $WIZ$ module_configuration = "bertos/cfg/cfg_ramp.h"
52 #include "hw/hw_stepper.h"
54 #include "cfg/cfg_ramp.h"
56 #include <cfg/compiler.h>
60 * Convert microseconds to timer clock ticks
62 #define TIME2CLOCKS(micros) ((uint32_t)(micros) * (STEPPER_CLOCK / 1000000))
65 * Convert timer clock ticks back to microseconds
67 #define CLOCKS2TIME(clocks) ((uint32_t)(clocks) / (STEPPER_CLOCK / 1000000))
70 * Convert microseconds to Hz
72 #define MICROS2FREQ(micros) (1000000UL / ((uint32_t)(micros)))
75 * Convert frequency (in Hz) to time (in microseconds)
77 #define FREQ2MICROS(hz) (1000000UL / ((uint32_t)(hz)))
80 * Multiply \p a and \p b two integer at 32 bit and extract the high 16 bit word.
82 #define FIX_MULT32(a,b) (((uint64_t)(a)*(uint32_t)(b)) >> 16)
85 * Structure holding pre-calculated data for speeding up real-time evaluation
86 * of the ramp. This structure is totally different between the fixed and the
87 * floating point version of the code.
89 * Consult the file-level documentation of ramp.c for more information about
90 * the values of this structure.
94 #if RAMP_USE_FLOATING_POINT
100 uint32_t inv_total_time;
111 uint16_t clocksMinWL;
112 uint16_t clocksMaxWL;
114 struct RampPrecalc precalc; ///< pre-calculated values for speed
119 * Function prototypes
123 uint32_t clocksInRamp,
124 uint16_t clocksInMinWavelength,
125 uint16_t clocksInMaxWavelength);
128 /** Setup an acceleration ramp for a stepper motor
130 * \param ramp Ramp to fill
131 * \param length Length of the ramp (milliseconds)
132 * \param minFreq Minimum operating frequency of the motor (hertz)
133 * \param maxFreq Maximum operating frequency of the motor (hertz)
136 void ramp_setup(struct Ramp* ramp, uint32_t length, uint32_t minFreq, uint32_t maxFreq);
140 * Initialize a new ramp with default values
142 void ramp_default(struct Ramp *ramp);
146 * Evaluate the ramp at the given point. Given a \a ramp, and the current \a clock since
147 * the start of the acceleration, compute the next step, that is the interval at which
148 * send the signal to the motor.
150 * \note The fixed point version does not work when curClock is zero. Anyway,
151 * the first step is always clocksMaxWL, as stored within the ramp structure.
153 #if RAMP_USE_FLOATING_POINT
154 float ramp_evaluate(const struct Ramp* ramp, float curClock);
156 uint16_t ramp_evaluate(const struct Ramp* ramp, uint32_t curClock);
161 int ramp_testSetup(void);
162 int ramp_testRun(void);
163 int ramp_testTearDown(void);
165 #endif /* ALGO_RAMP_H */