示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            serialPort = new SerialPort("COM12", 115200, Parity.None, 8, StopBits.One);

            serialPort.ReadTimeout = 1000;

            serialPort.Open();

            try
            {
                Send("p " + (Kp.Value / 1000.0).ToString());
                Send("i " + (Ki.Value / 1000.0).ToString());
                Send("d " + (Kd.Value / 1000.0).ToString());
                Send("s " + Setpoint.Value.ToString());
                Send("l 500");
                Send("g");
                string s_Kp       = serialPort.ReadLine();
                string s_Ki       = serialPort.ReadLine();
                string s_Kd       = serialPort.ReadLine();
                string s_SetPoint = serialPort.ReadLine();
                string loops      = serialPort.ReadLine();

                DataBlock db = new DataBlock();
                db.m_channels          = 2;
                db.m_samplesPerChannel = 500;
                db.Alloc();

                for (int i = 0; i < 500; i++)
                {
                    string str = serialPort.ReadLine();

                    string[] ss = str.Split(' ');

                    int val1;
                    int val2;
                    int.TryParse(ss[0], out val1);
                    int.TryParse(ss[1], out val2);

                    if (val1 < 0)
                    {
                        val1 = 0;
                    }

                    db.SetVoltage(0, i, (int)(val1 * 128 / Setpoint.Value));
                    db.SetVoltage(1, i, (int)((val2 / 2) + 128));
                }
                db.m_sampleRate = 25;
                db.m_channels   = 2;
                db.m_dataType   = DataBlock.DATA_TYPE.ANALOG;


                graphControl1.SetScopeData(db);
            }
            catch
            {
                graphControl1.SetScopeData(null);
            }
            serialPort.Close();
        }
示例#2
0
文件: PID.cs 项目: karawin/xoscillo
        private void button1_Click(object sender, EventArgs e)
        {
            serialPort = new SerialPort("COM12", 115200, Parity.None, 8, StopBits.One);

             serialPort.ReadTimeout = 1000;

             serialPort.Open();

             try
             {
            Send("p " + (Kp.Value / 1000.0).ToString());
            Send("i " + (Ki.Value / 1000.0).ToString());
            Send("d " + (Kd.Value / 1000.0).ToString());
            Send("s " + Setpoint.Value.ToString());
            Send("l 500");
            Send("g");
            string s_Kp = serialPort.ReadLine();
            string s_Ki = serialPort.ReadLine();
            string s_Kd = serialPort.ReadLine();
            string s_SetPoint = serialPort.ReadLine();
            string loops = serialPort.ReadLine();

            DataBlock db = new DataBlock();
            db.m_channels = 2;
            db.m_samplesPerChannel = 500;
            db.Alloc();

            for (int i = 0; i < 500; i++)
            {
               string str = serialPort.ReadLine();

               string[] ss = str.Split(' ');

               int val1;
               int val2;
               int.TryParse(ss[0], out val1);
               int.TryParse(ss[1], out val2);

               if (val1 < 0)
                  val1 = 0;

               db.SetVoltage(0, i,  (int)(val1 * 128 / Setpoint.Value));
               db.SetVoltage(1, i,  (int)((val2 / 2) + 128));
            }
            db.m_sampleRate = 25;
            db.m_channels = 2;
            db.m_dataType = DataBlock.DATA_TYPE.ANALOG;

            graphControl1.SetScopeData(db);
             }
             catch
             {
            graphControl1.SetScopeData(null);
             }
             serialPort.Close();
        }
