查看代码,分析代码的问题nt forceInt = 0;//用于记录上次的力 //1.算好100个电压值。0到8V int numPoints = 20; double[]…
- User
查看代码,分析代码的问题nt forceInt = 0;//用于记录上次的力 //1.算好100个电压值。0到8V int numPoints = 20; double[] sineWave = new double[numPoints];
for (int i = 0; i < numPoints; i++) { double angle = 2 * Math.PI * i / numPoints; // 0 to 2*PI in numPoints steps sineWave[i] = Math.Round(4 * (1 + Math.Sin(angle - Math.PI / 2)), 3); //sineWave[i] = 4 * (1 + Math.Sin(angle - Math.PI / 2)); // Scale sine wave from -1 to 1 to 0 to 8V } // Print the sine wave values for (int i = 0; i < numPoints; i++) { Console.WriteLine("Point:" + i + "," + sineWave[i] + ",V"); } dataPoints.Clear(); //2.记录当前的上传感器和下传感器的值 numbersUp.Clear(); await Task.Delay(2000); double upperResolution = 0; //double upperResolution = numbersUp.Average(); if (numbersUp.Any()) { upperResolution = numbersUp.Average(); } else { // 处理集合为空的情况 upperResolution = 0; // 或者其他你认为合适的默认值 } numbersUp.Clear(); //double upperResolution = Convert.ToDouble(textBox3.Text); double lowerResolution = Convert.ToDouble(textBox4.Text); List<double> numbers = new List<double>(); int initial = (int)numericUpDown11.Value; int increment = (int)numericUpDown8.Value; int target = (int)numericUpDown9.Value; for (int j = initial; j < target; j += increment) { xValue = 0.0; yValue = 0.0; textBox11.Text = "0"; textBox12.Text = "0"; textBox15.Text = "0"; textBox16.Text = "0"; //dataPoints.Clear(); textBox14.Text = (j).ToString("F0"); //3.发第一个电压值1,等待10000ms for (int i = 0; i < numPoints; i++) { if (!isTest) return; Data_Output("当前序号:" + i); ApplyVoltage(sineWave[i] * 25); await Task.Delay(2000); //4.计算当前的位移,F= k * x ,计算出F发给发力电机 double upperResolution1 = Convert.ToDouble(textBox3.Text); double lowerResolution1 = Convert.ToDouble(textBox4.Text); float displacement = (float)((upperResolution - upperResolution1) + (lowerResolution - lowerResolution1)); stiffness = j; //stiffness = (int)numericUpDown13.Value; int force = (int)(displacement * stiffness) + forceValue; // 增加电压以后,从现在的力每次加一,计算是否大于计算的力,如果符合条件 退出。减少电压相反 bool isIncreasing = i < numPoints / 2; // 判断是增加还是减少 forceInt int delay = 300; // 延迟时间 //double upperResolution1, lowerResolution1; //float displacement; //int stiffness = j; //int force; while ((isIncreasing && forceInt < force) || (!isIncreasing && forceInt > force)) { await Task.Delay(delay); // 延迟 forceInt += isIncreasing ? 100 : -100; // 根据 isIncreasing 增加或减少 forceInt // 确保 forceInt 不小于 forceValue if (forceInt < forceValue) forceInt = forceValue + 1; Force_Set(forceInt); // 设置力值 // 读取分辨率值 if (isIncreasing) { numbersUp.Clear(); await Task.Delay(2000); if (numbersUp.Any()) { upperResolution1 = numbersUp.Average(); } else { // 处理集合为空的情况 upperResolution1 = 0; // 或者其他你认为合适的默认值 } //upperResolution1 = numbersUp.Average(); numbersUp.Clear(); } else { upperResolution1 = Convert.ToDouble(textBox3.Text); } //upperResolution1 = Convert.ToDouble(textBox3.Text); lowerResolution1 = Convert.ToDouble(textBox4.Text); // 计算位移 displacement = (float)((upperResolution - upperResolution1) + (lowerResolution - lowerResolution1)); // 计算力值 force = (int)(displacement * stiffness) + forceValue; } //if (force < forceValue) // force = forceValue; //Force_Set(force); Console.WriteLine("加电压到" + sineWave[i] * 25 + "V, 后....位移: " + Math.Round(displacement, 2) + "力:" + force); //检查力是否到位,如果不到位记录一下。 int maxChecks = 100; int interval = 200; // milliseconds int tolerance = 1; // tolerance range int currentValue; for (int k = 0; k < maxChecks; k++) { // Wait for the specified interval await Task.Delay(interval); if (int.TryParse(textBox5.Text, out currentValue)) { // Check if the value is within the tolerance range if (Math.Abs(currentValue - (force - forceValue)) <= tolerance) { break; } } if (k == maxChecks - 1) { Console.WriteLine("没有到达目标力"); } } //保存数据 Math.Round(4 * (1 + Math.Sin(angle - Math.PI / 2)), 3) dataLines.Add(string.Format("{0},{1},{2}", (force - forceValue), Math.Round(displacement,2), stiffness)); // 添加数据点 dataPoints.Add(Math.Round(displacement, 2), (force - forceValue)); if (isIncreasing) { double value = Math.Round(displacement, 2); if (value < 0) value = 0; numbers.Add(value); if (numbers.Count > numPoints / 2 - 1) { double max = numbers.Max(); double average = numbers.Average(); if (this.textBox11.InvokeRequired) { } else { textBox11.Text = (max).ToString("F2"); } if (this.textBox12.InvokeRequired) { } else { textBox12.Text = (average).ToString("F2"); } double forceMax = max * int.Parse(textBox14.Text); double forceAve = average * int.Parse(textBox14.Text); if (this.textBox15.InvokeRequired) { } else { textBox15.Text = (forceMax).ToString("F0"); } if (this.textBox16.InvokeRequired) { } else { textBox16.Text = (forceAve).ToString("F0"); } numbers.Clear(); } } // 更新 ZedGraph //zedGraphControl1.GraphPane.CurveList.Clear(); //LineItem curve = zedGraphControl1.GraphPane.AddCurve("", dataPoints, Color.Red, SymbolType.None); //zedGraphControl1.AxisChange(); //zedGraphControl1.Invalidate(); // 更新 ZedGraph zedGraphControl1.Invoke((Action)(() => { zedGraphControl1.GraphPane.CurveList.Clear(); LineItem curve = zedGraphControl1.GraphPane.AddCurve("", dataPoints, Color.Red, SymbolType.None); zedGraphControl1.AxisChange(); zedGraphControl1.Invalidate(); })); } } Data_Output("比例测试法测试完成"); - Assistant