-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
249 lines (198 loc) · 8.25 KB
/
Program.cs
File metadata and controls
249 lines (198 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
namespace BerrySonar
{
using System.Device.Gpio;
using System.Timers;
internal class Program : Firebase
{
// Sonar variables
private static DateTime startTime = DateTime.Now;
private static float speed = 34300; // Speed of sound in cm/s
private static float degree = 0;
private static int step = 0;
private static int step_counter = 0;
private static double distance = 0;
private static bool switch_direction = false;
// Servo motor GPIO pins [29, 31, 33, 37] (coil1, coil2, coil3, coil4)
private readonly static int coil1 = 29;
private readonly static int coil2 = 31;
private readonly static int coil3 = 33;
private readonly static int coil4 = 37;
// Ultrasonic sensor GPIO pins [38, 40] (echo, trigger)
private readonly static int echo = 38;
private readonly static int trigger = 40;
private static GpioController gpioController = new GpioController(PinNumberingScheme.Board);
private static Timer servoTimer = new Timer(25);
private static Timer ultrasonicPulse = new Timer(100);
private static Timer databaseWriter = new Timer(300);
private static bool shutdown;
static void Main()
{
Console.CancelKeyPress += (s, e) => Shutdown();
AppDomain.CurrentDomain.ProcessExit += (s, e) => Shutdown();
_ = Operation();
while(shutdown == false);
}
private static async Task Operation()
{
try
{
Console.WriteLine("Starting Sonar...");
UninitializePins();
Config config = await SonarConfiguration.ReadConfig();
Metadata? metadata = await SonarPositionCache.ReadFile();
InitateDatabase(config);
degree = metadata?.degree ?? 0;
step = metadata?.step ?? 0;
step_counter = metadata?.step_counter ?? 0;
switch_direction = metadata?.switch_direction ?? false;
InitializePins();
servoTimer.Elapsed += ServoMotorControl;
servoTimer.Start();
ultrasonicPulse.Elapsed += SonarOperation;
ultrasonicPulse.Start();
databaseWriter.Elapsed += UpdatePositionData;
databaseWriter.Start();
gpioController.RegisterCallbackForPinValueChangedEvent(echo, PinEventTypes.Falling | PinEventTypes.Rising, ReadUltrasonicSensor);
}
catch { }
}
private static async void UpdatePositionData(object? sender, ElapsedEventArgs e)
{
await UpdateSonarData(new SonarMetadata
{
degree = degree,
distance = distance,
switch_direction = switch_direction
});
}
private static void ServoMotorControl(object? sender, ElapsedEventArgs e)
{
//Console.WriteLine($"Step: {step}, Step Counter: {step_counter}, Switch Direction: {switch_direction}, Degree: {degree}°");
if (switch_direction == false)
{
ExecuteServoStep(step);
step++;
step_counter++;
degree += 0.17578125f;
if (step > 3) step = 0; // correct range check
if (step_counter >= 1024)
{
switch_direction = true;
}
}
else
{
ExecuteServoStep(step);
step--;
step_counter--;
degree -= 0.17578125f;
if (step < 0) step = 3; // reverse range check
if (step_counter <= 0)
{
switch_direction = false;
}
}
}
private static void TriggerUltrasonicSensor()
{
startTime = DateTime.Now;
gpioController.Write(trigger, PinValue.High);
gpioController.Write(trigger, PinValue.Low);
}
private static void ReadUltrasonicSensor(object? sender, PinValueChangedEventArgs e)
{
if (e.ChangeType == PinEventTypes.Falling)
{
double duration = (DateTime.Now - startTime).TotalSeconds;
distance = duration * speed / 2; // Divide by 2 because the signal travels to the object and back
Console.WriteLine($"Distance: {distance} cm");
}
}
private static void SonarOperation(object? sender, ElapsedEventArgs e) => TriggerUltrasonicSensor();
private static void ExecuteServoStep(int step)
{
switch (step)
{
case 0:
gpioController.Write(coil1, PinValue.High);
gpioController.Write(coil2, PinValue.High);
gpioController.Write(coil3, PinValue.Low);
gpioController.Write(coil4, PinValue.Low);
break;
case 1:
gpioController.Write(coil1, PinValue.Low);
gpioController.Write(coil2, PinValue.High);
gpioController.Write(coil3, PinValue.High);
gpioController.Write(coil4, PinValue.Low);
break;
case 2:
gpioController.Write(coil1, PinValue.Low);
gpioController.Write(coil2, PinValue.Low);
gpioController.Write(coil3, PinValue.High);
gpioController.Write(coil4, PinValue.High);
break;
case 3:
gpioController.Write(coil1, PinValue.High);
gpioController.Write(coil2, PinValue.Low);
gpioController.Write(coil3, PinValue.Low);
gpioController.Write(coil4, PinValue.High);
break;
}
}
private static void InitializePins()
{
try
{
// Initialize GPIO pins for servo motor
gpioController.OpenPin(coil1, PinMode.Output);
gpioController.OpenPin(coil2, PinMode.Output);
gpioController.OpenPin(coil3, PinMode.Output);
gpioController.OpenPin(coil4, PinMode.Output);
// Initialize GPIO pins for ultrasonic sensor
gpioController.OpenPin(echo, PinMode.InputPullDown);
gpioController.OpenPin(trigger, PinMode.Output);
}
catch (Exception ex)
{
Console.WriteLine($"Error initializing pins: {ex.Message}");
}
}
private static void UninitializePins()
{
try
{
// Uninitialize GPIO pins for servo motor
gpioController.ClosePin(coil1);
gpioController.ClosePin(coil2);
gpioController.ClosePin(coil3);
gpioController.ClosePin(coil4);
// Uninitialize GPIO pins for ultrasonic sensor
gpioController.ClosePin(echo);
gpioController.ClosePin(trigger);
gpioController.UnregisterCallbackForPinValueChangedEvent(echo, ReadUltrasonicSensor);
}
catch { }
}
private static async void Shutdown()
{
shutdown = true;
UninitializePins();
gpioController?.Dispose();
await SonarPositionCache.CreateFile(new Metadata
{
degree = degree,
step = step,
step_counter = step_counter,
switch_direction = switch_direction
});
await UpdateSonarData(new SonarMetadata
{
degree = degree,
distance = distance
});
servoTimer?.Dispose();
ultrasonicPulse?.Dispose();
databaseWriter?.Dispose();
}
}
}