Skip to content

Commit 746fdcd

Browse files
committed
Update CA_MotorShield Library to v1.0.1 with Stability Improvements and Bug Fixes
Key changes in v1.0.1: Improved motor control logic for CodingArray RC cars enhances the stability of RC cars. Minimized errors related to I2C communication and the mainboard's reset issues during motor direction changes. This issue occurs when using a single power source for both the mainboard and the motor shield during motor direction changes. Bug fixes for existing examples and updates for additional examples have been made. Users are encouraged to update to v1.0.1 to take advantage of these improvements and continue to share their feedback for future enhancements.
1 parent 3722a76 commit 746fdcd

File tree

10 files changed

+558
-185
lines changed

10 files changed

+558
-185
lines changed

examples/Accel_ConstantSpeed/Accel_ConstantSpeed.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
* MIT License - all text here must be included in any redistribution.
2323
*
2424
*/
25-
25+
#include <AccelStepper.h>
2626
#include <CA_MotorShield.h>
2727

2828
// Create the motor shield object with the default I2C address
29-
CA_MotorShield STEP_Motor(0x60); // Adjust the I2C address as needed
29+
CA_MotorShield STEP_Motor = CA_MotorShield(); // default address 0x60
30+
// Or, create it with a different I2C address (say for stacking)
31+
// CA_MotorShield STEP_Motor = CA_MotorShield(0x61);
32+
// CA_MotorShield STEP_Motor(0x62); // Adjust the I2C address as needed
3033

3134
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
3235
// to motor port #1 and #2 respectively
@@ -40,7 +43,7 @@ void backwardStep() {
4043
}
4144

4245
// Use functions to step
43-
CA_Stepper Astepper1 = CA_Stepper(forwardStep, backwardStep);
46+
AccelStepper Astepper1(forwardStep, backwardStep);
4447

4548
void setup() {
4649
Serial.begin(115200); // set up Serial library at 115200 bps

examples/Accel_MultiStepper/Accel_MultiStepper.ino

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,35 @@
2020
* MIT License - all text here must be included in any redistribution.
2121
*
2222
*/
23-
23+
#include <AccelStepper.h>
2424
#include <CA_MotorShield.h>
2525

2626
// Initialize CA_MotorShield objects for two shields
2727
CA_MotorShield AFMSbot(0x61); // Rightmost jumper closed
2828
CA_MotorShield AFMStop(0x60); // Default address, no jumpers
2929

30-
// Connect two steppers to the top shield
30+
// Connect two steppers with 200 steps per revolution (1.8 degree)
31+
// to the top shield
3132
CA_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
3233
CA_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);
3334

34-
// Connect one stepper to the bottom shield
35+
// Connect one stepper with 200 steps per revolution (1.8 degree)
36+
// to the bottom shield
3537
CA_StepperMotor *myStepper3 = AFMSbot.getStepper(200, 2);
3638

37-
// Function wrappers for each motor
39+
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
40+
// wrappers for the first motor!
3841
void forwardStep1() { myStepper1->onestep(FORWARD, SINGLE); }
3942
void backwardStep1() { myStepper1->onestep(BACKWARD, SINGLE); }
4043
void forwardStep2() { myStepper2->onestep(FORWARD, DOUBLE); }
4144
void backwardStep2() { myStepper2->onestep(BACKWARD, DOUBLE); }
4245
void forwardStep3() { myStepper3->onestep(FORWARD, INTERLEAVE); }
4346
void backwardStep3() { myStepper3->onestep(BACKWARD, INTERLEAVE); }
4447

45-
// Wrap the steppers in CA_Stepper objects
46-
CA_Stepper stepper1(forwardStep1, backwardStep1);
47-
CA_Stepper stepper2(forwardStep2, backwardStep2);
48-
CA_Stepper stepper3(forwardStep3, backwardStep3);
48+
// Now we'll wrap the 3 steppers in an AccelStepper object
49+
AccelStepper stepper1(forwardStep1, backwardStep1);
50+
AccelStepper stepper2(forwardStep2, backwardStep2);
51+
AccelStepper stepper3(forwardStep3, backwardStep3);
4952

