Files
ParkingRobot/ClumsyPilot/Movements.cs
T

128 lines
3.7 KiB
C#
Raw Normal View History

2026-07-21 17:38:16 +08:00
using ClumsyCore;
2026-07-21 11:11:01 +08:00
using ClumsyCore.DTools;
using ClumsyCore.Interfaces;
using ClumsyCore.Pilot;
using CommonUsage.Chassis;
using MDCSToolBox.Clumsy.Movements;
using MDCSToolBox.Clumsy.Tracks;
using MDCSToolBox.Commons.Controllers;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Numerics;
using System.Threading;
namespace MultiWheelC
{
2026-07-21 17:38:16 +08:00
public class DstTracker : MovementDefinition
2026-07-21 11:11:01 +08:00
{
2026-07-21 17:38:16 +08:00
public Vector2 Src;
public Vector2 Dst;
public float CarDirectionBias = 0f;
public Painter Painter = UI.GetPainter("DstTracker");
2026-07-21 11:11:01 +08:00
public override IEnumerable<bool> Get()
{
2026-07-21 17:38:16 +08:00
var chassis = (MultiWheelChassis)PilotDefinition.Chassis;
DriveTask task = null;
try
{
Console.WriteLine($"DstTracker src:({Src.X:F2}, {Src.Y:F2}) dst:({Dst.X:F2}, {Dst.Y:F2})");
Painter.DrawLine(Color.Cyan, Src.X, Src.Y, Dst.X, Dst.Y, width: 3);
2026-07-21 11:11:01 +08:00
2026-07-21 17:38:16 +08:00
var tracker = new ChassisController().Get();
var linePath = new LineTrack(Src, Dst)
{
CarDirectionBias = CarDirectionBias,
Speed = PilotDefinition.Conf.DstTrackerMaxSpeed
};
tracker.AddTrack(linePath);
task = new DriveTask(tracker.Track());
task.Wait();
yield return false;
}
finally
2026-07-21 11:11:01 +08:00
{
2026-07-21 17:38:16 +08:00
task?.Stop();
chassis.SendXYThSpeed(0f, 0f, 0f);
2026-07-21 11:11:01 +08:00
}
}
2026-07-21 17:38:16 +08:00
}
2026-07-21 11:11:01 +08:00
2026-07-21 17:38:16 +08:00
public class Sleep : MovementDefinition
2026-07-21 11:11:01 +08:00
{
2026-07-21 17:38:16 +08:00
public float Second = 2f;
2026-07-21 11:11:01 +08:00
public override IEnumerable<bool> Get()
{
2026-07-21 17:38:16 +08:00
if (Second <= 0)
2026-07-21 11:11:01 +08:00
{
2026-07-21 17:38:16 +08:00
yield return false;
yield break;
2026-07-21 11:11:01 +08:00
}
2026-07-21 17:38:16 +08:00
var endTime = DateTime.UtcNow.AddSeconds(Second);
while (DateTime.UtcNow < endTime)
2026-07-21 11:11:01 +08:00
{
2026-07-21 17:38:16 +08:00
Thread.Sleep(50);
2026-07-21 11:11:01 +08:00
yield return true;
}
yield return false;
}
}
2026-07-21 17:38:16 +08:00
public class MultiWheelRotateInPlace : MovementDefinition
2026-07-21 11:11:01 +08:00
{
2026-07-21 17:38:16 +08:00
/// <summary>
/// 旋转目标角度
/// </summary>
public float AngleTarget;
2026-07-21 11:11:01 +08:00
2026-07-21 17:38:16 +08:00
public float MaxSpeed;
2026-07-21 11:11:01 +08:00
2026-07-21 17:38:16 +08:00
public Func<float> ThetaReader = () => (float)DetourInterface.getCartLocation().th;
2026-07-21 11:11:01 +08:00
2026-07-21 17:38:16 +08:00
public MultiWheelChassis Chassis = (MultiWheelChassis)PilotDefinition.Chassis;
2026-07-21 11:11:01 +08:00
2026-07-21 17:38:16 +08:00
public Func<PIDParams> PidparamsRead = () => new PIDParams() { };
2026-07-21 11:11:01 +08:00
2026-07-21 17:38:16 +08:00
public PIDController thPid;
2026-07-21 11:11:01 +08:00
2026-07-21 17:38:16 +08:00
// 将角度归一化到零到三百六十度范围内。
private static float RangeAngle(float theta)
{
return (float)(theta - Math.Round(theta / 360.0f) * 360);
2026-07-21 11:11:01 +08:00
}
2026-07-21 17:38:16 +08:00
// 使用 PID 控制原地旋转到目标角度。
2026-07-21 11:11:01 +08:00
public override IEnumerable<bool> Get()
{
2026-07-21 17:38:16 +08:00
try
2026-07-21 11:11:01 +08:00
{
2026-07-21 17:38:16 +08:00
var targetAngle = RangeAngle(AngleTarget);
var p = PidparamsRead();
thPid = new PIDController(ThetaReader, p.Kp);
thPid.ChangeParameters(p.Kp, p.Ki, p.Kd, p.MaxI, p.DeadZone,
p.OutputUpperThreshold, p.SpeedAccPerSec);
while (true)
2026-07-21 11:11:01 +08:00
{
2026-07-21 17:38:16 +08:00
var s = thPid.GetResponse(targetAngle, true);
Console.WriteLine($"s:{s} AngleTarget:{AngleTarget}");
Chassis.SendXYThSpeed(0, 0, s);
if (thPid.IsArrived()) break;
2026-07-21 11:11:01 +08:00
yield return true;
}
2026-07-21 17:38:16 +08:00
Console.WriteLine($"final rotate to {targetAngle}");
2026-07-21 11:11:01 +08:00
}
2026-07-21 17:38:16 +08:00
finally
2026-07-21 11:11:01 +08:00
{
2026-07-21 17:38:16 +08:00
Chassis.SendXYThSpeed(0, 0, 0);
2026-07-21 11:11:01 +08:00
}
}
}
}