发表于:2021/8/12 10:21:42
#10楼
如果用绝对位置,代码如下,主要是暂停的时候获取当前位置,如果还没达到目标位置,点击继续的时候重新执行移动到位置命令,相当于在线变位.本人专注工控上位机,运动控制以及机器视觉.代码是c#代码,电机函数换成自己的api即可.希望对你有帮助!
static bool bPause = false;
bool posCommandFinished = false;
void StartMotosThreads()
{
Task task0 = Task.Factory.StartNew(()=> {
while (true)
{
if (!bPause)
{
if (!posCommandFinished)
{
posCommandFinished = false;
double targetPos = 5000000;
//-----------------------------------------------------------------
// Create a command value.
//-----------------------------------------------------------------
Motion.PosCommand posCommand = new Motion.PosCommand();
posCommand.Profile.Type = WMX3ApiCLR.ProfileType.SCurve;
posCommand.Axis = 0;
posCommand.Target = targetPos;
posCommand.Profile.Velocity = 100000;
posCommand.Profile.Acc = 1000000;
posCommand.Profile.Dec = 1000000;
//-----------------------------------------------------------------
// Execute command to move from current position to
// specified position.
//-----------------------------------------------------------------
//Wmx3Lib_cm.Motion.StartMov(posCommand);
Wmx3Lib_cm.Motion.StartPos(posCommand);
//-----------------------------------------------------------------
// Wait until the axis moves to the target position and stops.
//-----------------------------------------------------------------
Wmx3Lib_cm.Motion.Wait(0);
double currPos = 0;
Wmx3Lib_cm.AxisControl.GetPosFeedback(0, ref currPos);
if (currPos == targetPos)
{
posCommandFinished = true;
}
System.Threading.Thread.Sleep(2000);
}
}
}
});
}
private void button_PauseMotos_Click(object sender, EventArgs e)
{
if ("暂停" == button_PauseMotos.Text)
{
button_PauseMotos.Text = "恢复";
bPause = true;
Wmx3Lib_cm.Motion.Stop(0);
Logger.Warning("电机暂停...");
}
else
{
button_PauseMotos.Text = "暂停";
bPause = false;
Logger.Warning("电机恢复...");
}
}
static bool bPause = false;
bool posCommandFinished = false;
void StartMotosThreads()
{
Task task0 = Task.Factory.StartNew(()=> {
while (true)
{
if (!bPause)
{
if (!posCommandFinished)
{
posCommandFinished = false;
double targetPos = 5000000;
//-----------------------------------------------------------------
// Create a command value.
//-----------------------------------------------------------------
Motion.PosCommand posCommand = new Motion.PosCommand();
posCommand.Profile.Type = WMX3ApiCLR.ProfileType.SCurve;
posCommand.Axis = 0;
posCommand.Target = targetPos;
posCommand.Profile.Velocity = 100000;
posCommand.Profile.Acc = 1000000;
posCommand.Profile.Dec = 1000000;
//-----------------------------------------------------------------
// Execute command to move from current position to
// specified position.
//-----------------------------------------------------------------
//Wmx3Lib_cm.Motion.StartMov(posCommand);
Wmx3Lib_cm.Motion.StartPos(posCommand);
//-----------------------------------------------------------------
// Wait until the axis moves to the target position and stops.
//-----------------------------------------------------------------
Wmx3Lib_cm.Motion.Wait(0);
double currPos = 0;
Wmx3Lib_cm.AxisControl.GetPosFeedback(0, ref currPos);
if (currPos == targetPos)
{
posCommandFinished = true;
}
System.Threading.Thread.Sleep(2000);
}
}
}
});
}
private void button_PauseMotos_Click(object sender, EventArgs e)
{
if ("暂停" == button_PauseMotos.Text)
{
button_PauseMotos.Text = "恢复";
bPause = true;
Wmx3Lib_cm.Motion.Stop(0);
Logger.Warning("电机暂停...");
}
else
{
button_PauseMotos.Text = "暂停";
bPause = false;
Logger.Warning("电机恢复...");
}
}
[此贴子已经被作者于2021/8/12 10:29:05编辑过]