-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.cpp
More file actions
137 lines (118 loc) · 4 KB
/
lib.cpp
File metadata and controls
137 lines (118 loc) · 4 KB
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
#include "lib.h"
/* NewPing Lib */
// 获取距离
float ping_distance(NewPing &sonar) { return sonar.ping() / 58.82; }
/* WIT Gyroscope Lib */
// 定义全局变量(不可优化)
volatile uint8_t g_gyroDataUpdate = 0;
// 函数指针: 写入函数(不可直接调用)
void gyro_sensor_uart_send(uint8_t *uiData, uint32_t uiSize) {
Serial1.write(uiData, uiSize);
Serial1.flush();
}
// 函数指针: 获取更新情况(不可直接调用)
void gyro_sensor_data_update(uint32_t uiReg, uint32_t uiRegNum) {
if (uiRegNum == 0) return;
for (uint32_t i = 0; i < uiRegNum; i++, uiReg++) {
switch (uiReg) {
case GZ:
g_gyroDataUpdate |= GYRO_UPDATE;
break;
case Yaw:
g_gyroDataUpdate |= ANGLE_UPDATE;
break;
default:
g_gyroDataUpdate |= READ_UPDATE;
break;
}
}
}
// 函数指针: 延时(不可直接调用)
void gyro_delay_ms(uint16_t uiMs) { delay(uiMs); }
// 放在 setup(), gyro 设置
void gyro_setup() {
WitInit(WIT_PROTOCOL_NORMAL, 0x50);
WitSerialWriteRegister(gyro_sensor_uart_send);
WitRegisterCallBack(gyro_sensor_data_update);
WitDelayMsRegister(gyro_delay_ms);
WitSetUartBaud(WIT_BAUD_115200);
delay(200);
WitSetYAW(STARTING_ANGLE);
delay(300);
WitSetOutputRate(RRATE_50HZ);
delay(300);
}
// 放在 loop(), gyro 调用. 用途是实现函数: zGyro.getGyro();zAngle.geyAngle();
// 放入两个变量, 完成自动更新
void gyro_get(float *fGyro, float *fAngle) {
while (Serial1.available()) {
WitSerialDataIn(Serial1.read());
}
if (g_gyroDataUpdate != 0) {
*fGyro = sReg[GYRO_Z_REG] / 32768.0f * 2000.0f;
*fAngle = sReg[ANGLE_Z_REG] / 32768.0f * 180.0f;
/* Use for debugging
if (g_gyroDataUpdate & GYRO_UPDATE) {
Serial.print("GyroZ: ");
Serial.print(*fGyro, 1);
Serial.print("\r\n");
g_gyroDataUpdate &= ~GYRO_UPDATE;
}
if (g_gyroDataUpdate & ANGLE_UPDATE) {
Serial.print("AngleZ: ");
Serial.print(*fAngle, 3);
Serial.print("\r\n");
g_gyroDataUpdate &= ~ANGLE_UPDATE;
}
*/
g_gyroDataUpdate = 0;
}
}
/* WARNING: Below functions may result in serious errors */
// Get current rpm
/*
float current_rpm_fetch(int &oldPosition, unsigned long &oldTime,
const Encoder &encoder,
const unsigned float &intervalMs = DEFAULT_INTERVAL_MS) {
int newPosition = encoder.read();
unsigned long currentTime = millis();
if (currentTime - oldTime >= intervalMs) {
int pulses = newPosition - oldPosition;
float rpm = (pulses / (float)ENCODER_LINES) * (60000.0 / intervalMs);
oldPosition = newPosition;
oldTime = currentTime;
return rpm;
}
return -1.0;
}
*/
/*
float current_rpm_fetch(int &oldPosition, unsigned long &oldTime, const int &encoder) {
int currentPosition = encoder.read();
unsigned long currentTime = micros();
if (currentPosition != oldPosition) {
unsigned long deltaTime = (currentTime >= oldTime) ?
(currentTime - oldTime) :
(currentTime + (0xFFFFFFFF - oldTime + 1));
if (deltaTime > 200 && deltaTime < 1000000) {
float rpm = (60.0f * 1e6) / (ENCODER_LINES * deltaTime);
oldTime = currentTime;
oldPosition = currentPosition;
return rpm;
}
}
return -1.0f;
}
*/
// Detect current fAngle to see if there's need in adjusting posture
void posture_change(const float fAngle, State ¤tState,
State &lastState, const float &distance2) {
if (fabs(fAngle) >= NORMAL_ANGLE_TOLERANCE) {
lastState = currentState;
currentState = POSTURE_CHANGE;
}
if (distance2 <= 15 || distance2 >= 25) {
lastState = currentState;
currentState = POSITION_CHANGE;
}
}