当前位置:
文档之家› 舵机、直流、步进电机的控制原理
舵机、直流、步进电机的控制原理
}
【君悦智控】
参考《Faulhaber空心杯直流减速电机码盘资料》
【君悦智控】
FAST PWM
【君悦智控】
void SetMotorSpeed (unsigned char ucChannel,char cSpeed) { unsigned char ucLocalChannel; char cLocalSpeed; cLocalSpeed=cValueLimit(cSpeed,-100,100); switch (ucLocalChannel) { case 1: #ifdef MOTOR1 if (cLocalSpeed>0) OUT2=1; else OUT2=0; if (cLocalSpeed==0) { //为零则关闭输出 } TCCR0A&=~0x80; M1Velocity=0;
【君悦智控】
【君悦智控】
【君悦智控】
【君悦智控】
EN 1 1
A1 1 1
A2 0 0
状态 A B
nB
B
1
1
1
1
1
1
0
0
1
1
nA
nB
nA
正转: A->B->nA->nB 反转: A->nB->nA->B
【君悦智控】
void SetStepMotor (unsigned char ucChannel,char cSpeed,int iStep) { #ifdef STEPMOTOR1 if (ucLocalChannel==1) { OCR0A=210 - 2*ucLocalSpeed; if (iStep==0) {iStepMotor1Step=0; TIMSK0&=0xfd;} else {iStepMotor1Step=iStep;TIMSK0|=0x02;} } #endif
【君悦智
void GetRemoteKey(void) { static unsigned char ucCnt=0; unsigned char ucKey=0; ucKey=ucSerialIn(); if (ucKey==0) ucCnt=0; else if (ucCnt==1) { KeyProc(ucKey); } ucCnt++; if (ucCnt>=20) ucCnt=0; }
EN 0 1 1 1 1
A1 0 1 0 1
A2 0 0 1 1
状态 不转 不转 正转 反转 制动
【君悦智控】
EN 0 1 1 1
A1 0 1 0
A2 0 0 1
状态 不转 不转 正转 反转 EN 0 1 DIR 0 状态 不转 正转
1
void KeyProc(unsigned char ucKey) { switch (ucKey) { case 0b00000001: break; case 0b00000010: break; case 0b00000011: \\12组合键 break; case 0b00001100: \\34组合键 break; default:break; }
【君悦智控】
case 7:
PWM7=0; OCR1AH=1000>>8; OCR1AL=1000; uiTimeCnt+=1000; ucT1CompareCnt++; break; case 8: OCR1AH=(18432-uiTimeCnt)>>8; OCR1AL=(18432-uiTimeCnt)&0XFF; ucT1CompareCnt=0; T20ms_Routine(); break; default: ucT1CompareCnt=0; break; } UpdatePWM(); }
TCCR1B|=0x0A; TIMSK1|=0x02;
ucT1CompareCnt++;
break; case 1: PWM1=0; PWM2=1; OCR1AH=SteeringMotor2Data>>8; OCR1AL=SteeringMotor2Data; uiTimeCnt+=SteeringMotor2Data; ucT1CompareCnt++; break;
#define SCK PORTB.5 #define SDI PINB.4 #define L1 PORTB.0
1; 1; 1; 1; 1; 1; 1;
【君悦智控】
正电流:正转 反电流:反转
【君悦智控】
1
反转
1
1
1
制动
Out1->EN1 OUT2->DIR1
Out3->EN2 Out4->DIR2
【君悦智控】
EN 0 1 1
DIR 1 0
状态 不转 正转 反转
T=1ms f= 1kHz
【君悦智控】
0.5ms<T<2.5ms 0<Angle<180
【君悦智控】
【君悦智控】
interrupt [TIM1_COMPA] void timer1_compa_isr(void) { static unsigned char ucT1CompareCnt=0; static unsigned int uiTimeCnt=0; switch (ucT1CompareCnt) { case 0: PWM1=1; OCR1AH=SteeringMotor1Data>>8; OCR1AL=SteeringMotor1Data; uiTimeCnt=SteeringMotor1Data;
#ifdef MOTOR1 DDRM1V=1; TCCR0A|=0x83; TCCR0B=0x03; SetMotorSpeed(1,0); #endif
else if(cabs(cLocalSpeed)>=100) //为100则全速输出
{
else
TCCR0A&=~0x80; M1Velocity=1;
}
【君悦智控】
interrupt [TIM0_COMPA] void timer0_compa_isr(void) {
static signed char ucIntCnt=1;
unsigned char ucTotalStep = 4; switch (ucIntCnt) { case 1:M1Velocity=1;OUT2=1;M2Velocity=0;OUT4=0; break; case 2:M1Velocity=0;OUT2=0;M2Velocity=1;OUT4=0; break; case 3:M1Velocity=1;OUT2=0;M2Velocity=0;OUT4=0; break; case 4:M1Velocity=0;OUT2=0;M2Velocity=1;OUT4=1; break; default:M1Velocity=0;M2Velocity=0;break; } #endif UpdateOut(); if (iStepMotor1Step>=30000) else if (iStepMotor1Step>0) else if (iStepMotor1Step==0) { TIMSK0&=0xfd; } else if (iStepMotor1Step>-30000) else {ucIntCnt--;} if(ucIntCnt<=0) ucIntCnt=ucTotalStep; else if (ucIntCnt>=ucTotalStep+1) ucIntCnt=1; } {iStepMotor1Step++,ucIntCnt--;} {ucIntCnt++;} {iStepMotor1Step--,ucIntCnt++;}
【君悦智控】
【君悦智控】
void UpdatePWM(void) { L1 = 0; SDO = 0;SCK = 0;SCK = 1; SDO = PWM7;SCK = 0;SCK = SDO = PWM6;SCK = 0;SCK = SDO = PWM5;SCK = 0;SCK = SDO = PWM4;SCK = 0;SCK = SDO = PWM3;SCK = 0;SCK = SDO = PWM2;SCK = 0;SCK = SDO = PWM1;SCK = 0;SCK = L1 = 1; L1 = 0; }
// PWM
}
{
#endif break; }; UpdateOut(); }
TCCR0A|=0x80; OCR0A=(float)cabs(cLocalSpeed)*2.55;
}
【君悦智控】
A
EN 1 1
DIR 0 0