Skip to content

Commit cedb04f

Browse files
GrayHoangpre-commit-ci-lite[bot]itsarune
authored
Halt Play to FSM (#3398)
* Moves halt_play files to their own directory and adds the necessary files * Implemented halt_play_fsm header and cpp file * Added a cpp unit test to /halt_play to test the FSM * Implements the FSM for Halt Play and adds a build statement to build the test * [pre-commit.ci lite] apply automatic fixes * Addresses comments in pull request * [pre-commit.ci lite] apply automatic fixes * fixes changes to gl_world_layer.py * Removes halt_play_cpp_test from the build file * Deletes the halt_play_test.cpp test file * Adds dependency to build file to fix build error * [pre-commit.ci lite] apply automatic fixes * Edits the dependencies in filter/BUILD to fix build errors. * [pre-commit.ci lite] apply automatic fixes * Patches failing halt_play_fsm_test * Update src/software/ai/hl/stp/play/BUILD Co-authored-by: itsarune <[email protected]> * Update src/software/sensor_fusion/filter/BUILD Co-authored-by: itsarune <[email protected]> * [pre-commit.ci lite] apply automatic fixes * address comments, add update tactics * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: itsarune <[email protected]>
1 parent c285029 commit cedb04f

21 files changed

+199
-135
lines changed

docs/fsm-diagrams.md

+13
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ ChipState --> Terminate:::terminate : [chipDone]
130130
131131
```
132132

133+
## [HaltPlayFSM](/src/software/ai/hl/stp/play/halt_play/halt_play_fsm.h)
134+
135+
```mermaid
136+
137+
stateDiagram-v2
138+
classDef terminate fill:white,color:black,font-weight:bold
139+
direction LR
140+
[*] --> HaltState
141+
HaltState --> HaltState : <i>updateStop</i>
142+
Terminate:::terminate --> Terminate:::terminate : <i>updateStop</i>
143+
144+
```
145+
133146
## [OffensePlayFSM](/src/software/ai/hl/stp/play/offense/offense_play_fsm.h)
134147

135148
```mermaid

src/software/ai/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ cc_library(
1818
"//proto:play_info_msg_cc_proto",
1919
"//software/ai/hl/stp/play:all_plays",
2020
"//software/ai/hl/stp/play:assigned_tactics_play",
21-
"//software/ai/hl/stp/play:halt_play",
2221
"//software/ai/hl/stp/play:play_factory",
22+
"//software/ai/hl/stp/play/halt_play",
2323
"//software/ai/hl/stp/tactic:tactic_factory",
2424
"//software/time:timestamp",
2525
"//software/tracy:tracy_constants",

src/software/ai/ai.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <Tracy.hpp>
44

5-
#include "software/ai/hl/stp/play/halt_play.h"
5+
#include "software/ai/hl/stp/play/halt_play/halt_play.h"
66
#include "software/ai/hl/stp/play/play_factory.h"
77
#include "software/tracy/tracy_constants.h"
88

src/software/ai/hl/stp/play/BUILD

+1-47
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@ cc_library(
2020
alwayslink = True,
2121
)
2222

23-
cc_library(
24-
name = "halt_play",
25-
srcs = ["halt_play.cpp"],
26-
hdrs = ["halt_play.h"],
27-
deps = [
28-
":play",
29-
"//shared:constants",
30-
"//software/ai/hl/stp/tactic/halt:halt_tactic",
31-
"//software/logger",
32-
"//software/util/generic_factory",
33-
],
34-
alwayslink = True,
35-
)
36-
3723
cc_library(
3824
name = "kickoff_enemy_play",
3925
srcs = ["kickoff_enemy_play.cpp"],
@@ -134,7 +120,6 @@ cc_library(
134120
name = "all_plays",
135121
deps = [
136122
":example_play",
137-
":halt_play",
138123
":kickoff_enemy_play",
139124
":kickoff_friendly_play",
140125
":shoot_or_chip_play",
@@ -145,6 +130,7 @@ cc_library(
145130
"//software/ai/hl/stp/play/enemy_ball_placement:enemy_ball_placement_play",
146131
"//software/ai/hl/stp/play/enemy_free_kick:enemy_free_kick_play",
147132
"//software/ai/hl/stp/play/free_kick:free_kick_play",
133+
"//software/ai/hl/stp/play/halt_play",
148134
"//software/ai/hl/stp/play/hardware_challenge_plays:dribbling_parcour_play",
149135
"//software/ai/hl/stp/play/hardware_challenge_plays:pass_endurance_play",
150136
"//software/ai/hl/stp/play/hardware_challenge_plays:scoring_from_contested_possession_play",
@@ -234,38 +220,6 @@ cc_test(
234220
],
235221
)
236222

237-
cc_test(
238-
name = "halt_play_cpp_test",
239-
srcs = ["halt_play_test.cpp"],
240-
deps = [
241-
"//shared/test_util:tbots_gtest_main",
242-
"//software/ai/hl/stp/play:halt_play",
243-
"//software/simulated_tests:simulated_er_force_sim_play_test_fixture",
244-
"//software/simulated_tests/terminating_validation_functions",
245-
"//software/simulated_tests/validation:validation_function",
246-
"//software/test_util",
247-
"//software/time:duration",
248-
"//software/world",
249-
],
250-
)
251-
252-
py_test(
253-
name = "halt_play_test",
254-
srcs = [
255-
"halt_play_test.py",
256-
],
257-
# TODO (#2619) Remove tag to run in parallel
258-
tags = [
259-
"exclusive",
260-
],
261-
deps = [
262-
"//software:conftest",
263-
"//software/simulated_tests:speed_threshold_helpers",
264-
"//software/simulated_tests:validation",
265-
requirement("pytest"),
266-
],
267-
)
268-
269223
cc_test(
270224
name = "shoot_or_chip_play_cpp_test",
271225
srcs = ["shoot_or_chip_play_test.cpp"],

src/software/ai/hl/stp/play/halt_play.cpp

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@simulated_tests_deps//:requirements.bzl", "requirement")
4+
5+
cc_library(
6+
name = "halt_play",
7+
srcs = [
8+
"halt_play.cpp",
9+
"halt_play_fsm.cpp",
10+
],
11+
hdrs = [
12+
"halt_play.h",
13+
"halt_play_fsm.h",
14+
],
15+
deps = [
16+
"//shared:constants",
17+
"//software/ai/hl/stp/play",
18+
"//software/ai/hl/stp/tactic/halt:halt_tactic",
19+
"//software/logger",
20+
"//software/util/generic_factory",
21+
],
22+
alwayslink = True,
23+
)
24+
25+
cc_test(
26+
name = "halt_play_fsm_test",
27+
srcs = ["halt_play_fsm_test.cpp"],
28+
deps = [
29+
":halt_play",
30+
"//shared/test_util:tbots_gtest_main",
31+
"//software/test_util",
32+
],
33+
)
34+
35+
py_test(
36+
name = "halt_play_test",
37+
srcs = [
38+
"halt_play_test.py",
39+
],
40+
# TODO (#2619) Remove tag to run in parallel
41+
tags = [
42+
"exclusive",
43+
],
44+
deps = [
45+
"//software:conftest",
46+
"//software/simulated_tests:speed_threshold_helpers",
47+
"//software/simulated_tests:validation",
48+
requirement("pytest"),
49+
],
50+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "software/ai/hl/stp/play/halt_play/halt_play.h"
2+
3+
#include "software/ai/hl/stp/tactic/halt/halt_tactic.h"
4+
#include "software/util/generic_factory/generic_factory.h"
5+
6+
HaltPlay::HaltPlay(TbotsProto::AiConfig config)
7+
: Play(config, false), fsm{HaltPlayFSM{config}}, control_params{}
8+
{
9+
}
10+
11+
void HaltPlay::getNextTactics(TacticCoroutine::push_type &yield,
12+
const WorldPtr &world_ptr)
13+
{
14+
// This function doesn't get called and will be removed when coroutines are phased
15+
// out.
16+
}
17+
18+
void HaltPlay::updateTactics(const PlayUpdate &play_update)
19+
{
20+
fsm.process_event(HaltPlayFSM::Update(control_params, play_update));
21+
}
22+
23+
// Register this play in the genericFactory
24+
static TGenericFactory<std::string, Play, HaltPlay, TbotsProto::AiConfig> factory;

src/software/ai/hl/stp/play/halt_play.h renamed to src/software/ai/hl/stp/play/halt_play/halt_play.h

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "proto/parameters.pb.h"
4+
#include "software/ai/hl/stp/play/halt_play/halt_play_fsm.h"
45
#include "software/ai/hl/stp/play/play.h"
56

67
/**
@@ -14,4 +15,9 @@ class HaltPlay : public Play
1415

1516
void getNextTactics(TacticCoroutine::push_type &yield,
1617
const WorldPtr &world_ptr) override;
18+
void updateTactics(const PlayUpdate &play_update) override;
19+
20+
private:
21+
FSM<HaltPlayFSM> fsm;
22+
HaltPlayFSM::ControlParams control_params;
1723
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
#include "software/ai/hl/stp/play/halt_play/halt_play_fsm.h"
3+
4+
#include <algorithm>
5+
#include <iterator>
6+
7+
HaltPlayFSM::HaltPlayFSM(TbotsProto::AiConfig ai_config) : halt_tactics({{}})
8+
{
9+
std::generate_n(std::back_inserter(halt_tactics.front()), MAX_ROBOT_IDS_PER_SIDE,
10+
[]() { return std::make_shared<HaltTactic>(); });
11+
}
12+
13+
void HaltPlayFSM::updateStop(const Update& event)
14+
{
15+
event.common.set_tactics(halt_tactics);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
3+
#include "proto/parameters.pb.h"
4+
#include "shared/constants.h"
5+
#include "software/ai/hl/stp/play/play_fsm.h"
6+
#include "software/ai/hl/stp/tactic/halt/halt_tactic.h"
7+
#include "software/logger/logger.h"
8+
9+
struct HaltPlayFSM
10+
{
11+
struct ControlParams
12+
{
13+
};
14+
class HaltState;
15+
16+
DEFINE_PLAY_UPDATE_STRUCT_WITH_CONTROL_AND_COMMON_PARAMS
17+
18+
/**
19+
* Creates a Halt Play FSM
20+
*
21+
* @param ai_config the play config for this FSM
22+
*/
23+
explicit HaltPlayFSM(TbotsProto::AiConfig ai_config);
24+
25+
/**
26+
* Action to make each robot stop
27+
*
28+
* @param event the HaltPlayFSM Event
29+
*/
30+
void updateStop(const Update& event);
31+
32+
33+
auto operator()()
34+
{
35+
using namespace boost::sml;
36+
37+
DEFINE_SML_STATE(HaltState)
38+
39+
DEFINE_SML_EVENT(Update)
40+
41+
DEFINE_SML_ACTION(updateStop)
42+
43+
return make_transition_table(
44+
// src_state + event [guard] / action = dest_state
45+
*HaltState_S + Update_E / updateStop_A = HaltState_S,
46+
47+
X + Update_E / updateStop_A = X);
48+
}
49+
50+
private:
51+
PriorityTacticVector halt_tactics;
52+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "software/ai/hl/stp/play/halt_play/halt_play_fsm.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
#include "proto/parameters.pb.h"
6+
#include "software/test_util/equal_within_tolerance.h"
7+
#include "software/test_util/test_util.h"
8+
9+
TEST(HaltPlayFSMTest, test_transitions)
10+
{
11+
std::shared_ptr<World> world = ::TestUtil::createBlankTestingWorld();
12+
TbotsProto::AiConfig ai_config;
13+
14+
FSM<HaltPlayFSM> fsm(HaltPlayFSM{ai_config});
15+
16+
EXPECT_TRUE(fsm.is(boost::sml::state<HaltPlayFSM::HaltState>));
17+
18+
int num_tactics = 6;
19+
20+
fsm.process_event(HaltPlayFSM::Update(
21+
HaltPlayFSM::ControlParams{},
22+
PlayUpdate(
23+
world, num_tactics, [](PriorityTacticVector new_tactics) {},
24+
InterPlayCommunication{}, [](InterPlayCommunication comm) {})));
25+
26+
EXPECT_TRUE(fsm.is(boost::sml::state<HaltPlayFSM::HaltState>));
27+
}

src/software/ai/hl/stp/play/halt_play_test.cpp

-51
This file was deleted.

src/software/ai/hl/stp/stp_referee_command_play_selection_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <exception>
44

55
#include "proto/parameters.pb.h"
6-
#include "software/ai/hl/stp/play/halt_play.h"
6+
#include "software/ai/hl/stp/play/halt_play/halt_play.h"
77
#include "software/ai/hl/stp/stp.h"
88
#include "software/test_util/test_util.h"
99
#include "software/world/world.h"

0 commit comments

Comments
 (0)