-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
122 lines (108 loc) · 5.32 KB
/
Copy pathProgram.cs
File metadata and controls
122 lines (108 loc) · 5.32 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
using RaspberryRobot.Navigation;
using RaspberryRobot.Robot;
// ══════════════════════════════════════════════════════════════════════════════
// Robô com LIDAR Xiaomi LDS01RR – Raspberry Pi (.NET 8)
// ══════════════════════════════════════════════════════════════════════════════
const string DefaultLidarPort = "/dev/ttyUSB0";
// Analisar argumentos da linha de comando
string lidarPort = args.Length > 0 ? args[0] : DefaultLidarPort;
bool showMap = args.Contains("--map");
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.CursorVisible = false;
PrintBanner(lidarPort);
// ── Inicialização ─────────────────────────────────────────────────────────
using var robot = new RobotController(lidarPort);
using var cts = new CancellationTokenSource();
// Assinatura para exibição de status
robot.CommandChanged += (_, cmd) =>
{
Console.ForegroundColor = CommandColor(cmd);
Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] Comando: {CommandLabel(cmd),-20} RPM LIDAR: {robot.LidarRpm:F1}");
Console.ResetColor();
};
// Ctrl+C para encerramento limpo
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
Console.WriteLine("\n[Sistema] Ctrl+C detectado – encerrando...");
cts.Cancel();
};
// ── Laço de exibição do mapa ASCII (opcional) ─────────────────────────────
Task? mapTask = null;
if (showMap)
{
mapTask = Task.Run(async () =>
{
while (!cts.Token.IsCancellationRequested)
{
var scan = robot.LastScan;
if (scan is not null)
{
string map = ObstacleAvoidance.BuildAsciiMap(scan);
Console.SetCursorPosition(0, 12); // posição abaixo do log
Console.WriteLine(map);
}
await Task.Delay(200, cts.Token).ConfigureAwait(false);
}
}, cts.Token).ContinueWith(_ => { }, TaskContinuationOptions.OnlyOnCanceled);
}
// ── Execução principal ────────────────────────────────────────────────────
try
{
robot.Start();
await Task.Delay(Timeout.Infinite, cts.Token).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// encerramento normal
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine($"\n[ERRO FATAL] {ex.Message}");
Console.ResetColor();
Environment.Exit(1);
}
finally
{
Console.CursorVisible = true;
Console.WriteLine("[Sistema] Robô desligado com sucesso.");
}
// ══════════════════════════════════════════════════════════════════════════════
// Funções auxiliares locais
// ══════════════════════════════════════════════════════════════════════════════
static void PrintBanner(string port)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("╔══════════════════════════════════════════════╗");
Console.WriteLine("║ Robô Raspberry Pi + LIDAR LDS01RR ║");
Console.WriteLine("║ Sistema de desvio de obstáculos autônomo ║");
Console.WriteLine("╚══════════════════════════════════════════════╝");
Console.ResetColor();
Console.WriteLine($" Porta LIDAR : {port}");
Console.WriteLine($" Data/Hora : {DateTime.Now:dd/MM/yyyy HH:mm:ss}");
Console.WriteLine(" Pressione Ctrl+C para encerrar.");
Console.WriteLine(new string('─', 50));
}
static ConsoleColor CommandColor(NavigationCommand cmd) => cmd switch
{
NavigationCommand.Forward => ConsoleColor.Green,
NavigationCommand.Backward => ConsoleColor.Yellow,
NavigationCommand.TurnLeft => ConsoleColor.Cyan,
NavigationCommand.TurnRight => ConsoleColor.Cyan,
NavigationCommand.CurveLeft => ConsoleColor.DarkCyan,
NavigationCommand.CurveRight => ConsoleColor.DarkCyan,
NavigationCommand.Stop => ConsoleColor.Red,
_ => ConsoleColor.White,
};
static string CommandLabel(NavigationCommand cmd) => cmd switch
{
NavigationCommand.Forward => "↑ Avançar",
NavigationCommand.Backward => "↓ Recuar",
NavigationCommand.TurnLeft => "↺ Girar Esquerda",
NavigationCommand.TurnRight => "↻ Girar Direita",
NavigationCommand.CurveLeft => "↖ Curva Esquerda",
NavigationCommand.CurveRight => "↗ Curva Direita",
NavigationCommand.Stop => "■ Parado",
_ => cmd.ToString(),
};