forked from CPL/Simulator
Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
bb32111f29 | |||
65011e65ee | |||
d3c3b013ff | |||
bee4b7fc75 | |||
c0a5c1c349 | |||
3362cf8262 | |||
d1db427915 | |||
565209d6b9 | |||
446e45d9df | |||
9061794bf1 | |||
d8ed10b4be | |||
fb24fe0f7b | |||
771440f642 | |||
f6cdc57e7b | |||
518c19fd2b | |||
b8b3140cea | |||
e6108e40b1 | |||
28965a3609 | |||
35ab5b2f4e | |||
5357dd69fc | |||
87c1210b4b | |||
9289a9ca99 | |||
5a2f0cc807 | |||
3ce096ba33 | |||
edc86c0106 | |||
bf389cdf06 | |||
782d24a8e6 |
@ -1,8 +1,8 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
using static VisualData.VisualDrone;
|
||||
|
||||
namespace DroneSimulator
|
||||
{
|
||||
@ -10,10 +10,12 @@ namespace DroneSimulator
|
||||
{
|
||||
public int ID;
|
||||
|
||||
public bool Active; // Живой?
|
||||
private Socket? Client = null;
|
||||
|
||||
public bool Active = false; // Живой?
|
||||
public const float Dynamic = 10; // Динамика вращения
|
||||
public Vector3 PosXYZ, SpdXYZ, AccXYZ; // Положение в пространстве: Позиция, Скорость, Ускорение
|
||||
public Quaternion Quat; // Основной кватернион
|
||||
public Vector3 PosXYZ = Vector3.Zero, SpdXYZ = Vector3.Zero, AccXYZ = Vector3.Zero; // Положение в пространстве: Позиция, Скорость, Ускорение
|
||||
public Quaternion Quat = Quaternion.Identity; // Основной кватернион
|
||||
public float Power = 0; // Тяга всех двигателей (0-1)
|
||||
|
||||
public Vector3 SpdPRY, AccPRY; // Поворот в пространстве: pitch roll yaw
|
||||
@ -23,8 +25,6 @@ namespace DroneSimulator
|
||||
|
||||
public Vector4 Orientation;
|
||||
|
||||
private uint DataTimer;
|
||||
|
||||
private const float Gravity = 9.8f;
|
||||
|
||||
private const float TO_GRAD = 180 / MathF.PI;
|
||||
@ -32,15 +32,16 @@ namespace DroneSimulator
|
||||
|
||||
public static List<Drone> AllDrones = new List<Drone>();
|
||||
private static Thread? DroneThread = null;
|
||||
public static long Timing = 0;
|
||||
public static long Lag = 0;
|
||||
public static long Freq = 1000;
|
||||
public static bool Boost = false;
|
||||
private uint StepTime = 0;
|
||||
|
||||
public static Semaphore StepSemaphore = new Semaphore(0, 10);
|
||||
|
||||
private uint Timer;
|
||||
|
||||
public static bool TimeLimit = false;
|
||||
|
||||
private Vector2 MoveOF = Vector2.Zero;
|
||||
private uint CountOF = 0;
|
||||
private bool ReadyOF = false;
|
||||
|
||||
public struct Physics
|
||||
{
|
||||
@ -49,12 +50,19 @@ namespace DroneSimulator
|
||||
static public float MaxPower; // Максимальная Тяга всех двигателей (КГ)
|
||||
}
|
||||
|
||||
public struct Propeller
|
||||
{
|
||||
static public float Diameter; // Диаметр лопостей
|
||||
static public float MaxRotate; // Максимальнные обороты в секунду
|
||||
}
|
||||
|
||||
private RealMode.Accelerometer RealAcc = new RealMode.Accelerometer();
|
||||
private RealMode.Gyroscope RealGyr = new RealMode.Gyroscope();
|
||||
private RealMode.Position RealPos = new RealMode.Position();
|
||||
private RealMode.Barometer RealBar = new RealMode.Barometer();
|
||||
private RealMode.Range RealRange = new RealMode.Range();
|
||||
private RealMode.OpticalFlow RealOF = new RealMode.OpticalFlow();
|
||||
private RealMode.Magnetometer RealMag = new RealMode.Magnetometer();
|
||||
|
||||
public static byte[] getBytes(object data)
|
||||
{
|
||||
@ -139,42 +147,35 @@ namespace DroneSimulator
|
||||
|
||||
private static void ThreadFunction()
|
||||
{
|
||||
long last = Stopwatch.GetTimestamp();
|
||||
long prev = Stopwatch.GetTimestamp();
|
||||
|
||||
while (DroneThread != null)
|
||||
{
|
||||
if(!Boost) Thread.Yield();
|
||||
if (StepSemaphore.WaitOne(1))
|
||||
{
|
||||
lock (AllDrones)
|
||||
{
|
||||
foreach (Drone drone in AllDrones)
|
||||
{
|
||||
if (drone.StepTime > 0)
|
||||
{
|
||||
uint prev = drone.Timer;
|
||||
uint next = prev + drone.StepTime;
|
||||
|
||||
long tick = Stopwatch.GetTimestamp();
|
||||
|
||||
if (tick == prev) continue;
|
||||
|
||||
Timing = Stopwatch.Frequency / (tick - prev);
|
||||
prev = tick;
|
||||
|
||||
long quant = Stopwatch.Frequency / Freq;
|
||||
|
||||
if (tick < last + quant) continue;
|
||||
|
||||
if (tick > (last + quant) + quant * 0.1) Lag++;
|
||||
|
||||
last = tick;
|
||||
|
||||
lock (AllDrones)
|
||||
foreach (Drone drone in AllDrones)
|
||||
drone.Action((uint)(tick / (Stopwatch.Frequency / 1000)));
|
||||
drone.Action(next);
|
||||
drone.StepTime = 0;
|
||||
drone.SendStep(next, prev);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Drone(int id)
|
||||
public Drone(int id, Socket? client)
|
||||
{
|
||||
ID = id;
|
||||
Active = false;
|
||||
PosXYZ = Vector3.Zero;
|
||||
SpdXYZ = Vector3.Zero;
|
||||
AccXYZ = Vector3.Zero;
|
||||
Quat = Quaternion.Identity;
|
||||
Timer = 0;
|
||||
Client = client;
|
||||
}
|
||||
|
||||
public int Create()
|
||||
@ -213,6 +214,17 @@ namespace DroneSimulator
|
||||
Quat = Quaternion.Normalize(map);
|
||||
}
|
||||
|
||||
Vector2 RotateToZ(Vector2 vec, bool Rev = false)
|
||||
{
|
||||
Quaternion v = new Quaternion(vec.X, vec.Y, 0, 0);
|
||||
Quaternion q = new Quaternion(0, 0, Rev ? -Quat.Z : Quat.Z, Quat.W);
|
||||
q = Quaternion.Normalize(q);
|
||||
|
||||
q = (v * q) * q;
|
||||
|
||||
return new Vector2(q.X, q.Y);
|
||||
}
|
||||
|
||||
public Vector4 GetOrientation()
|
||||
{
|
||||
Quaternion grav = new Quaternion(0, 0, 1, 0);
|
||||
@ -225,11 +237,14 @@ namespace DroneSimulator
|
||||
return new Vector4(GetAngle(grav.Y, grav.X, grav.Z), GetAngle(-grav.X, grav.Y, grav.Z), yaw, grav.Z);
|
||||
}
|
||||
|
||||
int TestGyr = 0;
|
||||
int TestDir = 1;
|
||||
|
||||
public void Action(uint tick)
|
||||
{
|
||||
uint period = tick - Timer;
|
||||
|
||||
if (period <= 0) return;
|
||||
if (period == 0) return;
|
||||
|
||||
float time = period / 1000.0f;
|
||||
Timer = tick;
|
||||
@ -288,7 +303,7 @@ namespace DroneSimulator
|
||||
Quat = Quaternion.Identity;
|
||||
}
|
||||
else */
|
||||
Rotate(SpdPRY.X * time, SpdPRY.Y * time, SpdPRY.Z * time);
|
||||
Rotate(SpdPRY.X * time, SpdPRY.Y * time, SpdPRY.Z * time);
|
||||
|
||||
Vector4 ori = GetOrientation();
|
||||
|
||||
@ -325,7 +340,7 @@ namespace DroneSimulator
|
||||
}*/
|
||||
else
|
||||
{
|
||||
if (ori.W < 0)
|
||||
if (ori.W < 0) // это обман
|
||||
{
|
||||
//Active = false; // Перевернулся вверх ногами
|
||||
}
|
||||
@ -349,16 +364,23 @@ namespace DroneSimulator
|
||||
RealBar.Update(PosXYZ.Z, tick);
|
||||
RealPos.Update(PosXYZ, tick);
|
||||
|
||||
bool of = RealOF.Update(new Vector2(SpdXYZ.X * range - Gyr.Y, SpdXYZ.Y * range + Gyr.X), LaserRange, tick);
|
||||
Vector2 xy = new Vector2(SpdXYZ.X * TO_GRAD / range, SpdXYZ.Y * TO_GRAD / range);
|
||||
xy = RotateToZ(xy, true);
|
||||
|
||||
if(of) lock (this)
|
||||
Vector2 of_xy;
|
||||
if (range > 0.1) of_xy = new Vector2(xy.X - Gyr.Y, xy.Y + Gyr.X);
|
||||
else of_xy = Vector2.Zero;
|
||||
|
||||
bool of = RealOF.Update(of_xy, LaserRange, tick);
|
||||
|
||||
RealMag.Update(Quat, tick);
|
||||
|
||||
lock (this)
|
||||
{
|
||||
MoveOF += RealOF.result;
|
||||
CountOF += 1;
|
||||
}
|
||||
|
||||
MoveOF += RealOF.result * time;
|
||||
|
||||
DataTimer = tick;
|
||||
if (of) ReadyOF = true;
|
||||
}
|
||||
}
|
||||
|
||||
private float Range(float pow)
|
||||
@ -373,6 +395,13 @@ namespace DroneSimulator
|
||||
{
|
||||
ul = Range(ul); ur = Range(ur); dl = Range(dl); dr = Range(dr);
|
||||
|
||||
float coef = Area.Wind.Density * MathF.Pow(Propeller.Diameter, 4);
|
||||
|
||||
//ul = MathF.Pow(ul * Propeller.MaxRotate, 2) * coef; // я хз как делать
|
||||
//ur = MathF.Pow(ur * Propeller.MaxRotate, 2) * coef;
|
||||
//dl = MathF.Pow(dl * Propeller.MaxRotate, 2) * coef;
|
||||
//dr = MathF.Pow(dr * Propeller.MaxRotate, 2) * coef;
|
||||
|
||||
Power = (ul + ur + dl + dr) / 4;
|
||||
|
||||
AccPRY.Y = ((ul + dl) - (ur + dr));
|
||||
@ -380,6 +409,32 @@ namespace DroneSimulator
|
||||
AccPRY.Z = ((ul + dr) - (dl + ur)) / 4;
|
||||
}
|
||||
|
||||
private void SendStep(uint time, uint prev)
|
||||
{
|
||||
DroneData.Step step = new DroneData.Step();
|
||||
|
||||
step.Head.Size = Marshal.SizeOf(typeof(DroneData.Step));
|
||||
step.Head.Mode = DroneData.DataMode.Response;
|
||||
step.Head.Type = DroneData.DataType.Step;
|
||||
step.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
step.StepTime = time;
|
||||
step.PrevTime = prev;
|
||||
|
||||
byte[] bytes = getBytes(step);
|
||||
|
||||
try { Client?.Send(bytes); } catch { }
|
||||
}
|
||||
|
||||
private void RecvStep(byte[] data)
|
||||
{
|
||||
DroneData.Step step = (DroneData.Step)fromBytes(data, typeof(DroneData.Step));
|
||||
|
||||
StepTime = step.StepTime;
|
||||
|
||||
StepSemaphore.Release();
|
||||
}
|
||||
|
||||
private void RecvDataMotor4(byte[] data)
|
||||
{
|
||||
DroneData.DataMotor4 mot = (DroneData.DataMotor4)fromBytes(data, typeof(DroneData.DataMotor4));
|
||||
@ -394,7 +449,7 @@ namespace DroneSimulator
|
||||
acc.Head.Size = Marshal.SizeOf(typeof(DroneData.DataAcc));
|
||||
acc.Head.Mode = DroneData.DataMode.Response;
|
||||
acc.Head.Type = DroneData.DataType.DataAcc;
|
||||
acc.Head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
acc.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
acc.Acc.X = RealAcc.result.X; acc.Acc.Y = RealAcc.result.Y; acc.Acc.Z = RealAcc.result.Z;
|
||||
acc.Time = RealAcc.timer;
|
||||
@ -409,7 +464,7 @@ namespace DroneSimulator
|
||||
gyr.Head.Size = Marshal.SizeOf(typeof(DroneData.DataGyr));
|
||||
gyr.Head.Mode = DroneData.DataMode.Response;
|
||||
gyr.Head.Type = DroneData.DataType.DataGyr;
|
||||
gyr.Head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
gyr.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
gyr.Gyr.X = RealGyr.result.X; gyr.Gyr.Y = RealGyr.result.Y; gyr.Gyr.Z = RealGyr.result.Z;
|
||||
gyr.Time = RealGyr.timer;
|
||||
@ -424,10 +479,10 @@ namespace DroneSimulator
|
||||
mag.Head.Size = Marshal.SizeOf(typeof(DroneData.DataMag));
|
||||
mag.Head.Mode = DroneData.DataMode.Response;
|
||||
mag.Head.Type = DroneData.DataType.DataMag;
|
||||
mag.Head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
mag.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
mag.Mag.X = 0; mag.Mag.Y = 0; mag.Mag.Z = 0;
|
||||
mag.Time = DataTimer;
|
||||
mag.Mag.X = RealMag.result.X; mag.Mag.Y = RealMag.result.Y; mag.Mag.Z = RealMag.result.Z;
|
||||
mag.Time = RealMag.timer;
|
||||
|
||||
return getBytes(mag);
|
||||
}
|
||||
@ -439,7 +494,7 @@ namespace DroneSimulator
|
||||
range.Head.Size = Marshal.SizeOf(typeof(DroneData.DataRange));
|
||||
range.Head.Mode = DroneData.DataMode.Response;
|
||||
range.Head.Type = DroneData.DataType.DataRange;
|
||||
range.Head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
range.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
range.LiDAR = RealRange.result;
|
||||
range.Time = RealRange.timer;
|
||||
@ -454,7 +509,7 @@ namespace DroneSimulator
|
||||
local.Head.Size = Marshal.SizeOf(typeof(DroneData.DataLocal));
|
||||
local.Head.Mode = DroneData.DataMode.Response;
|
||||
local.Head.Type = DroneData.DataType.DataLocal;
|
||||
local.Head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
local.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
local.Local.X = RealPos.result.X; local.Local.Y = RealPos.result.Y; local.Local.Z = RealPos.result.Z;
|
||||
local.Time = RealPos.timer;
|
||||
@ -469,7 +524,7 @@ namespace DroneSimulator
|
||||
bar.Head.Size = Marshal.SizeOf(typeof(DroneData.DataBar));
|
||||
bar.Head.Mode = DroneData.DataMode.Response;
|
||||
bar.Head.Type = DroneData.DataType.DataBar;
|
||||
bar.Head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
bar.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
bar.Pressure = RealBar.result;
|
||||
bar.Time = RealBar.timer;
|
||||
@ -484,16 +539,25 @@ namespace DroneSimulator
|
||||
of.Head.Size = Marshal.SizeOf(typeof(DroneData.DataOF));
|
||||
of.Head.Mode = DroneData.DataMode.Response;
|
||||
of.Head.Type = DroneData.DataType.DataOF;
|
||||
of.Head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
of.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
lock (this)
|
||||
{
|
||||
of.X = MoveOF.X / CountOF;
|
||||
of.Y = MoveOF.Y / CountOF;
|
||||
if (ReadyOF)
|
||||
{
|
||||
of.X = MoveOF.X;
|
||||
of.Y = MoveOF.Y;
|
||||
MoveOF = Vector2.Zero;
|
||||
}
|
||||
else
|
||||
{
|
||||
of.X = 0;
|
||||
of.Y = 0;
|
||||
}
|
||||
|
||||
of.Time = RealOF.timer;
|
||||
|
||||
MoveOF = Vector2.Zero;
|
||||
CountOF = 0;
|
||||
ReadyOF = false;
|
||||
}
|
||||
|
||||
return getBytes(of);
|
||||
@ -506,7 +570,7 @@ namespace DroneSimulator
|
||||
gps.Head.Size = Marshal.SizeOf(typeof(DroneData.DataGPS));
|
||||
gps.Head.Mode = DroneData.DataMode.Response;
|
||||
gps.Head.Type = DroneData.DataType.DataGPS;
|
||||
gps.Head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
gps.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
GPS.Point p = new GPS.Point();
|
||||
p.x = RealPos.result.Y; p.y= RealPos.result.X;
|
||||
@ -544,7 +608,7 @@ namespace DroneSimulator
|
||||
quat.Head.Size = Marshal.SizeOf(typeof(DroneData.DataQuat));
|
||||
quat.Head.Mode = DroneData.DataMode.Response;
|
||||
quat.Head.Type = DroneData.DataType.DataQuat;
|
||||
quat.Head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
quat.Head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
quat.X = Quat.X; quat.Y = Quat.Y; quat.Z = Quat.Z; quat.W = Quat.W;
|
||||
|
||||
@ -557,8 +621,8 @@ namespace DroneSimulator
|
||||
|
||||
head.Size = Marshal.SizeOf(typeof(DroneData.DataHead));
|
||||
head.Mode = DroneData.DataMode.Response;
|
||||
head.Type = DroneData.DataType.None;
|
||||
head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000));
|
||||
head.Type = DroneData.DataType.Ping;
|
||||
head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
|
||||
|
||||
return getBytes(head);
|
||||
}
|
||||
@ -569,6 +633,8 @@ namespace DroneSimulator
|
||||
|
||||
switch (head.Type)
|
||||
{
|
||||
case DroneData.DataType.Step: if (head.Mode == DroneData.DataMode.Request) RecvStep(body); return zero;
|
||||
|
||||
case DroneData.DataType.DataAcc:
|
||||
{
|
||||
if (head.Mode == DroneData.DataMode.Request)
|
||||
@ -603,7 +669,7 @@ namespace DroneSimulator
|
||||
|
||||
case DroneData.DataType.DataMotor4: if (head.Mode == DroneData.DataMode.Response) RecvDataMotor4(body); return zero;
|
||||
|
||||
case DroneData.DataType.None: if (head.Mode == DroneData.DataMode.Request) return SendPingPong(); else return zero;
|
||||
case DroneData.DataType.Ping: if (head.Mode == DroneData.DataMode.Request) return SendPingPong(); else return zero;
|
||||
}
|
||||
|
||||
return zero;
|
||||
@ -612,7 +678,7 @@ namespace DroneSimulator
|
||||
private const int DroneStreamCount = 512;
|
||||
private byte[] DroneStreamData = new byte[DroneStreamCount];
|
||||
private int DroneStreamIndex = 0;
|
||||
private DroneData.DataHead DroneStreamHead = new DroneData.DataHead() { Mode = DroneData.DataMode.None, Size = 0, Type = DroneData.DataType.None };
|
||||
private DroneData.DataHead DroneStreamHead = new DroneData.DataHead() { Mode = DroneData.DataMode.None, Size = 0, Type = DroneData.DataType.Ping };
|
||||
|
||||
public List<byte[]?>? DataStream(byte[]? data, int size)
|
||||
{
|
||||
@ -633,6 +699,8 @@ namespace DroneSimulator
|
||||
DroneStreamHead = (DroneData.DataHead)fromBytes(DroneStreamData, typeof(DroneData.DataHead));
|
||||
}
|
||||
|
||||
if (DroneStreamHead.Size == 0) return null; // Поток сломан (конец)
|
||||
|
||||
if (DroneStreamHead.Size > DroneStreamIndex) break; // Пакет ещё не полный
|
||||
|
||||
byte[] body = new byte[DroneStreamHead.Size];
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System.Net;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DroneData
|
||||
{
|
||||
@ -9,8 +8,8 @@ namespace DroneData
|
||||
};
|
||||
|
||||
public enum DataType : ushort
|
||||
{
|
||||
None = 0, Head = 1,
|
||||
{
|
||||
None = 0, Ping = 1, Step = 2,
|
||||
|
||||
// Output
|
||||
DataAcc = 1001, DataGyr = 1002, DataMag = 1003, DataRange = 1004, DataLocal = 1005, DataBar = 1006, DataOF = 1007, DataGPS = 1008,
|
||||
@ -29,17 +28,27 @@ namespace DroneData
|
||||
public DataMode Mode;
|
||||
public DataType Type;
|
||||
|
||||
public uint Time; // Общее время
|
||||
public uint Time; // ответ: Общее время на симуляторе
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataHead));
|
||||
}
|
||||
|
||||
public struct Step
|
||||
{
|
||||
public DataHead Head;
|
||||
|
||||
public uint StepTime; // ms, запрос: шаг работы симулятора; ответ: Последнее время изменения данных
|
||||
public uint PrevTime; // ms, ответ: Предпоследнее время изменения данных
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.Step));
|
||||
}
|
||||
|
||||
public struct XYZ { public float X, Y, Z; }
|
||||
|
||||
public struct DataAcc
|
||||
{
|
||||
public DataHead Head;
|
||||
public XYZ Acc;
|
||||
public XYZ Acc; // G, ускорения по осям
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
|
||||
@ -49,7 +58,7 @@ namespace DroneData
|
||||
public struct DataGyr
|
||||
{
|
||||
public DataHead Head;
|
||||
public XYZ Gyr;
|
||||
public XYZ Gyr; // dps, угловые скорости
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
|
||||
@ -69,7 +78,7 @@ namespace DroneData
|
||||
public struct DataRange
|
||||
{
|
||||
public DataHead Head;
|
||||
public float LiDAR; // Датчик посадки
|
||||
public float LiDAR; // m, Датчик посадки
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
|
||||
@ -79,7 +88,7 @@ namespace DroneData
|
||||
public struct DataLocal
|
||||
{
|
||||
public DataHead Head;
|
||||
public XYZ Local; // Локальные координаты
|
||||
public XYZ Local; // m, Локальные координаты
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
|
||||
@ -89,7 +98,7 @@ namespace DroneData
|
||||
public struct DataBar
|
||||
{
|
||||
public DataHead Head;
|
||||
public float Pressure; // Давление
|
||||
public float Pressure; // Pa, Давление
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
|
||||
@ -99,7 +108,7 @@ namespace DroneData
|
||||
public struct DataOF
|
||||
{
|
||||
public DataHead Head;
|
||||
public float X, Y; // Угловой сдвиг
|
||||
public float X, Y; // degree, Угловой сдвиг
|
||||
|
||||
public uint Time; // Последнее время изменения данных
|
||||
|
||||
@ -129,7 +138,7 @@ namespace DroneData
|
||||
public struct DataMotor4
|
||||
{
|
||||
public DataHead Head;
|
||||
public float UL, UR, DL, DR;
|
||||
public float UL, UR, DL, DR; // тяга 0.0 - 1.0
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataMotor4));
|
||||
}
|
||||
@ -137,7 +146,7 @@ namespace DroneData
|
||||
public struct DataMotor6
|
||||
{
|
||||
public DataHead Head;
|
||||
public float UL, UR, LL, RR, DL, DR;
|
||||
public float UL, UR, LL, RR, DL, DR; // тяга 0.0 - 1.0
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataMotor6));
|
||||
}
|
||||
@ -145,7 +154,7 @@ namespace DroneData
|
||||
public struct DataQuat
|
||||
{
|
||||
public DataHead Head;
|
||||
public float X, Y, Z, W;
|
||||
public float X, Y, Z, W; // Кватернион дрона
|
||||
|
||||
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataQuat));
|
||||
}
|
||||
|
1198
DroneSimulator/FormMain.Designer.cs
generated
1198
DroneSimulator/FormMain.Designer.cs
generated
File diff suppressed because it is too large
Load Diff
@ -44,7 +44,7 @@ namespace DroneSimulator
|
||||
|
||||
if (data.Connect)
|
||||
{
|
||||
Drone drone = new Drone(data.ID);
|
||||
Drone drone = new Drone(data.ID, data.Client);
|
||||
drone.Create();
|
||||
|
||||
screen2D.CreateDrone(Color.Red, data.ID);
|
||||
@ -115,6 +115,9 @@ namespace DroneSimulator
|
||||
{
|
||||
button_Client_Start.Text = "Stop";
|
||||
button_Client_Start.BackColor = Color.LimeGreen;
|
||||
numericUpDown_Clients_Limit.Enabled = false;
|
||||
numericUpDown_Clients_Port.Enabled = false;
|
||||
checkBox_Lockstep_Limit.Enabled = false;
|
||||
break;
|
||||
}
|
||||
case NetServerClients.ServerState.Stop:
|
||||
@ -122,6 +125,9 @@ namespace DroneSimulator
|
||||
label_Clients_Num.Text = "0";
|
||||
button_Client_Start.Text = "Start";
|
||||
button_Client_Start.BackColor = Color.Transparent;
|
||||
numericUpDown_Clients_Limit.Enabled = true;
|
||||
numericUpDown_Clients_Port.Enabled = true;
|
||||
checkBox_Lockstep_Limit.Enabled = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -151,10 +157,9 @@ namespace DroneSimulator
|
||||
|
||||
private void timer_Test_Tick(object sender, EventArgs e)
|
||||
{
|
||||
DateTime test = DateTime.Now;
|
||||
|
||||
int tim = test.Second + test.Minute * 100 + test.Hour * 10000;
|
||||
DateTime time = DateTime.Now;
|
||||
|
||||
label_Lockstep_Time.Text = time.Hour.ToString("D2") + ":" + time.Minute.ToString("D2") + ":" + time.Second.ToString("D2") + "." + time.Millisecond.ToString("D3");
|
||||
|
||||
if (screen2D == null) return;
|
||||
|
||||
@ -166,7 +171,7 @@ namespace DroneSimulator
|
||||
{
|
||||
foreach (Drone d in Drone.AllDrones)
|
||||
{
|
||||
screen2D.Move(d.ID, d.PosXYZ, d.GetOrientation());
|
||||
screen2D.Move(d.ID, d.PosXYZ, d.GetOrientation(), d.Quat);
|
||||
|
||||
string line = "ID:" + d.ID.ToString() + " Pitch:" + ((int)d.Orientation.X).ToString() + " Roll:" + ((int)d.Orientation.Y).ToString() + " Yaw:" + ((int)d.Orientation.Z).ToString();
|
||||
|
||||
@ -176,9 +181,6 @@ namespace DroneSimulator
|
||||
}
|
||||
catch { }
|
||||
|
||||
label_Timing.Text = Drone.Timing.ToString() + " Hz";
|
||||
label_Timing_Lag.Text = Drone.Lag.ToString();
|
||||
|
||||
screen2D.DrawScene();
|
||||
}
|
||||
|
||||
@ -367,10 +369,15 @@ namespace DroneSimulator
|
||||
Drone.StopThread();
|
||||
}
|
||||
|
||||
private void numericUpDown_Timing_Freq_ValueChanged(object sender, EventArgs e)
|
||||
private void checkBox_Lockstep_Limit_CheckedChanged(object sender, EventArgs e)
|
||||
{
|
||||
Drone.Freq = (long)numericUpDown_Timing_Freq.Value;
|
||||
Drone.Boost = checkBox_Freq_Boost.Checked;
|
||||
Drone.TimeLimit = checkBox_Lockstep_Limit.Checked;
|
||||
}
|
||||
|
||||
private void numericUpDown_Propeller_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
Drone.Propeller.Diameter = (float)numericUpDown_Propeller_Diameter.Value;
|
||||
Drone.Propeller.MaxRotate = ((float)numericUpDown_Propeller_Rotation.Value) / 60;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,67 +8,67 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace DroneSimulator
|
||||
{
|
||||
internal class GPS
|
||||
internal class GPS
|
||||
{
|
||||
static double PI = 3.14159265358979323846;
|
||||
public struct Home
|
||||
{
|
||||
static double PI = 3.14159265358979323846;
|
||||
public struct Home
|
||||
{
|
||||
public static double Lat, Lon;
|
||||
public static float Alt;
|
||||
}
|
||||
|
||||
public struct State
|
||||
{
|
||||
public static byte Fix; // Тип решения 0-8 (NMEA Fix type)
|
||||
public static byte SatVisible; // Количество видимых спутников
|
||||
public static byte SatUsed; // Количество используемых спутников
|
||||
public static float Hdop, Vdop, Pdop; // Геометрический фактор
|
||||
public static float Noise; // Шум (db)
|
||||
}
|
||||
|
||||
public struct GlobalCoords
|
||||
{
|
||||
public double latitude, longitude;
|
||||
}
|
||||
|
||||
public struct Point
|
||||
{
|
||||
public double x, y;
|
||||
}
|
||||
|
||||
// Конвертация градусов в радианы
|
||||
static double deg2rad(double deg)
|
||||
{
|
||||
return deg * PI / 180.0;
|
||||
}
|
||||
|
||||
// Конвертация радиан в градусы
|
||||
static double rad2deg(double rad)
|
||||
{
|
||||
return rad * 180.0 / PI;
|
||||
}
|
||||
|
||||
// Перевод локальных координат в глобальные
|
||||
public static GlobalCoords localToGlobal(Point local, GlobalCoords origin)
|
||||
{
|
||||
const double er = 6371000; // Radius of the earth in m
|
||||
|
||||
// Преобразование приращений координат
|
||||
double dLat = local.x / er; // В радианах
|
||||
double originLatRad = deg2rad(origin.latitude);
|
||||
|
||||
// Вычисление новой широты
|
||||
double newLatRad = originLatRad + dLat;
|
||||
double newLat = rad2deg(newLatRad);
|
||||
|
||||
// Вычисление новой долготы (с использованием средней широты для точности)
|
||||
double avgLatRad = (originLatRad + newLatRad) / 2.0;
|
||||
double dLon = local.y / (er * Math.Cos(avgLatRad)); // В радианах
|
||||
double newLon = origin.longitude + rad2deg(dLon);
|
||||
GlobalCoords coord = new GlobalCoords();
|
||||
coord.latitude = newLat;
|
||||
coord.longitude = newLon;
|
||||
return coord;
|
||||
}
|
||||
public static double Lat, Lon;
|
||||
public static float Alt;
|
||||
}
|
||||
|
||||
public struct State
|
||||
{
|
||||
public static byte Fix; // Тип решения 0-8 (NMEA Fix type)
|
||||
public static byte SatVisible; // Количество видимых спутников
|
||||
public static byte SatUsed; // Количество используемых спутников
|
||||
public static float Hdop, Vdop, Pdop; // Геометрический фактор
|
||||
public static float Noise; // Шум (db)
|
||||
}
|
||||
|
||||
public struct GlobalCoords
|
||||
{
|
||||
public double latitude, longitude;
|
||||
}
|
||||
|
||||
public struct Point
|
||||
{
|
||||
public double x, y;
|
||||
}
|
||||
|
||||
// Конвертация градусов в радианы
|
||||
static double deg2rad(double deg)
|
||||
{
|
||||
return deg * PI / 180.0;
|
||||
}
|
||||
|
||||
// Конвертация радиан в градусы
|
||||
static double rad2deg(double rad)
|
||||
{
|
||||
return rad * 180.0 / PI;
|
||||
}
|
||||
|
||||
// Перевод локальных координат в глобальные
|
||||
public static GlobalCoords localToGlobal(Point local, GlobalCoords origin)
|
||||
{
|
||||
const double er = 6371000; // Radius of the earth in m
|
||||
|
||||
// Преобразование приращений координат
|
||||
double dLat = local.x / er; // В радианах
|
||||
double originLatRad = deg2rad(origin.latitude);
|
||||
|
||||
// Вычисление новой широты
|
||||
double newLatRad = originLatRad + dLat;
|
||||
double newLat = rad2deg(newLatRad);
|
||||
|
||||
// Вычисление новой долготы (с использованием средней широты для точности)
|
||||
double avgLatRad = (originLatRad + newLatRad) / 2.0;
|
||||
double dLon = local.y / (er * Math.Cos(avgLatRad)); // В радианах
|
||||
double newLon = origin.longitude + rad2deg(dLon);
|
||||
GlobalCoords coord = new GlobalCoords();
|
||||
coord.latitude = newLat;
|
||||
coord.longitude = newLon;
|
||||
return coord;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,18 @@
|
||||
using System;
|
||||
using Microsoft.VisualBasic.Devices;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
|
||||
|
||||
namespace DroneSimulator
|
||||
{
|
||||
internal class RealMode
|
||||
{
|
||||
|
||||
|
||||
|
||||
internal class Accelerometer
|
||||
{
|
||||
@ -27,7 +29,7 @@ namespace DroneSimulator
|
||||
|
||||
private const int count = 1000;
|
||||
private Vector3[] laten = new Vector3[count];
|
||||
private uint index = 0;
|
||||
private int index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public Vector3 result;
|
||||
@ -53,22 +55,25 @@ namespace DroneSimulator
|
||||
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)
|
||||
uint clock = time - last;
|
||||
while (true)
|
||||
{
|
||||
tick--;
|
||||
laten[index++] = value;
|
||||
if (index >= clock) index = 0;
|
||||
laten[index] = value;
|
||||
clock--;
|
||||
if (clock == 0) break;
|
||||
index++;
|
||||
if (index >= count) index = 0;
|
||||
}
|
||||
last = time;
|
||||
|
||||
value = laten[index];
|
||||
int move = (int)(Lateness * count);
|
||||
move = index - move;
|
||||
while (move < 0) move += count;
|
||||
value = laten[move];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
if (timer + freq <= time)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
@ -90,7 +95,7 @@ namespace DroneSimulator
|
||||
|
||||
private const int count = 1000;
|
||||
private Vector3[] laten = new Vector3[count];
|
||||
private uint index = 0;
|
||||
private int index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public Vector3 result;
|
||||
@ -113,22 +118,25 @@ namespace DroneSimulator
|
||||
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)
|
||||
uint clock = time - last;
|
||||
while (true)
|
||||
{
|
||||
tick--;
|
||||
laten[index++] = value;
|
||||
if (index >= clock) index = 0;
|
||||
laten[index] = value;
|
||||
clock--;
|
||||
if (clock == 0) break;
|
||||
index++;
|
||||
if (index >= count) index = 0;
|
||||
}
|
||||
last = time;
|
||||
|
||||
value = laten[index];
|
||||
int move = (int)(Lateness * count);
|
||||
move = index - move;
|
||||
while (move < 0) move += count;
|
||||
value = laten[move];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
if (timer + freq <= time)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
@ -138,7 +146,106 @@ namespace DroneSimulator
|
||||
|
||||
internal class Magnetometer
|
||||
{
|
||||
/**
|
||||
* The model is produced by the United States’ National Geospatial-Intelligence Agency (NGA)
|
||||
* and the United Kingdom’s Defence Geographic Centre (DGC)
|
||||
* NCEI and the British Geological Survey (BGS) jointly developed the WMM.
|
||||
*/
|
||||
/* Taganrog
|
||||
* 47° 12' 32" N
|
||||
* 38° 56' 10" E
|
||||
* Declination: 8° 32' 28"
|
||||
* Inclination: 65° 34' 9"
|
||||
* Total Field: 51,120.8 nT
|
||||
*/
|
||||
public static bool Enable;
|
||||
public static uint Freq;
|
||||
public static float Noise;
|
||||
public static Vector3 Shift;
|
||||
public static float Lateness;
|
||||
public static bool RealSimulation;
|
||||
|
||||
private uint last = 0;
|
||||
|
||||
private Random rand = new Random();
|
||||
|
||||
public static float fieldStrength = 51.1208F; // uT
|
||||
public static float fieldDeclination = (8 + 32 / 60 + 28 / 3600) * (MathF.PI / 180);
|
||||
public static float fieldInclination = (65 + 34 / 60 + 9 / 3600) * (MathF.PI / 180);
|
||||
|
||||
private static Vector3 InitializeMagneticField()
|
||||
{
|
||||
float horizontalComponent = fieldStrength * MathF.Cos(fieldInclination);
|
||||
|
||||
float northComponent = horizontalComponent * MathF.Cos(fieldDeclination); // X
|
||||
float eastComponent = horizontalComponent * MathF.Sin(fieldDeclination); // Y
|
||||
float downComponent = fieldStrength * MathF.Sin(fieldInclination); // Z
|
||||
|
||||
return new Vector3(northComponent, eastComponent, downComponent);
|
||||
}
|
||||
|
||||
private static Vector3 magneticField = InitializeMagneticField();
|
||||
|
||||
private const int count = 1000;
|
||||
private Vector3[] laten = new Vector3[count];
|
||||
private int index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public Vector3 result;
|
||||
|
||||
public void Update(Quaternion oreintantion, uint time)
|
||||
{
|
||||
Vector3 value = Vector3.Transform(magneticField, oreintantion);
|
||||
|
||||
Vector3 v = value;
|
||||
|
||||
v.X += Shift.X;
|
||||
v.Y += Shift.Y;
|
||||
v.Z += Shift.Z;
|
||||
|
||||
int noise = (int)(Noise * 1000);
|
||||
v.X += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
v.Y += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
v.Z += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
|
||||
uint clock = time - last;
|
||||
while (true)
|
||||
{
|
||||
laten[index] = v;
|
||||
clock--;
|
||||
if (clock == 0) break;
|
||||
index++;
|
||||
if (index >= count) index = 0;
|
||||
}
|
||||
last = time;
|
||||
|
||||
if (!Enable)
|
||||
{
|
||||
result = Vector3.NaN;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!RealSimulation)
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
int move = (int)(Lateness * count);
|
||||
move = index - move;
|
||||
while (move < 0) move += count;
|
||||
v = laten[move];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq <= time)
|
||||
{
|
||||
result = v;
|
||||
timer = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class Position
|
||||
@ -155,16 +262,35 @@ namespace DroneSimulator
|
||||
|
||||
private const int count = 1000;
|
||||
private Vector3[] laten = new Vector3[count];
|
||||
private uint index = 0;
|
||||
private int index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public Vector3 result;
|
||||
|
||||
public void Update(Vector3 value, uint time)
|
||||
{
|
||||
Vector3 v = value;
|
||||
|
||||
int noise = (int)(Noise * 1000);
|
||||
v.X += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
v.Y += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
v.Z += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
|
||||
uint clock = time - last;
|
||||
while (true)
|
||||
{
|
||||
laten[index] = v;
|
||||
clock--;
|
||||
if (clock == 0) break;
|
||||
index++;
|
||||
if (index >= count) index = 0;
|
||||
}
|
||||
last = time;
|
||||
|
||||
if (!Enable)
|
||||
{
|
||||
result = Vector3.NaN;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -175,29 +301,15 @@ namespace DroneSimulator
|
||||
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];
|
||||
int move = (int)(Lateness * count);
|
||||
move = index - move;
|
||||
while (move < 0) move += count;
|
||||
v = laten[move];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
if (timer + freq <= time)
|
||||
{
|
||||
result = value;
|
||||
result = v;
|
||||
timer = time;
|
||||
}
|
||||
}
|
||||
@ -211,6 +323,7 @@ namespace DroneSimulator
|
||||
public static float Noise;
|
||||
public static float Lateness;
|
||||
public static bool RealSimulation;
|
||||
public static float Temperature = 25.0f;
|
||||
|
||||
private uint last = 0;
|
||||
|
||||
@ -218,18 +331,35 @@ namespace DroneSimulator
|
||||
|
||||
private const int count = 1000;
|
||||
private float[] laten = new float[count];
|
||||
private uint index = 0;
|
||||
private int index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public float result;
|
||||
|
||||
public void Update(float value, uint time)
|
||||
{
|
||||
value = Pressure - value * 12.15f;
|
||||
value = Pressure * MathF.Exp(-0.02896f * 9.81f * value / (8.314f * (Temperature + 273.15f)));
|
||||
|
||||
float v = value;
|
||||
|
||||
int noise = (int)(Noise * 1000);
|
||||
v += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
|
||||
uint clock = time - last;
|
||||
while (true)
|
||||
{
|
||||
laten[index] = v;
|
||||
clock--;
|
||||
if (clock == 0) break;
|
||||
index++;
|
||||
if (index >= count) index = 0;
|
||||
}
|
||||
last = time;
|
||||
|
||||
if (!Enable)
|
||||
{
|
||||
result = float.NaN;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -237,30 +367,18 @@ namespace DroneSimulator
|
||||
{
|
||||
result = value;
|
||||
timer = time;
|
||||
return;
|
||||
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];
|
||||
int move = (int)(Lateness * count);
|
||||
move = index - move;
|
||||
while (move < 0) move += count;
|
||||
v = laten[move];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
if (timer + freq <= time)
|
||||
{
|
||||
result = value;
|
||||
result = v;
|
||||
timer = time;
|
||||
}
|
||||
}
|
||||
@ -282,7 +400,7 @@ namespace DroneSimulator
|
||||
|
||||
private const int count = 1000;
|
||||
private Vector2[] laten = new Vector2[count];
|
||||
private uint index = 0;
|
||||
private int index = 0;
|
||||
|
||||
public uint delay = 0;
|
||||
|
||||
@ -292,9 +410,31 @@ namespace DroneSimulator
|
||||
{
|
||||
value *= Lens;
|
||||
|
||||
Vector2 v = value;
|
||||
|
||||
if (Range > MaxHeight) v = Vector2.Zero;
|
||||
else
|
||||
{
|
||||
int noise = (int)(Noise * 1000);
|
||||
v.X += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
v.Y += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
}
|
||||
|
||||
uint clock = time - last;
|
||||
while (true)
|
||||
{
|
||||
laten[index] = v;
|
||||
clock--;
|
||||
if (clock == 0) break;
|
||||
index++;
|
||||
if (index >= count) index = 0;
|
||||
}
|
||||
last = time;
|
||||
|
||||
if (!Enable)
|
||||
{
|
||||
result = Vector2.NaN;
|
||||
timer = time;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -305,32 +445,14 @@ namespace DroneSimulator
|
||||
return true;
|
||||
}
|
||||
|
||||
if (Range > MaxHeight) value = Vector2.Zero;
|
||||
else
|
||||
int move = (int)(Lateness * count);
|
||||
move = index - move;
|
||||
while (move < 0) move += count;
|
||||
result = laten[move];
|
||||
|
||||
uint freq = count / Freq;
|
||||
if (timer + freq <= time)
|
||||
{
|
||||
int noise = (int)(Noise * 1000);
|
||||
value.X += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
value.Y += ((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;
|
||||
return true;
|
||||
}
|
||||
@ -354,16 +476,37 @@ namespace DroneSimulator
|
||||
|
||||
private const int count = 1000;
|
||||
private float[] laten = new float[count];
|
||||
private uint index = 0;
|
||||
private int index = 0;
|
||||
|
||||
public uint timer = 0;
|
||||
public float result;
|
||||
|
||||
public void Update(float value, uint time)
|
||||
{
|
||||
float v = value;
|
||||
|
||||
if (v > MaxHeight) v = -1;
|
||||
else
|
||||
{
|
||||
int noise = (int)(Noise * 1000);
|
||||
v += ((float)rand.Next(-noise, noise)) / 1000;
|
||||
}
|
||||
|
||||
uint clock = time - last;
|
||||
while (true)
|
||||
{
|
||||
laten[index] = v;
|
||||
clock--;
|
||||
if (clock == 0) break;
|
||||
index++;
|
||||
if (index >= count) index = 0;
|
||||
}
|
||||
last = time;
|
||||
|
||||
if (!Enable)
|
||||
{
|
||||
result = float.NaN;
|
||||
timer = time;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -374,31 +517,15 @@ namespace DroneSimulator
|
||||
return;
|
||||
}
|
||||
|
||||
if (value > MaxHeight) value = MaxHeight;
|
||||
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];
|
||||
int move = (int)(Lateness * count);
|
||||
move = index - move;
|
||||
while (move < 0) move += count;
|
||||
v = laten[move];
|
||||
|
||||
uint freq = 1000 / Freq;
|
||||
|
||||
if (timer + freq < time)
|
||||
if (timer + freq <= time)
|
||||
{
|
||||
result = value;
|
||||
result = v;
|
||||
timer = time;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Numerics;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Numerics;
|
||||
|
||||
namespace DroneSimulator
|
||||
{
|
||||
@ -22,6 +23,7 @@ namespace DroneSimulator
|
||||
|
||||
public PointF TiltXY = new Point(0, 0);
|
||||
public int Azimuth = 0;
|
||||
public Quaternion Quaternion;
|
||||
}
|
||||
|
||||
private float Scale = 100;
|
||||
@ -93,9 +95,50 @@ namespace DroneSimulator
|
||||
if (i.ID != ID) continue;
|
||||
DroneList.Remove(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Bitmap DrawImageByQuaternion(Bitmap bmp, Quaternion orientation)
|
||||
{
|
||||
if (bmp == null) return null;
|
||||
orientation.X = -orientation.X;
|
||||
|
||||
int canvasSize = (int)System.Math.Sqrt(bmp.Width * bmp.Width + bmp.Height * bmp.Height);
|
||||
Bitmap result = new Bitmap(canvasSize, canvasSize);
|
||||
|
||||
float halfWidth = bmp.Width / 2f;
|
||||
float halfHeight = bmp.Height / 2f;
|
||||
|
||||
|
||||
Vector3[] sourceCorners = new Vector3[]
|
||||
{
|
||||
new Vector3(-halfWidth, -halfHeight, 0), // верхний левый
|
||||
new Vector3( halfWidth, -halfHeight, 0), // верхний правый
|
||||
new Vector3(-halfWidth, halfHeight, 0), // нижний левый
|
||||
};
|
||||
|
||||
PointF[] destPoints = new PointF[3];
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
Vector3 rotatedPoint = Vector3.Transform(sourceCorners[i], orientation);
|
||||
|
||||
destPoints[i] = new PointF(
|
||||
rotatedPoint.X + canvasSize / 2f,
|
||||
rotatedPoint.Y + canvasSize / 2f
|
||||
);
|
||||
}
|
||||
|
||||
using (Graphics g = Graphics.FromImage(result))
|
||||
{
|
||||
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
g.SmoothingMode = SmoothingMode.AntiAlias;
|
||||
g.DrawImage(bmp, destPoints);
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
public void DrawScene()
|
||||
{
|
||||
using (Graphics g = Graphics.FromImage(MainArea))
|
||||
@ -109,50 +152,15 @@ namespace DroneSimulator
|
||||
try
|
||||
{
|
||||
if (d.Azimuth >= 360) d.Azimuth -= 360;
|
||||
var bmp = RotateImage(d.Drone, d.Azimuth);
|
||||
|
||||
var bmp = DrawImageByQuaternion(d.Drone, d.Quaternion);
|
||||
|
||||
g.FillEllipse(new SolidBrush(Color.FromArgb(50, d.RGB)), d.PosXY.X + d.Height, d.PosXY.Y + d.Height, 130, 130);
|
||||
|
||||
g.DrawLine(new Pen(Color.Black), new Point(d.PosXY.X + d.Drone.Width / 2, d.PosXY.Y + d.Drone.Height / 2), new Point(d.PosXY.X + d.Height + d.Drone.Width / 2, d.PosXY.Y + d.Height + d.Drone.Height / 2));
|
||||
|
||||
//g.DrawImage(bmp, new Rectangle(d.PosXY.X+32, d.PosXY.Y, 65, 130));
|
||||
g.DrawImage(bmp, d.PosXY.X - d.Drone.Width / 2, d.PosXY.Y - d.Drone.Height / 2); // Draw the transformed image
|
||||
|
||||
float x1 = 0, y1 = 0;
|
||||
float x2 = 130, y2 = 0;
|
||||
float x3 = 0, y3 = 130;
|
||||
|
||||
const float TO_RADI = MathF.PI / 180;
|
||||
|
||||
Quaternion tilt = new Quaternion(d.TiltXY.X, d.TiltXY.Y, 0, 0);
|
||||
Quaternion rotate = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, 1), d.Azimuth * TO_RADI);
|
||||
|
||||
tilt = tilt * rotate * rotate;
|
||||
|
||||
if (tilt.Y > 0)
|
||||
{
|
||||
x1 = (int)(Math.Sin(tilt.Y) * 130);
|
||||
x3 = (int)(Math.Sin(tilt.Y) * 130);
|
||||
}
|
||||
else
|
||||
{
|
||||
x2 = (int)(Math.Cos(tilt.Y) * 130);
|
||||
}
|
||||
|
||||
if (tilt.X > 0)
|
||||
{
|
||||
y1 = (int)(Math.Sin(tilt.X) * 130);
|
||||
y2 = (int)(Math.Sin(tilt.X) * 130);
|
||||
}
|
||||
else
|
||||
{
|
||||
y3 = (int)(Math.Cos(tilt.X) * 130);
|
||||
}
|
||||
|
||||
PointF ul = new PointF(d.PosXY.X + x1, d.PosXY.Y + y1); PointF ur = new PointF(d.PosXY.X + x2, d.PosXY.Y + y2);
|
||||
PointF dl = new PointF(d.PosXY.X + x3, d.PosXY.Y + y3);
|
||||
PointF[] dest = { ul, ur, dl };
|
||||
|
||||
g.DrawImage(bmp, dest);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
@ -161,27 +169,25 @@ namespace DroneSimulator
|
||||
drawCallback(MainArea);
|
||||
}
|
||||
|
||||
public void Move(int id, Vector3 pos, Vector4 tilt)
|
||||
public void Move(int id, Vector3 pos, Vector4 tilt, Quaternion quaternion)
|
||||
{
|
||||
const float TO_GRAD = 180 / MathF.PI;
|
||||
const float TO_RADI = MathF.PI / 180;
|
||||
|
||||
pos *= Scale;
|
||||
|
||||
pos.X += MainArea.Width / 2;
|
||||
pos.Y += MainArea.Height / 2;
|
||||
|
||||
foreach (var d in DroneList)
|
||||
{
|
||||
if (d.ID != id) continue;
|
||||
|
||||
d.PosXY.X = (int)pos.X;
|
||||
d.PosXY.Y = MainArea.Height - (int)pos.Y;
|
||||
d.PosXY.X = MainArea.Width / 2 + (int)pos.Y;
|
||||
d.PosXY.Y = MainArea.Height / 2 - (int)pos.X;
|
||||
d.Height = (int)pos.Z;
|
||||
|
||||
d.TiltXY.X = tilt.X * TO_RADI;
|
||||
d.TiltXY.Y = tilt.Y * TO_RADI;
|
||||
d.Azimuth = (int)tilt.Z;
|
||||
d.Quaternion = quaternion;
|
||||
|
||||
break;
|
||||
}
|
||||
|
Reference in New Issue
Block a user