grblHAL core  20241107
spindle_sync.h
Go to the documentation of this file.
1 /*
2  spindle_sync.h - An embedded CNC Controller with rs274/ngc (g-code) support
3 
4  Spindle sync data structures
5 
6  NOTE: not referenced in the core grbl code
7 
8  Part of grblHAL
9 
10  Copyright (c) 2020-2021 Terje Io
11 
12  Grbl is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  Grbl is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
24 */
25 
26 #ifndef _SPINDLE_SYNC_H_
27 #define _SPINDLE_SYNC_H_
28 
29 #include "pid.h"
30 
31 // Free running timer log data.
32 // The free running timer is used to timestamp pulse events from the encoder.
33 typedef struct {
34  volatile uint32_t last_index; // Timer value at last encoder index pulse
35  volatile uint32_t last_pulse; // Timer value at last encoder pulse
36  volatile uint32_t pulse_length; // Last timer tics between spindle encoder pulse interrupts.
38 
39 // Pulse counter timer log data.
40 // This counter is used to "prescale" the encoder pulses in order to
41 // reduce pulse interrupt frequency. This allows the use of high PPR encoders
42 // without overloading the MCU while still making use of the better resolution.
43 // NOTE: if a 16bit counter is used then it is important than proper casting
44 // is performed in order to handle counter overflow correctly.
45 typedef struct {
46  volatile uint32_t last_count; // Counter value at last encoder pulse interrupt
47  volatile uint32_t last_index; // Counter value at last encoder index interrupt
48  volatile uint32_t index_count;
49  volatile uint32_t pulse_count;
51 
52 typedef struct {
53  uint32_t ppr; // Encoder pulses per revolution
54  float rpm_factor; // Inverse of event timer tics per RPM
55  float pulse_distance; // Encoder pulse distance in fraction of one revolution
56  uint32_t maximum_tt; // Maximum timer tics since last spindle encoder pulse before RPM = 0 is returned
57  spindle_encoder_timer_t timer; // Event timestamps
58  spindle_encoder_counter_t counter; // Encoder event counts
59  uint32_t error_count; // Incremented when actual PPR count differs from ppr setting
60  uint32_t tics_per_irq; // Counts per interrupt generated (prescaler value)
61  volatile bool spin_lock;
63 
64 typedef struct {
65  float prev_pos; // Target position of previous segment
66  float steps_per_mm; // Steps per mm for current block
67  float programmed_rate; // Programmed feed in mm/rev for current block
68  int32_t min_cycles_per_tick; // Minimum cycles per tick for PID loop
69  uint_fast8_t segment_id; // Used for detecting start of new segment
70  pidf_t pid; // PID data for position
71  stepper_pulse_start_ptr stepper_pulse_start_normal; // Driver pulse function to restore after spindle sync move is completed
72 #ifdef PID_LOG
73  int32_t log[PID_LOG];
74  int32_t pos[PID_LOG];
75 #endif
77 
78 #endif
void(* stepper_pulse_start_ptr)(stepper_t *stepper)
Pointer to function for setting up steppers for the next step pulse.
Definition: hal.h:287
Definition: pid.h:42
Definition: spindle_sync.h:45
volatile uint32_t index_count
Definition: spindle_sync.h:48
volatile uint32_t pulse_count
Definition: spindle_sync.h:49
volatile uint32_t last_count
Definition: spindle_sync.h:46
volatile uint32_t last_index
Definition: spindle_sync.h:47
Definition: spindle_sync.h:52
uint32_t tics_per_irq
Definition: spindle_sync.h:60
float rpm_factor
Definition: spindle_sync.h:54
uint32_t maximum_tt
Definition: spindle_sync.h:56
uint32_t ppr
Definition: spindle_sync.h:53
spindle_encoder_timer_t timer
Definition: spindle_sync.h:57
float pulse_distance
Definition: spindle_sync.h:55
volatile bool spin_lock
Definition: spindle_sync.h:61
spindle_encoder_counter_t counter
Definition: spindle_sync.h:58
uint32_t error_count
Definition: spindle_sync.h:59
Definition: spindle_sync.h:33
volatile uint32_t last_pulse
Definition: spindle_sync.h:35
volatile uint32_t pulse_length
Definition: spindle_sync.h:36
volatile uint32_t last_index
Definition: spindle_sync.h:34
Definition: spindle_sync.h:64
float prev_pos
Definition: spindle_sync.h:65
int32_t min_cycles_per_tick
Definition: spindle_sync.h:68
uint_fast8_t segment_id
Definition: spindle_sync.h:69
stepper_pulse_start_ptr stepper_pulse_start_normal
Definition: spindle_sync.h:71
pidf_t pid
Definition: spindle_sync.h:70
float programmed_rate
Definition: spindle_sync.h:67
float steps_per_mm
Definition: spindle_sync.h:66