Skip to content

Commit c3087d7

Browse files
committed
init
1 parent 2489d36 commit c3087d7

10 files changed

Lines changed: 122 additions & 19 deletions

src/software/constants.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ const std::string ROBOT_KICK_CONSTANT_CONFIG_KEY = "kick_constant";
5252
const std::string ROBOT_KICK_EXP_COEFF_CONFIG_KEY = "kick_coeff";
5353
const std::string ROBOT_CHIP_PULSE_WIDTH_CONFIG_KEY = "chip_pulse_width";
5454

55+
// Position (x/y) controller PID gains
56+
const std::string ROBOT_POSITION_CONTROLLER_KP_CONFIG_KEY = "position_controller_kp";
57+
const std::string ROBOT_POSITION_CONTROLLER_KI_CONFIG_KEY = "position_controller_ki";
58+
const std::string ROBOT_POSITION_CONTROLLER_KD_CONFIG_KEY = "position_controller_kd";
59+
const std::string ROBOT_POSITION_CONTROLLER_MAX_INTEGRAL_CONFIG_KEY =
60+
"position_controller_max_integral";
61+
62+
// Orientation (heading) controller PID gains
63+
const std::string ROBOT_ORIENTATION_CONTROLLER_KP_CONFIG_KEY =
64+
"orientation_controller_kp";
65+
const std::string ROBOT_ORIENTATION_CONTROLLER_KI_CONFIG_KEY =
66+
"orientation_controller_ki";
67+
const std::string ROBOT_ORIENTATION_CONTROLLER_KD_CONFIG_KEY =
68+
"orientation_controller_kd";
69+
const std::string ROBOT_ORIENTATION_CONTROLLER_MAX_INTEGRAL_CONFIG_KEY =
70+
"orientation_controller_max_integral";
71+
5572
const std::string SSL_VISION_ADDRESS = "224.5.23.2";
5673
static constexpr unsigned int SSL_VISION_PORT = 10006;
5774

src/software/embedded/motion_control/orientation_controller.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#include "software/embedded/motion_control/orientation_controller.h"
22

3+
OrientationController::OrientationController(const OrientationControllerConfig& config)
4+
: w_pid_(config.kp, config.ki, config.kd, config.max_integral)
5+
{
6+
}
7+
38
AngularVelocity OrientationController::step(
49
const Angle& orientation, const BangBangTrajectory1DAngular& target_trajectory,
510
Duration elapsed_time, Duration delta_time)

src/software/embedded/motion_control/orientation_controller.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@
77
#include "software/geom/angular_velocity.h"
88
#include "software/time/duration.h"
99