示例#3
0
        override public bool GetDataBlock(ref DataBlock db)
        {
            bool result;

            Config();

            time = DateTime.Now;

            Read(res, 1);
            if (res[0] == 85)
            {
                db.m_sample = SampleID++;
                db.m_start  = DateTime.Now;

                db.m_channels          = 6;
                db.m_triggerVoltage    = 0;
                db.m_triggerPos        = 0;
                db.m_sampleRate        = GetSampleRate();
                db.m_samplesPerChannel = GetNumberOfSamplesPerChannel();
                db.m_channelsBitField  = GetChannelBitField();
                db.m_dataType          = DataBlock.DATA_TYPE.DIGITAL;

                db.Alloc();

                if (m_arduinoBuffer.Length != db.m_samplesPerChannel)
                {
//                if (db.m_samplesPerChannel * db.m_channels > 94000) db.m_samplesPerChannel = 94000 / db.m_channels;
                    m_arduinoBuffer = new byte[db.m_samplesPerChannel];
                }

                result = Read(m_arduinoBuffer, m_arduinoBuffer.Length);

                for (int i = 0; i < m_arduinoBuffer.Length; i++)
                {
                    db.SetVoltage(0, i, m_arduinoBuffer[i]);
                }

                db.m_stop = DateTime.Now;
                return(result);
            }

            return(false);
        }
示例#4
0
        public override bool GetDataBlock(ref DataBlock db)
        {
            bool result;

             Config();

             time = DateTime.Now;

             Read(res, 1);
             if (res[0] == 85)
             {
            db.m_sample = SampleID++;
            db.m_start = DateTime.Now;

            db.m_channels = 6;
            db.m_triggerVoltage = 0;
            db.m_triggerPos = 0;
            db.m_sampleRate = GetSampleRate();
            db.m_samplesPerChannel = GetNumberOfSamplesPerChannel();
            db.m_channelsBitField = GetChannelBitField();
            db.m_dataType = DataBlock.DATA_TYPE.DIGITAL;

            db.Alloc();

            if (m_arduinoBuffer.Length != db.m_samplesPerChannel)
            {
            //                if (db.m_samplesPerChannel * db.m_channels > 94000) db.m_samplesPerChannel = 94000 / db.m_channels;
                m_arduinoBuffer = new byte[db.m_samplesPerChannel];
            }

            result = Read(m_arduinoBuffer, m_arduinoBuffer.Length);

            for (int i = 0; i < m_arduinoBuffer.Length; i++)
            {
                db.SetVoltage(0, i, m_arduinoBuffer[i]);
            }

            db.m_stop = DateTime.Now;
            return result;
             }

             return false;
        }
示例#5
0
        override public void SetDataBlock(DataBlock db)
        {
            dataBlock.Copy(db);

            if (enabled)
            {
                filter.SetSampleRate(db.m_sampleRate);

                //filter a few times the first value to initialize filter
                for (int i = 0; i < 4; i++)
                {
                    double volt = db.GetVoltage(0, 0);
                    filter.DoFilter(volt);
                }

                for (int i = 0; i < db.GetChannelLength(); i++)
                {
                    double volt = db.GetVoltage(0, i);
                    dataBlock.SetVoltage(0, i, (int)filter.DoFilter(volt));
                }
            }
            base.SetDataBlock(dataBlock);
        }
示例#6
0
        override public bool GetDataBlock(ref DataBlock db)
        {
            bool result;

            //assume it timed out
            db.m_result = DataBlock.RESULT.TIMEOUT;

            if (GetNumberOfEnabledChannels() == 0)
            {
                return(false);
            }

            //-------------Get settings this needs a crytical section)
            //
            int  numberOfSamples;
            int  numberOfEnabledChannels;
            uint channelsBitField;
            int  sampleRate;

            lock (thisLock)
            {
                numberOfSamples         = GetNumberOfSamplesPerChannel();
                numberOfEnabledChannels = GetNumberOfEnabledChannels();
                channelsBitField        = GetChannelBitField();
                sampleRate = GetSampleRate();
            }

            //-------------Request data
            byte[] configBuffer = new byte[10];
            configBuffer[0] = (byte)COMMANDS.READ_ADC_TRACE;
            configBuffer[1] = m_triggerValue;
            configBuffer[2] = (byte)(numberOfSamples >> 8);
            configBuffer[3] = (byte)(numberOfSamples & 0xff);
            configBuffer[4] = (byte)numberOfEnabledChannels;

            int index = 0;

            for (byte i = 0; i < 4; i++)
            {
                if (((channelsBitField >> i) & 1) != 0)
                {
                    configBuffer[5 + index] = i;
                    index++;
                }
            }

            configBuffer[9] = (byte)127; //pwm

            Write(configBuffer, configBuffer.Length);

            //-------------Get data
            Read(res, 1);
            if (res[0] == 85)
            {
                db.m_min               = 0;
                db.m_max               = 5;
                db.m_channels          = numberOfEnabledChannels;
                db.m_channelsBitField  = channelsBitField;
                db.m_triggerVoltage    = 0;
                db.m_triggerPos        = 0;
                db.m_sampleRate        = (db.m_channels > 0) ? (sampleRate / db.m_channels) : 0;
                db.m_samplesPerChannel = numberOfSamples;
                db.m_dataType          = DataBlock.DATA_TYPE.ANALOG;
                db.Alloc();

                //read actual data
                if (m_arduinoBuffer.Length != db.m_samplesPerChannel * db.m_channels)
                {
                    m_arduinoBuffer = new byte[db.m_samplesPerChannel * db.m_channels];
                }
                result = Read(m_arduinoBuffer, m_arduinoBuffer.Length);

                index = 0;
                for (int ch = 0; ch < 2; ch++)
                {
                    if (((db.m_channelsBitField >> ch) & 1) == 1)
                    {
                        for (int i = 0; i < db.GetChannelLength(); i++)
                        {
                            db.SetVoltage(index, i, m_arduinoBuffer[i * db.m_channels + index]);
                        }
                        index++;
                    }
                }

                db.m_result = DataBlock.RESULT.OK;

                return(result);
            }

            return(false);
        }
