모터 바퀴 앞바퀴가 정회전 뒷바퀴가 역회전 하는 상황 때문에 앞바퀴 둘과 뒷바퀴 둘을 묶은 후 뒷바퀴 묶은 결선을 플러스 마이너스 교환연결하여 모든 바퀴를 정회전 시켰습니다.
하지만 앞바퀴 뒷바퀴를 한데 묶다보니 코딩예제에서 우회전 좌회전할때 오른쪽 왼쪽이아닌 앞 뒤가 멈춰버리는 현상이 있습니다.
바꾼 상태로 좌회전과 우회전을 할 수 있게 하려면 코딩을 어떤식으로 짜야할까요? 도저히 모르겠어서 문의드립니다.
*/
int RightMotor_E_pin = 5; // 오른쪽 모터의 Enable & PWM
int LeftMotor_E_pin = 6; // 왼쪽 모터의 Enable & PWM
int RightMotor_1_pin = 8; // 오른쪽 모터 제어선 IN1
int RightMotor_2_pin = 9; // 오른쪽 모터 제어선 IN2
int LeftMotor_3_pin = 10; // 왼쪽 모터 제어선 IN3
int LeftMotor_4_pin = 11; // 왼쪽 모터 제어선 IN4
int L_Line = A5; // 왼쪽 라인트레이서 센서는 A3 핀에 연결
int C_Line = A4; // 가운데 라인트레이서 센서는 A4 핀에 연결
int R_Line = A3; // 오른쪽 라인트레이서 센서는 A5 핀에 연결
int motor_s = 230; // 모터 속도 최대 255
int SL = 1;
int SC = 1;
int SR = 1;
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(RightMotor_E_pin, OUTPUT);
pinMode(LeftMotor_E_pin, OUTPUT);
pinMode(RightMotor_1_pin, OUTPUT);
pinMode(RightMotor_2_pin, OUTPUT);
pinMode(LeftMotor_3_pin, OUTPUT);
pinMode(LeftMotor_4_pin, OUTPUT);
Serial.begin(9600); // PC와의 시리얼 통신 9600bps로 설정
Serial.println("Welcome Eduino!");
}
void loop() {
int L = digitalRead(L_Line);
int C = digitalRead(C_Line);
int R = digitalRead(R_Line);
Serial.print("digital : "); Serial.print(L); Serial.print(", "); Serial.print(C); Serial.print(", "); Serial.print(R); Serial.print(" ");
if ( L == LOW && C == LOW && R == LOW ) { // 0 0 0
L = SL; C = SC; R = SR;
}
else if ( L == HIGH && C == LOW && R == HIGH ) { // 0 0 0
L = SL; C = SC; R = SR;
}
if ( L == 1 && C == 1 && R == 1 ) { // 0 1 0
motor_role(HIGH, HIGH, motor_s);
Serial.println("직진");
}
else if ( L == 0 && C == 1 && R == 0 ) { // 0 1 0
motor_role(HIGH, HIGH, motor_s);
Serial.println("직진");
}
else if (L == 0 && C == 1 && R == 1 ){ // 0 1 1
motor_role(HIGH, HIGH, motor_s);
Serial.println("우회전");
}
else if ( L == 0 && C == 0 && R == 1 ) { // , 0 0 1
Right_role(LOW, HIGH, motor_s);
Serial.println("우회전");
}
else if (L == 1 && C == 1 && R == 0 ){ // 1 1 0,
motor_role(HIGH, HIGH, motor_s);
Serial.println("좌회전");
}
else if ( L == 1 && C == 0 && R == 0 ) { // 1 0 0
Left_role(HIGH, LOW, motor_s);
Serial.println("좌회전");
}
else if ( L == 0 && C == 0 && R == 0 ) { // 1 1 1, 1 0 1
motor_role(HIGH, HIGH, 0);
Serial.println("정지");
}
SL = L; SC = C; SR = R;
}
void motor_role(int R_motor, int L_motor, int Speed) {
digitalWrite(RightMotor_1_pin, R_motor);
digitalWrite(RightMotor_2_pin, !R_motor);
digitalWrite(LeftMotor_3_pin, L_motor);
digitalWrite(LeftMotor_4_pin, !L_motor);
analogWrite(RightMotor_E_pin, Speed); // 우측 모터 속도값
analogWrite(LeftMotor_E_pin, Speed); // 좌측 모터 속도값
}
void Right_role(int R_motor, int L_motor, int Speed) {
digitalWrite(RightMotor_1_pin, R_motor);
digitalWrite(RightMotor_2_pin, !R_motor);
digitalWrite(LeftMotor_3_pin, L_motor);
digitalWrite(LeftMotor_4_pin, !L_motor);
analogWrite(RightMotor_E_pin, max(Speed * 0.4, 50)); // 우측 모터 속도값
analogWrite(LeftMotor_E_pin, min(Speed * 1.4, 255)); // 좌측 모터 속도값
}
void Left_role(int R_motor, int L_motor, int Speed) {
digitalWrite(RightMotor_1_pin, R_motor);
digitalWrite(RightMotor_2_pin, !R_motor);
digitalWrite(LeftMotor_3_pin, L_motor);
digitalWrite(LeftMotor_4_pin, !L_motor);
analogWrite(RightMotor_E_pin, min(Speed * 1.4, 255)); // 우측 모터 속도값
analogWrite(LeftMotor_E_pin, max(Speed * 0.2, 50)); // 좌측 모터 속도값
}
모터의 구동방향이 바뀌어 모터케이블을 하나로 묶어 사용할 경우 기존 제공되는 소스코드를 사용할 수 없습니다.
강의자료를 기준으로 모터만 결선했을 경우, 모터의 검정색 케이블선이 위로 향해있는 구조라면 모터드라이버의 결선되는 모터케이블의 검정색 빨강색 순서를 바꿔서 결선해주시기바랍니다.
결선이 완료되면, 소스코드의 motor_role 또는 motor_right / left의 코드의 신호 값을 수정해 테스트를 진행해주시기 바랍니다.
모터의 결선상태가 모두 확인이 가능한 상태의이미지를 추가적으로 첨부해주시면 답변에 더욱 도움이되는점 참고부탁드리겠습니다.
감사합니다.