本文主要是介绍遥控器与电调相关知识点整理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
无线遥控就是利用高频无线电波实现对模型的控制。如天地飞的的6通道2.4 GHz遥控器,一套200多块,具有自动跳频抗干扰能力,从理论上讲可以让上百人在同一场地同时遥控自己的模型而不会相互干扰。而且在遥控距离方面也颇具优势,2.4 GHz遥控系统的功率仅仅在100 mW以下,而它的遥控距离可以达到1km以上。
每个通道信号脉宽0~2ms,变化范围为1~2ms之间。1帧PPM信号长度为20ms,理论上最多可以有10个通道,但是同步脉冲也需要时间,模型遥控器最多9个通道。
1、单独读取遥控信号,串口输出所有通道的信号,看和遥控手势是否匹配;
2、单独控制电机转速,在一次启动里让转速变化,不要只用一个转速控制;
3、综合起来,如果有问题,看看中断会不会有干扰、外部数据调用是否合理等待。
下面的解决办法(亲测可用),希望有帮助:
1、遥控信号
用的也是天地飞A6的遥控,他的接收机各个通道高电平时间正好是错开的,所以我用了一片或门(或非门)将所有通道加在一起,形成下面的样子
===================
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = ppm_pin;
GPIO_Init(ppm_gpio, &GPIO_InitStructure);
===================
TIM_DeInit(TIM1);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 72-1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 0xFFFF;
TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStruct );
TIM_ICStructInit(&TIM_ICInitStruct);
TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICFilter = 0x00;
TIM_ICInit(TIM1, &TIM_ICInitStruct );
TIM_ClearFlag(TIM1,TIM_FLAG_CC1 );
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE);
TIM_Cmd(TIM1, ENABLE);
====================
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
====================
rt_uint8_t ppm_step = 0;
rt_uint8_t ppm_c = 0;
rt_uint16_t buffer = 0;
rt_uint16_t channel[6];
void ppm_isr()
{
/* enter interrupt */
rt_interrupt_enter(); //防止中断冲突
switch (ppm_step)
{
case 0:
TIM1->SR &= 0XFFFC; //clear flag
TIM1->CNT = 0;
TIM1->CCER = 0X0003; //change to falling
ppm_step = 1;
break;
case 1:
TIM1->SR &= 0XFFFC; //clear flag
buffer = TIM1->CNT;
TIM1->CNT = 0;
TIM1->CCER = 0X0001; //change to rising
(buffer<2500)?(ppm_step=0):(ppm_step=2);
break;
case 2:
TIM1->SR &= 0XFFFC; //clear flag
channel[ppm_c] = TIM1->CNT;
TIM1->CCER = 0X0003; //change to falling
ppm_step = 3;
ppm_c++;
if(ppm_c == 6)
{
ppm_c = 0;
ppm_step = 0;
goto end;
}
break;
case 3:
TIM1->SR &= 0XFFFC; //clear flag
TIM1->CNT = 0;
TIM1->CCER = 0X0001; //change to rising
ppm_step = 2;
break;
default:
ppm_c = 0;
ppm_step = 0;
}
/* leave interrupt */
end:rt_interrupt_leave();
}
2、电机控制
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //³¬Éù²¨ÐźÅÊäÈë
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = sona1_pin;
GPIO_Init(sona_gpio, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //³¬Éù²¨²Ù×÷IO
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = sona2_pin;
GPIO_Init(sona_gpio, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¶¨Ê±Æ÷pwmÊä³ö¹Ü½Å
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM_Configuration(void)
{
//TIM3 CH1,2 TIM3 CH3,4 TIM4 CH1234
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure_pwm;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4, ENABLE);
TIM_DeInit(TIM3);
TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Period = 20* 1000 -1;
TIM_TimeBaseStructure.TIM_Prescaler = 72-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x00;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//TIM_TimeBaseStructure.TIM_Period = 20 * 1000 -1;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);
TIM_OCInitStructure_pwm.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure_pwm.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure_pwm.TIM_Pulse = 0;
TIM_OCInitStructure_pwm.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure_pwm);
TIM_OC2Init(TIM3, &TIM_OCInitStructure_pwm);
TIM_OC3Init(TIM3, &TIM_OCInitStructure_pwm);
TIM_OC4Init(TIM3, &TIM_OCInitStructure_pwm);
TIM_OC1Init(TIM4, &TIM_OCInitStructure_pwm);
TIM_OC2Init(TIM4, &TIM_OCInitStructure_pwm);
TIM_OC3Init(TIM4, &TIM_OCInitStructure_pwm);
TIM_OC4Init(TIM4, &TIM_OCInitStructure_pwm);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
}
#define CH1 1
#define CH2 2
#define CH3 3
#define CH4 4
#define CH5 5
#define CH6 6
#define CH7 7
#define CH8 8
void updata_pwm(unsigned int channel, unsigned int pulse)
{
if(pulse > 0xffff)pulse = 0xffff;
switch(channel)
{
case 1: TIM3->CCR1 = pulse;break;
case 2: TIM3->CCR2 = pulse;break;
case 3: TIM3->CCR3 = pulse;break;
case 4: TIM3->CCR4 = pulse;break;
case 8: TIM4->CCR1 = pulse;break;
case 7: TIM4->CCR2 = pulse;break;
case 6: TIM4->CCR3 = pulse;break;
case 5: TIM4->CCR4 = pulse;break;
default:break;
}
}
==========================
updata_pwm(CH1, 1000);
updata_pwm(CH2, 1000);
updata_pwm(CH3, 1000);
updata_pwm(CH4, 1000);
updata_pwm(CH5, 1000);
updata_pwm(CH6, 1000);
updata_pwm(CH7, 1000);
updata_pwm(CH8, 1000);
这篇关于遥控器与电调相关知识点整理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!