步进电机 挡料地

This commit is contained in:
Admin
2025-04-22 18:03:12 +08:00
parent 74051a4287
commit 33e94b92f9
13 changed files with 1378 additions and 585 deletions

View File

@ -737,7 +737,55 @@ namespace DH.Devices.PLC
// LogAsync(DateTime.Now, LogLevel.Information, $"关闭定位");
//写入流程启动点位配置
StartProcessAction();
// LogAsync(DateTime.Now, LogLevel.Information, $"写入流程加载点位配置");
PLCItem? pLCItem = ConfigModel.GlobalList?
.FirstOrDefault()?
.StartProcessList?
.Where(it => it.Name == "挡料电机回原点速度").FirstOrDefault();
if (pLCItem == null)
{
throw new Exception( $"未找到挡料电机回原点速度地址,请检查该地址是否存在于点位表!");
}
PLCItem? pLCItem1 = ConfigModel.GlobalList?
.FirstOrDefault()?
.StartProcessList?
.Where(it => it.Name == "挡料电机位置").FirstOrDefault();
if (pLCItem1 == null)
{
throw new Exception($"未找到挡料电机位置地址,请检查该地址是否存在于点位表!");
}
FeedingMotor(true, Convert.ToInt32(pLCItem.Value), Convert.ToInt32(pLCItem1.Value));
PLCItem? pLCItemmontor = ConfigModel.GlobalList?
.FirstOrDefault()?
.StartProcessList?
.Where(it => it.Name == "相机步进速度").FirstOrDefault();
if (pLCItemmontor == null)
{
throw new Exception($"未找到相机步进速度地址,请检查该地址是否存在于点位表!");
}
PLCItem? pLCItemmontor1 = ConfigModel.GlobalList?
.FirstOrDefault()?
.StartProcessList?
.Where(it => it.Name == "相机步进位置").FirstOrDefault();
if (pLCItemmontor1 == null)
{
throw new Exception($"未找到相机步进位置地址,请检查该地址是否存在于点位表!");
}
Motor(true, Convert.ToInt32(pLCItemmontor.Value), Convert.ToInt32(pLCItemmontor1.Value));
// if (_PLCConfig?.Enable == true)
//挡料电机操作
// _PLC.FeedingMotor(_PLCConfig.CunToZeroSpeed, _PLCConfig.CunPos, _PLCConfig.CunSpeed, _PLCConfig.CunDirection);
//流程开启操作配置
// ProcessInitialAction();
// if (_PLC?Enabled == true)
//皮带
// _PLC.Belt(true);
// LogAsync(DateTime.Now, LogLevel.Information, $"写入流程加载点位配置");
//计数清零
CountToZero();
LogAsync(DateTime.Now, LogLevel.Information, $"状态复位-关闭定位-写入流程加载点位配置-计数清零");
@ -827,6 +875,7 @@ namespace DH.Devices.PLC
if (pLCItem == null)
return;
WriteBool(pLCItem.Address, b);
}
/// <summary>
@ -839,6 +888,7 @@ namespace DH.Devices.PLC
if (pLCItem == null)
return;
WriteUInt16(pLCItem.Address, (ushort)speed);
}
/// <summary>
@ -850,7 +900,6 @@ namespace DH.Devices.PLC
if (DiskRunItem == null)
return;
WriteBool(DiskRunItem.Address, b);
Thread.Sleep(30);
piecesCount = 0;
}
@ -914,6 +963,59 @@ namespace DH.Devices.PLC
return;
WriteBool(pLCItem.Address, b);
Thread.Sleep(30);
}
public void Motor(bool direction, int speed, int pos)
{
int timeout = 5000;
int elapsedTime = 0;
int checkInterval = 100;
MotorToZero(false);
// 检查是否不在原点,如果不在,则回原点
MotorSpeed(speed); // 速度
Thread.Sleep(300);
// 发送回原点指令
MotorToZero(true);
Thread.Sleep(1000); // 给设备一些时间响应
// 等待回到原点
while (!ReadMotorToZero())
{
if (elapsedTime >= timeout)
{
break;
}
Thread.Sleep(checkInterval);
elapsedTime += checkInterval;
}
// }
// 无论是刚回到原点还是已经在原点,执行目标位置、速度和方向设置
MotorSpeed(speed);
if (direction)
{
MotorClockwise(true); // 顺时针转动
}
else
{
MotorCounterclockwise(true); // 逆时针转动
}
Thread.Sleep(30);
MotorPos(pos); // 目标位置
Thread.Sleep(2000);
}
/// <summary>
@ -922,60 +1024,26 @@ namespace DH.Devices.PLC
/// False: 逆时针
/// </summary>
/// <param name="u"></param>
public void FeedingMotor( bool direction)
public void FeedingMotor( bool direction,int speed,int pos)
{
// 设置最大等待时间,假设为 3 秒
int timeout = 3000;
int timeout = 5000;
int elapsedTime = 0;
int checkInterval = 100; // 每次检查等待 100ms
PLCItem pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机回原点");
if (pLCItem == null)
return;
PLCItem zerospeeditem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机回原点速度");
if (zerospeeditem == null)
return;
PLCItem CunSpeed = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机速度");
if (CunSpeed == null)
return;
PLCItem CunClockwiseItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机顺时针");
if (CunClockwiseItem == null)
return;
PLCItem CunCounterclockwiseItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机逆时针");
if (CunCounterclockwiseItem == null)
return;
PLCItem CunPosItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机位置");
if (CunPosItem == null)
return;
string CunToZero = pLCItem.Type + pLCItem.Address;
string CunToZeroSpeed = zerospeeditem.Type + zerospeeditem.Address;
string CunSpeedadress = CunSpeed.Type + CunSpeed.Address;
string CunClockwise = CunClockwiseItem.Type + CunClockwiseItem.Address;
string CunCounterclockwise = CunCounterclockwiseItem.Type + CunCounterclockwiseItem.Address;
string CunPos = CunPosItem.Type + CunPosItem.Address;
UInt16 zerospeed = UInt16.Parse(zerospeeditem.Value);
UInt16 cunSpeed = UInt16.Parse(CunSpeed.Value);
UInt16 u = UInt16.Parse(CunPosItem.Value);
// WriteBool(CountToZero, true);
int checkInterval = 100;
BarrierToZero(false);
// 检查是否不在原点,如果不在,则回原点
if (!ReadBool(CunToZero))
{
WriteUInt16(CunToZeroSpeed, zerospeed); // 速度
Thread.Sleep(30);
BarrierToZeroSpeed(speed); // 速度
Thread.Sleep(300);
// 发送回原点指令
WriteBool(CunToZero, true);
BarrierToZero(true);
Thread.Sleep(1000); // 给设备一些时间响应
// 等待回到原点
while (!ReadBool(CunToZero))
while (!ReadBarrierToZero())
{
if (elapsedTime >= timeout)
{
@ -985,33 +1053,32 @@ namespace DH.Devices.PLC
Thread.Sleep(checkInterval);
elapsedTime += checkInterval;
}
}
// 无论是刚回到原点还是已经在原点,执行目标位置、速度和方向设置
BarrierSpeed(speed);
WriteUInt16(CunSpeedadress,cunSpeed);
Thread.Sleep(2000);
string dir = string.Empty;
if (direction)
{
WriteBool(CunClockwise, true); // 顺时针转动
dir = "顺时针";
BarrierClockwise(true); // 顺时针转动
}
else
{
WriteBool(CunCounterclockwise, true); // 逆时针转动
dir = "逆时针";
BarrierCounterclockwise(true); // 逆时针转动
}
Thread.Sleep(30);
WriteUInt16(CunPos, u); // 目标位置
BarrierPos(pos); // 目标位置
Thread.Sleep(2000);
}
/// <summary>
/// 转盘清料
/// </summary>
@ -1052,16 +1119,226 @@ namespace DH.Devices.PLC
}
/// <summary>
/// 读取定位脉冲
/// </summary>
/// <returns></returns>
public int ReadVisionPos()
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "定位完成脉冲值");
if (pLCItem == null)
return 0;
int value = ReadInt32(pLCItem.Address);
Thread.Sleep(10);
Thread.Sleep(100);
return Math.Abs(value);
}
/// <summary>
/// 挡杆回原点
/// </summary>
public void BarrierToZero(bool b)
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机回原点");
if (pLCItem == null)
return;
WriteBool(pLCItem.Address, b);
}
/// <summary>
/// 变焦相机回原点
/// </summary>
public void ZoomcameraToZero(bool b)
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "步进电机回原点");
if (pLCItem == null)
return;
WriteBool(pLCItem.Address, b);
}
/// <summary>
/// 读取 挡杆回原点状态
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public bool ReadBarrierToZero()
{
//PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机回原点");
//if (pLCItem == null)
//{
// throw new Exception("未找到挡料电机回原点点位");
//}
//挡料电机传感器感应点
return ReadBool("X11");
}
/// <summary>
/// 挡杆回原点速度
/// </summary>
public void BarrierToZeroSpeed(int speed)
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机回原点速度");
if (pLCItem == null)
return;
WriteUInt32(pLCItem.Address, (uint)speed);
}
/// <summary>
/// 挡杆位置
/// </summary>
public void BarrierPos(int value)
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机位置");
if (pLCItem == null)
return;
WriteUInt16(pLCItem.Address, (ushort)value);
}
/// <summary>
/// 挡杆速度
/// </summary>
public void BarrierSpeed(int value)
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机速度");
if (pLCItem == null)
return;
WriteUInt32(pLCItem.Address, (uint)value);
}
/// <summary>
/// 挡杆顺时针
/// </summary>
public void BarrierClockwise(bool b)
{
PLCItem? DiskRunItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机顺时针");
if (DiskRunItem == null)
return;
WriteBool(DiskRunItem.Address, b);
Thread.Sleep(30);
}
/// <summary>
/// 挡杆逆时针
/// </summary>
public void BarrierCounterclockwise(bool b)
{
PLCItem? DiskRunItem = PLCItemList.FirstOrDefault(u => u.Name == "挡料电机逆时针");
if (DiskRunItem == null)
return;
WriteBool(DiskRunItem.Address, b);
Thread.Sleep(30);
}
/// <summary>
/// 相机步进电机回原点
/// </summary>
/// <param name="b"></param>
public void MotorToZero(bool b)
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进原点");
if (pLCItem == null)
return;
WriteBool(pLCItem.Address, b);
}
/// <summary>
/// 读取相机步进电机回原点状态
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public bool ReadMotorToZero()
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进原点");
if (pLCItem == null)
{
throw new Exception("未找到挡料电机回原点点位");
}
return ReadBool(pLCItem.Address);
}
/// <summary>
/// 相机步进电机位置
/// </summary>
/// <param></param>
public void MotorPos(int value)
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进位置");
if (pLCItem == null)
return;
WriteUInt16(pLCItem.Address, (ushort)value);
}
public int ReadMotorRealPos()
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进实时位置");
if (pLCItem == null)
{
throw new Exception("未找到相机步进实时位置");
}
return ReadInt16(pLCItem.Address);
}
/// <summary>
/// 相机步进电机速度
/// </summary>
/// <param name="b"></param>
public void MotorSpeed(int speed)
{
PLCItem? pLCItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进速度");
if (pLCItem == null)
return;
WriteInt32(pLCItem.Address, speed);
}
/// <summary>
/// 相机步进顺时针
/// </summary>
public void MotorClockwise(bool b)
{
PLCItem? DiskRunItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进顺时针");
if (DiskRunItem == null)
return;
WriteBool(DiskRunItem.Address, b);
Thread.Sleep(30);
}
/// <summary>
/// 相机步进逆时针
/// </summary>
public void MotorCounterclockwise(bool b)
{
PLCItem? DiskRunItem = PLCItemList.FirstOrDefault(u => u.Name == "相机步进逆时针");
if (DiskRunItem == null)
return;
WriteBool(DiskRunItem.Address, b);
Thread.Sleep(30);
}
/// <summary>
/// 相机步进点动
/// </summary>
public void MotorTest(bool b)
{
PLCItem? DiskRunItem = PLCItemList.FirstOrDefault(u => u.Name == "点动相机步进");
if (DiskRunItem == null)
return;
WriteBool(DiskRunItem.Address, b);
Thread.Sleep(30);
}
public void LogAsync(LogMsg msg)
{
msg.MsgSource = "PLC";