본문 바로가기
2022-2/마이크로프로세서

7. Motor Drive

by 철없는민물장어 2022. 11. 7.
728x90
Motor drive
  • step motor
  • servo motor
  • DC motor

 

step motor

내부적으로 자력을 이용하여 모터를 회전한다.

 

동서남북으로 코일이 있어 전류를 주는쪽으로 회전된다고 생각하면 이해하기 편할 것 같다.

이 동서남북 역할을 하는 핀들을 이용하여 특정 패턴의 펄스를 순차적으로 적용하여, 정해진 방향과 각도로 움직인다.

이 펄스 주파수에 의해 모터 스피드가 결정된다.

step motor drive module을 이용한다.

 

포텐쇼미터를 돌려 스탭모터 회전시키기

//스탭 모터 신호핀 설정
int motorPin1=8;
int motorPin2=9;
int motorPin3=10;
int motorPin4=11;

//포텐쇼미터 핀. 아날로그임
int potentioMeterPin=0;

int motorSpeed;//스텝 사이의 지연시간. 4500~1000의 범위를 가짐
int motorSpeedPercent;//속도를 0~100%로 표현함

int steps[]={B1000,B1100,B0100,B0110,B0010,B0011,B0001,B1001,B0000}

int stopRange=100;

void motorSignalOutput(int out)
{
	//out 변수에 해당하는 모터시그날 출ㄹㄹㄹ력
	digitalWrite(motorPin0, bitRead(steps[out],0);
	digitalWrite(motorPin1, bitRead(steps[out],1);
	digitalWrite(motorPin2, bitRead(steps[out],2);
	digitalWrite(motorPin3, bitRead(steps[out],3);
}

void motorStop()
{
	motorSignalOut(8);
}

void clockwise()
{
	for(int i=7;i>=0;i--)
    {
    	motorSignalOutput(i);
        delayMicroseconds(motorSpeed);
    }
}

void counterClockwise()
{
	for(int i=0;i<8;i++)
    {
    	motorSignalOutput(i);
        delayMicroseconds(motorSpeed);
    }
}

void setup()
{
	//모터 신호핀을 출력으로 설정
	pinMode(motorPin1,OUTPUT);
	pinMode(motorPin2,OUTPUT);
	pinMode(motorPin3,OUTPUT);
	pinMode(motorPin4,OUTPUT);
    
    Serial.begin(9600);
}

void loop()
{
	int potentioMeter=analogRead(potentioMeterPin);//포텐쇼미터 값 읽기
    
    if(potentioMeter >= 512+(stopRange/2))//시계방향으로 돌린경우
    {
    	//값을 4500~1000범위로 변경함. 큰 값일수록 작아지게 매핑함(지연시간이므로)
    	motorSpeed=map(potentioMeter,512+(stopRange/2),1023, 4500,1000);
        
        motorSpeedPercent=map(motorSpeed,4500,1000,1,100);//속도를 퍼센트로. 
        
        Serial.print("CW Motor speed:");
        Serial.print(motorSpeedPercent);
        Serial.print("%");
        
        clockwise();
    }
    else if(potentioMeter <= 512-(stopRange/2))
    {
    	motorSpeed=map(potentioMeter,512-(stopRange/2),0, 4500,1000);
        
        motorSpeedPercent=map(motorSpeed,4500,1000,1,100);//속도를 퍼센트로. 
        
        Serial.print("CCW Motor speed:");
        Serial.print(motorSpeedPercent);
        Serial.print("%");
    	
        counterClockwise();
    }
    else
    {
    	Serial.println("Motor Stop");
        motorStop();
    }
}

 

servo motor

세밀하게 회전각 등을 조정가능하다.

로봇 팔 등에 쓰인다.

 

pulse width가 회전축을 지정해주는 역할이라, 움직이려면 pulse width를 가변적으로 지정해주어야 함.

 

ServoMotorName.attach(pin number);
ServoMotorName.attach(pin number, minimum pulse, maximum pulse);

ServoMotorName.write(angle);//해당 각으로 이동

 

포텐쇼미터 돌려 서보모터 회전시키기

#include <Servo.h>

Servo motor1;
int servoMotorPin=9;
int potentioMeterPin=0;

int motorAngle;
int motorAngleOld;

void setup()
{
	motor1.attach(servoMotorPin,600,2400);
    Serial.begin(9600);
}

void loop()
{
	//포텐쇼미터 값을 읽어 그만큼 서보모터 회전시키기
	int potentioMeter = analogRead(potentioMeterPin);
    motorAngle=map(potentioMeter,0,1023,0,180);
    motor1.write(motorAngle);
    
    if(motorAngle != motorAngleOld)
    {
    	Serial.print("ServoMotor Angle is:");
        Serial.print(motorAngle);
    }
    motorAngleOld=motorAngle;
    delay(20);
}

 

 

DC Motor

선풍기 등에 쓰이는 모터. 전류의 방향에 따라 회전방향이 바뀌고, 전류 크기에 따라 속도가 변하지만

컨트롤이 거의 불가능하여 단순히 윙윙 돌아가는 형태이다.

motor drive module을 이용하면 회전방향과 속도 조절을 할 수 있다.

int potentioMeterPin=0;
int ENAPin=3;
int in1Pin=2;
int in2Pin=4;
int motorPWM;
int motorVelocity;

void setup()
{
	Serial.begin(9600);
    pinMode(ENAPin,OUTPUT);
    pinMode(in1Pin,OUTPUT);
    pinMode(in2Pin,OUTPUT);
}

void loop()
{
	int potentioMeter = analogRead(potentioMeterPin);
    
    if((potentioMeter>=0)&&(potentioMeter<=500))
    {
    	motorPWM=map(potentioMeter,500,0,0,255);
        analogWrite(ENAPin,motoerPWM);
        
        //방향조절. CW
        digitalWrtie(in1Pin,HIGH);
        digitalWrite(in2Pin,LOW);
        
        motorVelocity=map(potentioMeter,500,0,0,100);
        Serial.print("CW ");
        Serial.print(motorVelocity);
        Serial.println("%");
    }
    else if((potentioMeter>=524)&&(potentioMeter<=1023))
    {
    	motorPWM=map(potentioMeter,524,1023,0,255);
        analogWrite(ENAPin,motoerPWM);
        
        //방향조절. CCW
        digitalWrtie(in1Pin,LOW);
        digitalWrite(in2Pin,HIGH);
        
        motorVelocity=map(potentioMeter,524,1023,0,100);
        Serial.print("CW ");
        Serial.print(motorVelocity);
        Serial.println("%");
    }
    else
    {
    	analogWrite(ENAPin,0);
        digitalWrite(in1Pin,LOW);
        digitalWrite(in2Pin,LOW);
        
        Serial.print("STOP");
    }
    delay(100);
}
728x90

'2022-2 > 마이크로프로세서' 카테고리의 다른 글

9. Various Modules  (0) 2022.11.18
8. Infrared Remote Control  (0) 2022.11.14
6. Analog Input  (1) 2022.11.01
5. Digital Signal Input  (0) 2022.10.23
아두이노와 기타등등 알아보기  (0) 2022.10.14

댓글