-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFormMain.cs
More file actions
256 lines (217 loc) · 6.23 KB
/
Copy pathFormMain.cs
File metadata and controls
256 lines (217 loc) · 6.23 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
250
251
252
253
254
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;
namespace SerialPortApp
{
public partial class FormMain : Form
{
private const int ArraySize = 100;
private const int MinReflectivity = 10;
private const double DegreeRadian = Math.PI * 2 / 360;
private Point Center;
private Point DragShift;
private Point DragInitial;
private Point DragFinal;
private int Count = 0;
private int[] Distances = new int[360];
private SerialPort SerialPort;
private Task TaskLidar;
private bool StopLidar = false;
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
cbListSerialPort.DataSource = SerialPort.GetPortNames();
UpdateCenter();
}
private void BtnConectar_Click(object sender, EventArgs e)
{
try
{
SerialPort = new SerialPort((String)cbListSerialPort.SelectedItem);
SerialPort.BaudRate = 115200;
SerialPort.ReadTimeout = 5000;
SerialPort.Open();
cbListSerialPort.Enabled = false;
btnConectar.Enabled = false;
btnDesconectar.Enabled = true;
}
catch (Exception except)
{
MessageBox.Show(except.Message);
}
TaskLidar = Task.Run(() => StartLidarLoop());
}
private void StartLidarLoop()
{
SerialPort.Write("$");
SerialPort.Write("startlds$");
StopLidar = false;
byte[] buffer = new byte[ArraySize];
int[] values = new int[ArraySize];
while (!StopLidar)
{
int b = SerialPort.ReadByte();
if (b == 0xFA && Count > 21)
{
if (Count == 22)
{
ProcessLidarData(values);
}
Count = 0;
values = new int[ArraySize];
values[0] = b;
}
else
{
if (Count < ArraySize)
{
values[Count] = b;
}
}
Count++;
}
SerialPort.Write("stoplds$");
SerialPort.Close();
}
private void ProcessLidarData(int[] values)
{
int angle = (values[1] - 0xA0) * 4;
int speed = GetInt(values[2], values[3]);
int[] distance = new int[4];
int[] reflectivity = new int[4];
for (int i = 0; i < 4; i++)
{
distance[i] = GetInt(values[i * 4 + 4], values[i * 4 + 5]);
reflectivity[i] = GetInt(values[i * 4 + 6], values[i * 4 + 7]);
}
int checksum2 = values.Take(20).Sum();
int checksum1 = GetInt(values[20], values[21]);
if (checksum1 == checksum2 && angle < 360)
{
if (angle == 0)
{
pbOutput.Invalidate();
}
for (int i = 0; i < 4; i++)
{
if (reflectivity[i] > MinReflectivity)
{
Distances[angle + i] = distance[i];
}
else
{
Distances[angle + i] = -1;
}
}
}
else
{
Console.WriteLine("Invalid data: " + checksum1 + ", " + checksum2 + ", " + angle);
}
}
private int GetInt(int lb, int hb)
{
return lb | (hb << 8);
}
private void BtnDesconectar_Click(object sender, EventArgs e)
{
Desconectar();
}
private void Desconectar()
{
try
{
StopLidar = true;
if (TaskLidar != null)
TaskLidar.Wait();
cbListSerialPort.Enabled = true;
btnConectar.Enabled = true;
btnDesconectar.Enabled = false;
}
catch (Exception except)
{
MessageBox.Show(except.Message);
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
if (SerialPort == null)
{
var listPortsNew = SerialPort.GetPortNames();
if (listPortsNew.Length != cbListSerialPort.Items.Count)
{
cbListSerialPort.DataSource = listPortsNew;
}
}
}
private void pbOutput_Paint(object sender, PaintEventArgs e)
{
for (int angle = 0; angle < 360; angle++)
{
if (Distances[angle] > 0)
{
var angleShift = (angle + trackBarRotate.Value) % 360;
int x = (int)(Math.Sin(angleShift * DegreeRadian) * Distances[angle] * (double)trackBarZoom.Value / 150d + Center.X);
int y = (int)(Math.Cos(angleShift * DegreeRadian) * Distances[angle] * (double)trackBarZoom.Value / 150d + Center.Y);
e.Graphics.DrawLine(Pens.Gray, Center.X, Center.Y, x, y);
e.Graphics.DrawArc(Pens.Lime, x, y, 3, 3, 0, 360);
}
}
e.Graphics.DrawLine(Pens.Red, Center.X, 0, Center.X, pbOutput.Height);
e.Graphics.DrawLine(Pens.Red, 0, Center.Y, pbOutput.Width, Center.Y);
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
Desconectar();
}
private void pbOutput_Resize(object sender, EventArgs e)
{
UpdateCenter();
}
private void UpdateCenter()
{
Center = new Point(pbOutput.Width / 2 + DragShift.X + (DragFinal.X - DragInitial.X), pbOutput.Height / 2 + DragShift.Y + (DragFinal.Y - DragInitial.Y));
}
private void pbOutput_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DragInitial = new Point(e.X, e.Y);
DragFinal = new Point(e.X, e.Y);
UpdateCenter();
pbOutput.Invalidate();
}
}
private void pbOutput_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DragFinal = new Point(e.X, e.Y);
UpdateCenter();
pbOutput.Invalidate();
}
}
private void pbOutput_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DragShift = new Point(DragShift.X + (DragFinal.X - DragInitial.X), DragShift.Y + (DragFinal.Y - DragInitial.Y));
DragInitial = Point.Empty;
DragFinal = Point.Empty;
UpdateCenter();
pbOutput.Invalidate();
}
}
}
}