Skip to content

Commit b3be8df

Browse files
committed
Add different high level command modes
1 parent b57ee4a commit b3be8df

File tree

7 files changed

+144
-37
lines changed

7 files changed

+144
-37
lines changed

bindings/pydairlib/analysis_scripts/log_plotter_cassie.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def main():
170170

171171
plot_state(x, t_x, u, t_u, x_datatypes, u_datatypes, t_osc_debug, fsm)
172172

173-
plot_osc_debug(t_osc_debug, fsm, osc_debug, osc_debug_reg_cost, t_cassie_out, estop_signal, osc_output)
173+
# plot_osc_debug(t_osc_debug, fsm, osc_debug, osc_debug_reg_cost, t_cassie_out, estop_signal, osc_output)
174174

175175
# plot_feet_positions(plant_w_spr, context, x, l_toe_frame, mid_contact_disp, world,
176176
# t_x, t_slice, t_osc_debug, fsm, "left foot", True)
@@ -225,6 +225,15 @@ def main():
225225
# print("shell command = ", cmd)
226226
# RunCommand(cmd, True)
227227

228+
### Compute torque square
229+
# # u_cost = 0.0
230+
# # for i in range(len(u)):
231+
# # u_cost += u[i].T @ u[i]
232+
# u_cost = np.sum(u**2)
233+
# u_cost /= len(u)
234+
# print(u_cost)
235+
# import pdb;pdb.set_trace()
236+
228237
plt.show()
229238

230239

examples/Cassie/osc/high_level_command.cc

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ using drake::systems::LeafSystem;
3434
using drake::multibody::JacobianWrtVariable;
3535
using drake::trajectories::PiecewisePolynomial;
3636

