Compare commits

27 Commits
main ... sergey

Author SHA1 Message Date
bb32111f29 Mag active 2025-07-26 02:35:15 +03:00
65011e65ee add WMM magnetometer 2025-07-26 00:51:14 +03:00
d3c3b013ff incorrect rotation direction 2025-07-25 23:40:06 +03:00
bee4b7fc75 fix axis direction in render 2025-07-25 23:28:27 +03:00
c0a5c1c349 new drone render method 2025-07-25 22:59:52 +03:00
3362cf8262 Update FormMain.Designer.cs 2025-07-22 17:30:42 +03:00
d1db427915 --- 2025-07-22 00:15:01 +03:00
565209d6b9 Update Drone.cs
Димончик нашёл ошибочку, которую поправили. Хвала всемогущему Димончику!!!
2025-07-21 23:28:10 +03:00
446e45d9df Update Drone.cs 2025-07-18 20:46:22 +03:00
9061794bf1 +++ 2025-07-17 23:10:22 +03:00
d8ed10b4be +++ 2025-07-17 02:57:50 +03:00
fb24fe0f7b Update Drone.cs 2025-07-17 00:19:54 +03:00
771440f642 +++ 2025-07-15 02:16:03 +03:00
f6cdc57e7b Update Drone.cs 2025-07-11 20:24:42 +03:00
518c19fd2b Update RealMode.cs 2025-07-08 17:57:11 +03:00
b8b3140cea Update RealMode.cs 2025-07-08 15:08:16 +03:00
e6108e40b1 +++ 2025-07-04 20:45:19 +03:00
28965a3609 Update Drone.cs 2025-07-03 23:44:35 +03:00
35ab5b2f4e Update Drone.cs 2025-07-03 01:34:37 +03:00
5357dd69fc Update Drone.cs 2025-07-03 01:32:11 +03:00
87c1210b4b Update RealMode.cs 2025-06-30 19:27:59 +03:00
9289a9ca99 Update RealMode.cs 2025-06-30 19:09:50 +03:00
5a2f0cc807 Update Drone.cs 2025-06-30 11:56:40 +03:00
3ce096ba33 Update Drone.cs 2025-06-30 11:37:54 +03:00
edc86c0106 Update Drone.cs 2025-06-30 00:40:10 +03:00
bf389cdf06 +++ 2025-06-28 04:19:45 +03:00
782d24a8e6 +++ 2025-06-28 02:42:27 +03:00
7 changed files with 1208 additions and 843 deletions

View File

