Skip to content

Commit ebf0b78

Browse files
authored
ArduSub: improve turn counting
1 parent 407b270 commit ebf0b78

File tree

2 files changed

+7
-36
lines changed

2 files changed

+7
-36
lines changed

ArduSub/GCS_MAVLink_Sub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ bool GCS_MAVLINK_Sub::send_info()
133133

134134
CHECK_PAYLOAD_SIZE(NAMED_VALUE_FLOAT);
135135
send_named_float("TetherTrn",
136-
sub.quarter_turn_count/4);
136+
(float)sub.quarter_turn_count / 4);
137137

138138
CHECK_PAYLOAD_SIZE(NAMED_VALUE_FLOAT);
139139
send_named_float("Lights1",

ArduSub/turn_counter.cpp

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
// Count total vehicle turns to avoid tangling tether
66
void Sub::update_turn_counter()
77
{
8-
// Determine state
8+
// Determine state
99
// 0: 0-90 deg, 1: 90-180 deg, 2: -180--90 deg, 3: -90--0 deg
1010
uint8_t turn_state;
1111
if (ahrs.get_yaw() >= 0.0f && ahrs.get_yaw() < radians(90)) {
1212
turn_state = 0;
13-
} else if (ahrs.get_yaw() > radians(90)) {
13+
} else if (ahrs.get_yaw() >= radians(90)) {
1414
turn_state = 1;
1515
} else if (ahrs.get_yaw() < -radians(90)) {
1616
turn_state = 2;
@@ -19,39 +19,10 @@ void Sub::update_turn_counter()
1919
}
2020

2121
// 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;
22+
if (turn_state == (last_turn_state + 1) % 4) {
23+
quarter_turn_count++;
24+
else if (turn_state == (uint8_t)(last_turn_state - 1) % 4) {
25+
quarter_turn_count--;
5526
}
5627
last_turn_state = turn_state;
5728
}

0 commit comments

Comments
 (0)