37+
using drake::MatrixX;
38+
3739
namespace dairlib {
3840
namespace cassie {
3941
namespace osc {
@@ -47,7 +49,8 @@ HighLevelCommand::HighLevelCommand(
4749
this->DeclareAbstractInputPort("lcmt_cassie_output",
4850
drake::Value<dairlib::lcmt_cassie_out>{})
4951
.get_index();
50-
use_radio_command_ = true;
52+
// use_radio_command_ = true;
53+
high_level_mode_ = radio;
5154

5255
vel_scale_rot_ = vel_scale_rot;
5356
vel_scale_trans_sagital_ = vel_scale_trans_sagital;
@@ -63,7 +66,9 @@ HighLevelCommand::HighLevelCommand(
6366
const Vector2d& global_target_position,
6467
const Vector2d& params_of_no_turning)
6568
: HighLevelCommand(plant, context) {
66-
use_radio_command_ = false;
69+
// use_radio_command_ = false;
70+
high_level_mode_ = desired_xy_position;
71+
6772
kp_yaw_ = kp_yaw;
6873
kd_yaw_ = kd_yaw;
6974
vel_max_yaw_ = vel_max_yaw;
@@ -107,10 +112,51 @@ HighLevelCommand::HighLevelCommand(
107112
des_vel_idx_ = DeclareDiscreteState(VectorXd::Zero(3));
108113
}
109114

115+
void HighLevelCommand::SetOpenLoopVelCommandTraj() {
116+
high_level_mode_ = open_loop_vel_command_traj;
117+
118+
std::vector<double> breaks = {0};
119+
std::vector<MatrixXd> knots = {(MatrixX<double>(3, 1) << 0, 0, 0).finished()};
120+
breaks.push_back(breaks.back() + 2);
121+
knots.push_back((MatrixX<double>(3, 1) << 0, 0, 0).finished());
122+
breaks.push_back(breaks.back() + 5);
123+
knots.push_back((MatrixX<double>(3, 1) << 0, 2, 0).finished());
124+
breaks.push_back(breaks.back() + 2);
125+
knots.push_back((MatrixX<double>(3, 1) << 0.7, 2, 0).finished());
126+
breaks.push_back(breaks.back() + 1.5);
127+
knots.push_back((MatrixX<double>(3, 1) << 0.7, 2, 0).finished());
128+
breaks.push_back(breaks.back() + 2);
129+
knots.push_back((MatrixX<double>(3, 1) << 0, 2, 0).finished());
130+
breaks.push_back(breaks.back() + 2);
131+
knots.push_back((MatrixX<double>(3, 1) << 0, 2, 0).finished());
132+
breaks.push_back(breaks.back() + 1);
133+
knots.push_back((MatrixX<double>(3, 1) << 0, 0, 0).finished());
134+
breaks.push_back(breaks.back() + 10000000);
135+
knots.push_back((MatrixX<double>(3, 1) << 0, 0, 0).finished());
136+
137+
// Construct the PiecewisePolynomial.
138+
desired_vel_command_traj_ =
139+
PiecewisePolynomial<double>::FirstOrderHold(breaks, knots);
140+
};
141+
142+
void HighLevelCommand::SetDesiredXYTraj() {
143+
high_level_mode_ = desired_xy_traj;
144+
145+
std::vector<double> breaks = {0};
146+
std::vector<MatrixXd> knots = {(MatrixX<double>(3, 1) << 0, 0, 0).finished()};
147+
148+
// TODO: finish this
149+
DRAKE_UNREACHABLE();
150+
151+
// Construct the PiecewisePolynomial.
152+
desired_xy_traj_ =
153+
PiecewisePolynomial<double>::FirstOrderHold(breaks, knots);
154+
};
155+
110156
EventStatus HighLevelCommand::DiscreteVariableUpdate(
111157
const Context<double>& context,
112158
DiscreteValues<double>* discrete_state) const {
113-
if (use_radio_command_) {
159+
if (high_level_mode_ == radio) {
114160
const auto& cassie_out = this->EvalInputValue<dairlib::lcmt_cassie_out>(
115161
context, cassie_out_port_);
116162
// TODO(yangwill) make sure there is a message available
@@ -123,9 +169,15 @@ EventStatus HighLevelCommand::DiscreteVariableUpdate(
123169
vel_scale_trans_lateral_ * cassie_out->pelvis.radio.channel[1];
124170
des_vel(1) += vel_command_offset_x_; //hack: help rom_iter=300 walk in place at start
125171
discrete_state->get_mutable_vector(des_vel_idx_).set_value(des_vel);
126-
} else {
172+
} else if (high_level_mode_ == desired_xy_position) {
127173
discrete_state->get_mutable_vector(des_vel_idx_)
128174
.set_value(CalcCommandFromTargetPosition(context));
175+
} else if (high_level_mode_ == open_loop_vel_command_traj) {
176+
discrete_state->get_mutable_vector(des_vel_idx_)
177+
.set_value(desired_vel_command_traj_.value(context.get_time()));
178+
} else if (high_level_mode_ == desired_xy_traj) {
179+
discrete_state->get_mutable_vector(des_vel_idx_)
180+
.set_value(CalcCommandFromDesiredXYTraj(context));
129181
}
130182

131183
return EventStatus::Succeeded();
@@ -228,6 +280,14 @@ VectorXd HighLevelCommand::CalcCommandFromTargetPosition(
228280
return des_vel;
229281
}
230282

283+
VectorXd HighLevelCommand::CalcCommandFromDesiredXYTraj(
284+
const Context<double>& context) const {
285+
Vector3d des_vel;
286+
287+
DRAKE_UNREACHABLE();
288+
return des_vel;
289+
}
290+
231291
void HighLevelCommand::CopyHeadingAngle(const Context<double>& context,
232292
BasicVector<double>* output) const {
233293
double desired_heading_pos =

examples/Cassie/osc/high_level_command.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class HighLevelCommand : public drake::systems::LeafSystem<double> {
6868
const Eigen::Vector2d& global_target_position,
6969
const Eigen::Vector2d& params_of_no_turning);
7070

71+
HighLevelCommand(const drake::multibody::MultibodyPlant<double>& plant,
72+
drake::systems::Context<double>* context);
73+
7174
// Input/output ports
7275
const drake::systems::InputPort<double>& get_state_input_port() const {
7376
return this->get_input_port(state_port_);
@@ -86,10 +89,11 @@ class HighLevelCommand : public drake::systems::LeafSystem<double> {
8689
double vel_command_offset_x_ = 0;
8790
/// ///
8891

89-
private:
90-
HighLevelCommand(const drake::multibody::MultibodyPlant<double>& plant,
91-
drake::systems::Context<double>* context);
92+
// Hacks -- setting desired command trajectory
93+
void SetOpenLoopVelCommandTraj();
94+
void SetDesiredXYTraj();
9295

96+
private:
9397
drake::systems::EventStatus DiscreteVariableUpdate(
9498
const drake::systems::Context<double>& context,
9599
drake::systems::DiscreteValues<double>* discrete_state) const;
@@ -108,7 +112,7 @@ class HighLevelCommand : public drake::systems::LeafSystem<double> {
108112
drake::systems::Context<double>* context_;
109113
const drake::multibody::BodyFrame<double>& world_;
110114
const drake::multibody::Body<double>& pelvis_;
111-
bool use_radio_command_;
115+
// bool use_radio_command_;
112116

113117
// Port index
114118
int state_port_;
@@ -141,6 +145,20 @@ class HighLevelCommand : public drake::systems::LeafSystem<double> {
141145
double vel_scale_rot_;
142146
double vel_scale_trans_sagital_;
143147
double vel_scale_trans_lateral_;
148+
149+
// Testing
150+
enum high_level_modes {
151+
radio,
152+
desired_xy_position,
153+
open_loop_vel_command_traj,
154+
desired_xy_traj
155+
};
156+
157+
int high_level_mode_;
158+
drake::trajectories::PiecewisePolynomial<double> desired_vel_command_traj_;
159+
drake::trajectories::PiecewisePolynomial<double> desired_xy_traj_;
160+
Eigen::VectorXd CalcCommandFromDesiredXYTraj(
161+
const drake::systems::Context<double>& context) const;
144162
};
145163

146164
} // namespace osc

examples/goldilocks_models/procman_scripts/cassie_controller_sim_fast_turning.pmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ group "1.rom-space-controller" {
3131
host = "localhost";
3232
}
3333
cmd "3.1.rom-planner (short time limit)" {
34-
exec = "./bazel-bin/examples/goldilocks_models/run_cassie_rom_planner_process --zero_touchdown_impact=true --fix_duration=true --use_ipopt=true --n_step=2 --feas_tol=1e-2 --log_solver_info=false --knots_per_mode=5 --spring_model=false --min_mpc_thread_loop_duration=0.04";
34+
exec = "./bazel-bin/examples/goldilocks_models/run_cassie_rom_planner_process --zero_touchdown_impact=true --fix_duration=true --use_ipopt=true --n_step=2 --feas_tol=1e-2 --log_solver_info=false --knots_per_mode=5 --spring_model=false --min_mpc_thread_loop_duration=0.04 --pelvis_height=0.9";
3535
host = "localhost";
3636
}
3737
cmd "3.3.rom-planner (debug a given solve_idx)" {

examples/goldilocks_models/procman_scripts/cassie_controller_sim_uneven_terrain.pmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ group "5.lcm-tools" {
177177

178178
group "1.rom-space-controller-with-init-traj" {
179179
cmd "2.1.rom-walking-controller (w init)" {
180-
exec = "bazel-bin/examples/goldilocks_models/run_cassie_rom_controller --channel_u=ROM_WALKING --use_IK=false --get_swing_foot_from_planner=true --close_sim_gap=false --spring_model=false --init_traj_file_name=0_rom_trajectory --offset_swing_hip_yaw_for_heading=true ";
180+
exec = "bazel-bin/examples/goldilocks_models/run_cassie_rom_controller --channel_u=ROM_WALKING --use_IK=false --get_swing_foot_from_planner=true --close_sim_gap=false --spring_model=false --init_traj_file_name=0_rom_trajectory --offset_swing_hip_yaw_for_heading=true --high_level_traj_mode=open_loop_desired_vel_command";
181181
host = "localhost";
182182
}
183183
cmd "2.2.rom-planner (w init)" {

examples/goldilocks_models/run_cassie_rom_controller.cc

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ DEFINE_bool(offset_swing_hip_yaw_for_heading, false,
182182

183183
DEFINE_double(ground_incline, 0.0, "ground incline");
184184

185+
// Testing -- to create different behavior for Cassie
186+
DEFINE_string(high_level_traj_mode, "default",
187+
"Options:"
188+
"default"
189+
"open_loop_desired_vel_command"
190+
"desired_x_y_yaw_path");
191+
185192
int DoMain(int argc, char* argv[]) {
186193
gflags::ParseCommandLineFlags(&argc, &argv, true);
187194

@@ -513,30 +520,43 @@ int DoMain(int argc, char* argv[]) {
513520
Eigen::Vector2d params_of_no_turning(gains.yaw_deadband_blur,
514521
gains.yaw_deadband_radius);
515522
cassie::osc::HighLevelCommand* high_level_command;
516-
if (gains.use_radio) {
517-
high_level_command = builder.AddSystem<cassie::osc::HighLevelCommand>(
518-
plant_w_spr, context_w_spr.get(), gains.vel_scale_rot,
519-
gains.vel_scale_trans_sagital, gains.vel_scale_trans_lateral);
520-
auto cassie_out_receiver =
521-
builder.AddSystem(LcmSubscriberSystem::Make<dairlib::lcmt_cassie_out>(
522-
gains.use_virtual_radio ? "CASSIE_OUTPUT_ONLY_RADIO"
523-
: FLAGS_cassie_out_channel,
524-
&lcm_local));
525-
builder.Connect(cassie_out_receiver->get_output_port(),
526-
high_level_command->get_cassie_output_port());
527-
528-
// hack: help rom_iter=300 walk in place at start
529-
if (!FLAGS_close_sim_gap) {
530-
high_level_command->vel_command_offset_x_ =
531-
gains.vel_command_offset_x * ((model_iter - 1) / 300);
523+
if (FLAGS_high_level_traj_mode == "default") {
524+
if (gains.use_radio) {
525+
high_level_command = builder.AddSystem<cassie::osc::HighLevelCommand>(
526+
plant_w_spr, context_w_spr.get(), gains.vel_scale_rot,
527+
gains.vel_scale_trans_sagital, gains.vel_scale_trans_lateral);
528+
auto cassie_out_receiver =
529+
builder.AddSystem(LcmSubscriberSystem::Make<dairlib::lcmt_cassie_out>(
530+
gains.use_virtual_radio ? "CASSIE_OUTPUT_ONLY_RADIO"
531+
: FLAGS_cassie_out_channel,
532+
&lcm_local));
533+
builder.Connect(cassie_out_receiver->get_output_port(),
534+
high_level_command->get_cassie_output_port());
535+
536+
// hack: help rom_iter=300 walk in place at start
537+
if (!FLAGS_close_sim_gap) {
538+
high_level_command->vel_command_offset_x_ =
539+
gains.vel_command_offset_x * ((model_iter - 1) / 300);
540+
}
541+
} else {
542+
high_level_command = builder.AddSystem<cassie::osc::HighLevelCommand>(
543+
plant_w_spr, context_w_spr.get(), gains.kp_yaw, gains.kd_yaw,
544+
gains.vel_max_yaw, gains.kp_pos_sagital, gains.kd_pos_sagital,
545+
gains.vel_max_sagital, gains.kp_pos_lateral, gains.kd_pos_lateral,
546+
gains.vel_max_lateral, gains.target_pos_offset,
547+
global_target_position, params_of_no_turning);
532548
}
533-
} else {
549+
} else if (FLAGS_high_level_traj_mode == "open_loop_desired_vel_command") {
550+
// hack -- testing desired command velocity traj
534551
high_level_command = builder.AddSystem<cassie::osc::HighLevelCommand>(
535-
plant_w_spr, context_w_spr.get(), gains.kp_yaw, gains.kd_yaw,
536-
gains.vel_max_yaw, gains.kp_pos_sagital, gains.kd_pos_sagital,
537-
gains.vel_max_sagital, gains.kp_pos_lateral, gains.kd_pos_lateral,
538-
gains.vel_max_lateral, gains.target_pos_offset, global_target_position,
539-
params_of_no_turning);
552+
plant_w_spr, context_w_spr.get());
553+
high_level_command->SetOpenLoopVelCommandTraj();
554+
} else if (FLAGS_high_level_traj_mode == "desired_x_y_yaw_path") {
555+
high_level_command = builder.AddSystem<cassie::osc::HighLevelCommand>(
556+
plant_w_spr, context_w_spr.get());
557+
high_level_command->SetDesiredXYTraj();
558+
} else {
559+
DRAKE_UNREACHABLE();
540560
}
541561
builder.Connect(state_receiver->get_output_port(0),
542562
high_level_command->get_state_input_port());

examples/goldilocks_models/terrains/stones.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ stones:
1616
# - [[1, 0.0, 0.5], [0.0, 0.0, 1.0], [0.25, 0.25, 0.5], [0.0]]
1717

1818
# - [[0.0, 0, 0.0], [0.0, 0.0, 1.0], [5.0, 5.0, 0.1], [0.0]] # small middle terrain
19-
# - [[0.0, 0, 0.0], [0.0, 0.0, 1.0], [25.0, 25.0, 0.1], [0.0]] # big middle terrain
19+
- [[0.0, 0, 0.0], [0.0, 0.0, 1.0], [25.0, 25.0, 0.1], [0.0]] # big middle terrain
2020

2121
################### Ramps #####################
2222

@@ -31,9 +31,9 @@ stones:
3131
# - [[5.5, 0, 0.2], [0.0, 0.0, 1.0], [5.0, 5.0, 0.1], [0.0]]
3232

3333
# Ramp with 20% incline
34-
- [[0.0, 0, 0.0], [0.0, 0.0, 1.0], [5.0, 5.0, 0.1], [0.0]]
35-
- [[1, 0, 0.0], [-0.2, 0.0, 1.0], [4.08, 4.0, 0.1], [0.0]] # 4 / cos(arctan(0.2))
36-
- [[5.5, 0, 0.4], [0.0, 0.0, 1.0], [5.0, 5.0, 0.1], [0.0]]
34+
# - [[0.0, 0, 0.0], [0.0, 0.0, 1.0], [5.0, 5.0, 0.1], [0.0]]
35+
# - [[1, 0, 0.0], [-0.2, 0.0, 1.0], [4.08, 4.0, 0.1], [0.0]] # 4 / cos(arctan(0.2))
36+
# - [[5.5, 0, 0.4], [0.0, 0.0, 1.0], [5.0, 5.0, 0.1], [0.0]]
3737

3838
################### Roads #####################
3939

0 commit comments

Comments
 (0)