public bool MoveRelAsync(char axis, int position)
        {
            mutex.WaitOne();
            double final_pos = GetPosition(axis) + (double)position;

            if (final_pos > motors[axis].limit || final_pos < 0)
            {
                return(false);
            }
            int rot = MicronToRotation(axis, position);

            if (!SelectChannel())
            {
                return(false);
            }
            if (!TMLLib.TS_SelectAxis(motors[axis].id))
            {
                return(false);
            }
            if (!TMLLib.TS_MoveRelative(rot, speed, acceleration, NO_ADDITIVE, TMLLib.UPDATE_IMMEDIATE, TMLLib.FROM_REFERENCE))
            {
                return(false);
            }
            mutex.ReleaseMutex();
            return(true);
        }
        public bool MoveAbsAsync(char axis, int position)
        {
            if (position > motors[axis].limit || position < 0)
            {
                return(false);
            }
            int rot = MicronToRotation(axis, position);

            mutex.WaitOne();
            if (!SelectChannel())
            {
                return(false);
            }
            if (!TMLLib.TS_SelectAxis(motors[axis].id))
            {
                return(false);
            }
            if (!TMLLib.TS_MoveAbsolute(rot, speed, acceleration, TMLLib.UPDATE_IMMEDIATE, TMLLib.FROM_REFERENCE))
            {
                return(false);
            }
            return(true);

            mutex.ReleaseMutex();
        }
        private int InitAxis(byte axis_id, string axis_file)
        {
            int idxSetup = TMLLib.TS_LoadSetup(axis_file);

            if (idxSetup < 0)
            {
                return(-1);
            }

            if (!TMLLib.TS_SetupAxis(axis_id, idxSetup))
            {
                return(-1);
            }

            if (!TMLLib.TS_SelectAxis(axis_id))
            {
                return(-1);
            }

            if (!TMLLib.TS_DriveInitialisation())
            {
                return(-1);
            }
            return(idxSetup);
        }
        public bool InitAxes()
        {
            int idxSetup = -1;

            foreach (var motor in motors.Values)
            {
                idxSetup = InitAxis(motor.id, motor.file);
                if (idxSetup == -1)
                {
                    return(false);
                }
            }
            if (motors.Count > 1)
            {
                if (!TMLLib.TS_SetupBroadcast(idxSetup))
                {
                    return(false);
                }

                if (!TMLLib.TS_SelectBroadcast())
                {
                    return(false);
                }
            }

            if (!TMLLib.TS_Power(TMLLib.POWER_ON))
            {
                return(false);
            }

            foreach (var motor in motors.Values)
            {
                UInt16 sAxiOn_flag = 0;
                while (sAxiOn_flag == 0)
                {
                    if (!TMLLib.TS_SelectAxis(motor.id))
                    {
                        return(false);
                    }
                    if (!TMLLib.TS_ReadStatus(TMLLib.REG_SRL, out sAxiOn_flag))
                    {
                        return(false);
                    }
                    sAxiOn_flag = (UInt16)((sAxiOn_flag & 1 << 15) != 0 ? 1 : 0);
                }
            }
            return(true);
        }
        public bool StopMotion(char axis)
        {
            mutex.WaitOne();
            if (!SelectChannel())
            {
                return(false);
            }
            if (!TMLLib.TS_SelectAxis(motors[axis].id))
            {
                return(false);
            }
            bool res = TMLLib.TS_Stop();

            mutex.ReleaseMutex();
            return(res);
        }
 public bool WaitForMotionComplete(char axis)
 {
     if (!SelectChannel())
     {
         return(false);
     }
     if (!TMLLib.TS_SelectAxis(motors[axis].id))
     {
         return(false);
     }
     if (!TMLLib.TS_SetEventOnMotionComplete(WAIT_EVENT, NO_STOP))
     {
         return(false);
     }
     return(true);
 }
        public double GetPosition(char axis)
        {
            mutex.WaitOne();
            Int32 position = 0;

            if (!SelectChannel())
            {
                return(-1);
            }
            if (!TMLLib.TS_SelectAxis(motors[axis].id))
            {
                return(-1);
            }
            if (!TMLLib.TS_GetLongVariable("APOS", out position))
            {
                return(-1);
            }
            mutex.ReleaseMutex();
            return(RotationToMicron(axis, position));
        }
        private bool HomeAxis(byte axis_id)
        {
            Int32  position      = -100000000;
            Int32  home_position = 0;
            Int32  back_step     = 1000;
            double high_speed    = 20;
            double low_speed     = 1.0;
            double acceleration  = 0.6;

            if (!TMLLib.TS_SelectAxis(axis_id))
            {
                return(false);
            }
            if (!TMLLib.TS_MoveRelative(position, high_speed, acceleration, NO_ADDITIVE, TMLLib.UPDATE_IMMEDIATE, TMLLib.FROM_REFERENCE))
            {
                return(false);
            }
            if (!TMLLib.TS_SetEventOnLimitSwitch(TMLLib.LSW_NEGATIVE, TMLLib.TRANSITION_LOW_TO_HIGH, WAIT_EVENT, NO_STOP))
            {
                return(false);
            }
            if (!TMLLib.TS_SetEventOnMotionComplete(WAIT_EVENT, NO_STOP))
            {
                return(false);
            }
            if (!TMLLib.TS_MoveRelative(back_step, low_speed, acceleration, NO_ADDITIVE, TMLLib.UPDATE_IMMEDIATE, TMLLib.FROM_REFERENCE))
            {
                return(false);
            }
            if (!TMLLib.TS_SetEventOnMotionComplete(WAIT_EVENT, NO_STOP))
            {
                return(false);
            }
            if (!TMLLib.TS_SetPosition(home_position))
            {
                return(false);
            }
            return(true);
        }
 private bool SelectChannel()
 {
     return(TMLLib.TS_SelectChannel(channel_id));
 }
 public bool InitCommunicationChannel()
 {
     channel_id = TMLLib.TS_OpenChannel(channel_name, CHANNEL_TYPE, host_id, BAUDRATE);
     return((channel_id < 0) ? false : true);
 }