-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDFRobot_WT61PC.cpp
116 lines (101 loc) · 2.52 KB
/
DFRobot_WT61PC.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
/*!
@file DFRobot_WT61PC.cpp
@brief Realize the basic structure of DFRobot_WT61PC sensor class
@copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
@licence The MIT License (MIT)
@author huyujie([email protected])
@version V1.0
@date 2020-12-3
@get from https://www.dfrobot.com
@url https://github.com/DFRobot
*/
#include <DFRobot_WT61PC.h>
DFRobot_WT61PC::DFRobot_WT61PC(Stream *s)
{
_s = s;
}
size_t DFRobot_WT61PC::readN(uint8_t *buf, size_t len)
{
size_t offset = 0, left = len;
long curr = millis();
while (left) {
if (_s -> available()) {
buf[offset++] = _s->read();
left--;
}
if (millis() - curr > TIMEOUT) {
break;
}
}
return offset;
}
bool DFRobot_WT61PC::recvData(uint8_t *buf, uint8_t header)
{
long curr = millis();
bool ret = false;
uint8_t ch;
while (!ret) {
if (millis() - curr > TIMEOUT) {
break;
}
if (readN(&ch, 1) != 1) {
continue;
}
if (ch == HEADER55) {
buf[0] = ch;
if (readN(&ch, 1) == 1) {
if (ch == header) {
buf[1] = ch;
if (readN(&buf[2], 9) == 9) {
if (getCS(buf) == buf[10]) {
ret = true;
}
}
}
}
}
}
return ret;
}
uint8_t DFRobot_WT61PC::getCS(uint8_t *buf)
{
uint8_t cs = 0;
for (int i = 0; i < 10; i++) {
cs = cs + buf[i];
}
return cs;
}
bool DFRobot_WT61PC::available(void)
{
bool ret = false;
if (recvData(receivedAccData , HEADERACC) && recvData(receivedGyroData , HEADERGYRO) && recvData(receivedAngleData , HEADERANGLE)) {
ret = true;
getAcc(receivedAccData);
getGyro(receivedGyroData);
getAngle(receivedAngleData);
}
return ret;
}
void DFRobot_WT61PC::getAcc(uint8_t *buf)
{
Acc.X = ((buf[XH] << 8) | buf[XL]) / 32768.000 * 16.000 * 9.8;
Acc.Y = ((buf[YH] << 8) | buf[YL]) / 32768.000 * 16.000 * 9.8;
Acc.Z = ((buf[ZH] << 8) | buf[ZL]) / 32768.000 * 16.000 * 9.8;
}
void DFRobot_WT61PC::getGyro(uint8_t *buf)
{
Gyro.X = ((buf[XH] << 8) | buf[XL]) / 32768.000 * 2000.000;
Gyro.Y = ((buf[YH] << 8) | buf[YL]) / 32768.000 * 2000.000;
Gyro.Z = ((buf[ZH] << 8) | buf[ZL]) / 32768.000 * 2000.000;
}
void DFRobot_WT61PC::getAngle(uint8_t *buf)
{
Angle.X = ((buf[XH] << 8) | buf[XL]) / 32768.000 * 180.000;
Angle.Y = ((buf[YH] << 8) | buf[YL]) / 32768.000 * 180.000;
Angle.Z = ((buf[ZH] << 8) | buf[ZL]) / 32768.000 * 180.000;
}
void DFRobot_WT61PC::modifyFrequency(uint8_t frequency)
{
Cmd[3] = frequency;
_s->write(Cmd, 5);
}