@ -1,8 +1,8 @@
using System.Collections.Concurrent; using System.Diagnostics;
using System.Diagnostics; using System.Net.Sockets;
using System.Numerics; using System.Numerics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using static System.Net.Mime.MediaTypeNames; using static VisualData.VisualDrone;
namespace DroneSimulator namespace DroneSimulator
{ {
@ -10,10 +10,12 @@ namespace DroneSimulator
{ {
public int ID; public int ID;
public bool Active; // Живой? private Socket? Client = null;
public bool Active = false; // Живой?
public const float Dynamic = 10; // Динамика вращения public const float Dynamic = 10; // Динамика вращения
public Vector3 PosXYZ, SpdXYZ, AccXYZ; // Положение в пространстве: Позиция, Скорость, Ускорение public Vector3 PosXYZ = Vector3.Zero, SpdXYZ = Vector3.Zero, AccXYZ = Vector3.Zero; // Положение в пространстве: Позиция, Скорость, Ускорение
public Quaternion Quat; // Основной кватернион public Quaternion Quat = Quaternion.Identity; // Основной кватернион
public float Power = 0; // Тяга всех двигателей (0-1) public float Power = 0; // Тяга всех двигателей (0-1)
public Vector3 SpdPRY, AccPRY; // Поворот в пространстве: pitch roll yaw public Vector3 SpdPRY, AccPRY; // Поворот в пространстве: pitch roll yaw
@ -23,8 +25,6 @@ namespace DroneSimulator
public Vector4 Orientation; public Vector4 Orientation;
private uint DataTimer;
private const float Gravity = 9.8f; private const float Gravity = 9.8f;
private const float TO_GRAD = 180 / MathF.PI; private const float TO_GRAD = 180 / MathF.PI;
@ -32,15 +32,16 @@ namespace DroneSimulator
public static List<Drone> AllDrones = new List<Drone>(); public static List<Drone> AllDrones = new List<Drone>();
private static Thread? DroneThread = null; private static Thread? DroneThread = null;
public static long Timing = 0; private uint StepTime = 0;
public static long Lag = 0;
public static long Freq = 1000; public static Semaphore StepSemaphore = new Semaphore(0, 10);
public static bool Boost = false;
private uint Timer; private uint Timer;
public static bool TimeLimit = false;
private Vector2 MoveOF = Vector2.Zero; private Vector2 MoveOF = Vector2.Zero;
private uint CountOF = 0; private bool ReadyOF = false;
public struct Physics public struct Physics
{ {
@ -49,12 +50,19 @@ namespace DroneSimulator
static public float MaxPower; // Максимальная Тяга всех двигателей (КГ) static public float MaxPower; // Максимальная Тяга всех двигателей (КГ)
} }
public struct Propeller
{
static public float Diameter; // Диаметр лопостей
static public float MaxRotate; // Максимальнные обороты в секунду
}
private RealMode.Accelerometer RealAcc = new RealMode.Accelerometer(); private RealMode.Accelerometer RealAcc = new RealMode.Accelerometer();
private RealMode.Gyroscope RealGyr = new RealMode.Gyroscope(); private RealMode.Gyroscope RealGyr = new RealMode.Gyroscope();
private RealMode.Position RealPos = new RealMode.Position(); private RealMode.Position RealPos = new RealMode.Position();
private RealMode.Barometer RealBar = new RealMode.Barometer(); private RealMode.Barometer RealBar = new RealMode.Barometer();
private RealMode.Range RealRange = new RealMode.Range(); private RealMode.Range RealRange = new RealMode.Range();
private RealMode.OpticalFlow RealOF = new RealMode.OpticalFlow(); private RealMode.OpticalFlow RealOF = new RealMode.OpticalFlow();
private RealMode.Magnetometer RealMag = new RealMode.Magnetometer();
public static byte[] getBytes(object data) public static byte[] getBytes(object data)
{ {
@ -139,42 +147,35 @@ namespace DroneSimulator
private static void ThreadFunction() private static void ThreadFunction()
{ {
long last = Stopwatch.GetTimestamp();
long prev = Stopwatch.GetTimestamp();
while (DroneThread != null) while (DroneThread != null)
{ {
if(!Boost) Thread.Yield(); if (StepSemaphore.WaitOne(1))
{
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) lock (AllDrones)
{
foreach (Drone drone in AllDrones) foreach (Drone drone in AllDrones)
drone.Action((uint)(tick / (Stopwatch.Frequency / 1000))); {
if (drone.StepTime > 0)
{
uint prev = drone.Timer;
uint next = prev + drone.StepTime;
drone.Action(next);
drone.StepTime = 0;
drone.SendStep(next, prev);
break;
}
}
}
}
} }
} }
public Drone(int id) public Drone(int id, Socket? client)
{ {
ID = id; ID = id;
Active = false; Timer = 0;
PosXYZ = Vector3.Zero; Client = client;
SpdXYZ = Vector3.Zero;
AccXYZ = Vector3.Zero;
Quat = Quaternion.Identity;
} }
public int Create() public int Create()
@ -213,6 +214,17 @@ namespace DroneSimulator
Quat = Quaternion.Normalize(map); 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() public Vector4 GetOrientation()
{ {
Quaternion grav = new Quaternion(0, 0, 1, 0); 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); 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) public void Action(uint tick)
{ {
uint period = tick - Timer; uint period = tick - Timer;
if (period <= 0) return; if (period == 0) return;
float time = period / 1000.0f; float time = period / 1000.0f;
Timer = tick; Timer = tick;
@ -325,7 +340,7 @@ namespace DroneSimulator
}*/ }*/
else else
{ {
if (ori.W < 0) if (ori.W < 0) // это обман
{ {
//Active = false; // Перевернулся вверх ногами //Active = false; // Перевернулся вверх ногами
} }
@ -349,16 +364,23 @@ namespace DroneSimulator
RealBar.Update(PosXYZ.Z, tick); RealBar.Update(PosXYZ.Z, tick);
RealPos.Update(PosXYZ, 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; MoveOF += RealOF.result * time;
CountOF += 1;
if (of) ReadyOF = true;
} }
DataTimer = tick;
} }
private float Range(float pow) private float Range(float pow)
@ -373,6 +395,13 @@ namespace DroneSimulator
{ {
ul = Range(ul); ur = Range(ur); dl = Range(dl); dr = Range(dr); 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; Power = (ul + ur + dl + dr) / 4;
AccPRY.Y = ((ul + dl) - (ur + dr)); AccPRY.Y = ((ul + dl) - (ur + dr));
@ -380,6 +409,32 @@ namespace DroneSimulator
AccPRY.Z = ((ul + dr) - (dl + ur)) / 4; 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) private void RecvDataMotor4(byte[] data)
{ {
DroneData.DataMotor4 mot = (DroneData.DataMotor4)fromBytes(data, typeof(DroneData.DataMotor4)); 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.Size = Marshal.SizeOf(typeof(DroneData.DataAcc));
acc.Head.Mode = DroneData.DataMode.Response; acc.Head.Mode = DroneData.DataMode.Response;
acc.Head.Type = DroneData.DataType.DataAcc; 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.Acc.X = RealAcc.result.X; acc.Acc.Y = RealAcc.result.Y; acc.Acc.Z = RealAcc.result.Z;
acc.Time = RealAcc.timer; acc.Time = RealAcc.timer;
@ -409,7 +464,7 @@ namespace DroneSimulator
gyr.Head.Size = Marshal.SizeOf(typeof(DroneData.DataGyr)); gyr.Head.Size = Marshal.SizeOf(typeof(DroneData.DataGyr));
gyr.Head.Mode = DroneData.DataMode.Response; gyr.Head.Mode = DroneData.DataMode.Response;
gyr.Head.Type = DroneData.DataType.DataGyr; 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.Gyr.X = RealGyr.result.X; gyr.Gyr.Y = RealGyr.result.Y; gyr.Gyr.Z = RealGyr.result.Z;
gyr.Time = RealGyr.timer; gyr.Time = RealGyr.timer;
@ -424,10 +479,10 @@ namespace DroneSimulator
mag.Head.Size = Marshal.SizeOf(typeof(DroneData.DataMag)); mag.Head.Size = Marshal.SizeOf(typeof(DroneData.DataMag));
mag.Head.Mode = DroneData.DataMode.Response; mag.Head.Mode = DroneData.DataMode.Response;
mag.Head.Type = DroneData.DataType.DataMag; 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.Mag.X = RealMag.result.X; mag.Mag.Y = RealMag.result.Y; mag.Mag.Z = RealMag.result.Z;
mag.Time = DataTimer; mag.Time = RealMag.timer;
return getBytes(mag); return getBytes(mag);
} }
@ -439,7 +494,7 @@ namespace DroneSimulator
range.Head.Size = Marshal.SizeOf(typeof(DroneData.DataRange)); range.Head.Size = Marshal.SizeOf(typeof(DroneData.DataRange));
range.Head.Mode = DroneData.DataMode.Response; range.Head.Mode = DroneData.DataMode.Response;
range.Head.Type = DroneData.DataType.DataRange; 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.LiDAR = RealRange.result;
range.Time = RealRange.timer; range.Time = RealRange.timer;
@ -454,7 +509,7 @@ namespace DroneSimulator
local.Head.Size = Marshal.SizeOf(typeof(DroneData.DataLocal)); local.Head.Size = Marshal.SizeOf(typeof(DroneData.DataLocal));
local.Head.Mode = DroneData.DataMode.Response; local.Head.Mode = DroneData.DataMode.Response;
local.Head.Type = DroneData.DataType.DataLocal; 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.Local.X = RealPos.result.X; local.Local.Y = RealPos.result.Y; local.Local.Z = RealPos.result.Z;
local.Time = RealPos.timer; local.Time = RealPos.timer;
@ -469,7 +524,7 @@ namespace DroneSimulator
bar.Head.Size = Marshal.SizeOf(typeof(DroneData.DataBar)); bar.Head.Size = Marshal.SizeOf(typeof(DroneData.DataBar));
bar.Head.Mode = DroneData.DataMode.Response; bar.Head.Mode = DroneData.DataMode.Response;
bar.Head.Type = DroneData.DataType.DataBar; 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.Pressure = RealBar.result;
bar.Time = RealBar.timer; bar.Time = RealBar.timer;
@ -484,16 +539,25 @@ namespace DroneSimulator
of.Head.Size = Marshal.SizeOf(typeof(DroneData.DataOF)); of.Head.Size = Marshal.SizeOf(typeof(DroneData.DataOF));
of.Head.Mode = DroneData.DataMode.Response; of.Head.Mode = DroneData.DataMode.Response;
of.Head.Type = DroneData.DataType.DataOF; 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) lock (this)
{ {
of.X = MoveOF.X / CountOF; if (ReadyOF)
of.Y = MoveOF.Y / CountOF; {
of.X = MoveOF.X;
of.Y = MoveOF.Y;
MoveOF = Vector2.Zero;
}
else
{
of.X = 0;
of.Y = 0;
}
of.Time = RealOF.timer; of.Time = RealOF.timer;
MoveOF = Vector2.Zero; ReadyOF = false;
CountOF = 0;
} }
return getBytes(of); return getBytes(of);
@ -506,7 +570,7 @@ namespace DroneSimulator
gps.Head.Size = Marshal.SizeOf(typeof(DroneData.DataGPS)); gps.Head.Size = Marshal.SizeOf(typeof(DroneData.DataGPS));
gps.Head.Mode = DroneData.DataMode.Response; gps.Head.Mode = DroneData.DataMode.Response;
gps.Head.Type = DroneData.DataType.DataGPS; 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(); GPS.Point p = new GPS.Point();
p.x = RealPos.result.Y; p.y= RealPos.result.X; 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.Size = Marshal.SizeOf(typeof(DroneData.DataQuat));
quat.Head.Mode = DroneData.DataMode.Response; quat.Head.Mode = DroneData.DataMode.Response;
quat.Head.Type = DroneData.DataType.DataQuat; 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; 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.Size = Marshal.SizeOf(typeof(DroneData.DataHead));
head.Mode = DroneData.DataMode.Response; head.Mode = DroneData.DataMode.Response;
head.Type = DroneData.DataType.None; head.Type = DroneData.DataType.Ping;
head.Time = (uint)(Stopwatch.GetTimestamp() / (Stopwatch.Frequency / 1000)); head.Time = (uint)(DateTime.Now.Ticks / Stopwatch.Frequency / 1000);
return getBytes(head); return getBytes(head);
} }
@ -569,6 +633,8 @@ namespace DroneSimulator
switch (head.Type) switch (head.Type)
{ {
case DroneData.DataType.Step: if (head.Mode == DroneData.DataMode.Request) RecvStep(body); return zero;
case DroneData.DataType.DataAcc: case DroneData.DataType.DataAcc:
{ {
if (head.Mode == DroneData.DataMode.Request) 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.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; return zero;
@ -612,7 +678,7 @@ namespace DroneSimulator
private const int DroneStreamCount = 512; private const int DroneStreamCount = 512;
private byte[] DroneStreamData = new byte[DroneStreamCount]; private byte[] DroneStreamData = new byte[DroneStreamCount];
private int DroneStreamIndex = 0; 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) public List<byte[]?>? DataStream(byte[]? data, int size)
{ {
@ -633,6 +699,8 @@ namespace DroneSimulator
DroneStreamHead = (DroneData.DataHead)fromBytes(DroneStreamData, typeof(DroneData.DataHead)); DroneStreamHead = (DroneData.DataHead)fromBytes(DroneStreamData, typeof(DroneData.DataHead));
} }
if (DroneStreamHead.Size == 0) return null; // Поток сломан (конец)
if (DroneStreamHead.Size > DroneStreamIndex) break; // Пакет ещё не полный if (DroneStreamHead.Size > DroneStreamIndex) break; // Пакет ещё не полный
byte[] body = new byte[DroneStreamHead.Size]; byte[] body = new byte[DroneStreamHead.Size];

View File

@ -1,5 +1,4 @@
using System.Net; using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
namespace DroneData namespace DroneData
{ {
@ -10,7 +9,7 @@ namespace DroneData
public enum DataType : ushort public enum DataType : ushort
{ {
None = 0, Head = 1, None = 0, Ping = 1, Step = 2,
// Output // Output
DataAcc = 1001, DataGyr = 1002, DataMag = 1003, DataRange = 1004, DataLocal = 1005, DataBar = 1006, DataOF = 1007, DataGPS = 1008, 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 DataMode Mode;
public DataType Type; public DataType Type;
public uint Time; // Общее время public uint Time; // ответ: Общее время на симуляторе
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataHead)); 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 XYZ { public float X, Y, Z; }
public struct DataAcc public struct DataAcc
{ {
public DataHead Head; public DataHead Head;
public XYZ Acc; public XYZ Acc; // G, ускорения по осям
public uint Time; // Последнее время изменения данных public uint Time; // Последнее время изменения данных
@ -49,7 +58,7 @@ namespace DroneData
public struct DataGyr public struct DataGyr
{ {
public DataHead Head; public DataHead Head;
public XYZ Gyr; public XYZ Gyr; // dps, угловые скорости
public uint Time; // Последнее время изменения данных public uint Time; // Последнее время изменения данных
@ -69,7 +78,7 @@ namespace DroneData
public struct DataRange public struct DataRange
{ {
public DataHead Head; public DataHead Head;
public float LiDAR; // Датчик посадки public float LiDAR; // m, Датчик посадки
public uint Time; // Последнее время изменения данных public uint Time; // Последнее время изменения данных
@ -79,7 +88,7 @@ namespace DroneData
public struct DataLocal public struct DataLocal
{ {
public DataHead Head; public DataHead Head;
public XYZ Local; // Локальные координаты public XYZ Local; // m, Локальные координаты
public uint Time; // Последнее время изменения данных public uint Time; // Последнее время изменения данных
@ -89,7 +98,7 @@ namespace DroneData
public struct DataBar public struct DataBar
{ {
public DataHead Head; public DataHead Head;
public float Pressure; // Давление public float Pressure; // Pa, Давление
public uint Time; // Последнее время изменения данных public uint Time; // Последнее время изменения данных
@ -99,7 +108,7 @@ namespace DroneData
public struct DataOF public struct DataOF
{ {
public DataHead Head; public DataHead Head;
public float X, Y; // Угловой сдвиг public float X, Y; // degree, Угловой сдвиг
public uint Time; // Последнее время изменения данных public uint Time; // Последнее время изменения данных
@ -129,7 +138,7 @@ namespace DroneData
public struct DataMotor4 public struct DataMotor4
{ {
public DataHead Head; 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)); static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataMotor4));
} }
@ -137,7 +146,7 @@ namespace DroneData
public struct DataMotor6 public struct DataMotor6
{ {
public DataHead Head; 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)); static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataMotor6));
} }
@ -145,7 +154,7 @@ namespace DroneData
public struct DataQuat public struct DataQuat
{ {
public DataHead Head; public DataHead Head;
public float X, Y, Z, W; public float X, Y, Z, W; // Кватернион дрона
static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataQuat)); static public int StrLen = Marshal.SizeOf(typeof(DroneData.DataQuat));
} }

File diff suppressed because it is too large Load Diff

View File

@ -44,7 +44,7 @@ namespace DroneSimulator
if (data.Connect) if (data.Connect)
{ {
Drone drone = new Drone(data.ID); Drone drone = new Drone(data.ID, data.Client);
drone.Create(); drone.Create();
screen2D.CreateDrone(Color.Red, data.ID); screen2D.CreateDrone(Color.Red, data.ID);
@ -115,6 +115,9 @@ namespace DroneSimulator
{ {
button_Client_Start.Text = "Stop"; button_Client_Start.Text = "Stop";
button_Client_Start.BackColor = Color.LimeGreen; button_Client_Start.BackColor = Color.LimeGreen;
numericUpDown_Clients_Limit.Enabled = false;
numericUpDown_Clients_Port.Enabled = false;
checkBox_Lockstep_Limit.Enabled = false;
break; break;
} }
case NetServerClients.ServerState.Stop: case NetServerClients.ServerState.Stop:
@ -122,6 +125,9 @@ namespace DroneSimulator
label_Clients_Num.Text = "0"; label_Clients_Num.Text = "0";
button_Client_Start.Text = "Start"; button_Client_Start.Text = "Start";
button_Client_Start.BackColor = Color.Transparent; button_Client_Start.BackColor = Color.Transparent;
numericUpDown_Clients_Limit.Enabled = true;
numericUpDown_Clients_Port.Enabled = true;
checkBox_Lockstep_Limit.Enabled = true;
break; break;
} }
} }
@ -151,10 +157,9 @@ namespace DroneSimulator
private void timer_Test_Tick(object sender, EventArgs e) private void timer_Test_Tick(object sender, EventArgs e)
{ {
DateTime test = DateTime.Now; DateTime time = DateTime.Now;
int tim = test.Second + test.Minute * 100 + test.Hour * 10000;
label_Lockstep_Time.Text = time.Hour.ToString("D2") + ":" + time.Minute.ToString("D2") + ":" + time.Second.ToString("D2") + "." + time.Millisecond.ToString("D3");
if (screen2D == null) return; if (screen2D == null) return;
@ -166,7 +171,7 @@ namespace DroneSimulator
{ {
foreach (Drone d in Drone.AllDrones) 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(); 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 { } catch { }
label_Timing.Text = Drone.Timing.ToString() + " Hz";
label_Timing_Lag.Text = Drone.Lag.ToString();
screen2D.DrawScene(); screen2D.DrawScene();
} }
@ -367,10 +369,15 @@ namespace DroneSimulator
Drone.StopThread(); 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.TimeLimit = checkBox_Lockstep_Limit.Checked;
Drone.Boost = checkBox_Freq_Boost.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;
} }
} }
} }

View File

@ -1,10 +1,12 @@
using System; using Microsoft.VisualBasic.Devices;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Numerics; using System.Numerics;
using System.Reflection; using System.Reflection;
using System.Text;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
namespace DroneSimulator namespace DroneSimulator
{ {
@ -27,7 +29,7 @@ namespace DroneSimulator
private const int count = 1000; private const int count = 1000;
private Vector3[] laten = new Vector3[count]; private Vector3[] laten = new Vector3[count];
private uint index = 0; private int index = 0;
public uint timer = 0; public uint timer = 0;
public Vector3 result; public Vector3 result;
@ -53,22 +55,25 @@ namespace DroneSimulator
value.Y += ((float)rand.Next(-noise, noise)) / 1000; value.Y += ((float)rand.Next(-noise, noise)) / 1000;
value.Z += ((float)rand.Next(-noise, noise)) / 1000; value.Z += ((float)rand.Next(-noise, noise)) / 1000;
uint clock = (uint)(Lateness * 1000); uint clock = time - last;
while (true)
uint tick = time - last;
last = time;
while (tick != 0)
{ {
tick--; laten[index] = value;
laten[index++] = value; clock--;
if (index >= clock) index = 0; 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; uint freq = 1000 / Freq;
if (timer + freq < time) if (timer + freq <= time)
{ {
result = value; result = value;
timer = time; timer = time;
@ -90,7 +95,7 @@ namespace DroneSimulator
private const int count = 1000; private const int count = 1000;
private Vector3[] laten = new Vector3[count]; private Vector3[] laten = new Vector3[count];
private uint index = 0; private int index = 0;
public uint timer = 0; public uint timer = 0;
public Vector3 result; public Vector3 result;
@ -113,22 +118,25 @@ namespace DroneSimulator
value.Y += ((float)rand.Next(-noise, noise)) / 1000; value.Y += ((float)rand.Next(-noise, noise)) / 1000;
value.Z += ((float)rand.Next(-noise, noise)) / 1000; value.Z += ((float)rand.Next(-noise, noise)) / 1000;
uint clock = (uint)(Lateness * 1000); uint clock = time - last;
while (true)
uint tick = time - last;
last = time;
while (tick != 0)
{ {
tick--; laten[index] = value;
laten[index++] = value; clock--;
if (index >= clock) index = 0; 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; uint freq = 1000 / Freq;
if (timer + freq < time) if (timer + freq <= time)
{ {
result = value; result = value;
timer = time; timer = time;
@ -138,7 +146,106 @@ namespace DroneSimulator
internal class Magnetometer internal class Magnetometer
{ {
/**
* The model is produced by the United States National Geospatial-Intelligence Agency (NGA)
* and the United Kingdoms 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 internal class Position
@ -155,16 +262,35 @@ namespace DroneSimulator
private const int count = 1000; private const int count = 1000;
private Vector3[] laten = new Vector3[count]; private Vector3[] laten = new Vector3[count];
private uint index = 0; private int index = 0;
public uint timer = 0; public uint timer = 0;
public Vector3 result; public Vector3 result;
public void Update(Vector3 value, uint time) 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) if (!Enable)
{ {
result = Vector3.NaN; result = Vector3.NaN;
timer = time;
return; return;
} }
@ -175,29 +301,15 @@ namespace DroneSimulator
return; return;
} }
int noise = (int)(Noise * 1000); int move = (int)(Lateness * count);
value.X += ((float)rand.Next(-noise, noise)) / 1000; move = index - move;
value.Y += ((float)rand.Next(-noise, noise)) / 1000; while (move < 0) move += count;
value.Z += ((float)rand.Next(-noise, noise)) / 1000; v = laten[move];
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; uint freq = 1000 / Freq;
if (timer + freq <= time)
if (timer + freq < time)
{ {
result = value; result = v;
timer = time; timer = time;
} }
} }
@ -211,6 +323,7 @@ namespace DroneSimulator
public static float Noise; public static float Noise;
public static float Lateness; public static float Lateness;
public static bool RealSimulation; public static bool RealSimulation;
public static float Temperature = 25.0f;
private uint last = 0; private uint last = 0;
@ -218,18 +331,35 @@ namespace DroneSimulator
private const int count = 1000; private const int count = 1000;
private float[] laten = new float[count]; private float[] laten = new float[count];
private uint index = 0; private int index = 0;
public uint timer = 0; public uint timer = 0;
public float result; public float result;
public void Update(float value, uint time) 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) if (!Enable)
{ {
result = float.NaN; result = float.NaN;
timer = time;
return; return;
} }
@ -240,27 +370,15 @@ namespace DroneSimulator
return; return;
} }
int noise = (int)(Noise * 1000); int move = (int)(Lateness * count);
value += ((float)rand.Next(-noise, noise)) / 1000; move = index - move;
while (move < 0) move += count;
uint clock = (uint)(Lateness * 1000); v = laten[move];
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; uint freq = 1000 / Freq;
if (timer + freq <= time)
if (timer + freq < time)
{ {
result = value; result = v;
timer = time; timer = time;
} }
} }
@ -282,7 +400,7 @@ namespace DroneSimulator
private const int count = 1000; private const int count = 1000;
private Vector2[] laten = new Vector2[count]; private Vector2[] laten = new Vector2[count];
private uint index = 0; private int index = 0;
public uint delay = 0; public uint delay = 0;
@ -292,9 +410,31 @@ namespace DroneSimulator
{ {
value *= Lens; 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) if (!Enable)
{ {
result = Vector2.NaN; result = Vector2.NaN;
timer = time;
return true; return true;
} }
@ -305,32 +445,14 @@ namespace DroneSimulator
return true; return true;
} }
if (Range > MaxHeight) value = Vector2.Zero; int move = (int)(Lateness * count);
else 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; timer = time;
return true; return true;
} }
@ -354,16 +476,37 @@ namespace DroneSimulator
private const int count = 1000; private const int count = 1000;
private float[] laten = new float[count]; private float[] laten = new float[count];
private uint index = 0; private int index = 0;
public uint timer = 0; public uint timer = 0;
public float result; public float result;
public void Update(float value, uint time) 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) if (!Enable)
{ {
result = float.NaN; result = float.NaN;
timer = time;
return; return;
} }
@ -374,31 +517,15 @@ namespace DroneSimulator
return; return;
} }
if (value > MaxHeight) value = MaxHeight; int move = (int)(Lateness * count);
else move = index - move;
{ while (move < 0) move += count;
int noise = (int)(Noise * 1000); v = laten[move];
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; uint freq = 1000 / Freq;
if (timer + freq <= time)
if (timer + freq < time)
{ {
result = value; result = v;
timer = time; timer = time;
} }
} }

View File

@ -1,4 +1,5 @@
using System.Numerics; using System.Drawing.Drawing2D;
using System.Numerics;
namespace DroneSimulator namespace DroneSimulator
{ {
@ -22,6 +23,7 @@ namespace DroneSimulator
public PointF TiltXY = new Point(0, 0); public PointF TiltXY = new Point(0, 0);
public int Azimuth = 0; public int Azimuth = 0;
public Quaternion Quaternion;
} }
private float Scale = 100; private float Scale = 100;
@ -96,6 +98,47 @@ namespace DroneSimulator
} }
} }
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() public void DrawScene()
{ {
using (Graphics g = Graphics.FromImage(MainArea)) using (Graphics g = Graphics.FromImage(MainArea))
@ -109,50 +152,15 @@ namespace DroneSimulator
try try
{ {
if (d.Azimuth >= 360) d.Azimuth -= 360; 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.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.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 { } catch { }
} }
@ -161,27 +169,25 @@ namespace DroneSimulator
drawCallback(MainArea); 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_GRAD = 180 / MathF.PI;
const float TO_RADI = MathF.PI / 180; const float TO_RADI = MathF.PI / 180;
pos *= Scale; pos *= Scale;
pos.X += MainArea.Width / 2;
pos.Y += MainArea.Height / 2;
foreach (var d in DroneList) foreach (var d in DroneList)
{ {
if (d.ID != id) continue; if (d.ID != id) continue;
d.PosXY.X = (int)pos.X; d.PosXY.X = MainArea.Width / 2 + (int)pos.Y;
d.PosXY.Y = MainArea.Height - (int)pos.Y; d.PosXY.Y = MainArea.Height / 2 - (int)pos.X;
d.Height = (int)pos.Z; d.Height = (int)pos.Z;
d.TiltXY.X = tilt.X * TO_RADI; d.TiltXY.X = tilt.X * TO_RADI;
d.TiltXY.Y = tilt.Y * TO_RADI; d.TiltXY.Y = tilt.Y * TO_RADI;
d.Azimuth = (int)tilt.Z; d.Azimuth = (int)tilt.Z;
d.Quaternion = quaternion;
break; break;
} }