Skip to content

Commit

Permalink
fix stepper motor error
Browse files Browse the repository at this point in the history
  • Loading branch information
ken committed Feb 12, 2022
1 parent 6bb2374 commit a3fed52
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 14 deletions.
21 changes: 15 additions & 6 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ MotorDriverBoard是由 [深圳市易创空间科技有限公司](www.emakefun.co



#### 1、只通过Uno的DC(6~12V)头单一电源给Uno主板,驱动板,舵机同时供电。
#### 1、只通过Uno的DC(7~12V)头单一电源给Uno主板,驱动板,舵机同时供电。

应用场景:

Expand Down Expand Up @@ -163,20 +163,29 @@ mServo1->writeServo(S1); /*设置舵机角度 0~180*/
#### [**stepper**](./arduino_lib/examples/motor_test/stepper/stepper.ino) 步进电机测试程序
```c++
Emakefun_MotorDriver mMotorDriver = Emakefun_MotorDriver(0x60);
Emakefun_StepperMotor *StepperMotor_1 = mMotorDriver.getStepper(1, 200);
/*初始化步进电机1,42步进电机走一步是1.8度,所以一圈的步数为200*/
mMotorDriver.begin(1526); /*设置频率为最大 1526HZ*/
mMotorDriver.begin(1600); /*设置频率为最大 1600*/
StepperMotor_1->setSpeed(400); /*设置步进电机每分钟转的圈数为400圈,速度越快力矩越小*/
StepperMotor_1->setSpeed(400); /*设置步进电机每分钟转的圈数为400圈, 速度越快力矩越小,这个速度不能太低,否则会抖动严重*/
StepperMotor_1->step(200, FORWARD, DOUBLE);
StepperMotor_1->step(200, FORWARD, SINGLE);
/*驱动步进电机按 DOUBLE(全步)的方式,FORWARD(前进)200步。*/
/*步进电机的驱动方式 全步DOUBLE、单步SINGLE、1/2步进INTERLEAVE这三种驱动方式(步进电机的驱动原理请查阅相关资料)*/
/*步进电机的驱动方式 全步DOUBLE、单步SINGLE、1/2步进INTERLEAVE这三种驱动方式(步进电机的驱动原理请查阅相关资料)*/
```

**接线图**![MotorDriverBoard_stepper](./doc/picture/ZH/stepper.png)
**接线图**

**不同步进电机的参数接线不一定一样,请一定要先确定步进电机每根线的颜色和对应A+ A- B+ B-相位(或者是A B C D)关系**

下图仅供接线参考

![MotorDriverBoard_stepper](./doc/picture/ZH/stepper.png)



#### [**encoder**](./arduino_lib/examples/motor_test/encoder/encoder.ino)四路编码电机测试程序

Expand Down
57 changes: 57 additions & 0 deletions arduino_lib/examples/base/gpio_step_test/gpio_step_test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "Emakefun_MotorDriver.h"

#define IN1 5 //A+
#define IN2 7 //A-
#define IN3 4 //B+
#define IN4 2 //B-
#define TIME 1000

void setup() {
Serial.begin(9600);
Serial.println("GPIO test!");
gpio.begin(1600);
}

void onesteps()
{
gpio.setPin(IN1, HIGH);
// gpio.setPin(IN2, LOW);
// gpio.setPin(IN3, LOW);
gpio.setPin(IN4, LOW);
delayMicroseconds(TIME);

gpio.setPin(IN1, LOW);
// gpio.setPin(IN2, LOW);
gpio.setPin(IN3, HIGH);
// gpio.setPin(IN4, LOW);
delayMicroseconds(TIME);

gpio.setPin(IN3, LOW);
gpio.setPin(IN2, HIGH);
// gpio.setPin(IN1, LOW);
// gpio.setPin(IN4, LOW);
delayMicroseconds(TIME);

gpio.setPin(IN2, LOW);
// gpio.setPin(IN1, LOW);
// gpio.setPin(IN3, LOW);
gpio.setPin(IN4, HIGH);
delayMicroseconds(TIME);
}

void StepStop()
{
gpio.setPin(IN1, LOW);
gpio.setPin(IN2, LOW);
gpio.setPin(IN3, LOW);
gpio.setPin(IN4, LOW);
}

void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < 200; i++) {
onesteps();
}
StepStop();
delay(2000);
}
4 changes: 2 additions & 2 deletions arduino_lib/examples/motor_test/stepper/stepper.ino
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "Emakefun_MotorDriver.h"

Emakefun_MotorDriver mMotorDriver = Emakefun_MotorDriver(0x40);
Emakefun_MotorDriver mMotorDriver = Emakefun_MotorDriver(0x60);

Emakefun_StepperMotor *StepperMotor_1 = mMotorDriver.getStepper(STEPPER1, 200);
Emakefun_StepperMotor *StepperMotor_2 = mMotorDriver.getStepper(STEPPER2, 200);

void setup()
{
Serial.begin(9600);
mMotorDriver.begin(1526);
mMotorDriver.begin(1600);
StepperMotor_1->setSpeed(400);
StepperMotor_2->setSpeed(400);
}
Expand Down
18 changes: 14 additions & 4 deletions arduino_lib/src/Emakefun_MotorDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,20 @@ Emakefun_StepperMotor *Emakefun_MotorDriver::getStepper(uint8_t num, uint16_t st
uint8_t ain1, ain2, bin1, bin2;
if (num == 0) {
ain1 = 8; ain2 = 10;
bin1 = 13; bin2 = 11;
#ifdef MOTOR_DRIVER_V5_2
bin1 = 11; bin2 = 13;
#else
bin1 = 13; bin2 = 11;
#endif

} else if (num == 1) {
ain1 = 5; ain2 = 7;
bin1 = 2; bin2 = 4;
#ifdef MOTOR_DRIVER_V5_2

bin1 = 4; bin2 = 2;
#else
bin1 = 2; bin2 = 4;
#endif
}
steppers[num].AIN1pin = ain1;
steppers[num].AIN2pin = ain2;
Expand Down Expand Up @@ -651,8 +661,8 @@ uint8_t Emakefun_StepperMotor::onestep(uint8_t dir, uint8_t style) {
//Serial.println("BIN1 1");
MC->setPWM(BIN1pin, 4096);
} else {
Serial.println("BIN1 0");
//MC->setPin(BIN1pin, LOW);
//Serial.println("BIN1 0");
MC->setPin(BIN1pin, LOW);
}
}
if (chang_state&0x4) {
Expand Down
5 changes: 3 additions & 2 deletions arduino_lib/src/Emakefun_MotorDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
#include <inttypes.h>

//#define MOTORDEBUG
#define MOTOR_DRIVER_V5_2

#define MICROSTEPS 16 // 8 or 16
#define MICROSTEPS 8 // 8 or 16

#define MOTOR1_A 2
#define MOTOR1_B 3
Expand Down Expand Up @@ -65,7 +66,7 @@
#define E4 4

#define STEPPER1 1
#define STEPPER1 2
#define STEPPER2 2

class Emakefun_MotorDriver;
/** Object that controls and keeps state for a Servo */
Expand Down

0 comments on commit a3fed52

Please sign in to comment.