SimDrive_Servo.cs 4.86 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FLY.Simulation.Flyad7
{
    /// <summary>
    /// 伺服模拟器,有内部脉冲,和外部脉冲比例区别!!
    /// </summary>
    public interface ISimDrive_Servo:ISimDrive
    {
        /// <summary>
        /// 是正向!!!!
        /// </summary>
        bool IsForw{get;set;}

        /// <summary>
        /// 以额定速度,输出总脉冲, 此脉冲没有方向
        /// </summary>
        /// <param name="velocity">单位 pps</param>
        /// <param name="total_pos"></param>
        void Drive(int velocity, int total_pos);

        /// <summary>
        /// 停止输出
        /// </summary>
        void Stop();

        /// <summary>
        /// 剩余脉冲, 此脉冲没有方向
        /// </summary>
        int Surplus{get;}
    }
    
    /// <summary>
    /// 伺服模拟
    /// </summary>
    public class SimDrive_Servo:ISimDrive_Servo
    {
        const double mmpp=0.2;//0.2mm/pulse
        const int m_maxpos = 9200;/*2000*/ //3米
        const int m_minpos = -500;/*-100*/
        const double inpout=4;//输入脉冲/输出脉冲 
        //当前速度,单位 pos/s
        double m_speed = 0;
        
        double m_currpos = 853;//4500;/*当前脉冲*/

        int m_offset = 0;//ResetPos() 时, m_offset= - m_currpos;

        public int Pos
        {
            get
            {
                return (int)(m_currpos) + m_offset;
            }
        }
        public void ResetPos()
        {
            m_offset = -(int)(m_currpos);
        }

        private bool pin_org = true;
        public bool PIN_IN_ORG
        {
            get { return pin_org; }
            set
            {
                pin_org = value;
            }
        }
        private bool pin_in_limit_forw = true;
        public bool PIN_IN_LIMIT_FORW
        {
            get { return pin_in_limit_forw; }
            set
            {
                pin_in_limit_forw = value;
            }
        }
        private bool pin_in_limit_backw = true;
        public bool PIN_IN_LIMIT_BACKW
        {
            get { return pin_in_limit_backw; }
            set
            {
                pin_in_limit_backw = value;
            }
        }

        /// <summary>
        /// 是正向!!!!
        /// </summary>
        public bool IsForw { get; set; }

        private double surplus = 0;
        /// <summary>
        /// 剩余脉冲, 此脉冲没有方向
        /// </summary>
        public int Surplus 
        {
            get { return (int)surplus; }
        }
        int m_inpos_dest;//目标位置


        public SimDrive_Servo()
        {
        }


        void OnPoll_Order(TimeSpan ts)
        {
            if (m_speed > 0)
            {
                double currpos;
                double s = m_speed * ts.TotalMinutes;
                if (surplus < s)
                    s = surplus;
                
                surplus -= s;

                if(IsForw)
                    currpos = m_currpos + s / inpout;
                else
                    currpos = m_currpos - s / inpout;

                if ((IsForw) && (currpos >= m_maxpos))
                {
                    //限位了
                    m_currpos = m_maxpos;
                    m_speed = 0;
                }
                else if ((!IsForw) && (currpos <= m_minpos))
                {
                    m_speed = 0;
                    m_currpos = m_minpos;
                }
                else
                {
                    m_currpos = currpos;
                }


                if (m_currpos < 0)
                    PIN_IN_ORG = false;
                else
                    PIN_IN_ORG = true;

                if (m_currpos >= m_maxpos)
                    PIN_IN_LIMIT_FORW = false;
                else
                    PIN_IN_LIMIT_FORW = true;

                if (m_currpos <= m_minpos)
                    PIN_IN_LIMIT_BACKW = false;
                else
                    PIN_IN_LIMIT_BACKW = true;
            }
        }

        DateTime dtLast = DateTime.MinValue;
        public void OnPoll(DateTime now)
        {
            DateTime dt = now;
            if (dtLast != DateTime.MinValue)
            {
                TimeSpan ts = dt - dtLast;
                OnPoll_Order(ts);
            }
            dtLast = dt;
        }

        /// <summary>
        /// 以额定速度,输出总脉冲, 此脉冲没有方向
        /// </summary>
        /// <param name="velocity">单位 pps</param>
        /// <param name="total_pos"></param>
        public void Drive(int velocity, int total_pos) 
        {
            m_speed = velocity;
            surplus = total_pos;
        }

        /// <summary>
        /// 停止输出
        /// </summary>
        public void Stop() 
        {
            m_speed = 0;
        }



    }
}