Merge branch 'feature/e-endstop' into merge-abc-with-homing
[clinton/Smoothieware.git] / src / libs / StepTicker.h
1 /*
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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8
9
10 #pragma once
11
12 #include <stdint.h>
13 #include <array>
14 #include <bitset>
15 #include <functional>
16 #include <atomic>
17
18 #include "ActuatorCoordinates.h"
19 #include "TSRingBuffer.h"
20
21 class StepperMotor;
22 class Block;
23
24 // handle 2.30 Fixed point
25 #define STEPTICKER_FPSCALE (1<<30)
26 #define STEPTICKER_TOFP(x) ((int32_t)roundf((float)(x)*STEPTICKER_FPSCALE))
27 #define STEPTICKER_FROMFP(x) ((float)(x)/STEPTICKER_FPSCALE)
28
29 class StepTicker{
30 public:
31 StepTicker();
32 ~StepTicker();
33 void set_frequency( float frequency );
34 void set_unstep_time( float microseconds );
35 int register_motor(StepperMotor* motor);
36 float get_frequency() const { return frequency; }
37 void unstep_tick();
38 const Block *get_current_block() const { return current_block; }
39
40 void step_tick (void);
41 void handle_finish (void);
42 void start();
43
44 // whatever setup the block should register this to know when it is done
45 std::function<void()> finished_fnc{nullptr};
46
47 static StepTicker *getInstance() { return instance; }
48
49 private:
50 static StepTicker *instance;
51
52 bool start_next_block();
53
54 float frequency;
55 uint32_t period;
56 std::array<StepperMotor*, k_max_actuators> motor;
57 std::bitset<k_max_actuators> unstep;
58
59 Block *current_block;
60 uint32_t current_tick{0};
61
62 struct {
63 volatile bool running:1;
64 uint8_t num_motors:4;
65 };
66 };