Files
Tutorial/MultiWheelM/Remote.cs
T
2026-06-19 23:12:05 +08:00

71 lines
2.4 KiB
C#

using CartActivator;
namespace MultiWheelM;
public class Remote : ManualController<CartDefinition>
{
[AsControlItem(name = "速度摇杆", keyboard = "Up,Down,Left,Right", joystick = "Axis0,Axis1")]
public Stick SpeedPad;
[AsControlItem(name = "速度比例", keyboard = "Q,W")]
public Throttle SpeedRatio;
[AsControlItem(name = "旋转模式", keyboard = "R")]
public Switch SpinMode;
[AsControlItem(name = "横移模式", keyboard = "C")]
public Switch CrabMode;
[AsControlItem(name = "斜行模式", keyboard = "S")]
public Switch SwayMode;
[AsControlItem(name = "车头方向", keyboard = "A,D")]
public DThrottle FrontDirection;
public override void Operation()
{
var speedThreshold = Math.Clamp(SpeedRatio.val, 0, 1) * Math.Max(0, cart.MaxManualSpeed);
var speed = Math.Clamp(SpeedPad.y, -1, 1) * speedThreshold;
var steerInput = Math.Clamp(SpeedPad.x, -1, 1);
var steering = (float)Math.Pow(Math.Abs(steerInput), cart.ManualThetaPow) * Math.Sign(steerInput);
var frontTh = -steering * cart.MaxManualTheta;
var rearTh = steering * cart.MaxManualTheta;
var frontDirection = Math.Clamp(FrontDirection.dval, -1, 1) * 180;
if (SpinMode.on)
{
var direction = steerInput == 0 ? Math.Sign(speed) : steerInput;
cart.Chassis.SendRotateMotion(direction * cart.MaxManualAngularSpeed * Math.Clamp(SpeedRatio.val, 0, 1));
cart.ManualMode = 1;
statusText = $"spin, w={direction * cart.MaxManualAngularSpeed:0.0}deg/s";
return;
}
if (CrabMode.on)
{
cart.Chassis.SetOriginBias(0, 0, 90);
cart.Chassis.SendMotion(speed, frontTh, rearTh);
cart.ManualMode = 2;
statusText = $"crab, v={speed:0.000}, front={frontTh:0.0}, rear={rearTh:0.0}";
return;
}
if (SwayMode.on)
{
cart.Chassis.SetOriginBias(0, 0, frontDirection);
cart.Chassis.SendMotion(speed, frontTh, -rearTh);
cart.ManualMode = 3;
statusText = $"sway, v={speed:0.000}, front={frontTh:0.0}, rear={-rearTh:0.0}";
return;
}
cart.Chassis.SetOriginBias(0, 0, frontDirection);
cart.Chassis.SendMotion(speed, frontTh, rearTh);
cart.ManualMode = 0;
statusText = $"normal, v={speed:0.000}, front={frontTh:0.0}, rear={rearTh:0.0}";
}
}