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 Form1_Load(object sender, EventArgs e) { pc = new PortControl(0); pc.AnalogPortConfigurationIn(); pc.AnalogPortConfigurationOut(); pc.DigitalConfigurationOut(); pc.ClearALLDigitalPort(); positionForEverySequence = new List <List <float> >(); torqueForEverySequence = new List <List <float> >(); RightOrLeftForEverySequence = new List <List <int> >(); cc = new CoreControl(); SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲 StreamReader sR = File.OpenText(Application.StartupPath + "\\set-1.txt"); int length = int.Parse(sR.ReadLine()); for (int i = 0; i != length; i++) { string temp = sR.ReadLine(); if (temp == "Train") { trainOrTest_1.Add(true); experimentTime_1.Add(int.Parse(sR.ReadLine())); lbExpSequence_1.Items.Add(trainOrTest_1.Count.ToString("00") + " " + "Train " + experimentTime_1[i] + "s"); } else { trainOrTest_1.Add(false); experimentTime_1.Add(int.Parse(sR.ReadLine())); lbExpSequence_1.Items.Add(trainOrTest_1.Count.ToString("00") + " " + "Test " + experimentTime_1[i] + "s"); } } sR.Close(); StreamReader sR2 = File.OpenText(Application.StartupPath + "\\set-2.txt"); int length2 = int.Parse(sR2.ReadLine()); for (int i = 0; i != length2; i++) { string temp = sR2.ReadLine(); if (temp == "Train") { trainOrTest_2.Add(true); experimentTime_2.Add(int.Parse(sR2.ReadLine())); lbExpSequence_2.Items.Add(trainOrTest_2.Count.ToString("00") + " " + "Train " + experimentTime_2[i] + "s"); } else { trainOrTest_2.Add(false); experimentTime_2.Add(int.Parse(sR2.ReadLine())); lbExpSequence_2.Items.Add(trainOrTest_2.Count.ToString("00") + " " + "Test " + experimentTime_2[i] + "s"); } } sR2.Close(); StreamReader sR3 = File.OpenText(Application.StartupPath + "\\set-2.txt"); int length3 = int.Parse(sR3.ReadLine()); for (int i = 0; i != length3; i++) { string temp = sR3.ReadLine(); if (temp == "Train") { trainOrTest_3.Add(true); experimentTime_3.Add(int.Parse(sR3.ReadLine())); lbExpSequence_3.Items.Add(trainOrTest_3.Count.ToString("00") + " " + "Train " + experimentTime_3[i] + "s"); } else { trainOrTest_3.Add(false); experimentTime_3.Add(int.Parse(sR3.ReadLine())); lbExpSequence_3.Items.Add(trainOrTest_3.Count.ToString("00") + " " + "Test " + experimentTime_3[i] + "s"); } } sR3.Close(); v = new visionStimulation(); v.Show(); timer1.Interval = 10; timer2.Interval = 100; }
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(); 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>()); this.btnStep3Start.Enabled = false; float settingDegree = -135; oritation = 1; vf.SetRandomPoint(); while (!ifStop) { Application.DoEvents(); newStart = timeGetTime(); if (newStart - start >= 50) { sw = new System.Diagnostics.Stopwatch(); sw.Start(); 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 settingDegree += 1.8f * oritation; if (settingDegree >= 135) { oritation = -1; } if (settingDegree <= -135) { oritation = 1; } this.lblEXPStateP.Text = settingDegree.ToString(); this.lblEXPStateTRaw.Text = troque.ToString(); this.lblEXPSTateT.Text = troque_trans.ToString(); //debug mode //degree += 1; //v.pictureBox1.CreateGraphics().DrawImage(vSti.DrawV_Test(degree), 0, 0); lpf3.Add(settingDegree); lpf4.Add(troque_trans); //Console.WriteLine(degree); positionForEverySequence[sequenceIndexForExperiment].Add(settingDegree); torqueForEverySequence[sequenceIndexForExperiment].Add(troque_trans); if (count == experimentTimeUsed[sequenceIndexForExperiment] * 20) { count = 0; sequenceIndexForExperiment++; //dp2.clearCommunitivePosition(); lpf3.Clear(); lpf4.Clear(); settingDegree = -135; oritation = 1; 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>()); //controls[sequenceIndexForExperiment].BackColor = Color.DarkCyan; } } //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); this.vf.pbCanvas.CreateGraphics().DrawImage(vf.getImage(settingDegree), 0, 0); this.vf.simpleOpenGlControl1.Refresh(); sw.Stop(); TimeSpan ts = sw.Elapsed; this.lblShowFrameTime.Text = ts.Milliseconds.ToString(); } } }
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; } }
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.AnalogInput(1, out torqueVoltageValue)); positionForEverySequence[sequenceIndexForExperiment].Add(position); torqueForEverySequence[sequenceIndexForExperiment].Add(troque); if (trainOrTestUsed[sequenceIndexForExperiment]) { if (rbUpT.Checked) { if ((position > 1488 & position < 2000) || (position > 2516 || position < 976)) { punishmentByHeat(); lblPunishmentStateValue.Text = "True"; } else { unPunishmentByHeat(); lblPunishmentStateValue.Text = "False"; } } else { if ((position > 1488 & position < 2000) || (position > 2516 || 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; } 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); 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 == 150) { lpf3.Remove(lpf3[0]); } if (lpf4.Count == 150) { 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); }