栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c语言两个范围值的比例转换

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c语言两个范围值的比例转换

比如我们在做遥控飞机时,遥控发过来的油门值范围是0~1000,并且这个值是线性的。但我们的接收机,针对不同阶段的值需要做一下比例的转换。

下面的实验结果是把

000~200转换值为000~400

200~300转换值为400~475

300~800转换值为475~650

800~900转换值为650~725

900~n转换值为725~n

//油门曲线调节
#define  SCT1_CH	200  
#define  SCT2_CH	300
#define  SCT3_CH	800
#define  SCT4_CH	900

#define  SCT1_TH	400
#define  SCT2_TH	475
#define  SCT3_TH	650
#define  SCT4_TH	725
#define  SCT5_TH	1000 



uint16_t adjuster(uint16_t throttle_cmd);

int main()
{
    for(u16 i=0;i<2000;i++)
    {
        adjuster(i);
    }

   
    return 0;
}



//油门曲线调节
uint16_t adjuster(uint16_t throttle_cmd)
{
    uint16_t throttle;
	if (throttle_cmd < SCT1_CH)
	{
		throttle = throttle_cmd*SCT1_TH/SCT1_CH; 
        printf("<200t%dt%drn",throttle_cmd,throttle);
	}
	else if (throttle_cmd < SCT2_CH)
	{
		throttle = SCT1_TH + (throttle_cmd-SCT1_CH)*(SCT2_TH-SCT1_TH)/(SCT2_CH-SCT1_CH); 
        printf("<300t%dt%drn",throttle_cmd,throttle);
	}
	else if (throttle_cmd < SCT3_CH)
	{
		throttle = SCT2_TH + (throttle_cmd-SCT2_CH)*(SCT3_TH-SCT2_TH)/(SCT3_CH-SCT2_CH); 
        printf("<800t%dt%drn",throttle_cmd,throttle);						
	}
	else if (throttle_cmd < SCT4_CH)
	{
		throttle = SCT3_TH + (throttle_cmd-SCT3_CH)*(SCT4_TH-SCT3_TH)/(SCT4_CH-SCT3_CH);
        printf("<900t%dt%drn",throttle_cmd,throttle); 						
	}
	else
	{
		throttle = SCT4_TH + (throttle_cmd-SCT4_CH)*(SCT5_TH-SCT4_TH)/(1024-SCT4_CH); 	
        printf(">=900t%dt%drn",throttle_cmd,throttle);	
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号