/// <summary> /// Stop the movement immediately /// </summary> /// <param name="AxisIndex">-1: Stop all axis; Otherwise, stop the specified axis</param> /// <returns></returns> public bool Stop(int AxisIndex) { // if the controller is not connected, return if (!this.IsConnected) { this.LastError = string.Format("The controller is not connected."); return(false); } try { CommandStruct cmd = new CommandStruct() { Command = EnumCommand.STOP, AxisIndex = AxisIndex }; _hid_device.Write(cmd.ToBytes()); return(true); } catch (Exception ex) { this.LastError = ex.Message; return(false); } }
/// <summary> /// Set the state of the general output I/O /// </summary> /// <param name="Channel">This should be 0 to 7</param> /// <param name="State">OFF/ON</param> /// <returns></returns> public bool SetGeneralOutput(int Channel, OutputState State) { // if the controller is not connected, return if (!this.IsConnected) { this.LastError = string.Format("The controller is not connected."); return(false); } try { CommandStruct cmd = new CommandStruct() { Command = EnumCommand.GENOUT, AxisIndex = Channel / 2, // calculate axis index by channel GenOutPort = Channel % 2, // calculate port by channel GenOutState = State }; _hid_device.Write(cmd.ToBytes()); return(true); } catch (Exception ex) { this.LastError = ex.Message; return(false); } }
bool SendMoveCommand(int AxisIndex, int Acceleration, int DriverVelocityPercent, int TotalSteps, out byte CommandOrderExpected) { bool ret = false; CommandOrderExpected = 0; lockUSBPort.Wait(); try { CommandOrderExpected = ++commandOrder; CommandStruct cmd = new CommandStruct() { Command = EnumCommand.MOVE, CommandOrder = (byte)CommandOrderExpected, AxisIndex = AxisIndex, AccSteps = Acceleration, DriveVelocity = DriverVelocityPercent * this.AxisCollection[AxisIndex].MaxSpeed / 100, TotalSteps = TotalSteps }; ret = hidPort.WriteData(cmd.ToBytes()); } catch { ret = false; } finally { lockUSBPort.Release(); } return(ret); }
public bool ReverseMoveDirection(int AxisIndex, bool IsReverse) { // if the controller is not connected, return if (!this.IsConnected) { this.LastError = string.Format("The controller is not connected."); return(false); } try { CommandStruct cmd = new CommandStruct() { Command = EnumCommand.REVERSE, AxisIndex = AxisIndex, IsReversed = IsReverse }; _hid_device.Write(cmd.ToBytes()); return(true); } catch (Exception ex) { this.LastError = ex.Message; return(false); } }
bool SendHomeCommand(int AxisIndex, out byte CommandOrderExpected) { bool ret = false; CommandOrderExpected = 0; lockUSBPort.Wait(); try { CommandOrderExpected = ++commandOrder; CommandStruct cmd = new CommandStruct() { Command = EnumCommand.HOME, CommandOrder = CommandOrderExpected, AxisIndex = AxisIndex }; ret = hidPort.WriteData(cmd.ToBytes()); } catch { ret = false; } finally { lockUSBPort.Release(); } return(ret); }
bool RequestAxisState(int AxisIndex, out AxisStateReport AxisState) { bool ret = false; AxisState = new AxisStateReport(); lockUSBPort.Wait(); //Trace.WriteLine(string.Format("Thread {0}, Enter request axis {1} ...", Thread.CurrentThread.ManagedThreadId, AxisIndex)); try { CommandStruct cmd = new CommandStruct() { Command = EnumCommand.REQ_AXIS_STATE, AxisIndex = AxisIndex }; if (hidPort.WriteData(cmd.ToBytes())) { var buff = hidPort.ReadData(); if (buff != null) { if (buff[0] == REPORT_ID_AXISSTATE) { var buffAvailable = new byte[buff.Length - 1]; Buffer.BlockCopy(buff, 1, buffAvailable, 0, buffAvailable.Length); AxisState.Parse(buffAvailable); ret = true; } } } } catch { ret = false; } finally { lockUSBPort.Release(); } //Trace.WriteLine(string.Format("Thread {0}, Exit request ...", Thread.CurrentThread.ManagedThreadId)); return(ret); }
/// <summary> /// read the value of PCA9534 /// </summary> /// <returns></returns> public bool ReadPCA9534() { buf_report_factinfo.Clear(); CommandStruct cmd = new CommandStruct() { Command = EnumCommand.READ9534 }; _hid_device.Write(cmd.ToBytes()); if (WaitReportFactinfo()) { return(this.PCA9534Info.Parse(buf_report_factinfo.ToArray())); } else { return(false); } }
/// <summary> /// Read firmware information /// after the information is returned, get the detail from FirmwareInfo property /// </summary> /// <returns></returns> public bool ReadFWInfo() { buf_report_factinfo.Clear(); CommandStruct cmd = new CommandStruct() { Command = EnumCommand.FWINFO }; _hid_device.Write(cmd.ToBytes()); if (WaitReportFactinfo()) { return(this.FirmwareInfo.Parse(buf_report_factinfo.ToArray())); } else { return(false); } }
/// <summary> /// Flip the specified output port /// </summary> /// <param name="Channel"></param> /// <returns></returns> public bool ToggleGeneralOutput(int Channel) { // if the controller is not connected, return if (!this.IsConnected) { this.LastError = string.Format("The controller is not connected."); return(false); } try { // flip the state of output port OutputState state = GetGeneralOutputState(Channel); if (state == OutputState.Disabled) { state = OutputState.Enabled; } else { state = OutputState.Disabled; } CommandStruct cmd = new CommandStruct() { Command = EnumCommand.GENOUT, AxisIndex = Channel / 2, // calculate axis index by channel GenOutPort = Channel % 2, // calculate port by channel GenOutState = state }; _hid_device.Write(cmd.ToBytes()); return(true); } catch (Exception ex) { this.LastError = ex.Message; return(false); } }
bool Request(EnumCommand Command, out byte[] Buffer) { bool ret = false; Buffer = null; lockUSBPort.Wait(); //Trace.WriteLine(string.Format("Thread {0}, Enter request ...", Thread.CurrentThread.ManagedThreadId)); try { CommandStruct cmd = new CommandStruct() { Command = Command, }; if (hidPort.WriteData(cmd.ToBytes())) { Buffer = hidPort.ReadData(); if (Buffer != null) { ret = true; } } } catch { ret = false; } finally { lockUSBPort.Release(); //Trace.WriteLine(string.Format("Thread {0}, Exit request ...", Thread.CurrentThread.ManagedThreadId)); } return(ret); }
/// <summary> /// Move the specified axis synchronously /// </summary> /// <param name="AxisIndex"></param> /// <param name="Velocity"></param> /// <param name="Position"></param> /// <param name="Mode"></param> /// <returns></returns> public bool Move(int AxisIndex, int Velocity, int Position, MoveMode Mode) { int _curr_pos = this.Report.AxisStateCollection[AxisIndex].AbsPosition; // Get current ABS position int _pos_aftermove = 0; if (AxisIndex >= this.TotalAxes) { this.LastError = string.Format("The param of axis index if error."); return(false); } // if the controller is not connected, return else if (!this.IsConnected) { this.LastError = string.Format("The controller is not connected."); return(false); } // If the axis is not homed, return. if (this.Report.AxisStateCollection[AxisIndex].IsHomed == false) { this.LastError = string.Format("Axis {0} is not homed.", AxisIndex); return(false); } // If the axis is busy, return. if (this.Report.AxisStateCollection[AxisIndex].IsRunning) { this.LastError = string.Format("Axis {0} is busy.", AxisIndex); return(false); } if (Velocity < 1 || Velocity > 100) { this.LastError = string.Format("The velocity should be 1 ~ 100."); return(false); } // // Validate the parameters restricted in the config file // // MaxDistance > 0 if (this.AxisCollection[AxisIndex].MaxSteps == 0) { this.LastError = string.Format("The value of the Max Distance has not been set."); return(false); } // SoftCWLS > SoftCCWLS if (this.AxisCollection[AxisIndex].SoftCWLS <= this.AxisCollection[AxisIndex].SoftCCWLS) { this.LastError = string.Format("The value of the SoftCWLS should be greater than the value of the SoftCCWLS."); return(false); } // SoftCWLS >= MaxDistance if (this.AxisCollection[AxisIndex].SoftCWLS < this.AxisCollection[AxisIndex].MaxSteps) { this.LastError = string.Format("The value of the SoftCWLS should be greater than the value of the Max Distance."); return(false); } // SoftCCWLS <= PosAfterHome <= SoftCWLS if ((this.AxisCollection[AxisIndex].SoftCCWLS > this.AxisCollection[AxisIndex].PosAfterHome) || (this.AxisCollection[AxisIndex].PosAfterHome > this.AxisCollection[AxisIndex].SoftCWLS)) { this.LastError = string.Format("The value of the PosAfterHome exceeds the soft limitaion."); return(false); } // // Validate the position after moving, // if the position exceeds the soft limitation, do not move // if (Mode == MoveMode.ABS) { if (Position < this.AxisCollection[AxisIndex].SoftCCWLS || Position > this.AxisCollection[AxisIndex].SoftCWLS) { this.LastError = string.Format("The target position is out of range."); return(false); } else { _pos_aftermove = Position; Position = Position - _curr_pos; } } else // rel positioning { _pos_aftermove = (int)(_curr_pos + Position); if (Position > 0) // CW { // if (_pos_aftermove > this.AxisCollection[AxisIndex].SoftCWLS) if (_pos_aftermove > this.AxisCollection[AxisIndex].MaxSteps) { this.LastError = string.Format("The position you are going to move exceeds the soft CW limitation."); return(false); } } else // CCW { // if (_pos_aftermove < this.AxisCollection[AxisIndex].SoftCCWLS) if (_pos_aftermove < 0) { this.LastError = string.Format("The position you are going to move exceeds the soft CCW limitation."); return(false); } } } try { // No need to move if (Position == 0) { return(true); } // write the 'move' command to the controller CommandStruct cmd = new CommandStruct() { Command = EnumCommand.MOVE, AxisIndex = AxisIndex, AccSteps = this.AxisCollection[AxisIndex].AccelerationSteps, DriveVelocity = Velocity * this.AxisCollection[AxisIndex].MaxSpeed / 100, TotalSteps = Position }; _hid_device.Write(cmd.ToBytes()); // wait for the next hid report uint _report_counter = this.Report.Counter + 1; do { Thread.Sleep(2); } while (this.Report.Counter <= _report_counter); // the TRUE value of the IsRunning property indicates that the axis is running // wait until the running process is done while (this.Report.AxisStateCollection[AxisIndex].IsRunning) { Thread.Sleep(2); } if (Report.AxisStateCollection[AxisIndex].Error != 0) { this.LastError = string.Format("error code {0:d}", Report.AxisStateCollection[AxisIndex].Error); return(false); } else { return(true); } } catch (Exception ex) { this.LastError = ex.Message; return(false); } }
/// <summary> /// Home the specified axis synchronously /// </summary> /// <param name="AxisIndex">The axis index, this parameter should be 0 ~ 2</param> /// <returns></returns> public bool Home(int AxisIndex) { if (AxisIndex >= this.Report.TotalAxes) { this.LastError = string.Format("The param of axis index if error."); return(false); } // if the controller is not connected, return else if (!this.IsConnected) { this.LastError = string.Format("The controller is not connected."); return(false); } // If the axis is busy, return. if (this.Report.AxisStateCollection[AxisIndex].IsRunning) { this.LastError = string.Format("Axis {0} is busy.", AxisIndex); return(false); } // start to home process try { // write the 'home' command to controller CommandStruct cmd = new CommandStruct() { Command = EnumCommand.HOME, AxisIndex = AxisIndex }; _hid_device.Write(cmd.ToBytes()); // wait for 10 HID reposts in order to ensure that the home command is actually executed uint _report_counter = this.Report.Counter + 10; do { Thread.Sleep(10); } while (this.Report.Counter <= _report_counter); /* * wait until the command execution is done; * the timeout is set to 2 minutes. */ bool _timeout = false; DateTime _start = DateTime.Now; while (this.Report.AxisStateCollection[AxisIndex].IsRunning == true) { Thread.Sleep(100); if ((DateTime.Now - _start).TotalSeconds > 120) { _timeout = true; break; } } if (_timeout) { this.LastError = "timeout occured while checking the IsHoming flag."; return(false); } else { Thread.Sleep(50); if (this.Report.AxisStateCollection[AxisIndex].IsHomed) { return(true); } else { this.LastError = string.Format("error code {0:d}", Report.AxisStateCollection[AxisIndex].Error); return(false); } } } catch (Exception ex) { this.LastError = ex.Message; return(false); } }