没有合适的资源?快使用搜索试试~ 我知道了~
首页两轮自平衡小车AVR程序
两轮自平衡小车AVR程序

AVR单片机的两轮自平衡小车控制程序,移用需自己调试。带PID调节和滤波代码,带注释。
资源详情
资源评论
资源推荐

#include "DSP281x_Device.h" // Headerfile Include File
#include "DSP281x_Examples.h" //
#define HIST 16
unsigned int k=0;
static float adc_current_samples[6][HIST];//定义行为 6,列为 16 的二维数组
static float adclo=0.0;
static Uint16 totsamples_0;
unsigned int count;
#define OCRMAX 1023
unsigned int pwm;
static float left_cof=1.00;
static float right_cof=1.00;
struct gyro_filter pitch_filter;
static Uint16 mode=0x00;//0x00----balance mode; 0x01-------assist mode
static Uint16 still=0x00; //still=1.0, no move; still=0.0,move.
static Uint16 stand=0x00;
static Uint16 last_balance_s0;
static Uint16 samples[6],ticks;
static float left_steer_cof=0.05;
static float right_steer_cof=0.05;
static float steer_lim=0.13;
static float initial_angle=0.0000874;
static float hard_speed_lim=0.90;
static float bus_current;
static float cmd;
static float lpf_angle;
static float lpf_angrate;
static float lpf_steer_knob;
static float left_motor_pwm, right_motor_pwm, steer_cmd;
static float left_motor_pwm, right_motor_pwm, steer_cmd;
static float batt_voltage1, ay, ax, in_steer_knob;
static float pitch_rate;
1

static float interval;
static float timer0_seconds_conv;
static float p_gain;
static float d_gain;
static float p1_gain;
static float d1_gain;
static float p0_gain;
static float d0_gain;
static float tp0,td0,tp1,td1,tp,td;
static float d_step=0.0001;
static float p_step;
static float cor_bat;
static float p_cmd,d_cmd;//next_cmd;
static float sample_conv = 1.0/1024.0/(float)HIST*5.0;//=3.052exp(-4)
static float sample_conv1 = 1.0/1024.0/(float)HIST*2.07;
static float sample_conv2 = 1.0/1024.0/(float)HIST*1.9;
static float sample_conv3 = 1.0/1024.0/(float)HIST*333.3*2.5;
interrupt void T2_AD_isr(void);
void InitEv(void);
void InitAdc(void);
void InitGpio(void);
struct gyro_filter {
float angle;
float ay_bias;
//float ax_bias;
float rate_bias;
float steer_knob_bias;
float rate;
float steer_knob;
float curr_bias;
float curr;
Uint16 inited;
};
void check_mode ()
{if((!pitch_rate) && (!in_steer_knob)) mode = 0x00;
else mode = 0x01; }
2

void delay (void)
{ Uint16 xt;
static Uint16 k_temp;
for (xt=0;xt<15;xt++){
k_temp++; }
}
void delay1(void)
{ Uint16 xt;
static Uint16 k_temp;
for (xt=0;xt<15000;xt++){
k_temp++; }
}
int adc_collect_samples(Uint16 *samples, Uint16 *lasts0)
{
unsigned int i,j;
if (*lasts0 == totsamples_0) {
return 0;//if no new adc samples, return with 0
}
*lasts0 = totsamples_0;
for (i=0; i<6; i++) {
Uint16 tot=0;
for (j=0; j<HIST; j++)
{
tot+=adc_current_samples[i][j];
}
samples[i] = tot;//samples[i]=Σadc_current_samples[i][j](j=1,2,...,ADC_HIST)
}
return 1;
}
Uint16 bat_judge( float x)
{
if(x>4.30) return 0x5;//25.8
else if(x>4.18) return 0x4;//25.1
else if(x>4.07) return 0x3;//24.4
else if(x>3.95) return 0x2;//23.7
3

else if(x>3.83) return 0x1;//23.0
else return 0x0;
}
void lpf_update(float *state, float tc, float interval, float input)
{
float frac=interval/tc;
if (frac>1.0) frac=1.0;
*state = input*frac + *state * (1.0-frac);
}
float fmax(float a, float b)
{
if (a>=b) {
return a;
} else {
return b;
}
}
float fmin(float a, float b)
{
if (a<=b) {
return a;
} else {
return b;
}
}
float flim(float x, float lo, float hi)
{
if (x>hi) return hi;
if (x<lo) return lo;
return x;
}
void gyro_init(struct gyro_filter *it)
{ it->inited =0x00;
it->angle=0.0;
it->ay_bias=0.0;
// it->ax_bias=0.0;
it->rate_bias=0.0;
it->steer_knob_bias=0.0;
4
剩余17页未读,继续阅读













安全验证
文档复制为VIP权益,开通VIP直接复制

评论1