Browse Source

fixed track control

main
jkpete 4 weeks ago
parent
commit
23c61600de
  1. 197
      Components/Pages/Control.razor
  2. 3
      Script/Model/ModelControlServer.cs
  3. 76
      Script/Model/ModelSerialTest.cs
  4. 33
      Script/Protocol/RequestTrackEngine.cs

197
Components/Pages/Control.razor

@ -5,93 +5,13 @@ @@ -5,93 +5,13 @@
@implements IEGFramework
@rendermode InteractiveServer
<PageTitle>空轨控制</PageTitle>
<PageTitle>靶机控制</PageTitle>
<h1>空轨控制</h1>
<h1>靶机控制</h1>
<div class="control-panel">
<div class="status-display">
<p>当前位置: <strong>@dataActionStatus.Position</strong> / 25</p>
<p>旋转角度: <strong>@dataActionStatus.Rotate</strong>°</p>
<p>当前定位点: <strong>@(currentLocationName ?? "无")</strong></p>
</div>
<div class="control-section">
<h3>位置控制</h3>
<div class="slider-container">
<div class="position-markers">
@foreach (var marker in locationMarkers)
{
<div class="position-marker"
style="left: @((marker.Key / 25.0) * 100)%;
background-color: @GetMarkerColor(marker.Value)"
title="@marker.Key - @GetLocationName(marker.Key)">
</div>
}
</div>
<input type="range"
class="form-range location-slider"
min="0"
max="25"
step="1"
value="@dataActionStatus.Position"
@oninput="OnPositionSliderInput"
disabled="@isMoving" />
<span class="slider-value">@dataActionStatus.Position</span>
</div>
<button class="btn btn-secondary mt-2" @onclick="MoveToPosition" disabled="@isMoving">
@(isMoving ? "移动中..." : "开始移动")
</button>
<br />
<button class="btn btn-secondary mt-2" @onclick="@ModelTrackControl.EnableTrack" >启用通讯
</button>
<button class="btn btn-secondary mt-2" @onclick="@ModelTrackControl.StartTrack" >前进
</button>
<button class="btn btn-secondary mt-2" @onclick="@ModelTrackControl.BackTrack" >后退
</button>
<button class="btn btn-secondary mt-2" @onclick="@ModelTrackControl.StartSlowTrack" >缓速前进
</button>
<button class="btn btn-secondary mt-2" @onclick="@ModelTrackControl.BackSlowTrack" >缓速后退
</button>
<button class="btn btn-secondary mt-2" @onclick="@ModelTrackControl.StopTrack" >停止
</button>
<button class="btn btn-secondary mt-2" @onclick="@ModelTrackControl.BreakTrack" >刹车
</button>
<br />
<button class="btn btn-secondary mt-2" @onclick="StartScanColor" >开启扫描
</button>
<button class="btn btn-secondary mt-2" @onclick="@ModelTrackControl.StopScanColor" >结束扫描
</button>
</div>
<div class="control-section">
<h3>快速定位</h3>
<div class="location-buttons">
@foreach (var location in locationMarkers)
{
<button class="btn location-btn"
style="background-color: @GetMarkerColor(location.Value); color: white"
@onclick="e => MoveToLocation(location.Key)"
disabled="@isMoving">
@GetLocationName(location.Key)
</button>
}
</div>
</div>
<div class="control-section">
<h3>颜色显示</h3>
<div class="color-display" style="background-color: @GetColorValue(CurrentColor)">
<span>RGB: @((int)(CurrentColor.X * 255)), @((int)(CurrentColor.Y * 255)), @((int)(CurrentColor.Z * 255))
HSV: @((int)(CurrentHSV.X)), @((int)(CurrentHSV.Y)), @((int)(CurrentHSV.Z))
</span>
</div>
</div>
<div class="control-section">
<h3>靶机控制</h3>
<button class="btn @(isTargetActive ? "btn-danger" : "btn-success")"
@onclick="ToggleTarget"
disabled="@isTargetOperationInProgress">
@ -123,92 +43,12 @@ @@ -123,92 +43,12 @@
private bool isMoving = false;
private bool isTargetActive = false;
private bool isTargetOperationInProgress = false;
private int targetPosition;
private string? currentLocationName;
private Vector3 CurrentColor = new Vector3(0.5f, 0.5f, 0.5f); // 默认灰色
private Vector3 CurrentHSV = new Vector3();
public ModelTrackControl ModelTrackControl { set; get; }
// 定义定位点字典
private Dictionary<int, Vector3> locationMarkers = new Dictionary<int, Vector3>
{
{ 0, new Vector3(0.3f, 0.8f, 0.12f) }, // 浅绿 - 起点
{ 7, new Vector3(0.1f, 0.5f, 0.2f) }, // 深绿色 - 中间点1
{ 10, new Vector3(0.0f, 0.5f, 1.0f) }, // 蓝色 - 中间点2
{ 15, new Vector3(0.5f, 0.2f, 0.75f) }, // 紫色 - 中间点3
{ 25, new Vector3(0.8f, 0.2f, 0.5f) } // 洋红色 - 终点
};
protected override void OnInitialized()
{
UpdateCurrentLocationName();
ModelTrackControl = this.GetModule<ModelTrackControl>();
}
private void OnPositionSliderInput(ChangeEventArgs e)
{
if (int.TryParse(e.Value?.ToString(), out int value))
{
// 寻找最近的定位点
var nearestLocation = locationMarkers.Keys
.OrderBy(x => Math.Abs(x - value))
.First();
targetPosition = nearestLocation;
UpdateCurrentLocationName(targetPosition);
}
}
private async Task StartScanColor(){
if(ModelTrackControl.IsScanning) return;
ModelTrackControl.StartScanColor();
StateHasChanged();
while(ModelTrackControl.IsScanning){
// 更新颜色显示
UpdateCurrentColor();
StateHasChanged();
await Task.Delay(50);
}
}
private async Task MoveToPosition()
{
if (isMoving) return;
isMoving = true;
StateHasChanged();
// 模拟移动过程
int steps = Math.Abs(targetPosition - dataActionStatus.Position);
int direction = targetPosition > dataActionStatus.Position ? 1 : -1;
while(ModelTrackControl.MoveStatus != StatusTrack.Stop)
{
if(targetPosition!=dataActionStatus.Position){
dataActionStatus.Position += direction;
}
// 更新颜色显示
UpdateCurrentColor();
StateHasChanged();
await Task.Delay(50);
}
UpdateCurrentLocationName();
isMoving = false;
}
private async Task MoveToLocation(int position)
{
targetPosition = position;
this.ModelTrackControl.MoveToPosition(position);
await MoveToPosition();
}
private async Task ToggleTarget()
{
if (isTargetOperationInProgress) return;
@ -233,39 +73,6 @@ @@ -233,39 +73,6 @@
StateHasChanged();
}
private string GetMarkerColor(Vector3 colorVector)
{
// 将Vector3转换为CSS颜色值
return $"rgb({(int)(colorVector.X * 255)}, {(int)(colorVector.Y * 255)}, {(int)(colorVector.Z * 255)})";
}
private string GetColorValue(Vector3 colorVector)
{
// 将Vector3转换为CSS颜色值
return $"rgb({(int)(colorVector.X * 255)}, {(int)(colorVector.Y * 255)}, {(int)(colorVector.Z * 255)})";
}
private string GetLocationName(int position)
{
return locationMarkers.ContainsKey(position) ?
$"定位点 {position}" :
position.ToString();
}
private void UpdateCurrentLocationName(int? position = null)
{
int pos = position ?? dataActionStatus.Position;
currentLocationName = locationMarkers.ContainsKey(pos) ?
$"定位点 {pos}" :
null;
}
private void UpdateCurrentColor()
{
// 根据传感器检测到的颜色进行更新
CurrentColor = this.ModelTrackControl.GetColor();
CurrentHSV = this.ModelTrackControl.GetColorHSV();
}
}
<style>

