//public SigleExpResultPre()
        //{
        //    InitializeComponent();
        //}
        public void setPositionAndTroque(List <List <float> > position, List <List <float> > torque)
        {
            this.position = position;
            this.torque   = torque;
            PICalc pc = new PICalc(position, torque, ifTPunishment);

            getPIValue = pc.getPIValue();
        }
        //public SigleExpResultPre()
        //{
        //    InitializeComponent();
        //}
        public void setPositionAndTroque(List <List <float> > position, List <List <float> > torque, List <List <int> > rightOrleft)
        {
            this.position = position;
            this.torque   = torque;
            this.positionForEverySequence    = position;
            this.torqueForEverySequence      = torque;
            this.rightOrLeftForEverySequence = rightOrleft;
            PICalc pc = new PICalc(position, torque, ifTPunishment);

            getPIValue = pc.getPIValue();
        }
Exemplo n.º 3
0
        private void btnStep3Start_Click(object sender, EventArgs e)
        {
            lblChooseDisplay.Visible = true;
            cbIsPosition.Visible     = true;
            cbIsTorque.Visible       = true;

            //vSti = new Stimulations(v.pictureBox1.Width, v.pictureBox1.Height, 1);

            timeBeginPeriod(1);
            uint start = timeGetTime();
            uint newStart;
            int  count = 0;
            int  i = 0, j = 0;

            ifStop = false;

            dp1 = new drawProcess(this.pbPosition.Width, this.pbPosition.Height, Color.DarkCyan);
            dp2 = new drawProcess(this.pbCommunitive.Width, this.pbCommunitive.Height, Color.DarkCyan);

            lpf3.Clear();
            lpf4.Clear();
            positionForEverySequence.Clear();
            torqueForEverySequence.Clear();
            RightOrLeftForEverySequence.Clear();

            sequenceIndexForExperiment = 0;
            this.flpTopForLabel.Controls.Clear();
            this.flpBottomForImageList.Controls.Clear();
            int controlsLength;

            if (cbSetSeqChoosed_1.Checked)
            {
                controlsLength     = trainOrTest_1.Count;
                trainOrTestUsed    = trainOrTest_1;
                experimentTimeUsed = experimentTime_1;
            }
            else if (cbSetSeqChoosed_2.Checked)
            {
                controlsLength     = trainOrTest_2.Count;
                trainOrTestUsed    = trainOrTest_2;
                experimentTimeUsed = experimentTime_2;
            }
            else
            {
                controlsLength     = trainOrTest_3.Count;
                trainOrTestUsed    = trainOrTest_3;
                experimentTimeUsed = experimentTime_3;
            }


            dp1.getTrainOrTestSequence(trainOrTestUsed);
            dp2.getTrainOrTestSequence(trainOrTestUsed);

            controls = new List <Control>();
            for (int ii = 0; ii < controlsLength; ii++)
            {
                Label l = new Label();
                l.Name        = "lblDForSequence" + i.ToString();
                l.AutoSize    = true;
                l.BorderStyle = BorderStyle.FixedSingle;
                l.Margin      = new System.Windows.Forms.Padding(3);
                l.Font        = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                if (trainOrTestUsed[i] == true)
                {
                    l.Text = "Tr: " + experimentTimeUsed[i].ToString();
                }
                else
                {
                    l.Text = "Te: " + experimentTimeUsed[i].ToString();
                }
                controls.Add(l);

                this.flpTopForLabel.Controls.Add(l);
            }
            controls[0].BackColor = Color.DarkCyan;

            positionForEverySequence.Add(new List <float>());
            torqueForEverySequence.Add(new List <float>());
            RightOrLeftForEverySequence.Add(new List <int>());

            this.btnStep3Start.Enabled = false;


            while (!ifStop)
            {
                Application.DoEvents();
                newStart = timeGetTime();

                if (newStart - start >= 100)
                {
                    float torqueVoltageValue;
                    float troque = float.Parse(pc.AnalogInput(1, out torqueVoltageValue));
                    //troque = troque / 100;
                    troque_trans = (troque - 2048) / 2048 * 80;


                    //debug mode
                    //troque_trans = 10;

                    count++;
                    start = newStart;

                    //Wert = p1_c1 * p1_c2 * trq + p1_c3 * p1_c4 * p1_bias_  'p1_c1=0/1 (open closed); p1_c2=+1/-1 (norm/inverted); p1_c3=0/1 (p1_bias on/off); p1_c4=+1/-1 (cw/ccw)
                    //dPsi1 = p1_k * Wert * dt_
                    //ArenaPos1 = ArenaPos1 + dPsi1


                    degree = v.getDegree();
                    this.lblEXPStateP.Text    = degree.ToString();
                    this.lblEXPStateTRaw.Text = troque.ToString();
                    this.lblEXPSTateT.Text    = troque_trans.ToString();


                    //debug mode
                    //degree += 1;

                    if (degree > 180)
                    {
                        degree = degree - 360;
                    }
                    if (degree < -180)
                    {
                        degree = degree + 360;
                    }


                    lpf3.Add(degree);
                    lpf4.Add(troque_trans);
                    //Console.WriteLine(degree);
                    positionForEverySequence[sequenceIndexForExperiment].Add(degree);
                    torqueForEverySequence[sequenceIndexForExperiment].Add(troque_trans);
                    if (v.getRightOrLeft())
                    {
                        RightOrLeftForEverySequence[sequenceIndexForExperiment].Add(1);
                    }
                    else
                    {
                        RightOrLeftForEverySequence[sequenceIndexForExperiment].Add(0);
                    }

                    if (trainOrTestUsed[sequenceIndexForExperiment])
                    {
                        if (rbUpT.Checked)
                        {
                            if ((degree > -45 & degree < 45) || (degree < -135 & degree > 135))
                            {
                                punishmentByHeat();
                                lblPunishmentStateValue.Text = "True";
                            }
                            else
                            {
                                unPunishmentByHeat();
                                lblPunishmentStateValue.Text = "False";
                            }
                        }
                        else
                        {
                            if ((degree <-45 & degree> -135) || (degree > 45 || degree < 135))
                            {
                                unPunishmentByHeat();
                                lblPunishmentStateValue.Text = "False";
                            }
                            else
                            {
                                punishmentByHeat();
                                lblPunishmentStateValue.Text = "True";
                            }
                        }
                    }



                    if (count == experimentTimeUsed[sequenceIndexForExperiment] * 10)
                    {
                        count = 0;
                        sequenceIndexForExperiment++;
                        dp2.clearCommunitivePosition();
                        lpf3.Clear();
                        lpf4.Clear();
                        if (sequenceIndexForExperiment == experimentTimeUsed.Count)
                        {
                            ifStop = true;
                            this.btnStep3Start.Enabled = true;
                            pc.ClearALLDigitalPort();
                            //OpenLoop();
                            cbOpenOrClosed.Checked = false;
                            cbOpenOrClosed.Text    = "Open";
                            DataSave();
                        }
                        else
                        {
                            positionForEverySequence.Add(new List <float>());
                            torqueForEverySequence.Add(new List <float>());
                            RightOrLeftForEverySequence.Add(new List <int>());
                            controls[sequenceIndexForExperiment].BackColor = Color.DarkCyan;
                        }



                        Bitmap   imageHere = new Bitmap(imageNow);
                        Graphics g1        = Graphics.FromImage(imageHere);

                        PICalc piC        = new PICalc(positionForEverySequence[sequenceIndexForExperiment - 1], torqueForEverySequence[sequenceIndexForExperiment - 1], rbUpT.Checked ? true : false);
                        float  PIValueNow = piC.getSinglePIValue();
                        g1.DrawString("PI: " + (PIValueNow).ToString("0.00"), new Font("Arial", 15), new SolidBrush(Color.LightGray), 30, 50);
                        PictureBox pb     = new PictureBox();
                        float      width  = this.flpBottomForImageList.Size.Width - 30;
                        float      height = (int)(((float)this.pbCommunitive.Size.Height / (float)this.pbCommunitive.Size.Width) * width);
                        pb.Size = new Size((int)width, (int)height);

                        pb.Image    = imageHere;
                        pb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

                        this.flpBottomForImageList.Controls.Add(pb);
                    }


                    //this.pictureBox2.CreateGraphics().DrawImage(dp1.drawSignalCurve(lpf3, lpf4), 0, 0);

                    if (lpf3.Count == 400)
                    {
                        lpf3.Remove(lpf3[0]);
                    }

                    if (lpf4.Count == 400)
                    {
                        lpf4.Remove(lpf4[0]);
                    }

                    this.pbPosition.CreateGraphics().DrawImage(dp1.drawSignalCurve(lpf3, lpf4), 0, 0);
                    imageNow = dp2.drawCommunitivePoint(degree, false, sequenceIndexForExperiment);
                    this.pbCommunitive.CreateGraphics().DrawImage(imageNow, 0, 0);
                }
            }
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            float positionVoltageValue;
            float torqueVoltageValue;


            float position = float.Parse(pc.AnalogInput10(0, out positionVoltageValue));
            float troque   = float.Parse(pc.AnalogInput10(1, out torqueVoltageValue));

            if (ifStartDebugMode)
            {
                position = 1982;
                troque   = 1111;
            }

            positionForEverySequence[sequenceIndexForExperiment].Add(position);
            torqueForEverySequence[sequenceIndexForExperiment].Add(troque);

            lblShowPosStep3.Text = position.ToString("00.00");
            lblShowTorStep3.Text = troque.ToString("00.00");
            if (trainOrTestUsed[sequenceIndexForExperiment])
            {
                if (rbUpT.Checked)
                {
                    if ((position > 1470 & position < 1982) || (position > 2494 || position < 976))
                    {
                        punishmentByHeat();
                        lblPunishmentStateValue.Text = "True";
                    }
                    else
                    {
                        unPunishmentByHeat();
                        lblPunishmentStateValue.Text = "False";
                    }
                }
                else
                {
                    if ((position > 1470 & position < 1982) || (position > 2494 || position < 976))
                    {
                        unPunishmentByHeat();
                        lblPunishmentStateValue.Text = "False";
                    }
                    else
                    {
                        punishmentByHeat();
                        lblPunishmentStateValue.Text = "True";
                    }
                }
            }
            else
            {
                unPunishmentByHeat();
                lblPunishmentStateValue.Text = "False";
            }

            timeIndex++;
            if (timeIndex == experimentTimeUsed[sequenceIndexForExperiment] * 10)
            {
                timeIndex = 0;
                sequenceIndexForExperiment++;
                dp2.clearCommunitivePosition();
                lpf3.Clear();
                lpf4.Clear();
                if (sequenceIndexForExperiment == experimentTimeUsed.Count)
                {
                    timer2.Stop();
                    this.btnStep3Start.Enabled = true;
                    pc.ClearALLDigitalPort();
                    OpenLoop();
                    cbOpenOrClosed.Checked = false;
                    cbOpenOrClosed.Text    = "Open";
                    DataSave();
                }
                else
                {
                    positionForEverySequence.Add(new List <float>());
                    torqueForEverySequence.Add(new List <float>());
                    controls[sequenceIndexForExperiment].BackColor = Color.DarkCyan;
                }



                PICalc pic     = new PICalc(positionForEverySequence[sequenceIndexForExperiment - 1], torqueForEverySequence[sequenceIndexForExperiment - 1], rbUpT.Checked);
                float  piValue = pic.getSiglePIValue();


                Bitmap     imageHere = new Bitmap(imageNow);
                PictureBox pb        = new PictureBox();
                float      width     = this.flpBottomForImageList.Size.Width - 30;
                float      height    = (int)(((float)this.pictureBox3.Size.Height / (float)this.pictureBox3.Size.Width) * width);
                pb.Size = new Size((int)width, (int)height);
                Graphics g = Graphics.FromImage(imageHere);
                g.DrawString("PIValue: " + piValue.ToString(), new Font("Arial", 15), new SolidBrush(Color.LightGray), 130, 20);
                pb.Image    = imageHere;
                pb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

                this.flpBottomForImageList.Controls.Add(pb);
            }



            ////debug mode
            //   position = 721;
            //   troque = 100;



            lpf3.Add(position);
            lpf4.Add(troque);
            if (lpf3.Count == 300)
            {
                lpf3.Remove(lpf3[0]);
            }

            if (lpf4.Count == 300)
            {
                lpf4.Remove(lpf4[0]);
            }



            this.pictureBox2.CreateGraphics().DrawImage(dp1.drawSignalCurve(lpf3, lpf4), 0, 0);
            imageNow = dp2.drawCommunitivePoint(position, ifStartDebugMode, sequenceIndexForExperiment);
            this.pictureBox3.CreateGraphics().DrawImage(imageNow, 0, 0);
            if (sequenceIndexForExperiment == 7 & ifBreakTime)
            {
                timer2.Stop();
                ifBreakTime        = false;
                btnReStart.Visible = true;
            }
        }