Skip to content

Commit ad63b42

Browse files
nycratwilliamckhapre-commit-ci-lite[bot]
authored
Pass in delta_time dynamically for prim exec (#3758)
* Remove unused member functions and variables * Fix call to setStopPrimitive this is removed even thought is technically used now cause future changes will get rid of it * Pass in delta_time dynamically for prim exec * Fix calls to prim exec constructor and stepPrimitive * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: William Ha <60044853+williamckha@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent dd9521c commit ad63b42

5 files changed

Lines changed: 35 additions & 38 deletions

File tree

src/software/embedded/primitive_executor.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "software/physics/velocity_conversion_util.h"
1212

1313
PrimitiveExecutor::PrimitiveExecutor(
14-
const Duration time_step, const robot_constants::RobotConstants& robot_constants)
15-
: current_primitive_(), robot_constants_(robot_constants), time_step_(time_step)
14+
const robot_constants::RobotConstants& robot_constants)
15+
: current_primitive_(), robot_constants_(robot_constants)
1616
{
1717
}
1818

@@ -78,9 +78,9 @@ AngularVelocity PrimitiveExecutor::getTargetAngularVelocity()
7878

7979

8080
std::unique_ptr<TbotsProto::DirectControlPrimitive> PrimitiveExecutor::stepPrimitive(
81-
TbotsProto::PrimitiveExecutorStatus& status)
81+
TbotsProto::PrimitiveExecutorStatus& status, Duration delta_time)
8282
{
83-
time_since_trajectory_creation_ += time_step_;
83+
time_since_trajectory_creation_ += delta_time;
8484
status.set_running_primitive(true);
8585

8686
switch (current_primitive_.primitive_case())

src/software/embedded/primitive_executor.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ class PrimitiveExecutor
1414
public:
1515
/**
1616
* Constructor
17-
* @param time_step Time step which this primitive executor operates in
1817
* @param robot_constants The robot constants for the robot which uses this primitive
1918
* executor
2019
*/
21-
explicit PrimitiveExecutor(const Duration time_step,
22-
const robot_constants::RobotConstants& robot_constants);
20+
explicit PrimitiveExecutor(const robot_constants::RobotConstants& robot_constants);
2321

2422
/**
2523
* Update primitive executor with a new Primitive
@@ -39,13 +37,15 @@ class PrimitiveExecutor
3937
/**
4038
* Steps the current primitive and returns a direct control primitive with the
4139
* target wheel velocities
40+
*
4241
* @param status The status of the primitive executor, set to false if current
4342
* primitive is a Stop primitive
43+
* @param delta_time The elapsed time since the last primitive step
4444
*
4545
* @returns DirectControlPrimitive The direct control primitive msg
4646
*/
4747
std::unique_ptr<TbotsProto::DirectControlPrimitive> stepPrimitive(
48-
TbotsProto::PrimitiveExecutorStatus& status);
48+
TbotsProto::PrimitiveExecutorStatus& status, Duration delta_time);
4949