示例#7
0
        override public bool GetDataBlock(ref DataBlock db)
        {
            //need to make a copy of this variable just in case it changes
            //remember the ui lives in a different thread and can change values.
            bool copyFastMode;

            lock (thisLock)
            {
                PrepareConfigBuffer();

                copyFastMode = fastMode;

                db.m_min               = 0;
                db.m_max               = 5;
                db.m_channels          = fastMode ? 1 : GetNumberOfEnabledChannels();
                db.m_channelsBitField  = fastMode ? 1 : GetChannelBitField();
                db.m_triggerVoltage    = triggerVoltage;
                db.m_triggerPos        = fastMode ? 3000 / 2 : 1500 / 2;
                db.m_sampleRate        = GetSampleRate();
                db.m_samplesPerChannel = fastMode ? 3000 : 1500;
                db.Alloc();
            }

            if (db.m_sampleRate > 0)
            {
                serialPort.ReadTimeout = (2 * db.m_samplesPerChannel * 1000) / db.m_sampleRate;
            }
            if (serialPort.ReadTimeout < 200)
            {
                serialPort.ReadTimeout = 200;
            }
            serialPort.Write(configBuffer, 0, 9);
            Read(res, 1);
            if (res[0] == 85)
            {
                Read(parallaxBuffer, 3000);
                if (copyFastMode)
                {
                    for (int i = 0; i < 3000; i++)
                    {
                        db.SetVoltage(0, i, parallaxBuffer[i]);
                    }
                }
                else
                {
                    int index = 0;
                    if ((db.m_channelsBitField & 1) > 0)
                    {
                        for (int i = 0; i < 1500; i++)
                        {
                            db.SetVoltage(index, i, parallaxBuffer[i]);
                        }
                        index++;
                    }
                    if ((db.m_channelsBitField & 2) > 0)
                    {
                        for (int i = 0; i < 1500; i++)
                        {
                            db.SetVoltage(index, i, parallaxBuffer[i + 1500]);
                        }
                        index++;
                    }
                }

                return(true);
            }
            return(false);
        }
