Skip to content

Commit a23da08

Browse files
committed
Simplest motor position control algoriphm.
1 parent 466f929 commit a23da08

File tree

5 files changed

+111
-35
lines changed

5 files changed

+111
-35
lines changed

source/control.adb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--
2+
-- Copyright (C) 2025, Vadim Godunko <vgodunko@gmail.com>
3+
--
4+
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
--
6+
7+
with Motor_Drivers;
8+
with Sensors;
9+
10+
package body Control is
11+
12+
Desired : A0B.Types.Unsigned_16 := 2_048;
13+
14+
----------------
15+
-- Initialize --
16+
----------------
17+
18+
procedure Initialize is
19+
begin
20+
null;
21+
end Initialize;
22+
23+
---------------
24+
-- Iteration --
25+
---------------
26+
27+
procedure Iteration is
28+
use type A0B.Types.Unsigned_16;
29+
30+
Current : constant A0B.Types.Unsigned_16 := Sensors.Get_Position;
31+
32+
begin
33+
if Current < Desired - 10 then
34+
Motor_Drivers.Set_Forward (1);
35+
36+
elsif Current > Desired + 10 then
37+
Motor_Drivers.Set_Backward (1);
38+
39+
else
40+
Motor_Drivers.Set_Break (1);
41+
end if;
42+
end Iteration;
43+
44+
---------
45+
-- Set --
46+
---------
47+
48+
procedure Set (To : A0B.Types.Unsigned_16) is
49+
begin
50+
Desired := To;
51+
end Set;
52+
53+
end Control;

source/control.ads

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--
2+
-- Copyright (C) 2025, Vadim Godunko <vgodunko@gmail.com>
3+
--
4+
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
--
6+
7+
with A0B.Types;
8+
9+
package Control is
10+
11+
procedure Iteration;
12+
13+
procedure Initialize;
14+
15+
procedure Set (To : A0B.Types.Unsigned_16);
16+
17+
end Control;

source/driver.adb

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55
--
66

7+
pragma Ada_2022;
8+
9+
with A0B.Types;
10+
711
with Configuration;
812
with Console;
9-
with Motor_Drivers;
13+
with Control;
1014
with Sensors;
1115

1216
procedure Driver is
13-
Motor : Motor_Drivers.Motor_Index := 1;
17+
use type A0B.Types.Unsigned_16;
18+
1419
Command : Character;
20+
Desired : A0B.Types.Unsigned_16 := 2_048;
1521

1622
begin
1723
Configuration.Initialize;
1824
Sensors.Initialize;
25+
Control.Initialize;
1926
Configuration.Enable_Timers;
2027

2128
Console.New_Line;
@@ -27,43 +34,21 @@ begin
2734
Console.Get (Command);
2835

2936
case Command is
30-
when '1' =>
31-
Console.Put_Line ("Motor 1 selected");
32-
Motor := 1;
33-
34-
when '2' =>
35-
Console.Put_Line ("Motor 2 selected");
36-
Motor := 2;
37-
38-
when '3' =>
39-
Console.Put_Line ("Motor 3 selected");
40-
Motor := 3;
41-
42-
when '4' =>
43-
Console.Put_Line ("Motor 4 selected");
44-
Motor := 4;
45-
46-
when 'f' | 'F' =>
47-
Console.Put_Line ("forward");
48-
Motor_Drivers.Set_Forward (Motor);
49-
50-
when 'r' | 'R' =>
51-
Console.Put_Line ("reverse");
52-
Motor_Drivers.Set_Backward (Motor);
53-
54-
when 'b' | 'B' =>
55-
Console.Put_Line ("break");
56-
Motor_Drivers.Set_Break (Motor);
57-
58-
when 'o' | 'O' =>
59-
Console.Put_Line ("off");
60-
Motor_Drivers.Set_Off (Motor);
61-
6237
when 'a' | 'A' =>
6338
Console.Put_Line ("collect sensors data");
6439
Sensors.Collect_Data;
6540
Sensors.Dump;
6641

42+
when '-' | '_' =>
43+
Desired := @ - 16;
44+
Control.Set (Desired);
45+
Console.Put_Line (A0B.Types.Unsigned_16'Image (Desired));
46+
47+
when '=' | '+' =>
48+
Desired := @ + 16;
49+
Control.Set (Desired);
50+
Console.Put_Line (A0B.Types.Unsigned_16'Image (Desired));
51+
6752
when others =>
6853
Console.Put_Line ("unknown command");
6954
end case;

source/sensors.adb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
pragma Ada_2022;
88

99
with A0B.Callbacks.Generic_Parameterless;
10-
with A0B.Types;
1110

1211
with Configuration;
1312
with Console;
13+
with Control;
1414

1515
package body Sensors is
1616

@@ -35,6 +35,8 @@ package body Sensors is
3535
Last : Natural := 0;
3636
-- Buffer to accumulate data.
3737

38+
Current : Sensors_Data;
39+
3840
procedure On_Conversion_Done;
3941

4042
package On_Conversion_Done_Callbacks is
@@ -107,6 +109,15 @@ package body Sensors is
107109
Console.New_Line;
108110
end Dump;
109111

112+
------------------
113+
-- Get_Position --
114+
------------------
115+
116+
function Get_Position return A0B.Types.Unsigned_16 is
117+
begin
118+
return Current.M1_Position;
119+
end Get_Position;
120+
110121
----------------
111122
-- Initialize --
112123
----------------
@@ -138,6 +149,8 @@ package body Sensors is
138149
Buffer (Buffer'First .. Buffer'Length / 2);
139150
Last := @ + Buffer'Length / 2;
140151
end if;
152+
153+
Current := Buffer (Buffer'Length / 2);
141154
end if;
142155

143156
if Configuration.ADC1_DMA_Stream.Get_Masked_And_Clear_Transfer_Completed
@@ -147,7 +160,11 @@ package body Sensors is
147160
Buffer (Buffer'First + Buffer'Length / 2 .. Buffer'Last);
148161
Last := @ + Buffer'Length / 2;
149162
end if;
163+
164+
Current := Buffer (Buffer'Last);
150165
end if;
166+
167+
Control.Iteration;
151168
end On_Conversion_Done;
152169

153170
end Sensors;

source/sensors.ads

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55
--
66

7+
with A0B.Types;
8+
79
package Sensors is
810

911
procedure Collect_Data;
@@ -12,4 +14,6 @@ package Sensors is
1214

1315
procedure Initialize;
1416

17+
function Get_Position return A0B.Types.Unsigned_16;
18+
1519
end Sensors;

0 commit comments

Comments
 (0)