-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathBT2560.ino
178 lines (162 loc) · 5.12 KB
/
BT2560.ino
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//电机引脚
#define PWMA 12 //A电机转速
#define DIRA1 34
#define DIRA2 35 //A电机方向
#define PWMB 8 //B电机转速
#define DIRB1 37
#define DIRB2 36 //B电机方向
#define PWMC 9 //C电机转速
#define DIRC1 43
#define DIRC2 42 //C电机方向
#define PWMD 5 //D电机转速
#define DIRD1 A4 //26
#define DIRD2 A5 //27 //D电机方向
#define MOTORA_FORWARD(pwm) do{digitalWrite(DIRA1,LOW); digitalWrite(DIRA2,HIGH);analogWrite(PWMA,pwm);}while(0)
#define MOTORA_STOP(x) do{digitalWrite(DIRA1,LOW); digitalWrite(DIRA2,LOW); analogWrite(PWMA,0);}while(0)
#define MOTORA_BACKOFF(pwm) do{digitalWrite(DIRA1,HIGH);digitalWrite(DIRA2,LOW); analogWrite(PWMA,pwm);}while(0)
#define MOTORB_FORWARD(pwm) do{digitalWrite(DIRB1,LOW); digitalWrite(DIRB2,HIGH);analogWrite(PWMB,pwm);}while(0)
#define MOTORB_STOP(x) do{digitalWrite(DIRB1,LOW); digitalWrite(DIRB2,LOW); analogWrite(PWMB,0);}while(0)
#define MOTORB_BACKOFF(pwm) do{digitalWrite(DIRB1,HIGH);digitalWrite(DIRB2,LOW); analogWrite(PWMB,pwm);}while(0)
#define MOTORC_FORWARD(pwm) do{digitalWrite(DIRC1,LOW); digitalWrite(DIRC2,HIGH);analogWrite(PWMC,pwm);}while(0)
#define MOTORC_STOP(x) do{digitalWrite(DIRC1,LOW); digitalWrite(DIRC2,LOW); analogWrite(PWMC,0);}while(0)
#define MOTORC_BACKOFF(pwm) do{digitalWrite(DIRC1,HIGH);digitalWrite(DIRC2,LOW); analogWrite(PWMC,pwm);}while(0)
#define MOTORD_FORWARD(pwm) do{digitalWrite(DIRD1,LOW); digitalWrite(DIRD2,HIGH);analogWrite(PWMD,pwm);}while(0)
#define MOTORD_STOP(x) do{digitalWrite(DIRD1,LOW); digitalWrite(DIRD2,LOW); analogWrite(PWMD,0);}while(0)
#define MOTORD_BACKOFF(pwm) do{digitalWrite(DIRD1,HIGH);digitalWrite(DIRD2,LOW); analogWrite(PWMD,pwm);}while(0)
#define SERIAL Serial
#define LOG_DEBUG
#ifdef LOG_DEBUG
#define M_LOG SERIAL.print
#else
#define M_LOG
#endif
#define MAX_PWM 200
#define MIN_PWM 130
int Motor_PWM = 130;
//控制电机运动 宏定义
// ↑A-----B↑
// | ↑ |
// | | |
// ↑C-----D↑
void ADVANCE()
{
MOTORA_FORWARD(Motor_PWM);MOTORB_FORWARD(Motor_PWM);
MOTORC_FORWARD(Motor_PWM);MOTORD_FORWARD(Motor_PWM);
}
// ↓A-----B↓
// | | |
// | ↓ |
// ↓C-----D↓
void BACK()
{
MOTORA_BACKOFF(Motor_PWM);MOTORB_BACKOFF(Motor_PWM);
MOTORC_BACKOFF(Motor_PWM);MOTORD_BACKOFF(Motor_PWM);
}
// =A-----B↑
// | ↖ |
// | ↖ |
// ↑C-----D=
void LEFT_1()
{
MOTORA_FORWARD(Motor_PWM);MOTORB_STOP(Motor_PWM);
MOTORC_STOP(Motor_PWM);MOTORD_FORWARD(Motor_PWM);
}
// ↓A-----B↑
// | ← |
// | ← |
// ↑C-----D↓
void LEFT_2()
{
MOTORA_BACKOFF(Motor_PWM);MOTORB_FORWARD(Motor_PWM);
MOTORC_FORWARD(Motor_PWM);MOTORD_BACKOFF(Motor_PWM);
}
// ↓A-----B=
// | ↙ |
// | ↙ |
// =C-----D↓
void LEFT_3()
{
MOTORA_BACKOFF(Motor_PWM);MOTORB_STOP(Motor_PWM);
MOTORC_STOP(Motor_PWM);MOTORD_BACKOFF(Motor_PWM);
}
// ↑A-----B=
// | ↗ |
// | ↗ |
// =C-----D↑
void RIGHT_1()
{
MOTORA_FORWARD(Motor_PWM);MOTORB_STOP(Motor_PWM);
MOTORC_STOP(Motor_PWM);MOTORD_FORWARD(Motor_PWM);
}
// ↑A-----B↓
// | → |
// | → |
// ↓C-----D↑
void RIGHT_2()
{
MOTORA_FORWARD(Motor_PWM);MOTORB_BACKOFF(Motor_PWM);
MOTORC_BACKOFF(Motor_PWM);MOTORD_FORWARD(Motor_PWM);
}
// =A-----B↓
// | ↘ |
// | ↘ |
// ↓C-----D=
void RIGHT_3()
{
MOTORA_STOP(Motor_PWM);MOTORB_BACKOFF(Motor_PWM);
MOTORC_BACKOFF(Motor_PWM);MOTORD_STOP(Motor_PWM);
}
// =A-----B=
// | = |
// | = |
// =C-----D=
void STOP()
{
MOTORA_STOP(Motor_PWM);MOTORB_STOP(Motor_PWM);
MOTORC_STOP(Motor_PWM);MOTORD_STOP(Motor_PWM);
}
void UART_Control()
{
char Uart_Date=0;
if(SERIAL.available())
{
Uart_Date = SERIAL.read();
}
switch(Uart_Date)
{
case 'A': ADVANCE(); M_LOG("Run!\r\n"); break;
case 'B': RIGHT_1(); M_LOG("Right up!\r\n"); break;
case 'C': RIGHT_2(); M_LOG("Right!\r\n"); break;
case 'D': RIGHT_3(); M_LOG("Right down!\r\n"); break;
case 'E': BACK(); M_LOG("Run!\r\n"); break;
case 'F': LEFT_3(); M_LOG("Left down!\r\n"); break;
case 'G': LEFT_2(); M_LOG("Left!\r\n"); break;
case 'H': LEFT_1(); M_LOG("Left up!\r\n"); break;
case 'Z': STOP(); M_LOG("Stop!\r\n"); break;
case 'L': Motor_PWM = 240; break;
case 'M': Motor_PWM = 130; break;
}
}
void IO_init()
{
pinMode(PWMA, OUTPUT);
pinMode(DIRA1, OUTPUT);pinMode(DIRA2, OUTPUT);
pinMode(PWMB, OUTPUT);
pinMode(DIRB1, OUTPUT);pinMode(DIRB2, OUTPUT);
pinMode(PWMC, OUTPUT);
pinMode(DIRC1, OUTPUT);pinMode(DIRC2, OUTPUT);
pinMode(PWMD, OUTPUT);
pinMode(DIRD1, OUTPUT);pinMode(DIRD2, OUTPUT);
STOP();
}
void setup()
{
SERIAL.begin(9600);
IO_init();
// SERIAL.print("Start");
}
void loop()
{
UART_Control();//串口接收处理
//CAR_Control();//小车控制
}