get direct step running from encoder
[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
3b1e82d2
AW
10#ifndef STEPTICKER_H
11#define STEPTICKER_H
12
8d54c34c 13#include <stdint.h>
1fce036c
JM
14#include <vector>
15#include <bitset>
a157d099 16#include <functional>
001c4b26 17#include <atomic>
5673fe39
MM
18
19class StepperMotor;
3b1e82d2
AW
20
21class StepTicker{
22 public:
2fa50ca0
JM
23 static StepTicker* global_step_ticker;
24
1fce036c 25 StepTicker();
3eadcfee 26 ~StepTicker();
1ad23cd3 27 void set_frequency( float frequency );
acf766a4 28 void signal_a_move_finished();
1ad23cd3 29 void set_reset_delay( float seconds );
1fce036c 30 int register_motor(StepperMotor* motor);
670fa10b
L
31 void add_motor_to_active_list(StepperMotor* motor);
32 void remove_motor_from_active_list(StepperMotor* motor);
a157d099 33 void set_acceleration_ticks_per_second(uint32_t acceleration_ticks_per_second);
cb2e6bc6 34 float get_frequency() const { return frequency; }
aed1f6ca 35 void unstep_tick();
c6796a29 36 uint32_t get_tick_cnt() const { return tick_cnt; }
d6023ce6 37 uint32_t ticks_since(uint32_t last) const { return (tick_cnt>=last) ? tick_cnt-last : (UINT32_MAX-last) + tick_cnt + 1; }
c6796a29 38
2fa50ca0 39 void TIMER0_IRQHandler (void);
16220afe 40 void PendSV_IRQHandler (void);
a157d099
JM
41 void register_acceleration_tick_handler(std::function<void(void)> cb){
42 acceleration_tick_handlers.push_back(cb);
43 }
dc3542cf 44 void acceleration_tick();
398e9edc 45 void synchronize_acceleration(bool fire_now);
f9a0f86d 46 void schedule_unstep(int motor);
13256955 47
dc3542cf 48 void start();
cb2e6bc6
JM
49
50 friend class StepperMotor;
a157d099 51
2fa50ca0 52 private:
1ad23cd3 53 float frequency;
feb204be 54 uint32_t period;
c6796a29 55 volatile uint32_t tick_cnt;
a157d099 56 std::vector<std::function<void(void)>> acceleration_tick_handlers;
1fce036c
JM
57 std::vector<StepperMotor*> motor;
58 std::bitset<32> active_motor; // limit to 32 motors
aed1f6ca 59 std::bitset<32> unstep; // limit to 32 motors
001c4b26 60 std::atomic_uchar do_move_finished;
37102904 61
cb2e6bc6 62 uint8_t num_motors;
001c4b26 63 volatile bool a_move_finished;
3b1e82d2
AW
64};
65
66
67
3b1e82d2 68#endif