Skip to content

Commit 9617f6d

Browse files
Add CoreXY support
1 parent ab29f7f commit 9617f6d

6 files changed

+196
-50
lines changed

gcode/src/gcode-execution.adb

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ package body Gcode.Execution is
161161
return;
162162
end if;
163163

164-
Planner.Planner_Add_Dwell (Ctx, Duration (Ctx.B ('P').Value));
164+
Planner.Planner_Add_Dwell (Ctx, Ctx.B ('P').Value);
165165
end Dwell_Command;
166166

167167
-------------

gcode/src/gcode-planner.adb

+35-27
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ with Settings; use Settings;
2727

2828
package body Gcode.Planner is
2929

30-
use Float_Functions;
3130
use type Float_Position;
3231
use type Step_Position;
3332

@@ -37,23 +36,22 @@ package body Gcode.Planner is
3736
type Motion_Block (Kind : Motion_Kind := Motion_Line) is record
3837
case Kind is
3938
when Motion_Line =>
40-
Target : Float_Position;
39+
Target : Float_Position;
4140

42-
Relative_Steps : Step_Position;
43-
-- Steps for each axis
41+
Abs_Relative_Steps : Step_Position;
42+
-- Absolute number of steps for each axis
4443

45-
Directions : Axis_Directions := (others => Forward);
44+
Directions : Axis_Directions := (others => Forward);
4645
-- Step direction for each axis
4746

48-
Step_Event_Count : Steps := 0;
47+
Step_Event_Count : Steps := 0;
4948
-- Number of steps required to complete this block
5049

51-
Entry_Speed : Step_Speed := 0.0;
52-
Nominal_Speed : Step_Speed := 0.0;
53-
Acceleration : Step_Acceleration := Step_Acceleration'Last;
54-
Remaining_Distance : Float_Value := 0.0;
50+
Entry_Speed : Step_Speed := 0.0;
51+
Nominal_Speed : Step_Speed := 0.0;
52+
Acceleration : Step_Acceleration := Step_Acceleration'Last;
5553
when Motion_Dwell =>
56-
Dwell_Duration : Duration;
54+
Dwell_Duration : Float_Value;
5755
when Motion_Homing =>
5856
null;
5957
when Motion_Enable_Motors =>
@@ -102,39 +100,48 @@ package body Gcode.Planner is
102100
pragma Unreferenced (Ctx);
103101
M_Block : Motion_Block (Kind => Motion_Line);
104102
Target_Steps : constant Step_Position := Milli_To_Step (Target);
103+
Relative_Steps : Step_Position := Target_Steps - Planner_Position;
105104
Delta_MM : Float_Value;
106105

107106
Unit_Vect : Float_Position;
108107
pragma Unreferenced (Unit_Vect);
109108
-- Unit vector of the block used for junction angle computation
110109
begin
111110

112-
M_Block.Relative_Steps := abs (Target_Steps - Planner_Position);
111+
if Settings.Use_CoreXY_Motion then
112+
declare
113+
DX, DY, DA, DB : Steps;
114+
begin
115+
DX := Relative_Steps (X_Axis);
116+
DY := Relative_Steps (Y_Axis);
117+
118+
DA := DX + DY;
119+
DB := DX - DY;
120+
121+
Relative_Steps (X_Axis) := DA;
122+
Relative_Steps (Y_Axis) := DB;
123+
end;
124+
end if;
125+
126+
M_Block.Abs_Relative_Steps := abs Relative_Steps;
113127

114128
for Axis in Axis_Name loop
115129
M_Block.Step_Event_Count :=
116130
Steps'Max (M_Block.Step_Event_Count,
117-
M_Block.Relative_Steps (Axis));
131+
M_Block.Abs_Relative_Steps (Axis));
118132

119133
-- Distance traveled in milimeters
120134
Delta_MM :=
121-
Float_Value (Target_Steps (Axis) - Planner_Position (Axis))
135+
Float_Value (Relative_Steps (Axis))
122136
/ Settings.Step_Per_Millimeter (Axis);
123137

124138
-- Direction of movement for this axis
125139
M_Block.Directions (Axis) :=
126140
(if Delta_MM < 0.0 then Backward else Forward);
127141

128-
-- Add square of each axis to compute distance
129-
M_Block.Remaining_Distance :=
130-
M_Block.Remaining_Distance + Delta_MM**2;
131-
132142
Unit_Vect (Axis) := Delta_MM;
133143
end loop;
134144

135-
-- Compute distance
136-
M_Block.Remaining_Distance := Sqrt (M_Block.Remaining_Distance);
137-
138145
if M_Block.Step_Event_Count = 0 then
139146
-- zero-length block
140147
return;
@@ -156,7 +163,7 @@ package body Gcode.Planner is
156163