5050
private:
5151
/*
@@ -70,10 +70,6 @@ class PrimitiveExecutor
7070
std::optional<TrajectoryPath> trajectory_path_;
7171
std::optional<BangBangTrajectory1DAngular> angular_trajectory_;
7272

73-
// TODO (#2855): Add dynamic time_step to `stepPrimitive` and remove this constant
74-
// time step to be used, in Seconds
75-
Duration time_step_;
76-
7773
// Estimated delay between a vision frame to AI processing to robot executing
7874
static constexpr double VISION_TO_ROBOT_DELAY_S = 0.03;
7975

src/software/embedded/thunderloop.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Thunderloop::Thunderloop(const robot_constants::RobotConstants& robot_constants,
8484
kick_constant_(std::stoi(toml_config_client_->get(ROBOT_KICK_CONSTANT_CONFIG_KEY))),
8585
chip_pulse_width_(
8686
std::stoi(toml_config_client_->get(ROBOT_CHIP_PULSE_WIDTH_CONFIG_KEY))),
87-
primitive_executor_(Duration::fromSeconds(1.0 / loop_hz), robot_constants)
87+
primitive_executor_(robot_constants)
8888
{
8989
waitForNetworkUp();
9090

@@ -286,8 +286,8 @@ void Thunderloop::runLoop()
286286
primitive_executor_.updatePrimitive(*createStopPrimitiveProto());
287287
}
288288

289-
direct_control_ =
290-
*primitive_executor_.stepPrimitive(primitive_executor_status_);
289+
direct_control_ = *primitive_executor_.stepPrimitive(
290+
primitive_executor_status_, Duration::fromSeconds(1.0 / loop_hz_));
291291
}
292292

293293
thunderloop_status_.set_primitive_executor_step_time_ms(

src/software/simulation/er_force_simulator.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ ErForceSimulator::ErForceSimulator(const TbotsProto::FieldType& field_type,
2020
const robot_constants::RobotConstants& robot_constants,
2121
std::unique_ptr<RealismConfigErForce>& realism_config,
2222
const bool ramping,
23-
double primitive_executor_time_step)
23+
Duration primitive_executor_time_step)
2424
: yellow_team_world_msg(std::make_unique<TbotsProto::World>()),
2525
blue_team_world_msg(std::make_unique<TbotsProto::World>()),
26-
primitive_executor_time_step_s(primitive_executor_time_step),
26+
primitive_executor_time_step(primitive_executor_time_step),
2727
frame_number(0),
2828
euclidean_to_four_wheel(robot_constants),
2929
robot_constants(robot_constants),
@@ -280,14 +280,14 @@ void ErForceSimulator::setRobots(
280280
{
281281
if (side == gameController::Team::BLUE)
282282
{
283-
auto robot_primitive_executor = std::make_shared<PrimitiveExecutor>(
284-
Duration::fromSeconds(primitive_executor_time_step_s), robot_constants);
283+
auto robot_primitive_executor =
284+
std::make_shared<PrimitiveExecutor>(robot_constants);
285285
blue_primitive_executor_map.insert({id, robot_primitive_executor});
286286
}
287287
else
288288
{
289-
auto robot_primitive_executor = std::make_shared<PrimitiveExecutor>(
290-
Duration::fromSeconds(primitive_executor_time_step_s), robot_constants);
289+
auto robot_primitive_executor =
290+
std::make_shared<PrimitiveExecutor>(robot_constants);
291291
yellow_primitive_executor_map.insert({id, robot_primitive_executor});
292292
}
293293
}
@@ -389,15 +389,17 @@ SSLSimulationProto::RobotControl ErForceSimulator::updateSimulatorRobots(
389389
TbotsProto::PrimitiveExecutorStatus status; // Added for compilation
390390
if (ramping)
391391
{
392-
auto direct_control_no_ramp = primitive_executor->stepPrimitive(status);
393-
direct_control = getRampedVelocityPrimitive(
394-
current_velocity_map.at(robot_id).first,
395-
current_velocity_map.at(robot_id).second, *direct_control_no_ramp,
396-
primitive_executor_time_step_s);
392+
auto direct_control_no_ramp =
393+
primitive_executor->stepPrimitive(status, primitive_executor_time_step);
394+
direct_control = getRampedVelocityPrimitive(
395+
current_velocity_map.at(robot_id).first,
396+
current_velocity_map.at(robot_id).second, *direct_control_no_ramp,
397+
primitive_executor_time_step);
397398
}
398399
else
399400
{
400-
direct_control = primitive_executor->stepPrimitive(status);
401+
direct_control =
402+
primitive_executor->stepPrimitive(status, primitive_executor_time_step);
401403
}
402404

403405
auto command = *getRobotCommandFromDirectControl(
@@ -411,8 +413,7 @@ std::unique_ptr<TbotsProto::DirectControlPrimitive>
411413
ErForceSimulator::getRampedVelocityPrimitive(
412414
const Vector current_local_velocity,
413415
const AngularVelocity current_local_angular_velocity,
414-
TbotsProto::DirectControlPrimitive& target_velocity_primitive,
415-
const double& time_to_ramp)
416+
TbotsProto::DirectControlPrimitive& target_velocity_primitive, Duration time_to_ramp)
416417
{
417418
TbotsProto::MotorControl_DirectVelocityControl direct_velocity =
418419
target_velocity_primitive.motor_control().direct_velocity_control();
@@ -435,7 +436,7 @@ ErForceSimulator::getRampedVelocityPrimitive(
435436
euclidean_to_four_wheel.getWheelVelocity(current_euclidean_velocity);
436437

437438
WheelSpace_t ramped_four_wheel = euclidean_to_four_wheel.rampWheelVelocity(
438-
current_wheel_velocity, target_wheel_velocity, time_to_ramp);
439+
current_wheel_velocity, target_wheel_velocity, time_to_ramp.toSeconds());
439440

440441
EuclideanSpace_t ramped_euclidean =
441442
euclidean_to_four_wheel.getEuclideanVelocity(ramped_four_wheel);

src/software/simulation/er_force_simulator.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class ErForceSimulator
2727
* @param robot_constants The robot constants
2828
* @param realism_config realism configuration
2929
*/
30-
explicit ErForceSimulator(const TbotsProto::FieldType& field_type,
31-
const robot_constants::RobotConstants& robot_constants,
32-
std::unique_ptr<RealismConfigErForce>& realism_config,
33-
const bool ramping = false,
34-
double primitive_executor_time_step_s =
35-
DEFAULT_SIMULATOR_TICK_RATE_SECONDS_PER_TICK);
30+
explicit ErForceSimulator(
31+
const TbotsProto::FieldType& field_type,
32+
const robot_constants::RobotConstants& robot_constants,
33+
std::unique_ptr<RealismConfigErForce>& realism_config, const bool ramping = false,
34+
Duration primitive_executor_time_step_s =
35+
Duration::fromSeconds(DEFAULT_SIMULATOR_TICK_RATE_SECONDS_PER_TICK));
3636
ErForceSimulator() = delete;
3737
~ErForceSimulator() = default;
3838

@@ -202,7 +202,7 @@ class ErForceSimulator
202202
const Vector current_local_velocity,
203203
const AngularVelocity current_local_angular_velocity,
204204
TbotsProto::DirectControlPrimitive& target_velocity_primitive,
205-
const double& time_to_ramp);
205+
Duration time_to_ramp);
206206

207207
// Map of Robot id to Primitive Executor
208208
std::unordered_map<unsigned int, std::shared_ptr<PrimitiveExecutor>>
@@ -212,7 +212,7 @@ class ErForceSimulator
212212
std::unique_ptr<TbotsProto::World> yellow_team_world_msg;
213213
std::unique_ptr<TbotsProto::World> blue_team_world_msg;
214214

215-
double primitive_executor_time_step_s;
215+
Duration primitive_executor_time_step;
216216
unsigned int frame_number;
217217

218218
// The current time.

0 commit comments

Comments
 (0)