5053
void setup() {
5154
AFMSbot.begin(); // Start the bottom shield

examples/DCMotorTest/DCMotorTest.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010

1111
#include <CA_MotorShield.h>
1212

13-
CA_MotorShield DC_Motor(0x60); // Create an object and pass the parameter
13+
CA_MotorShield dcMotor(0x60); // Create an object and pass the parameter
1414

15-
CA_DCMotor *motor1 = DC_Motor.getMotor(1); // Get motor 1
16-
CA_DCMotor *motor2 = DC_Motor.getMotor(2); // Get motor 2
17-
CA_DCMotor *motor3 = DC_Motor.getMotor(3); // Get motor 3
18-
CA_DCMotor *motor4 = DC_Motor.getMotor(4); // Get motor 4
15+
CA_DCMotor *motor1 = dcMotor.getMotor(1); // Get motor 1
16+
CA_DCMotor *motor2 = dcMotor.getMotor(2); // Get motor 2
17+
CA_DCMotor *motor3 = dcMotor.getMotor(3); // Get motor 3
18+
CA_DCMotor *motor4 = dcMotor.getMotor(4); // Get motor 4
1919

2020
void setup() {
2121
Serial.begin(115200);
2222

23-
DC_Motor.begin(); // Initialize the motor shield
24-
DC_Motor.setFrequency(1600); // Set the PWM frequency
23+
dcMotor.begin(); // Initialize the motor shield
24+
dcMotor.setFrequency(1600); // Set the PWM frequency
2525

2626
// Set motor1 to move forward and set its speed
2727
motor1->setSpeed(150);
@@ -60,7 +60,7 @@ void setup() {
6060
delay(500); // Wait for 500 milliseconds
6161

6262
// Reset all channels to their initial state
63-
DC_Motor.allChannelInitialize();
63+
dcMotor.allChannelInitialize();
6464
}
6565

6666
void loop() {

examples/ex01.LinearDrive_Ultrasonic4WD/ex01.LinearDrive_Ultrasonic4WD.ino

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ---------------------------------------------------------------------------
22
// Created by Coding Array - we@CodingArray.cc
33
// Copyright 2012 License: GNU GPL v3 http://www.gnu.org/licenses/gpl-3.0.html
4-
// Ultrasonic Obstacle Avoidance 4WD Rubber Wheel + 4WD Mecanum Wheel
4+
// Ultrasonic Obstacle Avoidance Example for 4WD Rubber Wheel + 4WD Mecanum Wheel
55
// ---------------------------------------------------------------------------
66

77
#include <CA_MotorShield.h> // 코딩어레이 모터 쉴드 라이브러리 포함
@@ -13,29 +13,34 @@
1313
#define MAX_DISTANCE 300 // 초음파 센서가 감지할 수 있는 최대 거리 설정 (300cm)
1414

1515
// 모터 속도 설정
16-
#define MIN_SPEED 80 // 모터가 돌 수 있는 최소 속도( TT모터 스타트 업 토크 = PWM 값 80 )
16+
#define MIN_SPEED 40 // 모터가 동작하기 시작하는 최소 속도, 속도 최대값을 100으로 조절할 때의 기준값
1717

18-
#define FORWARD_SPEED 150 // 전진 최대 속도(PWM 값), 초음파 센서 감지를 위해 보다 천천히 전진
19-
#define BACKWARD_SPEED 150 // 후진 최대 속도(PWM 값)
20-
#define TURN_SPEED 180 // 좌회전, 우회전 최대 속도(PWM 값) : 속도 값을 변경하면 회전 각도 변경
18+
#define FORWARD_SPEED 80 // 전진 시 설정 가능한 최대 속도, 속도 최대값 100 중 80으로 설정하여 안정적인 전진을 위함
19+
#define BACKWARD_SPEED 80 // 후진 시 설정 가능한 최대 속도, 속도 최대값 100 중 80으로 설정
20+
#define TURN_SPEED 100 // 좌회전우회전 시 설정 가능한 최대 속도, 속도 최대값을 100으로 설정
2121

2222
#define OBSTACLE_THRESHOLD 80 // 장애물을 감지하는 거리 임계값 (80cm)
2323

24-
// 모터 쉴드와 모터 초기화
25-
CA_MotorShield DC_Motor(0x60); // 모터 쉴드 객체 생성
26-
CA_DCMotor *motor1 = DC_Motor.getMotor(1); // 첫 번째 모터 연결
27-
CA_DCMotor *motor2 = DC_Motor.getMotor(2); // 두 번째 모터 연결
28-
CA_DCMotor *motor3 = DC_Motor.getMotor(3); // 세 번째 모터 연결
29-
CA_DCMotor *motor4 = DC_Motor.getMotor(4); // 네 번째 모터 연결
24+
// 모터 쉴드 객체를 생성하고 I2C 주소를 초기화합니다.
25+
CA_MotorShield dcMotor(0x60);
26+
27+
// 각 모터에 대한 포인터를 초기화합니다.
28+
CA_DCMotor *motor1 = dcMotor.getMotor(1); // 첫 번째 모터 연결
29+
CA_DCMotor *motor2 = dcMotor.getMotor(2); // 두 번째 모터 연결
30+
CA_DCMotor *motor3 = dcMotor.getMotor(3); // 세 번째 모터 연결
31+
CA_DCMotor *motor4 = dcMotor.getMotor(4); // 네 번째 모터 연결
3032

3133
// 초음파 센서 객체 생성
3234
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // 초음파 센서 설정
3335

34-
// RC카의 이동 방향을 나타내는 상수 정의
35-
#define C_LEFT 1 // 좌회전
36-
#define C_RIGHT 2 // 우회전
37-
#define C_FORWARD 3 // 전진
38-
#define C_BACKWARD 4 // 후진
36+
// 코딩어레이 스마트 RC카 방향 전환 제어 상수
37+
// 이 상수들은 메카넘 휠을 장착한 코딩어레이 스마트 RC카의 다양한 이동 및 회전 동작을 정의합니다.
38+
// 각 상수는 RC카의 특정한 동작 모드를 나타내며, 이를 통해 RC카를 다양한 방향으로 조작할 수 있습니다.
39+
#define CMD_RELEASE 0 // 정지: 코딩어레이 스마트 RC카를 정지시킵니다.
40+
#define CMD_FORWARD 1 // ↑: 코딩어레이 스마트 RC카를 전진시킵니다.
41+
#define CMD_BACKWARD 2 // ↓: 코딩어레이 스마트 RC카를 후진시킵니다.
42+
#define CMD_LEFT 3 // ←: 코딩어레이 스마트 RC카를 좌측으로 회전시킵니다.
43+
#define CMD_RIGHT 4 // →: 코딩어레이 스마트 RC카를 우측으로 회전시킵니다.
3944

4045
/**
4146
* @brief RC카의 초기 설정을 수행하는 함수입니다.
@@ -52,7 +57,8 @@ void setup() {
5257
Serial.begin(9600); // 시리얼 통신을 9600 보드레이트로 시작합니다. 이를 통해 컴퓨터와 데이터를 주고받을 수 있습니다.
5358
Serial.println("........Start........"); // 시리얼 모니터에 "........Start........"을 출력합니다. 이는 프로그램이 시작되었음을 나타냅니다.
5459

55-
if (!DC_Motor.begin()) { // 모터 쉴드를 기본 주파수인 1.6KHz로 시작합니다. 만약 모터 쉴드를 찾지 못하면 오류 메시지를 출력합니다.
60+
// 모터 쉴드를 초기화 및 모터의 동작 주파수를 1.6Khz로 설정합니다.
61+
if (!dcMotor.begin()) { // 모터 쉴드를 기본 주파수인 1.6KHz로 시작합니다. 만약 모터 쉴드를 찾지 못하면 오류 메시지를 출력합니다.
5662
Serial.println("모터 쉴드를 찾을 수 없습니다. 배선을 확인하세요."); // 모터 쉴드를 찾지 못했다는 메시지를 시리얼 모니터에 출력합니다.
5763
while (1)
5864
; // 무한 루프에 빠져 프로그램을 멈춥니다. 이는 모터 쉴드가 제대로 연결되지 않았을 경우를 나타냅니다.
@@ -86,8 +92,9 @@ void loop() {
8692
backwardLeftTurn(500); //0.5초간 대기하여 RC카가 회전 후 안정화될 수 있도록 합니다.
8793
} else {
8894
// 장애물이 멀리 있을 경우, RC카는 계속 전진합니다.
89-
setMotorsDirection(C_FORWARD, FORWARD_SPEED); // RC카를 전진 방향으로 설정하고 전진합니다.
95+
setMotorsDirection(CMD_FORWARD, FORWARD_SPEED); // RC카를 전진 방향으로 설정하고 전진합니다.
9096
delay(500); // 대기시간 동안 대기하여 RC카가 회전 후 안정화될 수 있도록 합니다.
97+
}
9198
}
9299

93100
/**
@@ -103,10 +110,10 @@ void loop() {
103110
*/
104111
void backwardLeftTurn(unsigned long delayTime) {
105112
StopAllMotors(); // RC카가 정지합니다.
106-
setMotorsDirection(C_BACKWARD, BACKWARD_SPEED); // RC카를 후진 방향으로 설정하고 후진 속도로 이동을 시작합니다.
113+
setMotorsDirection(CMD_BACKWARD, BACKWARD_SPEED); // RC카를 후진 방향으로 설정하고 후진 속도로 이동을 시작합니다.
107114
StopAllMotors(); // RC카가 정지합니다.
108115
delay(delayTime); // 대기시간 동안 대기하여 RC카가 회전 후 안정화될 수 있도록 합니다.
109-
setMotorsDirection(C_LEFT, TURN_SPEED); // RC카의 방향을 왼쪽으로 변경하고 회전 속도로 회전을 시작합니다.
116+
setMotorsDirection(CMD_LEFT, TURN_SPEED); // RC카의 방향을 왼쪽으로 변경하고 회전 속도로 회전을 시작합니다.
110117
StopAllMotors(); // RC카가 정지합니다.
111118
delay(delayTime); // 대기시간 동안 대기하여 RC카가 회전 후 안정화될 수 있도록 합니다.
112119
}
@@ -148,27 +155,27 @@ unsigned int measureDistance() {
148155
* 전진, 후진 등의 방향에 따라 모터들의 회전 방향을 설정합니다. 이를 통해 RC카가
149156
* 원하는 방향으로 움직일 수 있도록 합니다.
150157
*
151-
* @param direction 설정할 모터의 방향. C_LEFT, C_RIGHT, C_FORWARD, C_BACKWARD 중 하나를 사용합니다.
158+
* @param direction 설정할 모터의 방향. CMD_LEFT, CMD_RIGHT, CMD_FORWARD, CMD_BACKWARD 중 하나를 사용합니다.
152159
* @param speed 모터가 도달할 속도. 이 속도는 감속 또는 증가됩니다.
153160
*/
154161
void setMotorsDirection(uint8_t direction, uint8_t speed) {
155162
// 모터의 방향을 설정합니다.
156-
if (direction == C_LEFT) {
163+
if (direction == CMD_LEFT) {
157164
motor1->run(BACKWARD); // 1번 모터를 뒤로 돌려 왼쪽으로 회전시킵니다.
158165
motor2->run(BACKWARD); // 2번 모터도 뒤로 돌려 왼쪽으로 회전시킵니다.
159166
motor3->run(FORWARD); // 3번 모터를 앞으로 돌려 왼쪽으로 회전시킵니다.
160167
motor4->run(FORWARD); // 4번 모터도 앞으로 돌려 왼쪽으로 회전시킵니다.
161-
} else if (direction == C_RIGHT) {
168+
} else if (direction == CMD_RIGHT) {
162169
motor1->run(FORWARD); // 1번 모터를 앞으로 돌려 오른쪽으로 회전시킵니다.
163170
motor2->run(FORWARD); // 2번 모터도 앞으로 돌려 오른쪽으로 회전시킵니다.
164171
motor3->run(BACKWARD); // 3번 모터를 뒤로 돌려 오른쪽으로 회전시킵니다.
165172
motor4->run(BACKWARD); // 4번 모터도 뒤로 돌려 오른쪽으로 회전시킵니다.
166-
} else if (direction == C_FORWARD) {
173+
} else if (direction == CMD_FORWARD) {
167174
motor1->run(FORWARD); // 모든 모터를 앞으로 돌려 RC카가 전진하도록 합니다.
168175
motor2->run(FORWARD);
169176
motor3->run(FORWARD);
170177
motor4->run(FORWARD);
171-
} else if (direction == C_BACKWARD) {
178+
} else if (direction == CMD_BACKWARD) {
172179
motor1->run(BACKWARD); // 모든 모터를 뒤로 돌려 RC카가 후진하도록 합니다.
173180
motor2->run(BACKWARD);
174181
motor3->run(BACKWARD);

0 commit comments

Comments
 (0)