Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
1f97891439 |
@ -2,9 +2,6 @@
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using static DroneSimulator.Drone;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
|
||||
|
||||
namespace DroneSimulator
|
||||
{
|
||||
@ -34,13 +31,7 @@ namespace DroneSimulator
|
||||
private Thread DroneThread;
|
||||
private int Timer;
|
||||
|
||||
private Random MainRandom = new Random();
|
||||
|
||||
RealMode.Accelerometer RealAcc = new RealMode.Accelerometer();
|
||||
RealMode.Gyroscope RealGyr = new RealMode.Gyroscope();
|
||||
RealMode.Position RealPos = new RealMode.Position();
|
||||
RealMode.Barometer RealBar = new RealMode.Barometer();
|
||||
RealMode.Range RealRange = new RealMode.Range();
|
||||
private static int CounterID = 0;
|
||||
|
||||
public static byte[] getBytes(object data)
|
||||
{
|
||||
@ -249,12 +240,6 @@ namespace DroneSimulator
|
||||
else LaserRange = float.MaxValue;
|
||||
}
|
||||
|
||||
RealAcc.Update(Acc, (uint)tick);
|
||||
RealGyr.Update(Gyr, (uint)tick);
|
||||
RealRange.Update(LaserRange, (uint)tick);
|
||||
RealBar.Update(PosXYZ.Z * 11, (uint)tick);
|
||||
RealPos.Update(PosXYZ, (uint)tick);
|
||||
|
||||
DataTimer = (uint)tick;
|
||||
}
|
||||
|
||||
@ -293,8 +278,8 @@ namespace DroneSimulator
|
||||
acc.Head.Type = DroneData.DataType.DataAcc;
|
||||
acc.Head.Time = (uint)Environment.TickCount;
|
||||
|
||||
acc.Acc.X = RealAcc.result.X; acc.Acc.Y = RealAcc.result.Y; acc.Acc.Z = RealAcc.result.Z;
|
||||
acc.Time = RealAcc.timer;
|
||||
acc.Acc.X = Acc.X; acc.Acc.Y = Acc.Y; acc.Acc.Z = Acc.Z;
|
||||
acc.Time = DataTimer;
|
||||
|
||||
return getBytes(acc);
|
||||
}
|
||||
@ -308,8 +293,8 @@ namespace DroneSimulator
|
||||
gyr.Head.Type = DroneData.DataType.DataGyr;
|
||||
gyr.Head.Time = (uint)Environment.TickCount;
|
||||
|
||||
gyr.Gyr.X = RealGyr.result.X; gyr.Gyr.Y = RealGyr.result.Y; gyr.Gyr.Z = RealGyr.result.Z;
|
||||
gyr.Time = RealGyr.timer;
|
||||
gyr.Gyr.X = Gyr.X; gyr.Gyr.Y = Gyr.Y; gyr.Gyr.Z = Gyr.Z;
|
||||
gyr.Time = DataTimer;
|
||||
|
||||
return getBytes(gyr);
|
||||
}
|
||||
@ -338,8 +323,8 @@ namespace DroneSimulator
|
||||
range.Head.Type = DroneData.DataType.DataRange;
|
||||
range.Head.Time = (uint)Environment.TickCount;
|
||||
|
||||
range.LiDAR = RealRange.result;
|
||||
range.Time = RealRange.timer;
|
||||
range.LiDAR = LaserRange;
|
||||
range.Time = DataTimer;
|
||||
|
||||
return getBytes(range);
|
||||
}
|
||||
@ -353,27 +338,12 @@ namespace DroneSimulator
|
||||
local.Head.Type = DroneData.DataType.DataLocal;
|
||||
local.Head.Time = (uint)Environment.TickCount;
|
||||
|
||||
local.Local.X = RealPos.result.X; local.Local.Y = RealPos.result.Y; local.Local.Z = RealPos.result.Z;
|
||||
local.Time = RealPos.timer;
|
||||
local.Local.X = PosXYZ.X; local.Local.Y = PosXYZ.Y; local.Local.Z = PosXYZ.Z;
|
||||
local.Time = DataTimer;
|
||||
|
||||
return getBytes(local);
|
||||
}
|
||||
|
||||
private byte[] SendDataBarometer()
|
||||
{
|
||||
DroneData.DataBar bar = new DroneData.DataBar();
|
||||
|
||||
bar.Head.Size = Marshal.SizeOf(typeof(DroneData.DataBar));
|
||||
bar.Head.Mode = DroneData.DataMode.Response;
|
||||
bar.Head.Type = DroneData.DataType.DataBar;
|
||||
bar.Head.Time = (uint)Environment.TickCount;
|
||||
|
||||
bar.Pressure = RealBar.result;
|
||||
bar.Time = RealBar.timer;
|
||||
|
||||
return getBytes(bar);
|
||||
}
|
||||
|
||||
private byte[]? ServerRequestResponse(DroneData.DataHead head, byte[] body)
|
||||
{
|
||||
byte[] zero = new byte[0];
|
||||
@ -396,17 +366,86 @@ namespace DroneSimulator
|
||||
}
|
||||
}
|
||||
|
||||
case DroneData.DataType.DataGyr: if (head.Mode == DroneData.DataMode.Request) return SendDataGyr(); else return zero;
|
||||
case DroneData.DataType.DataGyr:
|
||||
{
|
||||
if (head.Mode == DroneData.DataMode.Request)
|
||||
{
|
||||
// Запрос данных
|
||||
return SendDataGyr();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Пришли данные
|
||||
// ... //
|
||||
//
|
||||
return zero;
|
||||
}
|
||||
}
|
||||
|
||||
case DroneData.DataType.DataMag: if (head.Mode == DroneData.DataMode.Request) return SendDataMag(); else return zero;
|
||||
case DroneData.DataType.DataMag:
|
||||
{
|
||||
if (head.Mode == DroneData.DataMode.Request)
|
||||
{
|
||||
// Запрос данных
|
||||
return SendDataMag();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Пришли данные
|
||||
// ... //
|
||||
//
|
||||
return zero;
|
||||
}
|
||||
}
|
||||
|
||||
case DroneData.DataType.DataRange: if (head.Mode == DroneData.DataMode.Request) return SendDataRange(); else return zero;
|
||||
case DroneData.DataType.DataRange:
|
||||
{
|
||||
if (head.Mode == DroneData.DataMode.Request)
|
||||
{
|
||||
// Запрос данных
|
||||
return SendDataRange();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Пришли данные
|
||||
// ... //
|
||||
//
|
||||
return zero;
|
||||
}
|
||||
}
|
||||
|
||||
case DroneData.DataType.DataLocal: if (head.Mode == DroneData.DataMode.Request) return SendDataLocal(); else return zero;
|
||||
case DroneData.DataType.DataLocal:
|
||||
{
|
||||
if (head.Mode == DroneData.DataMode.Request)
|
||||
{
|
||||
// Запрос данных
|
||||
return SendDataLocal();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Пришли данные
|
||||
// ... //
|
||||
//
|
||||
return zero;
|
||||
}
|
||||
}
|
||||
|
||||
case DroneData.DataType.DataBar: if (head.Mode == DroneData.DataMode.Request) return SendDataBarometer(); else return zero;
|
||||
|
||||
case DroneData.DataType.DataMotor4: if (head.Mode == DroneData.DataMode.Response) RecvDataMotor4(body); return zero;
|
||||
case DroneData.DataType.DataMotor4:
|
||||
{
|
||||
if (head.Mode == DroneData.DataMode.Request)
|
||||
{
|
||||
// Запрос данных
|
||||
// ... //
|
||||
//
|
||||
return zero;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Пришли данные
|
||||
RecvDataMotor4(body);
|
||||
return zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return zero;
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DroneData
|
||||
{
|
||||
@ -12,7 +12,7 @@ namespace DroneData
|
||||
None = 0, Head = 1,
|
||||
|
||||
// Output
|
||||
DataAcc = 1001, DataGyr = 1002, DataMag = 1003, DataRange = 1004, DataLocal = 1005, DataBar = 1006,
|
||||
DataAcc = 1001, DataGyr = 1002, DataMag = 1003, DataRange = 1004, DataLocal = 1005,
|
||||
|
||||
// Input
|
||||
DataMotor4 = 2001, DataMotor6 = 2002
|
||||
@ -25,7 +25,7 @@ namespace DroneData
|
||||
public DataMode Mode;
|
||||
public DataType Type;
|
||||
|
||||
public uint Time; // Общее время
|
||||
public uint Time; // Îáùåå âðåìÿ
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataHead));
|
||||
}
|
||||
@ -37,7 +37,7 @@ namespace DroneData
|
||||
public DataHead Head;
|
||||
public XYZ Acc;
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
public uint Time; // Ïîñëåäíåå âðåìÿ èçìåíåíèÿ äàííûõ
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataAcc));
|
||||
}
|
||||
@ -47,7 +47,7 @@ namespace DroneData
|
||||
public DataHead Head;
|
||||
public XYZ Gyr;
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
public uint Time; // Ïîñëåäíåå âðåìÿ èçìåíåíèÿ äàííûõ
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataGyr));
|
||||
}
|
||||
@ -57,39 +57,29 @@ namespace DroneData
|
||||
public DataHead Head;
|
||||
public XYZ Mag;
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
public uint Time; // Ïîñëåäíåå âðåìÿ èçìåíåíèÿ äàííûõ
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataMag));
|
||||
}
|
||||
|
||||
public struct DataRange
|
||||
{
|
||||
public DataHead Head;
|
||||
public float LiDAR; // Датчик посадки
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataRange));
|
||||
}
|
||||
|
||||
public struct DataLocal
|
||||
{
|
||||
public DataHead Head;
|
||||
public XYZ Local; // Локальные координаты
|
||||
public XYZ Local; // Ëîêàëüíûå êîîðäèíàòû
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
public uint Time; // Ïîñëåäíåå âðåìÿ èçìåíåíèÿ äàííûõ
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataLocal));
|
||||
}
|
||||
|
||||
public struct DataBar
|
||||
public struct DataRange
|
||||
{
|
||||
public DataHead Head;
|
||||
public float Pressure; // Давление
|
||||
public float LiDAR; // Äàò÷èê ïîñàäêè
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
public uint Time; // Ïîñëåäíåå âðåìÿ èçìåíåíèÿ äàííûõ
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataBar));
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataRange));
|
||||
}
|
||||
|
||||
public struct DataMotor4
|
||||
@ -107,4 +97,4 @@ namespace DroneData
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataMotor6));
|
||||
}
|
||||
}
|
||||
}
|
2641
DroneSimulator/FormMain.Designer.cs
generated
2641
DroneSimulator/FormMain.Designer.cs
generated
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,4 @@
|
||||
using System.Text;
|
||||
using System.Text;
|
||||
using System.Numerics;
|
||||
using System.Windows.Forms;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
@ -8,268 +8,210 @@ using System.Runtime.InteropServices;
|
||||
using System.CodeDom;
|
||||
using System.Linq;
|
||||
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace DroneSimulator
|
||||
{
|
||||
public partial class Form_Main : Form
|
||||
{
|
||||
Screen2D screen2D = null;
|
||||
|
||||
NetServerClients netServerClient = new NetServerClients();
|
||||
NetServerVisual netServerVisual = new NetServerVisual();
|
||||
|
||||
List<Drone> AllDrones = new List<Drone>();
|
||||
|
||||
public Form_Main()
|
||||
public partial class Form_Main : Form
|
||||
{
|
||||
InitializeComponent();
|
||||
Screen2D screen2D = null;
|
||||
|
||||
RealMode.RealSimulation = checkBox_Mode_Real.Checked;
|
||||
numericUpDown_Acc_Update(null, null);
|
||||
numericUpDown_Gyr_Update(null, null);
|
||||
numericUpDown_Pos_Update(null, null);
|
||||
numericUpDown_Bar_Update(null, null);
|
||||
numericUpDown_Range_Update(null, null);
|
||||
}
|
||||
NetServerClients netServerClient = new NetServerClients();
|
||||
NetServerVisual netServerVisual = new NetServerVisual();
|
||||
|
||||
private void ClientConnectionCallback(object o)
|
||||
{
|
||||
NetServerClients.ConnectData data = (NetServerClients.ConnectData)o;
|
||||
|
||||
Invoke((MethodInvoker)delegate
|
||||
{
|
||||
label_Clients_Num.Text = data.Count.ToString();
|
||||
});
|
||||
|
||||
if (data.Connect)
|
||||
{
|
||||
Drone drone = new Drone(data.ID);
|
||||
drone.Create(1.0f, 0.5f, 1.0f);
|
||||
|
||||
screen2D.CreateDrone(Color.Red, data.ID);
|
||||
|
||||
AllDrones.Add(drone);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (Drone drone in AllDrones)
|
||||
List<Drone> AllDrones = new List<Drone>();
|
||||
private ClientWebSocket _webSocket;
|
||||
private CancellationTokenSource _cts;
|
||||
private bool _isRunning;
|
||||
public Form_Main()
|
||||
{
|
||||
if (drone.ID != data.ID) continue;
|
||||
drone.Close();
|
||||
|
||||
screen2D.RemoveDrone(data.ID);
|
||||
|
||||
AllDrones.Remove(drone);
|
||||
break;
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ClientReceiveCallback(object o)
|
||||
{
|
||||
NetServerClients.ReceiveData data = (NetServerClients.ReceiveData)o;
|
||||
|
||||
Drone? drone = null;
|
||||
|
||||
foreach (Drone d in AllDrones)
|
||||
{
|
||||
if (d.ID != data.ID) continue;
|
||||
drone = d;
|
||||
break;
|
||||
}
|
||||
|
||||
if (drone == null) return;
|
||||
|
||||
List<byte[]?>? send = drone.DataStream(data.Buffer, data.Size);
|
||||
|
||||
if (send == null) return;
|
||||
try
|
||||
{
|
||||
foreach (byte[]? b in send)
|
||||
private void ClientConnectionCallback(object o)
|
||||
{
|
||||
if (b != null) data.Client?.Send(b);
|
||||
NetServerClients.ConnectData data = (NetServerClients.ConnectData)o;
|
||||
|
||||
Invoke((MethodInvoker)delegate
|
||||
{
|
||||
label_Clients_Num.Text = data.Count.ToString();
|
||||
});
|
||||
|
||||
if (data.Connect)
|
||||
{
|
||||
Drone drone = new Drone(data.ID);
|
||||
drone.Create(1.0f, 0.5f, 1.0f);
|
||||
|
||||
screen2D.CreateDrone(Color.Red, data.ID);
|
||||
|
||||
AllDrones.Add(drone);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (Drone drone in AllDrones)
|
||||
{
|
||||
if (drone.ID != data.ID) continue;
|
||||
drone.Close();
|
||||
|
||||
screen2D.RemoveDrone(data.ID);
|
||||
|
||||
AllDrones.Remove(drone);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
private void ClientReceiveCallback(object o)
|
||||
{
|
||||
NetServerClients.ReceiveData data = (NetServerClients.ReceiveData)o;
|
||||
|
||||
Drone? drone = null;
|
||||
|
||||
foreach (Drone d in AllDrones)
|
||||
{
|
||||
if (d.ID != data.ID) continue;
|
||||
drone = d;
|
||||
break;
|
||||
}
|
||||
|
||||
if (drone == null) return;
|
||||
|
||||
List<byte[]?>? send = drone.DataStream(data.Buffer, data.Size);
|
||||
|
||||
if (send == null) return;
|
||||
try
|
||||
{
|
||||
foreach (byte[]? b in send)
|
||||
{
|
||||
if (b != null) data.Client?.Send(b);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
private void button_Client_Start_Click(object sender, EventArgs e)
|
||||
{
|
||||
var done = netServerClient.StartServer((int)numericUpDown_Clients_Port.Value, (int)numericUpDown_Clients_Limit.Value, ClientConnectionCallback, ClientReceiveCallback);
|
||||
switch (done)
|
||||
{
|
||||
case NetServerClients.ServerState.Error:
|
||||
{
|
||||
MessageBox.Show("Error to start clients server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
break;
|
||||
}
|
||||
case NetServerClients.ServerState.Start:
|
||||
{
|
||||
button_Client_Start.Text = "Stop";
|
||||
button_Client_Start.BackColor = Color.LimeGreen;
|
||||
break;
|
||||
}
|
||||
case NetServerClients.ServerState.Stop:
|
||||
{
|
||||
label_Clients_Num.Text = "0";
|
||||
button_Client_Start.Text = "Start";
|
||||
button_Client_Start.BackColor = Color.Transparent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (done != NetServerClients.ServerState.Start) return;
|
||||
|
||||
pictureBox_2D.Image = null;
|
||||
|
||||
screen2D = new Screen2D(DrawCallback);
|
||||
}
|
||||
|
||||
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
private void DrawCallback(Bitmap bmp)
|
||||
{
|
||||
Invoke((MethodInvoker)delegate
|
||||
{
|
||||
if (pictureBox_2D.Image == null) pictureBox_2D.Image = bmp;
|
||||
pictureBox_2D.Refresh();
|
||||
});
|
||||
}
|
||||
|
||||
private void timer_Test_Tick(object sender, EventArgs e)
|
||||
{
|
||||
if (screen2D == null) return;
|
||||
|
||||
foreach (Drone d in AllDrones)
|
||||
{
|
||||
screen2D.Move(d.ID, d.PosXYZ, d.GetOrientation());
|
||||
}
|
||||
|
||||
screen2D.DrawScene();
|
||||
}
|
||||
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
foreach (Drone d in AllDrones) d.Close();
|
||||
}
|
||||
|
||||
private void VisualConnectionCallback(object o)
|
||||
{
|
||||
NetServerVisual.ConnectData data = (NetServerVisual.ConnectData)o;
|
||||
|
||||
Invoke((MethodInvoker)delegate
|
||||
{
|
||||
label_Clients_Num.Text = data.Count.ToString();
|
||||
});
|
||||
|
||||
if (data.Connect)
|
||||
{
|
||||
//---
|
||||
}
|
||||
else
|
||||
{
|
||||
//---
|
||||
}
|
||||
}
|
||||
|
||||
private void VisualReceiveCallback(object o)
|
||||
{
|
||||
NetServerVisual.ReceiveData data = (NetServerVisual.ReceiveData)o;
|
||||
|
||||
foreach (Drone d in AllDrones)
|
||||
{
|
||||
Drone.DataVisual v = d.GetVisual();
|
||||
|
||||
try { data.Client.Send(Drone.getBytes(v)); }
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
private void button_Visual_Start_Click(object sender, EventArgs e)
|
||||
{
|
||||
var done = netServerVisual.StartServer((int)numericUpDown_Visual_Port.Value, (int)numericUpDown_Visual_Limit.Value, VisualConnectionCallback, VisualReceiveCallback);
|
||||
switch (done)
|
||||
{
|
||||
case NetServerVisual.ServerState.Error:
|
||||
{
|
||||
MessageBox.Show("Error to start visual server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
break;
|
||||
}
|
||||
case NetServerVisual.ServerState.Start:
|
||||
{
|
||||
button_Visual_Start.Text = "Stop";
|
||||
button_Visual_Start.BackColor = Color.LimeGreen;
|
||||
break;
|
||||
}
|
||||
case NetServerVisual.ServerState.Stop:
|
||||
{
|
||||
label_Visual_Num.Text = "0";
|
||||
button_Visual_Start.Text = "Start";
|
||||
button_Visual_Start.BackColor = Color.Transparent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void button_Client_Start_Click(object sender, EventArgs e)
|
||||
{
|
||||
var done = netServerClient.StartServer((int)numericUpDown_Clients_Port.Value, (int)numericUpDown_Clients_Limit.Value, ClientConnectionCallback, ClientReceiveCallback);
|
||||
switch (done)
|
||||
{
|
||||
case NetServerClients.ServerState.Error:
|
||||
{
|
||||
MessageBox.Show("Error to start clients server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
break;
|
||||
}
|
||||
case NetServerClients.ServerState.Start:
|
||||
{
|
||||
button_Client_Start.Text = "Stop";
|
||||
button_Client_Start.BackColor = Color.LimeGreen;
|
||||
break;
|
||||
}
|
||||
case NetServerClients.ServerState.Stop:
|
||||
{
|
||||
label_Clients_Num.Text = "0";
|
||||
button_Client_Start.Text = "Start";
|
||||
button_Client_Start.BackColor = Color.Transparent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (done != NetServerClients.ServerState.Start) return;
|
||||
|
||||
pictureBox_2D.Image = null;
|
||||
|
||||
screen2D = new Screen2D(DrawCallback);
|
||||
}
|
||||
|
||||
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
private void DrawCallback(Bitmap bmp)
|
||||
{
|
||||
Invoke((MethodInvoker)delegate
|
||||
{
|
||||
if (pictureBox_2D.Image == null) pictureBox_2D.Image = bmp;
|
||||
pictureBox_2D.Refresh();
|
||||
});
|
||||
}
|
||||
|
||||
private void timer_Test_Tick(object sender, EventArgs e)
|
||||
{
|
||||
if (screen2D == null) return;
|
||||
|
||||
foreach (Drone d in AllDrones)
|
||||
{
|
||||
screen2D.Move(d.ID, d.PosXYZ, d.GetOrientation());
|
||||
}
|
||||
|
||||
screen2D.DrawScene();
|
||||
}
|
||||
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
|
||||
{
|
||||
foreach (Drone d in AllDrones) d.Close();
|
||||
}
|
||||
|
||||
private void VisualConnectionCallback(object o)
|
||||
{
|
||||
NetServerVisual.ConnectData data = (NetServerVisual.ConnectData)o;
|
||||
|
||||
Invoke((MethodInvoker)delegate
|
||||
{
|
||||
label_Clients_Num.Text = data.Count.ToString();
|
||||
});
|
||||
|
||||
if (data.Connect)
|
||||
{
|
||||
//---
|
||||
}
|
||||
else
|
||||
{
|
||||
//---
|
||||
}
|
||||
}
|
||||
|
||||
private void VisualReceiveCallback(object o)
|
||||
{
|
||||
NetServerVisual.ReceiveData data = (NetServerVisual.ReceiveData)o;
|
||||
|
||||
foreach (Drone d in AllDrones)
|
||||
{
|
||||
Drone.DataVisual v = d.GetVisual();
|
||||
|
||||
try { data.Client.Send(Drone.getBytes(v)); }
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
private void button_Visual_Start_Click(object sender, EventArgs e)
|
||||
{
|
||||
var done = netServerVisual.StartServer((int)numericUpDown_Visual_Port.Value, (int)numericUpDown_Visual_Limit.Value, VisualConnectionCallback, VisualReceiveCallback);
|
||||
switch (done)
|
||||
{
|
||||
case NetServerVisual.ServerState.Error:
|
||||
{
|
||||
MessageBox.Show("Error to start visual server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
break;
|
||||
}
|
||||
case NetServerVisual.ServerState.Start:
|
||||
{
|
||||
button_Visual_Start.Text = "Stop";
|
||||
button_Visual_Start.BackColor = Color.LimeGreen;
|
||||
break;
|
||||
}
|
||||
case NetServerVisual.ServerState.Stop:
|
||||
{
|
||||
label_Visual_Num.Text = "0";
|
||||
button_Visual_Start.Text = "Start";
|
||||
button_Visual_Start.BackColor = Color.Transparent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void numericUpDown_Bar_Update(object sender, EventArgs e)
|
||||
{
|
||||
try { RealMode.Barometer.Pressure = uint.Parse(textBox_Bar_Pressure.Text); }
|
||||
catch
|
||||
{
|
||||
RealMode.Barometer.Pressure = 102258;
|
||||
MessageBox.Show("Pressure invalid format", "Barometer error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
|
||||
|
||||
RealMode.Barometer.Freq = (uint)numericUpDown_Bar_Freq.Value;
|
||||
RealMode.Barometer.Noise = (float)numericUpDown_Bar_Noise.Value;
|
||||
RealMode.Barometer.Lateness = (float)numericUpDown_Bar_Laten.Value;
|
||||
RealMode.Barometer.Enable = checkBox_Bar_Enable.Checked;
|
||||
}
|
||||
|
||||
private void checkBox_Mode_Real_CheckedChanged(object sender, EventArgs e)
|
||||
{
|
||||
RealMode.RealSimulation = checkBox_Mode_Real.Checked;
|
||||
}
|
||||
|
||||
private void numericUpDown_Acc_Update(object sender, EventArgs e)
|
||||
{
|
||||
RealMode.Accelerometer.Freq = (uint)numericUpDown_Acc_Freq.Value;
|
||||
RealMode.Accelerometer.Noise = (float)numericUpDown_Acc_Noise.Value;
|
||||
RealMode.Accelerometer.Lateness = (float)numericUpDown_Acc_Laten.Value;
|
||||
|
||||
RealMode.Accelerometer.ScaleLeft = (float)numericUpDown_Acc_Scale_Left.Value;
|
||||
RealMode.Accelerometer.ScaleRight = (float)numericUpDown_Acc_Scale_Rigth.Value;
|
||||
}
|
||||
|
||||
private void numericUpDown_Gyr_Update(object sender, EventArgs e)
|
||||
{
|
||||
RealMode.Gyroscope.Freq = (uint)numericUpDown_Gyr_Freq.Value;
|
||||
RealMode.Gyroscope.Noise = (float)numericUpDown_Gyr_Noise.Value;
|
||||
RealMode.Gyroscope.Lateness = (float)numericUpDown_Gyr_Laten.Value;
|
||||
|
||||
RealMode.Gyroscope.Shift.X = (float)numericUpDown_Gyr_Shift_X.Value;
|
||||
RealMode.Gyroscope.Shift.Y = (float)numericUpDown_Gyr_Shift_Y.Value;
|
||||
RealMode.Gyroscope.Shift.Z = (float)numericUpDown_Gyr_Shift_Z.Value;
|
||||
}
|
||||
|
||||
private void numericUpDown_Pos_Update(object sender, EventArgs e)
|
||||
{
|
||||
RealMode.Position.Freq = (uint)numericUpDown_Pos_Freq.Value;
|
||||
RealMode.Position.Noise = (float)numericUpDown_Pos_Noise.Value;
|
||||
RealMode.Position.Lateness = (float)numericUpDown_Pos_Laten.Value;
|
||||
RealMode.Position.Enable = checkBox_Pos_Enable.Checked;
|
||||
}
|
||||
|
||||
private void numericUpDown_Range_Update(object sender, EventArgs e)
|
||||
{
|
||||
RealMode.Range.Freq = (uint)numericUpDown_Range_Freq.Value;
|
||||
RealMode.Range.Noise = (float)numericUpDown_Range_Noise.Value;
|
||||
RealMode.Range.Lateness = (float)numericUpDown_Range_Laten.Value;
|
||||
RealMode.Range.Enable = checkBox_Range_Enable.Checked;
|
||||
|
||||
RealMode.Range.MaxHeight = (float)numericUpDown_Range_Max.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,333 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DroneSimulator
|
||||
{
|
||||
internal class RealMode
|
||||
{
|
||||
public static bool RealSimulation;
|
||||
|
||||
internal class Accelerometer
|
||||
{
|
||||
public static uint Freq;
|
||||
public static float Noise;
|
||||
public static float ScaleLeft;
|
||||
public static float ScaleRight;
|
||||
public static float Lateness;
|
||||
|
||||
private uint last = 0;
|
||||
|
||||
private Random rand = new Random();
|
||||
|
||||
private const int count = 1000;
|
||||
private Vector3[] laten = new Vector3[count];
|
||||
private uint index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public Vector3 result;
|
||||
|
||||
public void Update(Vector3 value, uint time)
|
||||
{
|
||||
if (!RealSimulation)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
float scale = (ScaleRight - ScaleLeft) / 2;
|
||||
float shift = scale + ScaleLeft;
|
||||
|
||||
value.X = (value.X * scale) + shift;
|
||||
value.Y = (value.Y * scale) + shift;
|
||||
value.Z = (value.Z * scale) + shift;
|
||||
|
||||
int noise = (int)(Noise * 1000);
|
||||
value.X += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
value.Y += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
value.Z += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
|
||||
uint clock = (uint)(Lateness * 1000);
|
||||
|
||||
uint tick = time - last;
|
||||
last = time;
|
||||
while (tick != 0)
|
||||
{
|
||||
tick--;
|
||||
laten[index++] = value;
|
||||
if (index >= clock) index = 0;
|
||||
}
|
||||
|
||||
value = laten[index];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class Gyroscope
|
||||
{
|
||||
public static uint Freq;
|
||||
public static float Noise;
|
||||
public static Vector3 Shift;
|
||||
public static float Lateness;
|
||||
|
||||
private uint last = 0;
|
||||
|
||||
private Random rand = new Random();
|
||||
|
||||
private const int count = 1000;
|
||||
private Vector3[] laten = new Vector3[count];
|
||||
private uint index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public Vector3 result;
|
||||
|
||||
public void Update(Vector3 value, uint time)
|
||||
{
|
||||
if (!RealSimulation)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
value.X += Shift.X;
|
||||
value.Y += Shift.Y;
|
||||
value.Z += Shift.Z;
|
||||
|
||||
int noise = (int)(Noise * 1000);
|
||||
value.X += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
value.Y += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
value.Z += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
|
||||
uint clock = (uint)(Lateness * 1000);
|
||||
|
||||
uint tick = time - last;
|
||||
last = time;
|
||||
while (tick != 0)
|
||||
{
|
||||
tick--;
|
||||
laten[index++] = value;
|
||||
if (index >= clock) index = 0;
|
||||
}
|
||||
|
||||
value = laten[index];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class Magnetometer
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal class Position
|
||||
{
|
||||
public static bool Enable;
|
||||
public static uint Freq;
|
||||
public static float Noise;
|
||||
public static float Lateness;
|
||||
|
||||
private uint last = 0;
|
||||
|
||||
private Random rand = new Random();
|
||||
|
||||
private const int count = 1000;
|
||||
private Vector3[] laten = new Vector3[count];
|
||||
private uint index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public Vector3 result;
|
||||
|
||||
public void Update(Vector3 value, uint time)
|
||||
{
|
||||
if (!RealSimulation)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Enable)
|
||||
{
|
||||
result = Vector3.NaN;
|
||||
return;
|
||||
}
|
||||
|
||||
int noise = (int)(Noise * 1000);
|
||||
value.X += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
value.Y += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
value.Z += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
|
||||
uint clock = (uint)(Lateness * 1000);
|
||||
|
||||
uint tick = time - last;
|
||||
last = time;
|
||||
while (tick != 0)
|
||||
{
|
||||
tick--;
|
||||
laten[index++] = value;
|
||||
if (index >= clock) index = 0;
|
||||
}
|
||||
|
||||
value = laten[index];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class Barometer
|
||||
{
|
||||
public static bool Enable;
|
||||
public static float Pressure;
|
||||
public static uint Freq;
|
||||
public static float Noise;
|
||||
public static float Lateness;
|
||||
|
||||
private uint last = 0;
|
||||
|
||||
private Random rand = new Random();
|
||||
|
||||
private const int count = 1000;
|
||||
private float[] laten = new float[count];
|
||||
private uint index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public float result;
|
||||
|
||||
public void Update(float value, uint time)
|
||||
{
|
||||
value = Pressure - value;
|
||||
|
||||
if (!RealSimulation)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Enable)
|
||||
{
|
||||
result = float.NaN;
|
||||
return;
|
||||
}
|
||||
|
||||
int noise = (int)(Noise * 1000);
|
||||
value += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
|
||||
uint clock = (uint)(Lateness * 1000);
|
||||
|
||||
uint tick = time - last;
|
||||
last = time;
|
||||
while (tick != 0)
|
||||
{
|
||||
tick--;
|
||||
laten[index++] = value;
|
||||
if (index >= clock) index = 0;
|
||||
}
|
||||
|
||||
value = laten[index];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class OpticalFlow
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal class Range
|
||||
{
|
||||
public static bool Enable;
|
||||
public static float MaxHeight;
|
||||
public static uint Freq;
|
||||
public static float Noise;
|
||||
public static float Lateness;
|
||||
|
||||
private uint last = 0;
|
||||
|
||||
private Random rand = new Random();
|
||||
|
||||
private const int count = 1000;
|
||||
private float[] laten = new float[count];
|
||||
private uint index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public float result;
|
||||
|
||||
public void Update(float value, uint time)
|
||||
{
|
||||
if (!RealSimulation)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Enable)
|
||||
{
|
||||
result = float.NaN;
|
||||
return;
|
||||
}
|
||||
|
||||
if (value > MaxHeight) value = -1;
|
||||
else
|
||||
{
|
||||
int noise = (int)(Noise * 1000);
|
||||
value += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
}
|
||||
|
||||
uint clock = (uint)(Lateness * 1000);
|
||||
|
||||
uint tick = time - last;
|
||||
last = time;
|
||||
while (tick != 0)
|
||||
{
|
||||
tick--;
|
||||
laten[index++] = value;
|
||||
if (index >= clock) index = 0;
|
||||
}
|
||||
|
||||
value = laten[index];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user