Merge branch 'feature/e-endstop' into merge-abc-with-homing
[clinton/Smoothieware.git] / src / libs / StepTicker.h
CommitLineData
df27a6a3 1/*
cd011f58
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
cd011f58
AW
6*/
7
8
9
8b260c2c 10#pragma once
3b1e82d2 11
8d54c34c 12#include <stdint.h>
8b260c2c 13#include <array>
1fce036c 14#include <bitset>
a157d099 15#include <functional>
001c4b26 16#include <atomic>
5673fe39 17
8b260c2c 18#include "ActuatorCoordinates.h"
d1d120e1 19#include "TSRingBuffer.h"
8b260c2c 20
5673fe39 21class StepperMotor;
8b260c2c 22class Block;
3b1e82d2 23
f6542ad9
JM
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
3b1e82d2
AW
29class StepTicker{
30 public:
1fce036c 31 StepTicker();
3eadcfee 32 ~StepTicker();
1ad23cd3 33 void set_frequency( float frequency );
8b260c2c 34 void set_unstep_time( float microseconds );
1fce036c 35 int register_motor(StepperMotor* motor);
cb2e6bc6 36 float get_frequency() const { return frequency; }
1598a726 37 void unstep_tick();
23201534 38 const Block *get_current_block() const { return current_block; }
13256955 39
1598a726
JM
40 void step_tick (void);
41 void handle_finish (void);
dc3542cf 42 void start();
cb2e6bc6 43
8b260c2c
JM
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; }
a157d099 48
2fa50ca0 49 private:
8b260c2c
JM
50 static StepTicker *instance;
51
f6542ad9 52 bool start_next_block();
a3bb687b 53
1ad23cd3 54 float frequency;
feb204be 55 uint32_t period;
8b260c2c 56 std::array<StepperMotor*, k_max_actuators> motor;
8b260c2c 57 std::bitset<k_max_actuators> unstep;
d1d120e1 58
f6542ad9 59 Block *current_block;
b9ad75de 60 uint32_t current_tick{0};
3b1e82d2 61
8b260c2c 62 struct {
b5708347 63 volatile bool running:1;
8b260c2c
JM
64 uint8_t num_motors:4;
65 };
66};