CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/ArduSub/turn_counter.cpp
Views: 1798
1
// Code by Rustom Jehangir: [email protected]
2
3
#include "Sub.h"
4
5
// Count total vehicle turns to avoid tangling tether
6
void Sub::update_turn_counter()
7
{
8
// Determine state
9
// 0: 0-90 deg, 1: 90-180 deg, 2: -180--90 deg, 3: -90--0 deg
10
uint8_t turn_state;
11
if (ahrs.get_yaw() >= 0.0f && ahrs.get_yaw() < radians(90)) {
12
turn_state = 0;
13
} else if (ahrs.get_yaw() > radians(90)) {
14
turn_state = 1;
15
} else if (ahrs.get_yaw() < -radians(90)) {
16
turn_state = 2;
17
} else {
18
turn_state = 3;
19
}
20
21
// If yaw went from negative to positive (right turn)
22
switch (last_turn_state) {
23
case 0:
24
if (turn_state == 1) {
25
quarter_turn_count++;
26
}
27
if (turn_state == 3) {
28
quarter_turn_count--;
29
}
30
break;
31
case 1:
32
if (turn_state == 2) {
33
quarter_turn_count++;
34
}
35
if (turn_state == 0) {
36
quarter_turn_count--;
37
}
38
break;
39
case 2:
40
if (turn_state == 3) {
41
quarter_turn_count++;
42
}
43
if (turn_state == 1) {
44
quarter_turn_count--;
45
}
46
break;
47
case 3:
48
if (turn_state == 0) {
49
quarter_turn_count++;
50
}
51
if (turn_state == 2) {
52
quarter_turn_count--;
53
}
54
break;
55
}
56
last_turn_state = turn_state;
57
}
58
59