欢迎来到天天文库
浏览记录
ID:28764112
大小:154.66 KB
页数:186页
时间:2018-12-14
《第2章 基于Arduino的四轴飞行器.docx》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、第2章基于Arduino的四轴飞行器2.3.3模块介绍1、电机输出模块(2)相关代码//主要函数定义externuint8_tPWM_PIN[8];voidinitOutput();//初始化函数voidmixTable();//PID计算voidwriteMotors();//信号输出到电机//输出程序uint8_tPWM_PIN[8]={9,10,11,3,6,5,A2,12};//定义输出接口voidinitOutput(){for(uint8_ti=0;i<4;i++){pinMode(PWM_PIN[i],OUTPUT);}//初始化,使PWM引脚作为输出引脚v
2、oidmixTable(){int16_tmaxMotor;//定义最大转速电机编号uint8_ti;#definePIDMIX(X,Y,Z)rcCommand[THROTTLE]+axisPID[ROLL]*X+axisPID[PITCH]*Y+YAW_DIRECTION*axisPID[YAW]*Z//PID算法motor[0]=PIDMIX(-1,+1,-1);motor[1]=PIDMIX(-1,-1,+1);motor[2]=PIDMIX(+1,+1,+1);motor[3]=PIDMIX(+1,-1,-1);//4个电机输出计算(PID)maxMotor=mo
3、tor[0];//以下代码限制最大输出油门,防止异常for(i=1;i<4;i++)if(motor[i]>maxMotor)maxMotor=motor[i];for(i=0;i<4;i++){if(maxMotor>MAXTHROTTLE)//保证当某一个油门达到最大时,陀螺仪仍有良好的信号连接motor[i]-=maxMotor-MAXTHROTTLE;motor[i]=constrain(motor[i],conf.minthrottle,MAXTHROTTLE);if((rcData[THROTTLE]4、[i]=conf.minthrottle;if(!f.ARMED)motor[i]=MINCOMMAND;}}voidwriteMotors(){OCR1A=motor[0]>>3;//pin9输出电机1号OCR1B=motor[1]>>3;//pin10输出电机2号OCR2A=motor[2]>>3;//pin11输出电机3号OCR2B=motor[3]>>3;//pin3输出电机4号}voidwriteAllMotors(int16_tmc){//所有电机输出设定为mcfor(uint8_ti=0;i<4;i++){motor[i]=mc;}writeMotors()5、;}/*电调初始化函数,电调初始化完成后注释掉defined重新上传#ifdefined(ESC_CALIB_CANNOT_FLY)writeAllMotors(ESC_CALIB_HIGH);blinkLED(2,20,2);delay(4000);writeAllMotors(ESC_CALIB_LOW);blinkLED(3,20,2);while(1){delay(5000);blinkLED(4,20,2);#endif}exit;#endif*/2、遥控器发送/接收模块(2)相关代码//RX.h主要函数定义#include"Arduino.h"#defineR6、C_CHANS8#definePCINT_PIN_COUNT5#definePCINT_RX_PORTPORTB#definePCINT_RX_MASKPCMSK0#definePCIR_PORT_BIT(1<<0)#defineRX_PC_INTERRUPTPCINT0_vect#defineRX_PCINT_PIN_PORTPINB#defineROLLPIN4//预定义各信道的名称#defineTHROTTLEPIN3#definePITCHPIN5#defineYAWPIN2#defineAUX1PIN6#defineAUX2PIN7#defineAUX3PIN17、//保留#defineAUX4PIN0//保留#definePCINT_RX_BITS(1<<1),(1<<2),(1<<3),(1<<4),(1<<0)voidconfigureReceiver();voidcomputeRC();uint16_treadRawRC(uint8_tchan);//初始信号读取函数//接收代码volatileuint16_trcValue[RC_CHANS]={1502,1502,1502,1502,1502,1502,1502,1502};staticuint8_trcChannel[RC_
4、[i]=conf.minthrottle;if(!f.ARMED)motor[i]=MINCOMMAND;}}voidwriteMotors(){OCR1A=motor[0]>>3;//pin9输出电机1号OCR1B=motor[1]>>3;//pin10输出电机2号OCR2A=motor[2]>>3;//pin11输出电机3号OCR2B=motor[3]>>3;//pin3输出电机4号}voidwriteAllMotors(int16_tmc){//所有电机输出设定为mcfor(uint8_ti=0;i<4;i++){motor[i]=mc;}writeMotors()
5、;}/*电调初始化函数,电调初始化完成后注释掉defined重新上传#ifdefined(ESC_CALIB_CANNOT_FLY)writeAllMotors(ESC_CALIB_HIGH);blinkLED(2,20,2);delay(4000);writeAllMotors(ESC_CALIB_LOW);blinkLED(3,20,2);while(1){delay(5000);blinkLED(4,20,2);#endif}exit;#endif*/2、遥控器发送/接收模块(2)相关代码//RX.h主要函数定义#include"Arduino.h"#defineR
6、C_CHANS8#definePCINT_PIN_COUNT5#definePCINT_RX_PORTPORTB#definePCINT_RX_MASKPCMSK0#definePCIR_PORT_BIT(1<<0)#defineRX_PC_INTERRUPTPCINT0_vect#defineRX_PCINT_PIN_PORTPINB#defineROLLPIN4//预定义各信道的名称#defineTHROTTLEPIN3#definePITCHPIN5#defineYAWPIN2#defineAUX1PIN6#defineAUX2PIN7#defineAUX3PIN1
7、//保留#defineAUX4PIN0//保留#definePCINT_RX_BITS(1<<1),(1<<2),(1<<3),(1<<4),(1<<0)voidconfigureReceiver();voidcomputeRC();uint16_treadRawRC(uint8_tchan);//初始信号读取函数//接收代码volatileuint16_trcValue[RC_CHANS]={1502,1502,1502,1502,1502,1502,1502,1502};staticuint8_trcChannel[RC_
此文档下载收益归作者所有