forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturn_counter.cpp
57 lines (54 loc) · 1.33 KB
/
turn_counter.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
// Code by Rustom Jehangir: [email protected]
#include "Sub.h"
// Count total vehicle turns to avoid tangling tether
void Sub::update_turn_counter()
{
// Determine state
// 0: 0-90 deg, 1: 90-180 deg, 2: -180--90 deg, 3: -90--0 deg
uint8_t turn_state;
if (ahrs.yaw >= 0.0f && ahrs.yaw < radians(90)) {
turn_state = 0;
} else if (ahrs.yaw > radians(90)) {
turn_state = 1;
} else if (ahrs.yaw < -radians(90)) {
turn_state = 2;
} else {
turn_state = 3;
}
// If yaw went from negative to positive (right turn)
switch (last_turn_state) {
case 0:
if (turn_state == 1) {
quarter_turn_count++;
}
if (turn_state == 3) {
quarter_turn_count--;
}
break;
case 1:
if (turn_state == 2) {
quarter_turn_count++;
}
if (turn_state == 0) {
quarter_turn_count--;
}
break;
case 2:
if (turn_state == 3) {
quarter_turn_count++;
}
if (turn_state == 1) {
quarter_turn_count--;
}
break;
case 3:
if (turn_state == 0) {
quarter_turn_count++;
}
if (turn_state == 2) {
quarter_turn_count--;
}
break;
}
last_turn_state = turn_state;
}