-
Notifications
You must be signed in to change notification settings - Fork 28
/
MotorDriver.c
129 lines (123 loc) · 3.18 KB
/
MotorDriver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*****************************************************************************
* | File : MotorDriver.c
* | Author : Waveshare team
* | Function : Drive TB6612FNG
* | Info :
* TB6612FNG is a driver IC for DC motor with output transistor in
* LD MOS structure with low ON-resistor. Two input signals, IN1
* and IN2, can choose one of four modes such as CW, CCW, short
* brake, and stop mode.
*----------------
* | This version: V1.0
* | Date : 2018-09-04
* | Info : Basic version
*
******************************************************************************/
#include "lidarbot_base/MotorDriver.h"
#include "lidarbot_base/Debug.h"
UWORD ain1_value, ain2_value;
UWORD bin1_value, bin2_value;
/**
* Motor rotation.
*
* Example:
* Motor_Init();
*/
void Motor_Init(void)
{
PCA9685_Init(0x40);
PCA9685_SetPWMFreq(50);
}
/**
* Motor rotation.
*
* @param motor: Motor A and Motor B.
* @param dir: forward and backward.
* @param speed: Rotation speed. //(0~100)
*
* Example:
* @code
* Motor_Run(MOTORA, FORWARD, 50);
* Motor_Run(MOTORB, BACKWARD, 100);
*/
void Motor_Run(UBYTE motor, DIR dir, UWORD speed)
{
if(speed > 100)
speed = 100;
if(motor == MOTORA) {
DEBUG("Motor A Speed = %d\r\n", speed);
PCA9685_SetPwmDutyCycle(PWMA, speed);
if(dir == FORWARD) {
DEBUG("forward...\r\n");
PCA9685_SetLevel(AIN1, 0);
PCA9685_SetLevel(AIN2, 1);
ain1_value = 0;
ain2_value = 1;
} else {
DEBUG("backward...\r\n");
PCA9685_SetLevel(AIN1, 1);
PCA9685_SetLevel(AIN2, 0);
ain1_value = 1;
ain2_value = 0;
}
} else {
DEBUG("Motor B Speed = %d\r\n", speed);
PCA9685_SetPwmDutyCycle(PWMB, speed);
if(dir == FORWARD) {
DEBUG("forward...\r\n");
PCA9685_SetLevel(BIN1, 0);
PCA9685_SetLevel(BIN2, 1);
bin1_value = 0;
bin2_value = 1;
} else {
DEBUG("backward...\r\n");
PCA9685_SetLevel(BIN1, 1);
PCA9685_SetLevel(BIN2, 0);
bin1_value = 1;
bin2_value = 0;
}
}
}
/**
* Motor stop rotation.
*
* @param motor: Motor A and Motor B.
*
* Example:
* @code
* Motor_Stop(MOTORA);
*/
void Motor_Stop(UBYTE motor)
{
if(motor == MOTORA) {
PCA9685_SetPwmDutyCycle(PWMA, 0);
} else {
PCA9685_SetPwmDutyCycle(PWMB, 0);
}
}
/**
* Returns motor direction.
* 1 - forward
* 0 - backward
*
* @param motor: Motor A and Motor B
*
* Example:
* @code
* Motor_Direction(MOTORA);
*/
UBYTE Motor_Direction(UBYTE motor)
{
if(motor == MOTORA) {
if(ain1_value == 0 && ain2_value == 1)
return 1;
else if(ain1_value == 1 && ain2_value == 0)
return 0;
}
else {
if(bin1_value == 0 && bin2_value == 1)
return 1;
else if(bin1_value == 1 && bin2_value == 0)
return 0;
}
}