Skip to content

Commit 2047239

Browse files
authored
Use RobotState instead of individual variables for prim exec (#3759)
* 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 * Add setters to RobotState class * Replace state variables with one RobotState * Create default constructor for robot state * Replace use of std::pair with RobotState * Prim exec update velocity to update state (same logic as only update velocity) * Use auto * Formatting
1 parent ad63b42 commit 2047239

8 files changed

Lines changed: 125 additions & 70 deletions

File tree

src/software/embedded/primitive_executor.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,31 @@ void PrimitiveExecutor::updatePrimitive(const TbotsProto::Primitive& primitive_m
2222

2323
if (current_primitive_.has_move())
2424
{
25-
trajectory_path_ = createTrajectoryPathFromParams(
26-
current_primitive_.move().xy_traj_params(), velocity_, robot_constants_);
25+
trajectory_path_ =
26+
createTrajectoryPathFromParams(current_primitive_.move().xy_traj_params(),
27+
state_.velocity(), robot_constants_);
2728

2829
angular_trajectory_ =
2930
createAngularTrajectoryFromParams(current_primitive_.move().w_traj_params(),
30-
angular_velocity_, robot_constants_);
31+
state_.angularVelocity(), robot_constants_);
3132

3233
time_since_trajectory_creation_ = Duration::fromSeconds(VISION_TO_ROBOT_DELAY_S);
3334
}
3435
}
3536

36-
void PrimitiveExecutor::updateVelocity(const Vector& local_velocity,
37-
const AngularVelocity& angular_velocity)
37+
void PrimitiveExecutor::updateState(const RobotState& state)
3838
{
39-
Vector actual_global_velocity = localToGlobalVelocity(local_velocity, orientation_);
40-
velocity_ = actual_global_velocity;
41-
angular_velocity_ = angular_velocity;
39+
Vector actual_global_velocity =
40+
localToGlobalVelocity(state.localVelocity(), state_.orientation());
41+
state_.setVelocity(actual_global_velocity);
42+
state_.setAngularVelocity(state.angularVelocity());
4243
}
4344

4445
Vector PrimitiveExecutor::getTargetLinearVelocity()
4546
{
4647
Vector local_velocity = globalToLocalVelocity(
4748
trajectory_path_->getVelocity(time_since_trajectory_creation_.toSeconds()),
48-
orientation_);
49+
state_.orientation());
4950
Point position =
5051
trajectory_path_->getPosition(time_since_trajectory_creation_.toSeconds());
5152
double distance_to_destination =
@@ -61,13 +62,13 @@ Vector PrimitiveExecutor::getTargetLinearVelocity()
6162

