编辑代码

import java.util.Arrays;

class Main {
    
        static final int RC_CHANNEL_MIN = 1000,RC_CHANNEL_MAX = 2000,IBUS_CHANNEL_NUMBER = 18;
//    SBUS的每个RC通道值映射为:173 ->1000
    static final int SBUS_MIN_OFFSET = 173,SBUS_MID_OFFSET = 992,SBUS_MAX_OFFSET = 1811;

        public static int convertRange(int value, int oldMin, int oldMax, int newMin, int newMax) {
            if(value>oldMax)
                value = oldMax;
            if (value<oldMin)
                value = oldMin;
            int oldRange = oldMax - oldMin;
            int newRange = newMax - newMin;
            return (((value - oldMin) * newRange) / oldRange) + newMin;
    }
    static float[] VoltageKeyPoints = {2.75f,3.5f,3.73f,3.85f,3.95f,4.25f};
    static int[] VoltagePercents = {0,5,25,50,75,100};
    static int getBatteryVP(float batteryF){

        if (batteryF <= VoltageKeyPoints[0])
            return 0;

        for(int i = 1; i<VoltageKeyPoints.length; i++){
            if(batteryF <= VoltageKeyPoints[i])
                return (int) (VoltagePercents[i] - ((VoltagePercents[i] - VoltagePercents[i-1]) * (VoltageKeyPoints[i] - batteryF)) / (VoltageKeyPoints[i] - VoltageKeyPoints[i-1]));
            // 5 - (3.5 - 2.8)*5 / 5-0
            // 5 - (5-0) * (3.5 - 2.8) / (3.5f - 2.75f)
            // 5 -  5 * 0.7 / 0.75
        }
        return 100;
    }
	public static void main(String[] args) {
      float voltageAvalue = 4.25f;
      
      int batteryVP = convertRange((int) (voltageAvalue*100),275,425,0,100);

        System.out.println("voltageAvalue :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 
        
      voltageAvalue = 3.85f;
      
      batteryVP = convertRange((int) (voltageAvalue*100),275,425,0,100);
        System.out.println("voltageAvalue :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 

        voltageAvalue = 3.6f;
      
      batteryVP = convertRange((int) (voltageAvalue*100),275,425,0,100);
        System.out.println("voltageAvalue :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 

        voltageAvalue = 2.75f;
      
      batteryVP = convertRange((int) (voltageAvalue*100),275,425,0,100);
        System.out.println("voltageAvalue :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 

        voltageAvalue = 2.75f;
      
      batteryVP = getBatteryVP(voltageAvalue);
        System.out.println("voltageAvalue2 :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 
        voltageAvalue = 2.75f;
      
      batteryVP = getBatteryVP(voltageAvalue);
        System.out.println("voltageAvalue2 :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 
        voltageAvalue = 2.8f;
      
      batteryVP = getBatteryVP(voltageAvalue);
        System.out.println("voltageAvalue2 :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 
        voltageAvalue = 2.9f;
      
      batteryVP = getBatteryVP(voltageAvalue);
        System.out.println("voltageAvalue2 :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 
        voltageAvalue = 3.3f;
      
      batteryVP = getBatteryVP(voltageAvalue);
        System.out.println("voltageAvalue2 :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 
        voltageAvalue = 3.8f;
      
      batteryVP = getBatteryVP(voltageAvalue);
        System.out.println("voltageAvalue2 :"+ voltageAvalue+"V batteryVP:"+batteryVP+"%"); 

        
	}
}