Inital rewrite of stepticker and associated code to implement accleration per tick.
[clinton/Smoothieware.git] / src / libs / StepperMotor.cpp
CommitLineData
7b49793d 1/*
feb204be
AW
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
7b49793d 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
feb204be 6*/
670fa10b 7#include "StepperMotor.h"
5673fe39
MM
8
9#include "Kernel.h"
8f91e4e6 10#include "MRI_Hooks.h"
61134a65
JM
11#include "StepTicker.h"
12
13#include <math.h>
feb204be 14
3494f3d0 15// in steps/sec the default minimum speed (was 20steps/sec hardcoded)
9502f9d5 16float StepperMotor::default_minimum_actuator_rate= 20.0F;
3494f3d0 17
d337942a 18// A StepperMotor represents an actual stepper motor. It is used to generate steps that move the actual motor at a given speed
93694d6b 19
728477c4
JM
20StepperMotor::StepperMotor()
21{
22 init();
23}
df6a30f2 24
728477c4
JM
25StepperMotor::StepperMotor(Pin &step, Pin &dir, Pin &en) : step_pin(step), dir_pin(dir), en_pin(en)
26{
27 init();
28 enable(false);
29 set_high_on_debug(en.port_number, en.pin);
670fa10b
L
30}
31
3494f3d0
JM
32StepperMotor::~StepperMotor()
33{
3494f3d0
JM
34}
35
728477c4
JM
36void StepperMotor::init()
37{
96f12364 38 // register this motor with the step ticker, and get its index in that array and bit position
1fce036c 39 this->index= THEKERNEL->step_ticker->register_motor(this);
feb204be 40 this->moving = false;
4464301d 41 this->stepped = 0;
bd0f7508 42 this->is_move_finished = false;
778093ce 43 this->force_finish= false;
8f91e4e6 44
df6a30f2
MM
45 steps_per_mm = 1.0F;
46 max_rate = 50.0F;
47
78d0e16a
MM
48 last_milestone_steps = 0;
49 last_milestone_mm = 0.0F;
3494f3d0 50 current_position_steps= 0;
feb204be
AW
51}
52
8b260c2c
JM
53// // Instruct the StepperMotor to move a certain number of steps
54// StepperMotor* StepperMotor::move( bool direction, unsigned int steps, float initial_speed)
55// {
56// set_direction(direction);
57// this->direction = direction;
58// this->force_finish= false;
59
60// // Zero our tool counters
61// this->stepped = 0;
62// return this;
63// }
feb204be 64
78d0e16a
MM
65void StepperMotor::change_steps_per_mm(float new_steps)
66{
67 steps_per_mm = new_steps;
586cc733 68 last_milestone_steps = lroundf(last_milestone_mm * steps_per_mm);
58c32991 69 current_position_steps = last_milestone_steps;
78d0e16a
MM
70}
71
72void StepperMotor::change_last_milestone(float new_milestone)
73{
74 last_milestone_mm = new_milestone;
586cc733 75 last_milestone_steps = lroundf(last_milestone_mm * steps_per_mm);
58c32991 76 current_position_steps = last_milestone_steps;
78d0e16a
MM
77}
78
79int StepperMotor::steps_to_target(float target)
80{
586cc733 81 int target_steps = lroundf(target * steps_per_mm);
78d0e16a
MM
82 return target_steps - last_milestone_steps;
83}