/// <summary> /// Rotate the stepper motor for specific number of steps, where /// 512 steps is a full cycle of 360 degrees. The maximum number /// of rotation will be 512 steps. /// </summary> /// <param name="steps">Number of steps to rotate.</param> /// <returns> /// A bool indicating if the command is sent successfully. /// </returns> public bool Rotate(int steps) { // Check the current stepper state if (!(_StepperState == StepperState.Initialized || _StepperState == StepperState.Reset || _StepperState == StepperState.RotationFinished)) { return(false); } // Check if the step number is valid if (steps <= 0 || steps > 512) { return(false); } // Set the desired steps _DesireSteps = steps; // Set the stepper state as command sent _StepperState = StepperState.CommandSent; // Send the rotation command _StepperControllerPort.Write(steps.ToString()); // Return success return(true); }
/// <summary> /// Construct a StepperController class object with given port name. /// </summary> /// <param name="portName">Port name of the stepper controller port.</param> public StepperController(string portName) { // Initailze the stepper controller port _StepperControllerPort = new SerialPort(portName); _StepperControllerPort.DataReceived += _FeedbackHandler; // Reset the stepper state _StepperState = StepperState.Initialized; }
/// <summary> /// Construct a StepperController class object with full given parameters. /// </summary> /// <param name="portName">Name of the stepper controller port.</param> /// <param name="baudRate">Serial communication baud rate.</param> /// <param name="parity">Parity check protocal.</param> /// <param name="dataBits">Standard length of data bits per byte.</param> /// <param name="stopBits">Standard length of stop bits per byte.</param> public StepperController(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits) { // Initailze the stepper controller port _StepperControllerPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits); _StepperControllerPort.DataReceived += _FeedbackHandler; // Reset the stepper state _StepperState = StepperState.Initialized; }
StepperState _StepperState; // State of the stepper #endregion #region Local Functions /// <summary> /// The handler dealing with feedback messages from the /// stepper controller port. /// </summary> /// <param name="sender">Sender of the event.</param> /// <param name="args">Arguments of the event.</param> private void _FeedbackHandler(object sender, SerialDataReceivedEventArgs args) { string line; string feedback; // Read the received feedback line line = _StepperControllerPort.ReadLine(); // Check if the line matches format if (line.Length < 5) { OperationFailed(); _StepperControllerPort.Write("X"); return; } if (!(line[0] == '$' && line[1] == '$' && line[line.Length - 1] == '$' && line[line.Length - 2] == '$')) { OperationFailed(); _StepperControllerPort.Write("X"); return; } // Retrive the feedback message feedback = line.Substring(2, line.Length - 4); // Check if desired steps confirm message received if (feedback[0] == ':') { // Obtain the confirm steps int confirmSteps; if (!int.TryParse(feedback.Substring(1), out confirmSteps)) { OperationFailed(); _StepperControllerPort.Write("X"); return; } // Check if confirm and desired steps match if (confirmSteps == _DesireSteps && _StepperState == StepperState.CommandSent) { // Set the confirm flag _StepperState = StepperState.CommandConfirmed; // Confirm the rotation steps _StepperControllerPort.Write("OK"); } else { // Reset the confirm flag _StepperState = StepperState.CommandMismatched; // Send operation failed event OperationFailed(); // Trigger reset event _StepperControllerPort.Write("X"); } } else { switch (feedback) { case "INIT": { // Set the stepper state as initialized _DesireSteps = 0; _StepperState = StepperState.Initialized; } break; case "RESET": { // Set the stepper state as reset _DesireSteps = 0; _StepperState = StepperState.Reset; } break; case "START": { // Set the stepper state as started if (_StepperState == StepperState.CommandConfirmed) { // Set the stepper state as started _StepperState = StepperState.RotationStarted; // Send rotation started event RotationStarted(); } else { OperationFailed(); _StepperControllerPort.Write("X"); return; } } break; case "FINISH": { // Set the stepper state as finished _StepperState = StepperState.RotationFinished; // Send rotation finished event RotationFinished(); } break; default: { OperationFailed(); _StepperControllerPort.Write("X"); return; } } } }
public OwlStepperController(decimal stepsToPositionFactor) : base(stepsToPositionFactor) { //do anything PRIOR to having pins set State = new StepperState(); State.StatusReason = OwlDeviceStateBase.StatusReasonTypes.Unknown; }