6263
AngularVelocity PrimitiveExecutor::getTargetAngularVelocity()
6364
{
64-
orientation_ =
65-
angular_trajectory_->getPosition(time_since_trajectory_creation_.toSeconds());
65+
state_.setOrientation(
66+
angular_trajectory_->getPosition(time_since_trajectory_creation_.toSeconds()));
6667

6768
AngularVelocity angular_velocity =
6869
angular_trajectory_->getVelocity(time_since_trajectory_creation_.toSeconds());
6970
Angle orientation_to_destination =
70-
orientation_.minDiff(angular_trajectory_->getDestination());
71+
state_.orientation().minDiff(angular_trajectory_->getDestination());
7172
if (orientation_to_destination.toDegrees() < 5)
7273
{
7374
angular_velocity *= orientation_to_destination.toDegrees() / 5;

src/software/embedded/primitive_executor.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ class PrimitiveExecutor
2626
void updatePrimitive(const TbotsProto::Primitive& primitive_msg);
2727

2828
/**
29-
* Update primitive executor with the current velocity of the robot
29+
* Update primitive executor with the state of the robot
3030
*
31-
* @param local_velocity The current _local_ velocity
32-
* @param angular_velocity The current angular velocity
31+
* @param state The current robot state
3332
*/
34-
void updateVelocity(const Vector& local_velocity,
35-
const AngularVelocity& angular_velocity);
33+
void updateState(const RobotState& state);
3634

3735
/**
3836
* Steps the current primitive and returns a direct control primitive with the
@@ -61,11 +59,9 @@ class PrimitiveExecutor
6159
*/
6260
AngularVelocity getTargetAngularVelocity();
6361

62+
RobotState state_;
6463
TbotsProto::Primitive current_primitive_;
6564
Duration time_since_trajectory_creation_;
66-
Vector velocity_;
67-
AngularVelocity angular_velocity_;
68-
Angle orientation_;
6965
robot_constants::RobotConstants robot_constants_;
7066
std::optional<TrajectoryPath> trajectory_path_;
7167
std::optional<BangBangTrajectory1DAngular> angular_trajectory_;

src/software/embedded/thunderloop.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "software/networking/tbots_network_exception.h"
1919
#include "software/tracy/tracy_constants.h"
2020
#include "software/util/scoped_timespec_timer/scoped_timespec_timer.h"
21+
#include "software/world/robot_state.h"
2122

2223
/**
2324
* https://web.archive.org/web/20210308013218/https://rt.wiki.kernel.org/index.php/Squarewave-example
@@ -262,9 +263,9 @@ void Thunderloop::runLoop()
262263
if (motor_status_.has_value())
263264
{
264265
auto status = motor_status_.value();
265-
primitive_executor_.updateVelocity(
266-
createVector(status.local_velocity()),
267-
createAngularVelocity(status.angular_velocity()));
266+
primitive_executor_.updateState(
267+
RobotState(Point(), createVector(status.local_velocity()), Angle(),
268+
createAngularVelocity(status.angular_velocity())));
268269
}
269270

270271
// Timeout Overrides for Primitives

src/software/simulation/er_force_simulator.cpp

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -297,19 +297,19 @@ void ErForceSimulator::setYellowRobotPrimitiveSet(
297297
const TbotsProto::PrimitiveSet& primitive_set_msg,
298298
std::unique_ptr<TbotsProto::World> world_msg)
299299
{
300-
auto sim_state = getSimulatorState();
301-
const auto& sim_robots = sim_state.yellow_robots();
302-
const auto robot_to_vel_pair_map = getRobotIdToLocalVelocityMap(sim_robots);
300+
auto sim_state = getSimulatorState();
301+
const auto& sim_robots = sim_state.yellow_robots();
302+
const auto robot_map = getRobotIdToRobotStateMap(sim_robots);
303303

304304
yellow_team_world_msg = std::move(world_msg);
305305
const TbotsProto::World world_proto = *yellow_team_world_msg;
306306
for (auto& [robot_id, primitive] : primitive_set_msg.robot_primitives())
307307
{
308-
if (robot_to_vel_pair_map.contains(robot_id))
308+
if (robot_map.contains(robot_id))
309309
{
310-
auto& [local_vel, angular_vel] = robot_to_vel_pair_map.at(robot_id);
310+
const auto& robot_state = robot_map.at(robot_id);
311311
setRobotPrimitive(robot_id, primitive_set_msg, yellow_primitive_executor_map,
312-
world_proto, local_vel, angular_vel);
312+
world_proto, robot_state);
313313
}
314314
}
315315
}
@@ -318,20 +318,20 @@ void ErForceSimulator::setBlueRobotPrimitiveSet(
318318
const TbotsProto::PrimitiveSet& primitive_set_msg,
319319
std::unique_ptr<TbotsProto::World> world_msg)
320320
{
321-
auto sim_state = getSimulatorState();
322-
const auto& sim_robots = sim_state.blue_robots();
323-
const auto robot_to_vel_pair_map = getRobotIdToLocalVelocityMap(sim_robots);
321+
auto sim_state = getSimulatorState();
322+
const auto& sim_robots = sim_state.blue_robots();
323+
const auto robot_map = getRobotIdToRobotStateMap(sim_robots);
324324

325325
blue_team_world_msg = std::move(world_msg);
326326
const TbotsProto::World world_proto = *blue_team_world_msg;
327327

328328
for (auto& [robot_id, primitive] : primitive_set_msg.robot_primitives())
329329
{
330-
if (robot_to_vel_pair_map.contains(robot_id))
330+
if (robot_map.contains(robot_id))
331331
{
332-
auto& [local_vel, angular_vel] = robot_to_vel_pair_map.at(robot_id);
332+
const auto& robot_state = robot_map.at(robot_id);
333333
setRobotPrimitive(robot_id, primitive_set_msg, blue_primitive_executor_map,
334-
world_proto, local_vel, angular_vel);
334+
world_proto, robot_state);
335335
}
336336
}
337337
}
@@ -340,8 +340,7 @@ void ErForceSimulator::setRobotPrimitive(
340340
RobotId id, const TbotsProto::PrimitiveSet& primitive_set_msg,
341341
std::unordered_map<unsigned int, std::shared_ptr<PrimitiveExecutor>>&
342342
robot_primitive_executor_map,
343-
const TbotsProto::World& world_msg, const Vector& local_velocity,
344-
const AngularVelocity angular_velocity)
343+
const TbotsProto::World& world_msg, const RobotState& robot_state)
345344
{
346345
// Set to NEG_X because the world msg in this simulator is normalized
347346
// correctly
@@ -351,7 +350,7 @@ void ErForceSimulator::setRobotPrimitive(
351350
{
352351
auto primitive_executor = robot_primitive_executor_iter->second;
353352
primitive_executor->updatePrimitive(primitive_set_msg.robot_primitives().at(id));
354-
primitive_executor->updateVelocity(local_velocity, angular_velocity);
353+
primitive_executor->updateState(robot_state);
355354
}
356355
else
357356
{
@@ -367,34 +366,25 @@ SSLSimulationProto::RobotControl ErForceSimulator::updateSimulatorRobots(
367366
{
368367
SSLSimulationProto::RobotControl robot_control;
369368

370-
auto sim_state = getSimulatorState();
371-
std::map<RobotId, std::pair<Vector, Angle>> current_velocity_map;
372-
if (side == gameController::Team::BLUE)
373-
{
374-
const auto& sim_robots = sim_state.blue_robots();
375-
current_velocity_map = getRobotIdToLocalVelocityMap(sim_robots);
376-
}
377-
else
378-
{
379-
const auto& sim_robots = sim_state.yellow_robots();
380-
current_velocity_map = getRobotIdToLocalVelocityMap(sim_robots);
381-
}
369+
auto sim_state = getSimulatorState();
370+
const auto& sim_robots = (side == gameController::Team::BLUE)
371+
? sim_state.blue_robots()
372+
: sim_state.yellow_robots();
373+
const auto robot_map = getRobotIdToRobotStateMap(sim_robots);
382374

383-
for (auto& primitive_executor_with_id : robot_primitive_executor_map)
375+
for (auto& [robot_id, primitive_executor] : robot_primitive_executor_map)
384376
{
385-
unsigned int robot_id = primitive_executor_with_id.first;
386-
auto& primitive_executor = primitive_executor_with_id.second;
387377
std::unique_ptr<TbotsProto::DirectControlPrimitive> direct_control;
388378

389379
TbotsProto::PrimitiveExecutorStatus status; // Added for compilation
390380
if (ramping)
391381
{
392382
auto direct_control_no_ramp =
393383
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);
384+
const auto& robot_state = robot_map.at(robot_id);
385+
direct_control = getRampedVelocityPrimitive(
386+
robot_state.localVelocity(), robot_state.angularVelocity(),
387+
*direct_control_no_ramp, primitive_executor_time_step);
398388
}
399389
else
400390
{
@@ -563,18 +553,18 @@ void ErForceSimulator::resetCurrentTime()
563553
current_time = Timestamp::fromSeconds(0);
564554
}
565555

566-
std::map<RobotId, std::pair<Vector, AngularVelocity>>
567-
ErForceSimulator::getRobotIdToLocalVelocityMap(
556+
std::map<RobotId, RobotState> ErForceSimulator::getRobotIdToRobotStateMap(
568557
const google::protobuf::RepeatedPtrField<world::SimRobot>& sim_robots)
569558
{
570-
std::map<RobotId, std::pair<Vector, AngularVelocity>> robot_to_local_velocity;
559+
std::map<RobotId, RobotState> robot_map;
571560
for (const auto& sim_robot : sim_robots)
572561
{
573-
const Vector local_vel =
574-
globalToLocalVelocity(Vector(sim_robot.v_x(), sim_robot.v_y()),
575-
Angle::fromRadians(sim_robot.angle()));
576-
const AngularVelocity angular_vel = Angle::fromRadians(sim_robot.r_z());
577-
robot_to_local_velocity[sim_robot.id()] = {local_vel, angular_vel};
562+
const auto position = Point(sim_robot.p_x(), sim_robot.p_y());
563+
const auto velocity = Vector(sim_robot.v_x(), sim_robot.v_y());
564+
const auto orientation = Angle::fromRadians(sim_robot.angle());
565+
const auto angular_velocity = AngularVelocity::fromRadians(sim_robot.r_z());
566+
robot_map[sim_robot.id()] =
567+
RobotState(position, velocity, orientation, angular_velocity);
578568
}
579-
return robot_to_local_velocity;
569+
return robot_map;
580570
}

src/software/simulation/er_force_simulator.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "software/embedded/primitive_executor.h"
88
#include "software/physics/euclidean_to_wheel.h"
99
#include "software/world/field.h"
10+
#include "software/world/robot_state.h"
1011
#include "software/world/team_types.h"
1112
#include "software/world/world.h"
1213

@@ -158,8 +159,7 @@ class ErForceSimulator
158159
RobotId id, const TbotsProto::PrimitiveSet& primitive_set_msg,
159160
std::unordered_map<unsigned int, std::shared_ptr<PrimitiveExecutor>>&
160161
robot_primitive_executor_map,
161-
const TbotsProto::World& world_msg, const Vector& local_velocity,
162-
const AngularVelocity angular_velocity);
162+
const TbotsProto::World& world_msg, const RobotState& robot_state);
163163

164164
/**
165165
* Gets a map from robot id to local and angular velocity from repeated sim robots
@@ -168,8 +168,7 @@ class ErForceSimulator
168168
*
169169
* @return a map from robot id to local velocity and angular velocity
170170
*/
171-
static std::map<RobotId, std::pair<Vector, AngularVelocity>>
172-
getRobotIdToLocalVelocityMap(
171+
static std::map<RobotId, RobotState> getRobotIdToRobotStateMap(
173172
const google::protobuf::RepeatedPtrField<world::SimRobot>& sim_robots);
174173

175174
/**

src/software/world/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ cc_library(
139139
"//software/geom:angular_velocity",
140140
"//software/geom:point",
141141
"//software/geom:vector",
142+
"//software/physics:velocity_conversion_util",
142143
"//software/time:duration",
143144
],
144145
)

src/software/world/robot_state.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "software/world/robot_state.h"
22

3+
#include "software/physics/velocity_conversion_util.h"
4+
35
RobotState::RobotState(const Point& position, const Vector& velocity,
46
const Angle& orientation, const AngularVelocity& angular_velocity,
57
const bool breakbeam_tripped)
@@ -33,6 +35,11 @@ Vector RobotState::velocity() const
3335
return velocity_;
3436
}
3537

38+
Vector RobotState::localVelocity() const
39+
{
40+
return globalToLocalVelocity(velocity_, orientation_);
41+
}
42+
3643
Angle RobotState::orientation() const
3744
{
3845
return orientation_;
@@ -48,6 +55,26 @@ bool RobotState::breakbeamTripped() const
4855
return breakbeam_tripped_;
4956
}
5057

58+
void RobotState::setPosition(const Point& position)
59+
{
60+
position_ = position;
61+
}
62+
63+
void RobotState::setVelocity(const Vector& velocity)
64+
{
65+
velocity_ = velocity;
66+
}
67+
68+
void RobotState::setOrientation(const Angle& orientation)
69+
{
70+
orientation_ = orientation;
71+
}
72+
73+
void RobotState::setAngularVelocity(const AngularVelocity& angular_velocity)
74+
{
75+
angular_velocity_ = angular_velocity;
76+
}
77+
5178
bool RobotState::operator==(const RobotState& other) const
5279
{
5380
return this->position() == other.position() && this->velocity() == other.velocity() &&

src/software/world/robot_state.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ using RobotId = unsigned int;
1717
class RobotState
1818
{
1919
public:
20+
/**
21+
* Creates a robot state with default values
22+
*/
23+
RobotState() = default;
24+
2025
/**
2126
* Creates a new robot state with the given position, velocity, orientation, angular
2227
* velocity, and timestamp
@@ -54,6 +59,13 @@ class RobotState
5459
*/
5560
Vector velocity() const;
5661

62+
/**
63+
* Returns the local velocity of the robot represented by this state
64+
*
65+
* @return the local velocity of the robot represented by this state
66+
*/
67+
Vector localVelocity() const;
68+
5769
/**
5870
* Returns the orientation of the robot represented by this state
5971
*
@@ -75,6 +87,34 @@ class RobotState
7587
*/
7688
bool breakbeamTripped() const;
7789

90+
/**
91+
* Sets the position of the robot, with coordinates in metres
92+
*
93+
* @param position The new position of the robot
94+
*/
95+
void setPosition(const Point& position);
96+
97+
/**
98+
* Sets the velocity of the robot, in metres per second
99+
*
100+
* @param velocity The new velocity of the robot
101+
*/
102+
void setVelocity(const Vector& velocity);
103+
104+
/**
105+
* Sets the orientation of the robot
106+
*
107+
* @param orientation The new orientation of the robot
108+
*/
109+
void setOrientation(const Angle& orientation);
110+
111+
/**
112+
* Sets the angular velocity of the robot
113+
*
114+
* @param angular_velocity The new angular velocity of the robot
115+
*/
116+
void setAngularVelocity(const AngularVelocity& angular_velocity);
117+
78118
/**
79119
* Defines the equality operator for a RobotState. RobotStates are equal if
80120
* all their members are equal

0 commit comments

Comments
 (0)