private void teachButton_Click(object sender, EventArgs e) { if (!teaching) { SetManualControls(false); teaching = true; teachButton.Text = "!Teach"; debugTextbox.AppendText("Teaching mode enabled\r\n"); robot.set_drag_teach(true); cancellationSource2 = new CancellationTokenSource(); Task.Factory.StartNew(async(cancellationToken) => { CancellationToken token = (CancellationToken)cancellationToken; int i = 0; while (!token.IsCancellationRequested) { robot.get_scara_param(); SetTrackbar(wristTrackbar, wristTextbox, (int)robot.rotation); SetTrackbar(elbowTrackbar, elbowTextbox, (int)robot.angle2); SetTrackbar(shoulderTrackBar, shoulderTextbox, (int)robot.angle1); SetTrackbar(zAxisTrackbar, zAxisTextbox, (int)robot.z); //if (i++ % 10 == 0) //{ AppendTextBox(debugTextbox, $"A1: {robot.angle1:0.0000},\t" + $"A2: {robot.angle2:0.0000},\t" + $"A3: {robot.rotation:0.0000},\t" + $"X:{ robot.x:0.0000},\t" + $"Y: { robot.y:0.0000},\t" + $"Z: { robot.z:0.0000}\r\n"); //Console.WriteLine($"wrist: {robot.rotation}, elbow: {robot.angle2}, shoulder: {robot.angle1}, Z: {robot.z}"); //} await Task.Delay(50); } }, cancellationSource2.Token); } else { teaching = false; cancellationSource2.Cancel(false); teachButton.Text = "Teach"; debugTextbox.AppendText("Teaching mode disabled\r\n"); robot.set_drag_teach(false); SetManualControls(true); } }