智能车制作
标题:
舵机不转
[打印本页]
作者:
光电1233
时间:
2016-1-15 19:29
标题:
舵机不转
/*! 引脚连接说明
UART0
TX A14
RX A15
CCD
A0 E16 ADC0
SI E22
CLK E23
舵机PWM
TPM1_CH0 A12
电机PWM
TPM0_CH0 C1
TPM0_CH1 C2
TPM0_CH2 C3
TPM0_CH3 C4
*/
#include "common.h"
#include "include.h"
//电机相关
#define MOTOR_TPM TPM0
#define MOTOR1_PWM TPM_CH0
#define MOTOR2_PWM TPM_CH1
#define MOTOR3_PWM TPM_CH2
#define MOTOR4_PWM TPM_CH3
#define MOTOR1_PWM_IO TPM0_CH0
#define MOTOR2_PWM_IO TPM0_CH1
#define MOTOR3_PWM_IO TPM0_CH2
#define MOTOR4_PWM_IO TPM0_CH3
#define MOTOR_HZ (20*1000)
//舵机相关
#define S3010_TPM TPM1 //
#define S3010_CH TPM_CH0 // PTB0
#define S3010_HZ (100)
uint8 CCD_BUFF[TSL1401_SIZE]; //存储CCD图像的数组
uint8 s3010_middle = 73; //舵机中心值:对应的PWM占空比为(73/1000) 精度为1000u
uint8 picture_middle = 64;//图像中心值
uint8 leftline = 1; //赛道左边界
uint8 rightline = 127; //赛道右边界
struct s3010_pid //舵机PD控制
{
int err; //定义偏差值
int err_last; //定义上一个偏差值
float Kp; //定义比例系数
float Kd;//定义微分系数
}pid;
void PID_init(); //舵机PD算法初始化
uint32 PID_realize(int x);//舵机PD实现初始化
void PIT_IRQHandler();
void vcan_sendccd(uint8 *ccdaddr, uint32 ccdsize);
void bin(uint8 * bin, uint8 len);
void main(void)
{
uint8 time = 8; // 修改 PIT0 的定时时间即可修改曝光时间
uint32 s3010_pwm = s3010_middle;
tpm_pwm_init(MOTOR_TPM, MOTOR1_PWM,MOTOR_HZ,100); //初始化 电机 PWM
tpm_pwm_init(MOTOR_TPM, MOTOR2_PWM,MOTOR_HZ,100); //初始化 电机 PWM
tpm_pwm_init(MOTOR_TPM, MOTOR3_PWM,MOTOR_HZ,100); //初始化 电机 PWM
tpm_pwm_init(MOTOR_TPM, MOTOR4_PWM,MOTOR_HZ,100); //初始化 电机 PWM
tsl1401_set_addrs(TSL1401_MAX,CCD_BUFF); //初始化线性CCD
tsl1401_init(time); //初始化 线性CCD ,配置 中断时间为 time
//配置 中断函数 和 使能中断
set_vector_handler(PIT_VECTORn,PIT_IRQHandler);
enable_irq(PIT_IRQn);
tpm_pwm_init(S3010_TPM, S3010_CH,S3010_HZ,100); //初始化 舵机 PWM
while(1)
{
tsl1401_get_img();//采集 线性CCD 图像
bin((uint8 *)&CCD_BUFF[0], TSL1401_SIZE); //处理图像,找赛道边界
s3010_pwm = PID_realize(64 - picture_middle);
tpm_pwm_duty(S3010_TPM, S3010_CH,s3010_pwm);
DELAY_MS(200);
复制代码
欢迎光临 智能车制作 (http://dns.znczz.com/)
Powered by Discuz! X3.2