157164
procedure Planner_Add_Dwell
158165
(Ctx : in out GContext'Class;
159-
Dwell_Duration : Duration)
166+
Dwell_Duration : Float_Value)
160167
is
161168
pragma Unreferenced (Ctx);
162169
M_Block : Motion_Block (Kind => Motion_Dwell);
@@ -218,7 +225,7 @@ package body Gcode.Planner is
218225
Seg : Segment;
219226
Remaining_Steps : Steps;
220227
Remaining_Seg : Natural;
221-
Seg_Required_To_Stop : Natural;
228+
Seg_Required_To_Stop : Integer;
222229
begin
223230
loop
224231

@@ -232,13 +239,13 @@ package body Gcode.Planner is
232239

233240
-- 100Hz gives us a 10ms precision
234241
Seg.Frequency :=
235-
Duration'Max (100.0, Settings.Idle_Stepper_Frequency);
242+
Frequency_Value'Max (100.0, Settings.Idle_Stepper_Frequency);
236243

237244
Seg.Step_Count :=
238245
Steps (Motion.Dwell_Duration * Seg.Frequency);
239246

240247
-- No axis is moving
241-
Seg.Block_Steps := (others => 0);
248+
Seg.Abs_Block_Steps := (others => 0);
242249
Seg.Block_Event_Count := Steps'Last;
243250

244251
-- Blocking call
@@ -254,14 +261,16 @@ package body Gcode.Planner is
254261
when Motion_Enable_Motors =>
255262
Segment_Block_Buffer.Insert ((Kind => Enable_Motors_Segment,
256263
Enable => Motion.Enable));
264+
265+
257266
when Motion_Line =>
258267
Remaining_Steps := Motion.Step_Event_Count;
259268

260269
-- Signal this segment as first of the new block
261270
Seg.New_Block := True;
262271

263272
-- Segment values that will remain for the entire block
264-
Seg.Block_Steps := Motion.Relative_Steps;
273+
Seg.Abs_Block_Steps := Motion.Abs_Relative_Steps;
265274
Seg.Block_Event_Count := Motion.Step_Event_Count;
266275
Seg.Directions := Motion.Directions;
267276

@@ -307,7 +316,6 @@ package body Gcode.Planner is
307316

308317
-- Next segements are not first of the block
309318
Seg.New_Block := False;
310-
311319
end loop;
312320
end case;
313321
end loop;

gcode/src/gcode-planner.ads

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ package Gcode.Planner is
3131

3232
procedure Planner_Add_Dwell
3333
(Ctx : in out GContext'Class;
34-
Dwell_Duration : Duration);
34+
Dwell_Duration : Float_Value);
3535

3636
procedure Planner_Add_Homing
3737
(Ctx : in out GContext'Class;
@@ -59,15 +59,15 @@ package Gcode.Planner is
5959
Directions : Axis_Directions := (others => Forward);
6060
-- Step direction for each axis
6161

62-
Block_Steps : Step_Position;
63-
-- Steps for the current Motion block each axis
62+
Abs_Block_Steps : Step_Position;
63+
-- Absolute number of steps for the current Motion block each axis
6464

6565
Block_Event_Count : Steps;
6666
-- Step count for the current block
6767
when Homing_Segment =>
6868
null;
6969
when Dwell_Segment =>
70-
Dwell_Duration : Duration;
70+
Dwell_Duration : Float_Value;
7171
when Enable_Motors_Segment =>
7272
Enable : Motor_Enable_Array;
7373
end case;

gcode/src/gcode.adb

+12
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ package body Gcode is
7474
return Ret;
7575
end Milli_To_Step;
7676

77+
-------------------
78+
-- Milli_To_Step --
79+
-------------------
80+
81+
function Milli_To_Step (S : Float_Value;
82+
Axis : Axis_Name)
83+
return Steps
84+
is
85+
begin
86+
return Steps (S * Settings.Step_Per_Millimeter (Axis));
87+
end Milli_To_Step;
88+
7789
-------------------
7890
-- Inch_To_Milli --
7991
-------------------

gcode/src/gcode.ads

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ package Gcode is
2727

2828
subtype Float_Value is Float;
2929
subtype Line_Value is Natural;
30-
subtype Frequency_Value is Duration;
30+
subtype Frequency_Value is Float;
3131

3232
package Float_Functions is new
3333
Ada.Numerics.Generic_Elementary_Functions (Float_Value);
@@ -59,6 +59,9 @@ package Gcode is
5959

6060
function Step_To_Milli (S : Step_Position) return Float_Position;
6161
function Milli_To_Step (S : Float_Position) return Step_Position;
62+
function Milli_To_Step (S : Float_Value;
63+
Axis : Axis_Name)
64+
return Steps;
6265
function Inch_To_Milli (S : Float_Position) return Float_Position;
6366
function Inch_To_Milli (S : Float_Value) return Float_Value;
6467

0 commit comments

Comments
 (0)