伺服控制电缸运行转数,因夏天用电量较大,发生伺服卡顿现象,需要降低电缸运行速度,现在一帧跑的转数要分解成现在的五分之一的样子。速度太慢可能体感就没什么效果了,所以顺便更改成外部配置的加速度或者减速度。

下边标记代码
1.读取外部数据
外部数据配置["300","600","900","1200","1500","1800","2100","2400","2700","3000","3300","3600","3900","4000"]
使用json转换成数组
 if (File.Exists("array.Config"))
        {
            StreamReader sr = new StreamReader("array.Config");
            string arr = sr.ReadLine().Trim().ToString();
            sr.Close();
            numbers = JsonConvert.DeserializeObject<List<int>>(arr);
        }
2.数字拆解方法
    List<int> numbers = new List<int>();//数组在初始时获取
    List<int> ResoleNum(int number)
    {
        int index = 0;//数组下标
        int i = 0;//大小缓存
        int value = 0;//当前相加数字
        List<int> resoleNum = new List<int>();
        int sign = number > 0 ? 1 : -1;//判断参数正负,因为电机需要下降所以需要添加负数
        number = Mathf.Abs(number);
        while (i < number)
        {
            if (index < numbers.Count)
            {
                value = numbers[index];
                index++;
            }
            else
            {
                value = numbers[numbers.Count - 1];
            }
            if ((i + value) > number)
            {
                value = number - i;
            }
            i += value;
            //Debug.Log(value);
            resoleNum.Add(value*sign);//如果输入为负数则分解的所有数字都为负数
        }
        return resoleNum;
    }
  3.调用出资拆解并每帧发送给伺服电机
IEnumerator Movement(int target1,int target2,int target3) {
        List<int> nums1 = ResoleNum(target1-Value_A);
        List<int> nums2 = ResoleNum(target2-Value_B);
        List<int> nums3 = ResoleNum(target3-Value_D);
        List<List<int>> moves = new List<List<int>>();
        moves.Add(nums1);
        moves.Add(nums2);
        moves.Add(nums3);
        moves.OrderByDescending(m=>m.Count);
        int index = 0;
        while (index<moves[0].Count)
        {
            if (nums1.Count!=0&&index<nums1.Count)
                Value_A = Value_C += nums1[index];
            if (nums2.Count != 0 && index < nums2.Count)
                Value_B = Value_F += nums2[index];
            if (nums3.Count != 0 && index < nums3.Count)
                Value_D = Value_E += nums3[index];
            Debug.Log("Value_A:"+ Value_A+ "    Value_B:"+ Value_B+ "    Value_D:"+ Value_D);
            Work1();
            index++;
            yield return new WaitForEndOfFrame();
        }
    }
数组的数字可以随便写从小到大就是加速度 从个大到小就是就是减速度 都一样就是匀速
posted on 2021-10-09 18:51  陌洛  阅读(103)  评论(0编辑  收藏  举报