forked from monkeylover1234/mesacmsrobotics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationFunctionSet_xxx0.cpp
176 lines (169 loc) · 5.7 KB
/
ApplicationFunctionSet_xxx0.cpp
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
#include "DeviceDriverSet_xxx0.h"
#include "MPU6050_getdata.h"
extern DeviceDriverSet_Motor AppMotor;
extern MPU6050_getdata AppMPU6050getdata;
/*运动方向控制序列*/
enum ConquerorCarMotionControl
{
Forward, //(1)
Backward, //(2)
Left, //(3)
Right, //(4)
LeftForward, //(5)
LeftBackward, //(6)
RightForward, //(7)
RightBackward, //(8)
stop_it //(9)
}; //direction方向:(1)、(2)、 (3)、(4)、(5)、(6)
struct Application_xxx
{
ConquerorCarMotionControl Motion_Control;
};
extern Application_xxx Application_ConquerorCarxxx0;
static void ApplicationFunctionSet_ConquerorCarLinearMotionControl(ConquerorCarMotionControl direction, uint8_t directionRecord, uint8_t speed, uint8_t Kp, uint8_t UpperLimit)
{
static float Yaw; //偏航
static float yaw_So = 0;
static uint8_t en = 110;
static unsigned long is_time;
if (en != directionRecord || millis() - is_time > 10)
{
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_void, /*speed_A*/ 0,
/*direction_B*/ direction_void, /*speed_B*/ 0, /*controlED*/ control_enable); //Motor control
AppMPU6050getdata.MPU6050_dveGetEulerAngles(&Yaw);
is_time = millis();
}
if(en != directionRecord )
{
en = directionRecord;
yaw_So = Yaw;
}
//加入比例常数Kp
int R = (Yaw - yaw_So) * Kp + speed;
if (R > UpperLimit)
{
R = UpperLimit;
}
else if (R < 10)
{
R = 10;
}
int L = (yaw_So - Yaw) * Kp + speed;
if (L > UpperLimit)
{
L = UpperLimit;
}
else if (L < 10)
{
L = 10;
}
if (direction == Forward) //前进
{
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_just, /*speed_A*/ R,
/*direction_B*/ direction_just, /*speed_B*/ L, /*controlED*/ control_enable);
}
else if (direction == Backward) //后退
{
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_back, /*speed_A*/ L,
/*direction_B*/ direction_back, /*speed_B*/ R, /*controlED*/ control_enable);
}
}
static void ApplicationFunctionSet_ConquerorCarMotionControl(ConquerorCarMotionControl direction, uint8_t is_speed)
{
// ApplicationFunctionSet Application_FunctionSet;
static uint8_t directionRecord = 0;
uint8_t Kp, UpperLimit;
uint8_t speed = is_speed;
Kp = 10;
UpperLimit = 255;
switch (direction)
{
case /* constant-expression */
Forward:
ApplicationFunctionSet_ConquerorCarLinearMotionControl(Forward, directionRecord, speed, Kp, UpperLimit);
directionRecord = 1;
break;
case /* constant-expression */ Backward:
/* code */
ApplicationFunctionSet_ConquerorCarLinearMotionControl(Backward, directionRecord, speed, Kp, UpperLimit);
directionRecord = 2;
break;
case /* constant-expression */ Left:
/* code */
directionRecord = 3;
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_just, /*speed_A*/ speed,
/*direction_B*/ direction_back, /*speed_B*/ speed, /*controlED*/ control_enable); //Motor control
break;
case /* constant-expression */ Right:
/* code */
directionRecord = 4;
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_back, /*speed_A*/ speed,
/*direction_B*/ direction_just, /*speed_B*/ speed, /*controlED*/ control_enable); //Motor control
break;
case /* constant-expression */ LeftForward:
/* code */
directionRecord = 5;
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_just, /*speed_A*/ speed,
/*direction_B*/ direction_just, /*speed_B*/ speed / 2, /*controlED*/ control_enable); //Motor control
break;
case /* constant-expression */ LeftBackward:
/* code */
directionRecord = 6;
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_back, /*speed_A*/ speed,
/*direction_B*/ direction_back, /*speed_B*/ speed / 2, /*controlED*/ control_enable); //Motor control
break;
case /* constant-expression */ RightForward:
/* code */
directionRecord = 7;
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_just, /*speed_A*/ speed / 2,
/*direction_B*/ direction_just, /*speed_B*/ speed, /*controlED*/ control_enable); //Motor control
break;
case /* constant-expression */ RightBackward:
/* code */
directionRecord = 8;
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_back, /*speed_A*/ speed / 2,
/*direction_B*/ direction_back, /*speed_B*/ speed, /*controlED*/ control_enable); //Motor control
break;
case /* constant-expression */ stop_it:
/* code */
directionRecord = 9;
AppMotor.DeviceDriverSet_Motor_control(/*direction_A*/ direction_void, /*speed_A*/ 0,
/*direction_B*/ direction_void, /*speed_B*/ 0, /*controlED*/ control_enable); //Motor control
break;
default:
directionRecord = 10;
break;
}
}
//void Turn(uint8_t targetAngle) {
//
// static float Yaw;
// ConquerorCarMotionControl L = Left;
// ConquerorCarMotionControl R = Right;
//
// while( Yaw != targetAngle ) {
//
// if (Yaw >= targetAngle) {
//
// ApplicationFunctionSet_ConquerorCarMotionControl(L, 100);
//
// }
//
// else if (Yaw <= targetAngle) {
//
// ApplicationFunctionSet_ConquerorCarMotionControl(R, 100);
//
// }
//
// else {
//
// delay(100000);
//
// }
//
// AppMPU6050getdata.MPU6050_dveGetEulerAngles(&Yaw);
//
// }
//
//}
//