JSim 2026.06.01-p(1)
Loading...
Searching...
No Matches
intake_simulation.hpp
Go to the documentation of this file.
1// Copyright (c) JSim contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the LGPLv3 license file in the root directory of this project.
4
5#pragma once
6
7#include <cstddef>
8#include <deque>
9#include <functional>
10#include <string>
11
13
14namespace frcsim {
15
19 public:
21 struct Config {
23 std::size_t robot_index{0};
25 std::string targeted_type{"Ball"};
27 std::size_t capacity{1};
28 };
29
44
45 IntakeSimulation() = default;
46
51 explicit IntakeSimulation(const Config& config) : config_(config) {}
52
54 void setConfig(const Config& config) { config_ = config; }
57 const Config& config() const { return config_; }
58
61 void setRunning(bool running) { running_ = running; }
64 bool isRunning() const { return running_; }
65
71 const std::function<bool(std::size_t, const BallGamepieceSim&)>&
72 predicate) {
73 custom_condition_ = predicate;
74 }
75
78 std::size_t gamePiecesInIntakeCount() const { return intake_count_; }
79
85 if (intake_count_ == 0) {
86 return false;
87 }
88 --intake_count_;
89 return true;
90 }
91
94 const std::deque<ContactEvent>& recentEvents() const {
95 return recent_events_;
96 }
97
104 if (!running_) {
105 recent_events_.clear();
106 return;
107 }
108 if (config_.robot_index >= sim.robots().size()) {
109 recent_events_.clear();
110 return;
111 }
112
113 const auto& robot = sim.robots()[config_.robot_index];
114 const Vector3 intake_world = robot.position_m;
115
116 recent_events_.clear();
117 for (std::size_t i = 0; i < sim.balls().size(); ++i) {
118 const auto& ball = sim.balls()[i];
119 if (ball.sim.state().held) {
120 continue;
121 }
122 if (!config_.targeted_type.empty() &&
123 sim.ballTypeName(i) != config_.targeted_type) {
124 continue;
125 }
126
127 const double distance =
128 (ball.sim.state().position_m - intake_world).norm();
129 if (distance <= robot.intake_radius_m) {
130 recent_events_.push_back({i, ContactEvent::Phase::kBegin});
131 }
132 }
133
134 processIntakeQueue(sim);
135 }
136
137 private:
138 void processIntakeQueue(BallGamepieceSim& sim) {
139 if (intake_count_ >= config_.capacity) {
140 return;
141 }
142
143 for (const auto& event : recent_events_) {
144 if (event.phase != ContactEvent::Phase::kBegin) {
145 continue;
146 }
147 if (intake_count_ >= config_.capacity) {
148 break;
149 }
150 if (event.gamepiece_index >= sim.balls().size()) {
151 continue;
152 }
153 if (custom_condition_ && !custom_condition_(event.gamepiece_index, sim)) {
154 continue;
155 }
156 if (sim.removeBall(event.gamepiece_index)) {
157 ++intake_count_;
158 }
159 }
160 }
161
162 Config config_{};
163 bool running_{false};
164 std::size_t intake_count_{0};
165
166 std::function<bool(std::size_t, const BallGamepieceSim&)> custom_condition_{};
167 std::deque<ContactEvent> recent_events_{};
168};
169
170} // namespace frcsim
Resolves robot-ball contact response. projectiles.
Definition ball_gamepiece_sim.hpp:38
std::vector< RobotState > & robots()
Mutable robot state list.
Definition ball_gamepiece_sim.hpp:388
static constexpr std::size_t kNoBall
Definition ball_gamepiece_sim.hpp:52
bool removeBall(std::size_t ball_index)
Removes a ball by index and remaps carried-ball indices accordingly.
Definition ball_gamepiece_sim.hpp:477
std::vector< BallEntity > & balls()
Mutable grounded-ball list.
Definition ball_gamepiece_sim.hpp:395
std::string ballTypeName(std::size_t ball_index) const
Returns the registered type label for a ball index, or empty string if out of range.
Definition ball_gamepiece_sim.hpp:436
void setCustomIntakeCondition(const std::function< bool(std::size_t, const BallGamepieceSim &)> &predicate)
Sets optional custom acceptance predicate for candidate balls.
Definition intake_simulation.hpp:70
void setConfig(const Config &config)
Replaces intake configuration.
Definition intake_simulation.hpp:54
bool isRunning() const
Returns whether intake processing is active.
Definition intake_simulation.hpp:64
const Config & config() const
Returns current configuration.
Definition intake_simulation.hpp:57
IntakeSimulation(const Config &config)
Constructs intake simulation with explicit config.
Definition intake_simulation.hpp:51
std::size_t gamePiecesInIntakeCount() const
Returns current intake inventory count.
Definition intake_simulation.hpp:78
void update(BallGamepieceSim &sim)
Performs proximity scanning and intake processing for one simulation step.
Definition intake_simulation.hpp:103
void setRunning(bool running)
Enables/disables intake processing.
Definition intake_simulation.hpp:61
const std::deque< ContactEvent > & recentEvents() const
Returns contact events generated during the most recent update call.
Definition intake_simulation.hpp:94
bool obtainGamePieceFromIntake()
Removes one stored piece from intake inventory.
Definition intake_simulation.hpp:84
Definition vector.hpp:13
Runtime configuration for intake behavior.
Definition intake_simulation.hpp:21
std::string targeted_type
Definition intake_simulation.hpp:25
std::size_t robot_index
Definition intake_simulation.hpp:23
std::size_t capacity
Definition intake_simulation.hpp:27
Contact event generated during update() proximity checks.
Definition intake_simulation.hpp:31
std::size_t gamepiece_index
Definition intake_simulation.hpp:40
Phase
Contact lifecycle phase.
Definition intake_simulation.hpp:33
@ kEnd
Definition intake_simulation.hpp:36
@ kBegin
Definition intake_simulation.hpp:34
@ kPersist
Definition intake_simulation.hpp:35
Phase phase
Definition intake_simulation.hpp:42
3D vector utility used throughout JSim physics.
Definition vector.hpp:22