示例#8
0
        override public bool GetDataBlock(ref DataBlock db)
        {
            //need to make a copy of this variable just in case it changes
            //remember the ui lives in a different thread and can change values.
            bool copyFastMode;

            lock (thisLock)
            {
                PrepareConfigBuffer();

                copyFastMode = fastMode;

                db.m_min = 0;
                db.m_max = 5;
                db.m_channels = fastMode ? 1 : GetNumberOfEnabledChannels();
                db.m_channelsBitField = fastMode ? 1 : GetChannelBitField();
                db.m_triggerVoltage = triggerVoltage;
                db.m_triggerPos = fastMode ? 3000 / 2 : 1500 / 2;
                db.m_sampleRate = GetSampleRate();
                db.m_samplesPerChannel = fastMode ? 3000 : 1500;
                db.Alloc();
            }

            if (db.m_sampleRate > 0)
            {
                serialPort.ReadTimeout = (2 * db.m_samplesPerChannel * 1000) / db.m_sampleRate;
            }
            if (serialPort.ReadTimeout < 200)
            {
                serialPort.ReadTimeout = 200;
            }
            serialPort.Write(configBuffer, 0, 9);
            Read(res, 1);
            if (res[0] == 85)
            {

                Read(parallaxBuffer, 3000);
                if (copyFastMode)
                {
                    for (int i = 0; i < 3000; i++)
                    {
                        db.SetVoltage(0, i, parallaxBuffer[i]);
                    }
                }
                else
                {
                    int index = 0;
                    if ((db.m_channelsBitField & 1) > 0)
                    {
                        for (int i = 0; i < 1500; i++)
                        {
                            db.SetVoltage(index, i, parallaxBuffer[i]);
                        }
                        index++;
                    }
                    if ((db.m_channelsBitField & 2) > 0)
                    {
                        for (int i = 0; i < 1500; i++)
                        {
                            db.SetVoltage(index, i, parallaxBuffer[i + 1500]);
                        }
                        index++;
                    }
                }

                return true;
            }
            return false;
        }
示例#9
0
        override public bool GetDataBlock(ref DataBlock db)
        {
            bool result;

            //assume it timed out
            db.m_result = DataBlock.RESULT.TIMEOUT;

            if (GetNumberOfEnabledChannels() == 0)
            {
                return false;
            }

            //-------------Get settings this needs a crytical section)
            //
            int numberOfSamples;
            int numberOfEnabledChannels;
            uint channelsBitField;
            int sampleRate;
            lock (thisLock)
            {
                numberOfSamples = GetNumberOfSamplesPerChannel();
                numberOfEnabledChannels = GetNumberOfEnabledChannels();
                channelsBitField = GetChannelBitField();
                sampleRate = GetSampleRate();
            }

            //-------------Request data
            byte[] configBuffer = new byte[10];
            configBuffer[0] = (byte)COMMANDS.READ_ADC_TRACE;
            configBuffer[1] = m_triggerValue;
            configBuffer[2] = (byte)(numberOfSamples >> 8);
            configBuffer[3] = (byte)(numberOfSamples & 0xff);
            configBuffer[4] = (byte)numberOfEnabledChannels;

            int index = 0;
            for (byte i = 0; i < 4; i++)
            {
                if (((channelsBitField>>i)&1)!=0)
                {
                    configBuffer[5 + index] = i;
                    index++;
                }
            }

            configBuffer[9] = (byte)127; //pwm

            Write(configBuffer, configBuffer.Length);

            //-------------Get data
            Read(res, 1);
            if (res[0] == 85)
            {
                db.m_min = 0;
                db.m_max = 5;
                db.m_channels = numberOfEnabledChannels;
                db.m_channelsBitField = channelsBitField;
                db.m_triggerVoltage = 0;
                db.m_triggerPos = 0;
                db.m_sampleRate = (db.m_channels > 0) ? (sampleRate / db.m_channels) : 0;
                db.m_samplesPerChannel = numberOfSamples;
                db.m_dataType = DataBlock.DATA_TYPE.ANALOG;            
                db.Alloc();

                //read actual data
                if (m_arduinoBuffer.Length != db.m_samplesPerChannel * db.m_channels)
                {
                    m_arduinoBuffer = new byte[db.m_samplesPerChannel * db.m_channels];
                }            
                result = Read(m_arduinoBuffer, m_arduinoBuffer.Length);

                index = 0;
                for(int ch=0;ch<2;ch++)
                {
                    if ( ((db.m_channelsBitField>>ch)&1) == 1)
                    {
                        for (int i = 0; i < db.GetChannelLength(); i++)
                        {
                            db.SetVoltage(index, i, m_arduinoBuffer[i * db.m_channels + index]);
                        }
                        index++;
                    }
                }

                db.m_result = DataBlock.RESULT.OK;

                return result;
            }

            return false;
      }