10+
// TODO(#3737): tune constants
11+
// PID gains for OrientationController's underlying heading PID controller. Defaults
12+
// preserve the previously hardcoded tuning; robot_config.toml can override these on a
13+
// per-robot basis.
14+
struct OrientationControllerConfig
15+
{
16+
double kp = 0.4;
17+
double ki = 0.0;
18+
double kd = 0.0;
19+
double max_integral = 0.0;
20+
};
21+
1022
class OrientationController
1123
: public MotionController<Angle, BangBangTrajectory1DAngular, AngularVelocity>
1224
{
1325
public:
1426
/**
1527
* Constructs an orientation controller that uses measurements over multiple
1628
* time intervals to calculate the target angular velocity to minimize error.
29+
*
30+
* @param config The PID gains to use for the underlying heading PID controller.
1731
*/
18-
OrientationController() = default;
32+
explicit OrientationController(
33+
const OrientationControllerConfig& config = OrientationControllerConfig());
1934

2035
/**
2136
* Given an orientation and target orientation, returns a target angular
@@ -36,8 +51,7 @@ class OrientationController
3651
void reset() override;
3752

3853
private:
39-
// TODO(#3737): tune constants
40-
PidController<double> w_pid_{0.4, 0.0, 0.0, 0.0};
54+
PidController<double> w_pid_;
4155

4256
static constexpr double ANGULAR_DESTINATION_THRESHOLD_DEGREES = 5;
4357
};

src/software/embedded/motion_control/position_controller.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
#include "software/geom/algorithms/distance.h"
44

5+
PositionController::PositionController(const PositionControllerConfig& config)
6+
: x_pid_(config.kp, config.ki, config.kd, config.max_integral),
7+
y_pid_(config.kp, config.ki, config.kd, config.max_integral)
8+
{
9+
}
10+
511
Vector PositionController::step(const Point& position,
612
const TrajectoryPath& target_trajectory,
713
Duration elapsed_time, Duration delta_time)

src/software/embedded/motion_control/position_controller.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@
77
#include "software/geom/vector.h"
88
#include "software/time/duration.h"
99

10+
// TODO(#3737): tune constants
11+
// PID gains for PositionController's underlying x/y PID controllers. Both axes share
12+
// the same gains. Defaults preserve the previously hardcoded tuning; robot_config.toml
13+
// can override these on a per-robot basis.
14+
struct PositionControllerConfig
15+
{
16+
double kp = 1.2;
17+
double ki = 0.1;
18+
double kd = 0.0;
19+
double max_integral = 10.0;
20+
};
21+
1022
class PositionController : public MotionController<Point, TrajectoryPath, Vector>
1123
{
1224
public:
1325
/**
1426
* Constructs a position controller that uses measurements over multiple time
1527
* intervals to calculate the target velocity to minimize error.
28+
*
29+
* @param config The PID gains to use for the underlying x/y PID controllers.
1630
*/
17-
PositionController() = default;
31+
explicit PositionController(const PositionControllerConfig& config = PositionControllerConfig());
1832

1933
/**
2034
* Given a position and target trajectory, returns a target global velocity to
@@ -34,9 +48,8 @@ class PositionController : public MotionController<Point, TrajectoryPath, Vector
3448
void reset() override;
3549

3650
private:
37-
// TODO(#3737): tune constants
38-
PidController<double> x_pid_{1.2, 0.1, 0.0, 10.0};
39-
PidController<double> y_pid_{1.2, 0.1, 0.0, 10.0};
51+
PidController<double> x_pid_;
52+
PidController<double> y_pid_;
4053

4154
static constexpr double MAX_DAMPENING_VELOCITY_DISTANCE_M = 0.05;
4255
};

src/software/embedded/primitive_executor.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,14 @@ bool destinationsChangedSignificantly(const TrajectoryPath& current,
6666
}
6767

6868
PrimitiveExecutor::PrimitiveExecutor(
69-
const robot_constants::RobotConstants& robot_constants)
70-
: state_(), current_primitive_(), robot_constants_(robot_constants)
69+
const robot_constants::RobotConstants& robot_constants,
70+
const PositionControllerConfig& position_controller_config,
71+
const OrientationControllerConfig& orientation_controller_config)
72+
: state_(),
73+
current_primitive_(),
74+
robot_constants_(robot_constants),
75+
position_controller_(position_controller_config),
76+
orientation_controller_(orientation_controller_config)
7177
{
7278
}
7379

src/software/embedded/primitive_executor.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,17 @@ class PrimitiveExecutor
1919
* Constructor
2020
* @param robot_constants The robot constants for the robot which uses this primitive
2121
* executor
22+
* @param position_controller_config The PID gains for the position controller,
23+
* typically read from this robot's robot_config.toml
24+
* @param orientation_controller_config The PID gains for the orientation
25+
* controller, typically read from this robot's robot_config.toml
2226
*/
23-
explicit PrimitiveExecutor(const robot_constants::RobotConstants& robot_constants);
27+
explicit PrimitiveExecutor(
28+
const robot_constants::RobotConstants& robot_constants,
29+
const PositionControllerConfig& position_controller_config =
30+
PositionControllerConfig(),
31+
const OrientationControllerConfig& orientation_controller_config =
32+
OrientationControllerConfig());
2433

2534
/**
2635
* Update primitive executor with a new Primitive

src/software/embedded/thunderloop.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,26 @@ Thunderloop::Thunderloop(const robot_constants::RobotConstants& robot_constants,
8181
std::stoi(toml_config_client_->get(ROBOT_MULTICAST_CHANNEL_CONFIG_KEY))),
8282
network_interface_(toml_config_client_->get(ROBOT_NETWORK_INTERFACE_CONFIG_KEY)),
8383
loop_hz_(loop_hz),
84-
primitive_executor_(robot_constants),
84+
primitive_executor_(
85+
robot_constants,
86+
PositionControllerConfig{
87+
std::stod(toml_config_client_->get(
88+
ROBOT_POSITION_CONTROLLER_KP_CONFIG_KEY)),
89+
std::stod(toml_config_client_->get(
90+
ROBOT_POSITION_CONTROLLER_KI_CONFIG_KEY)),
91+
std::stod(toml_config_client_->get(
92+
ROBOT_POSITION_CONTROLLER_KD_CONFIG_KEY)),
93+
std::stod(toml_config_client_->get(
94+
ROBOT_POSITION_CONTROLLER_MAX_INTEGRAL_CONFIG_KEY))},
95+
OrientationControllerConfig{
96+
std::stod(toml_config_client_->get(
97+
ROBOT_ORIENTATION_CONTROLLER_KP_CONFIG_KEY)),
98+
std::stod(toml_config_client_->get(
99+
ROBOT_ORIENTATION_CONTROLLER_KI_CONFIG_KEY)),
100+
std::stod(toml_config_client_->get(
101+
ROBOT_ORIENTATION_CONTROLLER_KD_CONFIG_KEY)),
102+
std::stod(toml_config_client_->get(
103+
ROBOT_ORIENTATION_CONTROLLER_MAX_INTEGRAL_CONFIG_KEY))}),
85104
robot_localizer_(RobotLocalizer::RobotLocalizerConfig{
86105
robot_constants.kalman_process_noise_variance_rad_per_s_4,
87106
robot_constants.kalman_vision_noise_variance_rad_2,

src/software/embedded/toml_config/toml_config_client.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ void TomlConfigClient::loadConfig()
4747
{"battery_voltage", "0.0"},
4848
{"current_draw", "0.0"},
4949
{"cap_voltage", "0.0"},
50+
{"position_controller_kp", "1.2"},
51+
{"position_controller_ki", "0.1"},
52+
{"position_controller_kd", "0.0"},
53+
{"position_controller_max_integral", "10.0"},
54+
{"orientation_controller_kp", "0.4"},
55+
{"orientation_controller_ki", "0.0"},
56+
{"orientation_controller_kd", "0.0"},
57+
{"orientation_controller_max_integral", "0.0"},
5058
};
5159
writeConfig();
5260
return;
@@ -81,7 +89,7 @@ void TomlConfigClient::writeConfig()
8189
has_pending_changes_ = false;
8290
}
8391

84-
std::string TomlConfigClient::get(const std::string& key)
92+
std::string TomlConfigClient::get(const std::string& key, const std::string& default_value)
8593
{
8694
std::lock_guard<std::mutex> lock(config_mutex_);
8795

@@ -106,14 +114,17 @@ std::string TomlConfigClient::get(const std::string& key)
106114
}
107115
else
108116
{
109-
LOG(WARNING) << "TOML key '" << key << "' exists but is not a supported type";
110-
return "";
117+
LOG(WARNING) << "TOML key '" << key
118+
<< "' exists but is not a supported type, using default value '"
119+
<< default_value << "'";
120+
return default_value;
111121
}
112122
}
113123
catch (const std::exception& e)
114124
{
115-
LOG(WARNING) << "TOML key '" << key << "' not found: " << e.what();
116-
return "";
125+
LOG(WARNING) << "TOML key '" << key << "' not found, using default value '"
126+
<< default_value << "': " << e.what();
127+
return default_value;
117128
}
118129
}
119130

src/software/embedded/toml_config/toml_config_client.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ class TomlConfigClient
2525

2626
/**
2727
* Gets the value corresponding to the key as a string.
28-
* Returns empty string if key doesn't exist.
28+
* Returns default_value if the key doesn't exist. This allows config files that
29+
* predate the introduction of a key (e.g. an older robot_config.toml already
30+
* deployed on a robot) to keep working without needing to be regenerated.
2931
*
3032
* @param key The configuration key (e.g., "/robot_id")
31-
* @return The value as a string, or empty string if not found
33+
* @param default_value The value to return if the key doesn't exist
34+
* @return The value as a string, or default_value if not found
3235
*/
33-
std::string get(const std::string& key);
36+
std::string get(const std::string& key, const std::string& default_value = "");
3437

3538
/**
3639
* Sets a key-value pair in the TOML file.

0 commit comments

Comments
 (0)