-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.cpp
322 lines (262 loc) · 8.98 KB
/
robot.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "robot.h"
#include <cstdio>
#include "stm32f1xx_hal.h"
#include "PSTwo.h"
#include "config.h"
#include "oled.h"
#include "mpu9250.h"
#include "Wheel4Car.h"
#include "usart.h"
#include "common_uart.h"
#include "protocol.h"
#include "Wheel2Car.h"
#include "Ackermann.h"
#include "crc_code.h"
void show_oled();
short param_kp = KP*10;
short param_ki = KI*10;
short param_kd = KD*10;
//Wheel2Car car;
Ackermann car;
//Wheel4Car car;
uint8_t buffer[10]={0};
// 将下位机的所有数据发送出去
void publish_data();
// 数据每隔一段时间对外发送
uint32_t publish_time;
/**
* 初始化的操作
*/
void HeimarobotInit(){
printf("heimarobot init..\r\n");
// 初始化串口通讯
common_uart_init();
//psTwo.init();
// 初始化MPU_9250
MPU9250_Init();
// 初始化OLED
OLED_Init();
car.init();
HAL_UART_Receive_IT(&huart2,buffer,1);
// HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_SET);
// HAL_Delay(1000);
// HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET);
}
int flag=1;
int32_t change_time = 0;
/**
* Tick机器人心脏的跳动
*/
void HeimarobotTick(){
// printf("heimarobot tick..\r\n");
/* printf("protocol=%d \r\n", sizeof(TXProtocol));
HAL_Delay(500);
*/
car.tick();
// car.updateVel(-0.1,0,0);
publish_data();
/* uint8_t data[] = {0x01,0x02,0x03};
common_uart_send(data, sizeof(data));*/
//psTwo.tick();
show_oled();
}
void show_oled(){
if(car.carType == 0){
uint8_t data1[]="akerman";
OLED_ShowString(0,0,data1);
}else if(car.carType == 1){
uint8_t data1[]="wheel2";
OLED_ShowString(0,0,data1);
}else if(car.carType == 2){
uint8_t data1[]="wheel4";
OLED_ShowString(0,0,data1);
}
uint8_t data2[]="heimarobot";
OLED_ShowString(0,12,data2);
uint8_t data3[]="robot.czxy.com";
OLED_ShowString(0,24,data3);
// 显示100倍的PID
OLED_ShowString(0,48,(uint8_t*)"P:");
OLED_ShowNumber(12,48,param_kp,3,12);
OLED_ShowString(36,48,(uint8_t*)"I:");
OLED_ShowNumber(48,48,param_ki,3,12);
OLED_ShowString(72,48,(uint8_t*)"D:");
OLED_ShowNumber(84,48,param_kd,3,12);
// 刷新屏幕
OLED_Refresh_Gram();
}
void publish_data(){
// 按照一定的频率对外发送数据
if(HAL_GetTick() - publish_time < 1000/IMU_PUSH_RATE){
return ;
}
publish_time = HAL_GetTick();
// 温度
short temp = MPU_Get_Temperature();
// 三轴加速度
short ax,ay,az = 0;
MPU_Get_Accelerometer(&ax,&ay,&az);
// 三轴角速度
short gx,gy,gz = 0;
MPU_Get_Gyroscope(&gx,&gy,&gz);
// 三轴地磁数据
short mx,my,mz = 0;
MPU_Get_Magnetometer(&mx,&my,&mz);
// 将小车的线速度 ,角速度 放大1000倍的目的是为了减少数据量 , 方便处理
short xVel = (short)(car.getXVel()*1000);
short angularVel = (short)(car.getAngularVel()*1000);
uint8_t protocolSize = sizeof(TXProtocol);
struct TXProtocol* protocol = new TXProtocol;
protocol->head0 = FLAG_HEAD0;
protocol->head1 = FLAG_HEAD1;
protocol->type = 0x03;
protocol->len = protocolSize - 4;
// 温度
protocol->temperature = temp;
// 加速度
protocol->ax = ax;
protocol->ay = ay;
protocol->az = az;
// 陀螺仪的角速度
protocol->gx = gx;
protocol->gy = gy;
protocol->gz = gz;
// 地磁
protocol->mx = mx;
protocol->my = my;
protocol->mz = mz;
// 线速度和角速度
protocol->velocity = xVel;
protocol->angular = angularVel;
protocol->tail = FLAG_TAIL;
// 将速度发送出去
common_uart_send((uint8_t*)protocol,protocolSize);
// 切记 new 出来的东西一定要删除
delete protocol;
}
/**
* 接收外部发送过来的数据
* @param receive_buf
* @param receive_len
*/
void common_uart_idle_callback(uint8_t receive_buf[],uint16_t receive_len){
// 类 长 低 高 低 高
// [0xce, 0xfa, 0x05, 4, 250, 0, 244, 1]
uint8_t i = 0;
while(i < receive_len){
// 先找到帧头0
if(receive_buf[i] == FLAG_HEAD0){
// 再找帧头1
if(receive_buf[i+1] == FLAG_HEAD1){
// 若进到这里来了,说明帧头完全匹配
// 获取类型
uint8_t type = receive_buf[i+2];
if(type == 0x05){ // 判断是否为上位机发送过来的速度信息
// 获取数据长度
uint8_t len = receive_buf[i+3];
// 判断是否有足够多的数据, 为了防止获取数据越界
if( len < receive_len - i -3 ){
// 还原线速度
uint8_t vel_low = receive_buf[i+4];
uint8_t vel_high = receive_buf[i+5];
short vel = vel_high<<8|vel_low;
float velocity = (float)vel/1000;
// 还原角速度
uint8_t angular_low = receive_buf[i+6];
uint8_t angular_high = receive_buf[i+7];
short ang = angular_high<<8|angular_low;
float angular = (float)ang/1000;
// 为了表示数据在传输
// 为了表示数据在传输
// HAL_GPIO_TogglePin(BUZZER_PORT,BUZZER_PIN);
if(receive_buf[i+8] == 0xad) {
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
car.updateVel(velocity, 0, angular);
}
}
}else if(type == 0x06){
// 获取数据长度
uint8_t len = receive_buf[i+3];
// 判断是否有足够多的数据, 为了防止获取数据越界
if( len < receive_len - i -3 ) {
float ratio = 10;
// 还原KP
uint8_t kp_l = receive_buf[i + 4];
uint8_t kp_h = receive_buf[i + 5];
short p = kp_h << 8 | kp_l;
float kp = (float) p / ratio;
// 还原KI
uint8_t ki_l = receive_buf[i + 6];
uint8_t ki_h = receive_buf[i + 7];
short ki_ = ki_h << 8 | ki_l;
float ki = (float) ki_ / ratio;
// 还原KI
uint8_t kd_l = receive_buf[i + 8];
uint8_t kd_h = receive_buf[i + 9];
unsigned short kd_ = kd_h << 8 | kd_l;
// 为了表示数据在传输
float kd = (float) kd_ / ratio;
// 为了表示数据在传输
if(receive_buf[i+10] == 0xad){
car.updatePID(kp,ki,kd);
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
param_kp = kp;
param_ki = ki;
param_kd = kd;
}
}
}
}
}
i++;
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
// 判断串口1的中断处理逻辑
if(huart->Instance == USART2){
// 切换led灯的状态
HAL_GPIO_TogglePin(GPIOE,GPIO_PIN_10);
uint8_t key = buffer[0];
switch (key){
case 0x47:
car.updateVel(0.2,0,0);
break;
case 0x48:
car.updateVel(0,0,0.8);
break;
case 0x49:
car.updateVel(0,0,0);
break;
case 0x4A:
car.updateVel(0,0,-0.8);
break;
case 0x4B:
car.updateVel(-0.2,0,0);
break;
// 麦克娜姆轮
case 0x41:
car.updateVel(0.2,0.2,0); // 左前方
break;
case 0x42:
car.updateVel(0,0.2,0); // 水平向左
break;
case 0x43:
car.updateVel(0.2,-0.2,0); // 右前方
break;
case 0x44:
car.updateVel(-0.2,0.2,0); // 左后方
break;
case 0x45:
car.updateVel(0,-0.2,0); // 水平向右
break;
case 0x46:
car.updateVel(-0.2,-0.2,0); // 右后方
break;
default:
car.updateVel(0,0,0);
break;
}
HAL_UART_Receive_IT(&huart2,buffer,1);
}
}