private void upDown_Validating(object sender, CancelEventArgs e) { if (MyController.Generation == 1) { int status1 = MyController.Read(Controller.regStatus_1); int status2 = MyController.Read(Controller.regStatus_2); if (((status1 | status2) & 0x0ff) != 0) { MessageBox.Show("Motion must be stopped before setup parameters may be modified."); Close(); e.Cancel = true; return; } } NumericUpDown upDown = (NumericUpDown)sender; uint register = MapNumericToReg[upDown]; try { MyController.Write(register, (int)upDown.Value); } catch (Exception) { MessageBox.Show("Setting is out of range."); e.Cancel = true; return; } }
private NumericUpDown MakeNumeric(uint register) { NumericUpDown upDown = new NumericUpDown(); upDown.Maximum = 100000; upDown.Minimum = 0; upDown.Value = MyController.Read(register); upDown.Validating += new CancelEventHandler(this.upDown_Validating); upDown.Anchor = AnchorStyles.Left | AnchorStyles.Bottom; MapRegToNumeric.Add(register, upDown); MapNumericToReg.Add(upDown, register); return(upDown); }
private void timerPolling_Tick(object sender, EventArgs e) { if (MyController.Connected) { try { int regValue; if (timerPolling.Interval != 250) { timerPolling.Interval = 250; // Initialize info from controller regValue = MyController.Read(Controller.regVersionHW); this.labelHWVersion.Text = regValue.ToString("X"); regValue = MyController.Read(Controller.regVersionSW); this.labelFWVersion.Text = regValue.ToString("X"); regValue = MyController.Read(Controller.regVersionDate); this.labelDateOfManufacture.Text = regValue.ToString("X"); regValue = MyController.Read(Controller.regSetupPwmDefault); if (MyController.Generation == 1) { label5.Hide(); label6.Hide(); textBoxLED1.Hide(); textBoxLED2.Hide(); } } textBoxOnLine.Text = "On-Line"; textBoxOnLine.BackColor = Color.Green; this.Enabled = true; if (MyController.Generation == 2) { // poll for LED1 & LED2 status int status = MyController.Read(Controller.regStatus_1); textBoxLED1.BackColor = (((uint)status & 0x00000400) != 0) ? Color.Green : Color.LightGray; textBoxLED2.BackColor = (((uint)status & 0x00000800) != 0) ? Color.Red : Color.LightGray; } int pwmVal = MyController.Read(Controller.regPwmAbsolute); setPwmSetpoint(pwmVal); } catch (Exception) { } } else { textBoxOnLine.Text = "Off-Line"; textBoxOnLine.BackColor = Color.Gray; this.Enabled = false; if (timerPolling.Interval != 5000) { // Poll much more slowly if we are not connected timerPolling.Interval = 5000; } // Attempt to reconnect try { MyController.Connect(); } catch (Exception) { } } }
private void timerPolling_Tick(object sender, EventArgs e) { if (MyController.Connected) { try { int regValue; if (timerPolling.Interval != 250) { timerPolling.Interval = 250; // Initialize info from controller regValue = MyController.Read(Controller.regVersionHW); int major = (int)(((uint)regValue & 0xff000000) >> 24); int minor = (int)(((uint)regValue & 0xff0000) >> 16); int rev = (int)(((uint)regValue & 0xff00) >> 8); int bug = (int)((uint)regValue & 0xff); this.labelHWVersion.Text = major.ToString() + "." + minor.ToString() + "." + rev.ToString() + "." + bug.ToString(); regValue = MyController.Read(Controller.regVersionSW); major = (int)(((uint)regValue & 0xff000000) >> 24); minor = (int)(((uint)regValue & 0xff0000) >> 16); rev = (int)(((uint)regValue & 0xff00) >> 8); bug = (int)((uint)regValue & 0xff); this.labelFWVersion.Text = major.ToString() + "." + minor.ToString() + "." + rev.ToString() + "." + bug.ToString(); regValue = MyController.Read(Controller.regVersionDate); major = (int)(((uint)regValue & 0xff000000) >> 24); minor = (int)(((uint)regValue & 0xff0000) >> 16); rev = (int)(((uint)regValue & 0xff00) >> 8); bug = (int)((uint)regValue & 0xff); DateTime date = new DateTime(rev + 2000, major, minor); this.labelDateOfManufacture.Text = date.ToShortDateString(); regValue = MyController.Read(Controller.regSetupConfig_2); if ((regValue & 0x04) != 0) { this.motionAxis2.Enabled = false; } if (MyController.Generation == 1) { label4.Hide(); labelSerialNumber.Hide(); label5.Hide(); label6.Hide(); textBoxLED1.Hide(); textBoxLED2.Hide(); } else if (MyController.Generation == 2) { regValue = MyController.Read(Controller.regProductSerialNum); labelSerialNumber.Text = regValue.ToString(); } } textBoxOnLine.Text = "On-Line"; textBoxOnLine.BackColor = Color.Green; this.Enabled = true; // TODO: Add polling for LED1 & LED2 status int status = MyController.Read(Controller.regStatus_1); textBoxLED1.BackColor = (((uint)status & 0x00000400) != 0) ? Color.Green : Color.LightGray; textBoxLED2.BackColor = (((uint)status & 0x00000800) != 0) ? Color.Red : Color.LightGray; motionAxis1.UpdateAxis(); // Check for disabled 2nd axis here regValue = MyController.Read(Controller.regSetupConfig_2); if ((regValue & 0x04) == 0) { motionAxis2.UpdateAxis(); } else { this.motionAxis2.Enabled = false; } } catch (Exception) { MyController.Disconnect(); } } else { textBoxOnLine.Text = "Off-Line"; textBoxOnLine.BackColor = Color.Gray; this.Enabled = false; if (timerPolling.Interval != 5000) { // Poll much more slowly if we are not connected timerPolling.Interval = 5000; } // Attempt to reconnect try { MyController.Connect(); } catch (Exception) { } } }
private void setting_Changed(object sender, EventArgs e) { try { if (MyController.Generation == 1) { int status1 = MyController.Read(Controller.regStatus_1); int status2 = MyController.Read(Controller.regStatus_2); if (((status1 | status2) & 0x0ff) != 0) { MessageBox.Show("Motion must be stopped before setup parameters may be modified."); Close(); return; } } int regVal = MyController.Read(Controller.regSetupConfig_1); if (comboBoxZeroPosition1.SelectedIndex == 1) { regVal |= 0x01; } else { regVal &= ~((int)0x01); } if (checkBoxThruHome1.Checked) { regVal |= 0x02; } else { regVal &= ~((int)0x02); } if (MyController.Generation == 2 && checkBoxDisableAxis1.Checked) { regVal |= 0x04; } else { regVal &= ~((int)0x04); } MyController.Write(Controller.regSetupConfig_1, regVal); regVal = MyController.Read(Controller.regSetupConfig_2);; if (comboBoxZeroPosition2.SelectedIndex == 1) { regVal |= 0x01; } else { regVal &= ~((int)0x01); } if (checkBoxThruHome2.Checked) { regVal |= 0x02; } else { regVal &= ~((int)0x02); } if (checkBoxDisableAxis2.Checked) { regVal |= 0x04; } else { regVal &= ~((int)0x04); } MyController.Write(Controller.regSetupConfig_2, regVal); } catch (Exception) { // Problem writing new parameter values, probably a limit check, display generic error. MessageBox.Show("Error writing register."); return; } }
private void InitializeControls() { int row = 0; int col = 1; tableLayoutPanel1.Controls.Add(MakeLabel("Zoom"), col++, row); tableLayoutPanel1.Controls.Add(MakeLabel("Focus"), col++, row); row++; col = 0; if ((MyController.ProductID == 0x4001) && (MyController.ProductSubclass == 3)) { tableLayoutPanel1.Controls.Add(MakeLabel("Deceleration"), col++, row); } else { tableLayoutPanel1.Controls.Add(MakeLabel("Acceleration"), col++, row); } tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupAcceleration_1), col++, row); tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupAcceleration_2), col++, row); row++; col = 0; if ((MyController.ProductID == 0x4001) && (MyController.ProductSubclass == 3)) { tableLayoutPanel1.Controls.Add(MakeLabel("Gain"), col++, row); } else { tableLayoutPanel1.Controls.Add(MakeLabel("Initial Velocity"), col++, row); } tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupInitialVelocity_1), col++, row); tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupInitialVelocity_2), col++, row); row++; col = 0; if ((MyController.ProductID == 0x4001) && (MyController.ProductSubclass == 3)) { tableLayoutPanel1.Controls.Add(MakeLabel("Max Torque %"), col++, row); } else { tableLayoutPanel1.Controls.Add(MakeLabel("Maximum Velocity"), col++, row); } tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupMaxVelocity_1), col++, row); tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupMaxVelocity_2), col++, row); row++; col = 0; tableLayoutPanel1.Controls.Add(MakeLabel("Reverse Backlash"), col++, row); tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupReverseBacklash_1), col++, row); tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupReverseBacklash_2), col++, row); row++; col = 0; tableLayoutPanel1.Controls.Add(MakeLabel("Forward Backlash"), col++, row); tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupForwardBacklash_1), col++, row); tableLayoutPanel1.Controls.Add(MakeNumeric(Controller.regSetupForwardBacklash_2), col++, row); row++; col = 0; tableLayoutPanel1.Controls.Add(MakeLabel("Down Through Home"), col++, row); checkBoxThruHome1 = new CheckBox(); int config1 = MyController.Read(Controller.regSetupConfig_1); checkBoxThruHome1.Checked = ((config1 & 0x02) != 0); checkBoxThruHome1.CheckedChanged += new EventHandler(this.setting_Changed); checkBoxThruHome1.Anchor = AnchorStyles.Left | AnchorStyles.Bottom; tableLayoutPanel1.Controls.Add(checkBoxThruHome1, col++, row); checkBoxThruHome2 = new CheckBox(); int config2 = MyController.Read(Controller.regSetupConfig_2); checkBoxThruHome2.Checked = ((config2 & 0x02) != 0); checkBoxThruHome2.CheckedChanged += new EventHandler(this.setting_Changed); checkBoxThruHome2.Anchor = AnchorStyles.Left | AnchorStyles.Bottom; tableLayoutPanel1.Controls.Add(checkBoxThruHome2, col++, row); row++; col = 0; tableLayoutPanel1.Controls.Add(MakeLabel("Zero Position is"), col++, row); comboBoxZeroPosition1 = new ComboBox(); comboBoxZeroPosition1.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxZeroPosition1.Items.Add("Near"); comboBoxZeroPosition1.Items.Add("Far"); comboBoxZeroPosition1.SelectedIndex = (config1 & 0x01) == 0 ? 0 : 1; comboBoxZeroPosition1.SelectedIndexChanged += new EventHandler(this.setting_Changed); tableLayoutPanel1.Controls.Add(comboBoxZeroPosition1, col++, row); comboBoxZeroPosition2 = new ComboBox(); comboBoxZeroPosition2.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxZeroPosition2.Items.Add("Near"); comboBoxZeroPosition2.Items.Add("Far"); comboBoxZeroPosition2.SelectedIndex = (config2 & 0x01) == 0 ? 0 : 1; comboBoxZeroPosition2.SelectedIndexChanged += new EventHandler(this.setting_Changed); tableLayoutPanel1.Controls.Add(comboBoxZeroPosition2, col++, row); row++; col = 0; tableLayoutPanel1.Controls.Add(MakeLabel("Disable Axis"), col++, row); if (MyController.Generation == 2) { checkBoxDisableAxis1 = new CheckBox(); checkBoxDisableAxis1.Checked = ((config1 & 0x04) != 0); checkBoxDisableAxis1.CheckedChanged += new EventHandler(this.setting_Changed); checkBoxDisableAxis1.Anchor = AnchorStyles.Left | AnchorStyles.Bottom; tableLayoutPanel1.Controls.Add(checkBoxDisableAxis1, col++, row); } else { col++; } checkBoxDisableAxis2 = new CheckBox(); checkBoxDisableAxis2.Checked = ((config2 & 0x04) != 0); checkBoxDisableAxis2.CheckedChanged += new EventHandler(this.setting_Changed); checkBoxDisableAxis2.Anchor = AnchorStyles.Left | AnchorStyles.Bottom; tableLayoutPanel1.Controls.Add(checkBoxDisableAxis2, col++, row); }
/// <summary> /// Poll for axis motor status and update appropriate motor position and /// status controls. Must be called periodically to keep controls up to date. /// </summary> public void UpdateAxis() { int regVal; if (MyController.Connected) { try { int status = MyController.Read(Controller.regStatus_1 + RegOffset); if (MyController.ProductID == PRODUCT_GEN_2_CONTROLLER) { groupBoxAxis.Text = Axis == 0 ? ZOOM_STR : FOCUS_STR; if (((uint)status & STATUS_BITS_INSUFFICIENT_VOLTAGE) != 0) { groupBoxAxis.Text += " ("; groupBoxAxis.Text += INSUFFICIENT_VOLTAGE_STR; groupBoxAxis.Text += " )"; } // If this is a Gen 2 two-phase controller, then the motor type should be identifible. else if ((MyController.ProductSubclass == 2) || (MyController.ProductSubclass == 3) || (MyController.ProductSubclass == 1) || (MyController.ProductSubclass == (unchecked ((int)0xffffffd9)))) { uint motorId = ((uint)status & STATUS_MOTOR_TYPE_BITS) >> STATUS_MOTOR_TYPE_SHIFT; groupBoxAxis.Text += " ("; switch (motorId) { case TWO_PHASE_AM0820_MOTOR: groupBoxAxis.Text += AM0820_MOTOR_STR; break; case TWO_PHASE_AM1524_MOTOR: groupBoxAxis.Text += AM1524_MOTOR_STR; break; case TWO_PHASE_UNKNOWN_MOTOR: // Intentional fall through case TWO_PHASE_UNKNOWN_OPEN_SHORT: // Intentional fall through default: groupBoxAxis.Text += UNKNOWN_MOTOR_STR; break; } groupBoxAxis.Text += ")"; } } Idle = (((uint)status & 0x000000ff) == 0) ? true : false; buttonAtHome.BackColor = (((uint)status & 0x00000100) != 0) ? Color.Red : Color.LightGray; buttonAtLimit.BackColor = (((uint)status & 0x00000200) != 0) ? Color.Red : Color.LightGray; // Fetch motor limit position value. regVal = MyController.Read(Controller.regSetupLimit_1 + RegOffset); if (regVal == 0) { this.Enabled = false; return; } else { this.Enabled = true; } if (MotorLimit != regVal) { MotorLimit = regVal; this.textBoxLimit.Text = MotorLimit.ToString(); this.numericUpDownStepMin.Minimum = 1; this.numericUpDownStepMin.Maximum = MotorLimit / 10; this.numericUpDownStepMin.Value = MotorLimit / 100; this.numericUpDownStepMax.Minimum = 1; this.numericUpDownStepMax.Maximum = MotorLimit / 4; this.numericUpDownStepMax.Value = MotorLimit / 10; this.numericUpDownTarget1.Minimum = 0; this.numericUpDownTarget1.Maximum = MotorLimit; this.numericUpDownTarget2.Minimum = 0; this.numericUpDownTarget2.Maximum = MotorLimit; this.numericUpDownTarget3.Minimum = 0; this.numericUpDownTarget3.Maximum = MotorLimit; this.numericUpDownTarget4.Minimum = 0; this.numericUpDownTarget4.Maximum = MotorLimit; this.vProgressPosition.Maximum = MotorLimit; this.vProgressPosition.Minimum = 0; //regVal = MyController.Read(Controller.regTarget_1 + RegOffset); //SetTargetPostion(regVal); } MotorPosition = MyController.Read(Controller.regCurrent_1 + RegOffset); vProgressPosition.Value = Math.Min(Math.Max(0, MotorPosition), vProgressPosition.Maximum); textBoxPosition.Text = MotorPosition.ToString(); if (AutoStepping) { if (MotorTarget == MotorPosition || Idle) { if (numericUpDownDwellTime.Value == 0) { AutoStep(); } else if (!timerDwell.Enabled) { timerDwell.Interval = (int)numericUpDownDwellTime.Value * 1000; timerDwell.Start(); } } } if (Idle) { if (HomeRequested) { HomeRequested = false; MyController.Write(Controller.regLimit_1 + RegOffset, 0); } else if (LimitRequested) { LimitRequested = false; MyController.Write(Controller.regLimit_1 + RegOffset, 1); } else if (MoveRequested) { MoveRequested = false; MyController.Write(Controller.regTarget_1 + RegOffset, MotorTarget); } else if (!AutoStepping) { MotorTarget = MotorPosition; } } } catch (Exception) { } } else { Stop(); this.Enabled = false; } }