The Jog buttons: jog motion

Two jog buttons are used for jog motion: one for jog forward and another for jog backward. You can customize the button behavior. In this sample, the buttons are used this way:

Because we don't use the click behavior, the code is not written in the Btn_Click method. Instead, we use the MouseButtonDown event. The methods of these buttons are BtnJogForward_MouseLeftButtonDown, BtnJogForward_MouseLeftButtonUp, BtnJogBackward_MouseLeftButtonDown and BtnJogBackward_MouseLeftButtonUp (MainWindow.xaml.cs). When Jog Forward or Jog Backward is pressed and held, JogForward or JogBackward method (MainWindowViewModel.cs) is called. When one of them is released, Halt is called.

Jog Forward

private void BtnJogForward_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
   try
   {
      ViewModel.JogForward();
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.ToString());
   }
}
private async void BtnJogForward_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
   try
   {
      await ViewModel.Halt();
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.ToString());
   }
}

Jog Backward

private void BtnJogBackward_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
   try
   {
      ViewModel.JogBackward();
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.ToString());
   }
}
private async void BtnJogBackward_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
   try
   {
      await ViewModel.Halt();
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.ToString());
   }
}

 

The Jog buttons are available only when an axis is enabled. We bind the IsEnabled attribute with SelectedAxis.PowerOn, which uses IAxis.PowerOn to check whether the selected axis is enabled.

Jog Forward

Copy
<Button Grid.Row="4" Grid.Column="2" x:Name="BtnJogForward" 
 IsEnabled="{Binding SelectedAxis.PowerOn}"
 PreviewMouseLeftButtonDown="BtnJogForward_MouseLeftButtonDown"
 PreviewMouseLeftButtonUp="BtnJogForward_MouseLeftButtonUp"
 Click="BtnJogForward_Click">Jog Forward</Button>

 

Jog Backward

Copy
<Button Grid.Row="5" Grid.Column="2" x:Name="BtnJogBackward"
 IsEnabled="{Binding SelectedAxis.PowerOn}"
 PreviewMouseLeftButtonDown="BtnJogBackward_MouseLeftButtonDown"
 PreviewMouseLeftButtonUp="BtnJogBackward_MouseLeftButtonUp">Jog Backward</Button>

 

When the left mouse button is pressed and held, IAxis.Jog moves the axis forward or backward. When the button is released, IAxis.Halt stops jogging. In IAxis.Jog, we use the CommandVelocityInputValue variable, which is set to 360, to set the jog motion parameters. Since CommandVelocityInputValue is bound with the Command Velocity Text Box, you can change it to other value. The jog direction is set using the McDirection enum.

Jog Forward

public void JogForward()
{
   if (CommandVelocityInputValue is null)
   {
      MessageBox.Show("Invalid velocity input");
      return;
   }

   SelectedAxis.Jog(
   CommandVelocityInputValue.Value, 
   CommandVelocityInputValue.Value * 10, 
   CommandVelocityInputValue.Value * 10, 
   CommandVelocityInputValue.Value * 1000, 
   McDirection.mcPositiveDirection);
}

Jog Backward

public void JogBackward()
{
   if (CommandVelocityInputValue is null)
   {
      MessageBox.Show("Invalid velocity input");
      return;
   }

   SelectedAxis.Jog(
   CommandVelocityInputValue.Value,
   CommandVelocityInputValue.Value * 10,
   CommandVelocityInputValue.Value * 10,
   CommandVelocityInputValue.Value * 1000,
   McDirection.mcNegativeDirection);
}

 

Because an axis needs some time to be halted, we give IAxis.Halt 5,000 milliseconds to finish its job. If it is not done within the time span, the task will be cancelled.

Halt

public async Task Halt()
{
   if (CommandVelocityInputValue is null)
   {
      MessageBox.Show("Invalid velocity input");
      return;
   }

   await SelectedAxis.Halt(
   CommandVelocityInputValue.Value * 50,
   CommandVelocityInputValue.Value * 5000, 
   McBufferMode.mcAborting).WaitAsync(5000, true);
}