比如我们在做遥控飞机时,遥控发过来的油门值范围是0~1000,并且这个值是线性的。但我们的接收机,针对不同阶段的值需要做一下比例的转换。
下面的实验结果是把
000~200转换值为000~400
200~300转换值为400~475
300~800转换值为475~650
800~900转换值为650~725
900~1024转换值为725~1000
//油门曲线调节
#define SCT1_CH 200
#define SCT2_CH 300
#define SCT3_CH 800
#define SCT4_CH 900
#define SCT5_CH 1024
#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<=1024;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("<200\t%d\t%d\r\n",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("<300\t%d\t%d\r\n",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("<800\t%d\t%d\r\n",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("<900\t%d\t%d\r\n",throttle_cmd,throttle);
}
else
{
throttle = SCT4_TH + (throttle_cmd-SCT4_CH)*(SCT5_TH-SCT4_TH)/(SCT5_CH-SCT4_CH);
printf(">=900\t%d\t%d\r\n",throttle_cmd,throttle);
}
}
曲线如下