3
Script/Model/ModelControlServer.cs

@ -35,9 +35,6 @@ public class ModelControlServer : EGModule, IEGFramework @@ -35,9 +35,6 @@ public class ModelControlServer : EGModule, IEGFramework
Position = this.GetModule<ModelTrackControl>().CurrentPosition
}, sender, type);
return;
case (int)TypeControlFunction.TrackMove:
this.GetModule<ModelTrackControl>().MoveToPosition(data.Position);
break;
}
}
this.EGSendMessage(new RequestControl()

76
Script/Model/ModelSerialTest.cs

@ -8,17 +8,13 @@ public class ModelSerialTest : EGModule,IEGFramework @@ -8,17 +8,13 @@ public class ModelSerialTest : EGModule,IEGFramework
public const int BAUD_RATE_ENGINE = 9600;
public const int BAUD_RATE_COLOR = 38400;
public const string PORT_LINUX_0 = "/dev/ttyUSB0";
public const string PORT_LINUX_1 = "/dev/ttyUSB1";
public const string PORT_LINUX = "/dev/ttyUSB0";
public const string PORT_WIN_0 = "COM2";
public const string PORT_WIN_1 = "COM5";
public const string PORT_WIN = "COM2";
public bool IsLinux = true;
public string TestPort0 { set; get; } = PORT_LINUX_0;
public string TestPort1 { set; get; } = PORT_LINUX_1;
public bool IsLinux = false;
public string TestPort { set; get; } = PORT_LINUX;
public string ColorSerialPort { set; get; } = "";
public string EngineSerialPort { set; get; } = "";
@ -27,14 +23,11 @@ public class ModelSerialTest : EGModule,IEGFramework @@ -27,14 +23,11 @@ public class ModelSerialTest : EGModule,IEGFramework
public IUnRegister EngineInitEvent{ set; get; }
public override void Init()
{
ColorSensorInitEvent = this.EGRegisterMessageEvent<ResponseColorSensor>(OnColorSensorInit);
this.EGOnMessage<ResponseColorSensor>();
this.EGOnMessage<ResponseTrack>();
{;
this.EGOnMessage<ResponseTarget>();
if (!IsLinux)
{
TestPort0 = PORT_WIN_0;
TestPort1 = PORT_WIN_1;
TestPort = PORT_WIN;
}
InitSerialPort();
// throw new NotImplementedException();
@ -42,66 +35,25 @@ public class ModelSerialTest : EGModule,IEGFramework @@ -42,66 +35,25 @@ public class ModelSerialTest : EGModule,IEGFramework
public async void InitSerialPort()
{
this.EGSerialPort().Open(TestPort0, 38400);
this.EGSendMessage(new RequestColorSingleRead(), TestPort0, ProtocolType.SerialPort);
await Task.Delay(2000);
if(ColorSerialPort == "")
{
this.EGSerialPort().Close(TestPort0);
this.EGSerialPort().Open(TestPort1, 38400);
this.EGSendMessage(new RequestColorSingleRead(), TestPort1, ProtocolType.SerialPort);
await Task.Delay(2000);
if(ColorSerialPort == "")
{
this.EGSerialPort().Close(TestPort1);
EG.Print("Color sensor init failed,Time out.");
}
else
{
EG.Print("_"+this.EGSerialPort().SerialPortDevices.ContainsKey(TestPort0));
this.EGSerialPort().Open(TestPort0, 9600);
this.EGSendMessage(new RequestTrackReadEnabled(), TestPort0, ProtocolType.SerialPort);
EngineInitEvent = this.EGRegisterMessageEvent<ResponseTrack>(OnEngineInit);
await Task.Delay(2000);
if(EngineSerialPort == "")
{
this.EGSerialPort().Close(TestPort0);
EG.Print("Engine init failed USB0,Time out.");
}
}
}
else
{
this.EGSerialPort().Open(TestPort1, 9600);
this.EGSendMessage(new RequestTrackReadEnabled(), TestPort1, ProtocolType.SerialPort);
EngineInitEvent = this.EGRegisterMessageEvent<ResponseTrack>(OnEngineInit);
this.EGSerialPort().Open(TestPort, 9600);
this.EGSendMessage(new RequestTargetReadEnabled(), TestPort, ProtocolType.SerialPort);
EngineInitEvent = this.EGRegisterMessageEvent<ResponseTarget>(OnEngineInit);
await Task.Delay(2000);
if(EngineSerialPort == "")
{
this.EGSerialPort().Close(TestPort1);
EG.Print("Engine init failed USB1,Time out.");
}
}
}
public void OnColorSensorInit(ResponseColorSensor res, string sender, ProtocolType type)
{
if (type == ProtocolType.SerialPort)
{
ColorSensorInitEvent.UnRegister();
this.ColorSerialPort = sender;
EG.Print("Color sensor init success!");
this.EGSerialPort().Close(TestPort);
EG.Print("Engine init failed USB,Time out.");
}
}
public void OnEngineInit(ResponseTrack res,string sender,ProtocolType type)
public void OnEngineInit(ResponseTarget res,string sender,ProtocolType type)
{
if (type == ProtocolType.SerialPort)
{
EngineInitEvent.UnRegister();
this.EngineSerialPort = sender;
EG.Print("Engine sensor init success!");
this.GetModule<ModelTrackControl>().ResetTrack();
// this.GetModule<ModelTrackControl>().ResetTrack();
}
}

33
Script/Protocol/RequestTrackEngine.cs

@ -101,3 +101,36 @@ public class ResponseTrack : ModbusRTU_Response, IEGFramework @@ -101,3 +101,36 @@ public class ResponseTrack : ModbusRTU_Response, IEGFramework
return result;
}
}
public class RequestTargetReadEnabled: ModbusRTU_ReadHoldingRegisters, IEGFramework
{
public RequestTargetReadEnabled()
{
this.DeviceAddress = 0x01;
this.RegisterAddress = 0x44;
this.ReadCount = 0x01;
}
}
public class ResponseTarget : ModbusRTU_Response, IEGFramework
{
public uint ResValue { set; get; }
public override bool TrySetData(string protocolData, byte[] protocolBytes)
{
bool result = base.TrySetData(protocolData, protocolBytes);
if (!result)
{
return false;
}
if (this.HoldingRegister != null && this.HoldingRegister.Length >= 1)
{
ResValue = HoldingRegister[0];
}
else
{
return false;
}
return result;
}
}
Loading…
Cancel
Save