Skip to content

Commit a1a373d

Browse files
committed
doc: add documentation for publishers
1 parent 720d12b commit a1a373d

File tree

4 files changed

+132
-13
lines changed

4 files changed

+132
-13
lines changed

systems/publishers/force_publisher.cc

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

33
#include "systems/framework/c3_output.h"
44

5+
// Drake and Eigen namespace usages for convenience.
56
using drake::systems::Context;
67
using drake::systems::DiagramBuilder;
78
using drake::systems::lcm::LcmPublisherSystem;
@@ -13,10 +14,14 @@ using Eigen::VectorXd;
1314
namespace c3 {
1415
namespace systems {
1516
namespace publishers {
17+
18+
// Publishes contact force information to LCM for visualization and logging.
1619
ContactForcePublisher::ContactForcePublisher() {
20+
// Declare input port for the C3 solution.
1721
c3_solution_port_ = this->DeclareAbstractInputPort(
1822
"solution", drake::Value<C3Output::C3Solution>{})
1923
.get_index();
24+
// Declare input port for contact Jacobian and contact points.
2025
lcs_contact_info_port_ =
2126
this->DeclareAbstractInputPort(
2227
"J_lcs, p_lcs",
@@ -25,12 +30,14 @@ ContactForcePublisher::ContactForcePublisher() {
2530
.get_index();
2631

2732
this->set_name("c3_contact_force_publisher");
33+
// Declare output port for publishing contact forces.
2834
contact_force_output_port_ =
2935
this->DeclareAbstractOutputPort("lcmt_force", lcmt_forces(),
3036
&ContactForcePublisher::DoCalc)
3137
.get_index();
3238
}
3339

40+
// Calculates and outputs the contact forces based on the current context.
3441
void ContactForcePublisher::DoCalc(const Context<double>& context,
3542
lcmt_forces* output) const {
3643
// Get Solution from C3
@@ -53,6 +60,7 @@ void ContactForcePublisher::DoCalc(const Context<double>& context,
5360

5461
int contact_var_start;
5562
int force_index;
63+
// Iterate over all contact points and compute forces.
5664
for (int contact_index = 0; contact_index < (int)contact_points.size();
5765
++contact_index) {
5866
contact_var_start =
@@ -75,13 +83,17 @@ void ContactForcePublisher::DoCalc(const Context<double>& context,
7583
}
7684
}
7785
auto force = lcmt_contact_force();
86+
// Set contact point position.
7887
force.contact_point[0] = contact_points.at(contact_index)[0];
7988
force.contact_point[1] = contact_points.at(contact_index)[1];
8089
force.contact_point[2] = contact_points.at(contact_index)[2];
8190
// TODO(yangwill): find a cleaner way to figure out the equivalent forces
8291
// VISUALIZING FORCES FOR THE FIRST KNOT POINT
8392
// 6, 7, 8 are the indices for the x,y,z components of the tray
8493
// expressed in the world frame
94+
// Compute force vector in world frame.
95+
std::cout << J_c << std::endl;
96+
exit(0);
8597
force.contact_force[0] = solution->lambda_sol_(contact_var_index, 0) *
8698
J_c.row(contact_jacobian_row)(6);
8799
force.contact_force[1] = solution->lambda_sol_(contact_var_index, 0) *
@@ -91,21 +103,25 @@ void ContactForcePublisher::DoCalc(const Context<double>& context,
91103
output->forces[force_index + i] = force;
92104
}
93105
}
106+
// Set output timestamp in microseconds.
94107
output->utime = context.get_time() * 1e6;
95108
}
96109

110+
// Adds this publisher and an LCM publisher system to the diagram builder.
97111
LcmPublisherSystem* ContactForcePublisher::AddLcmPublisherToBuilder(
98112
DiagramBuilder<double>& builder,
99113
const drake::systems::OutputPort<double>& solution_port,
100114
const drake::systems::OutputPort<double>& lcs_contact_info_port,
101115
const std::string& channel, drake::lcm::DrakeLcmInterface* lcm,
102116
const drake::systems::TriggerTypeSet& publish_triggers,
103117
double publish_period, double publish_offset) {
118+
// Add and connect the ContactForcePublisher system.
104119
auto force_publisher = builder.AddSystem<ContactForcePublisher>();
105120
builder.Connect(solution_port, force_publisher->get_input_port_c3_solution());
106121
builder.Connect(lcs_contact_info_port,
107122
force_publisher->get_input_port_lcs_contact_info());
108123

124+
// Add and connect the LCM publisher system.
109125
auto lcm_force_publisher =
110126
builder.AddSystem(LcmPublisherSystem::Make<c3::lcmt_forces>(
111127
channel, lcm, publish_triggers, publish_period, publish_offset));

systems/publishers/force_publisher.h

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,73 @@
1313
namespace c3 {
1414
namespace systems {
1515
namespace publishers {
16+
17+
/**
18+
* @class ContactForcePublisher
19+
* @brief Converts solution vectors and LCS contact information into LCM contact
20+
* force messages for publishing.
21+
* @details
22+
* - Provides input ports for the C3 solution vector and LCS contact
23+
* information.
24+
* - Computes contact forces based on the provided inputs.
25+
* - Offers an output port for the constructed contact force message.
26+
* - Includes a static helper to add an LCM publisher system to a
27+
* DiagramBuilder for message transmission.
28+
*/
1629
class ContactForcePublisher : public drake::systems::LeafSystem<double> {
1730
public:
31+
/**
32+
* @brief Constructs a ContactForcePublisher system.
33+
*
34+
* Declares input and output ports for the solution vector, LCS contact info,
35+
* and contact force output.
36+
*/
1837
ContactForcePublisher();
1938

39+
/**
40+
* @brief Returns the input port for the c3 solution vector.
41+
* @return Reference to the input port for the c3 solution.
42+
*/
2043
const drake::systems::InputPort<double>& get_input_port_c3_solution() const {
2144
return this->get_input_port(c3_solution_port_);
2245
}
2346

47+
/**
48+
* @brief Returns the input port for the LCS contact information.
49+
* @return Reference to the input port for LCS contact info.
50+
*/
2451
const drake::systems::InputPort<double>& get_input_port_lcs_contact_info()
2552
const {
2653
return this->get_input_port(lcs_contact_info_port_);
2754
}
2855

29-
// LCS contact force, not actor input forces
56+
/**
57+
* @brief Returns the output port for the computed contact forces.
58+
* @return Reference to the output port for contact forces.
59+
*/
3060
const drake::systems::OutputPort<double>& get_output_port_contact_force()
3161
const {
3262
return this->get_output_port(contact_force_output_port_);
3363
}
3464

35-
static drake::systems::lcm::LcmPublisherSystem*
36-
AddLcmPublisherToBuilder(
65+
/**
66+
* @brief Adds an LCM publisher system to the given DiagramBuilder for
67+
* publishing contact forces.
68+
*
69+
* @param builder The DiagramBuilder to which the publisher will be added.
70+
* @param solution_port Output port providing the solution vector.
71+
* @param lcs_contact_info_port Output port providing LCS contact information.
72+
* @param channel The LCM channel name to publish on.
73+
* @param lcm The LCM interface to use for publishing.
74+
* @param publish_triggers Set of triggers that determine when publishing
75+
* occurs.
76+
* @param publish_period Optional period for periodic publishing (default:
77+
* 0.0).
78+
* @param publish_offset Optional offset for periodic publishing (default:
79+
* 0.0).
80+
* @return Pointer to the created LcmPublisherSystem.
81+
*/
82+
static drake::systems::lcm::LcmPublisherSystem* AddLcmPublisherToBuilder(
3783
drake::systems::DiagramBuilder<double>& builder,
3884
const drake::systems::OutputPort<double>& solution_port,
3985
const drake::systems::OutputPort<double>& lcs_contact_info_port,
@@ -42,12 +88,21 @@ class ContactForcePublisher : public drake::systems::LeafSystem<double> {
4288
double publish_period = 0.0, double publish_offset = 0.0);
4389

4490
private:
91+
/**
92+
* @brief Calculates the contact forces and populates the output message.
93+
*
94+
* @param context The system context containing input values.
95+
* @param c3_forces_output Pointer to the output message to populate.
96+
*/
4597
void DoCalc(const drake::systems::Context<double>& context,
4698
c3::lcmt_forces* c3_forces_output) const;
4799

48-
drake::systems::InputPortIndex c3_solution_port_;
49-
drake::systems::InputPortIndex lcs_contact_info_port_;
50-
drake::systems::OutputPortIndex contact_force_output_port_;
100+
drake::systems::InputPortIndex
101+
c3_solution_port_; ///< Index of the c3 solution input port.
102+
drake::systems::InputPortIndex
103+
lcs_contact_info_port_; ///< Index of the LCS contact info input port.
104+
drake::systems::OutputPortIndex
105+
contact_force_output_port_; ///< Index of the contact force output port.
51106
};
52107

53108
} // namespace publishers

systems/publishers/output_publisher.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,58 @@ namespace c3 {
1010
namespace systems {
1111
namespace publishers {
1212

13+
// Publishes C3Output as an LCM message.
1314
C3OutputPublisher::C3OutputPublisher() {
15+
// Declare input port for the C3 solution.
1416
c3_solution_port_ =
1517
this->DeclareAbstractInputPort("c3_solution",
1618
drake::Value<C3Output::C3Solution>{})
1719
.get_index();
20+
// Declare input port for C3 intermediates.
1821
c3_intermediates_port_ =
1922
this->DeclareAbstractInputPort("c3_intermediates",
2023
drake::Value<C3Output::C3Intermediates>{})
2124
.get_index();
2225

2326
this->set_name("c3_output_publisher");
27+
// Declare output port for the LCM message.
2428
c3_output_port_ =
2529
this->DeclareAbstractOutputPort("lcmt_c3_output", c3::lcmt_output(),
2630
&C3OutputPublisher::DoCalc)
2731
.get_index();
2832
}
2933

34+
// Calculates the LCM output message from the input ports.
3035
void C3OutputPublisher::DoCalc(const drake::systems::Context<double>& context,
3136
c3::lcmt_output* output) const {
37+
// Retrieve input values.
3238
const auto& c3_solution =
3339
this->EvalInputValue<C3Output::C3Solution>(context, c3_solution_port_);
3440
const auto& c3_intermediates =
3541
this->EvalInputValue<C3Output::C3Intermediates>(context,
3642
c3_intermediates_port_);
3743

44+
// Construct C3Output and generate the LCM message.
3845
C3Output c3_output = C3Output(*c3_solution, *c3_intermediates);
3946
*output = c3_output.GenerateLcmObject(context.get_time());
4047
}
4148

49+
// Adds this publisher and an LCM publisher system to the diagram builder.
4250
LcmPublisherSystem* C3OutputPublisher::AddLcmPublisherToBuilder(
4351
DiagramBuilder<double>& builder,
4452
const drake::systems::OutputPort<double>& solution_port,
4553
const drake::systems::OutputPort<double>& intermediates_port,
4654
const std::string& channel, drake::lcm::DrakeLcmInterface* lcm,
4755
const drake::systems::TriggerTypeSet& publish_triggers,
4856
double publish_period, double publish_offset) {
57+
// Add and connect the C3OutputPublisher system.
4958
auto output_publisher = builder.AddSystem<C3OutputPublisher>();
5059
builder.Connect(solution_port,
5160
output_publisher->get_input_port_c3_solution());
5261
builder.Connect(intermediates_port,
5362
output_publisher->get_input_port_c3_intermediates());
5463

64+
// Add and connect the LCM publisher system.
5565
auto lcm_output_publisher =
5666
builder.AddSystem(LcmPublisherSystem::Make<c3::lcmt_output>(
5767
channel, lcm, publish_triggers, publish_period, publish_offset));

systems/publishers/output_publisher.h

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,56 @@
1313
namespace c3 {
1414
namespace systems {
1515
namespace publishers {
16-
/// Converts a OutputVector object to LCM type lcmt_robot_output
16+
17+
/**
18+
* @class C3OutputPublisher
19+
* @brief Converts OutputVector objects to LCM type lcmt_output for publishing.
20+
* @details
21+
* - Provides input ports for C3 solution and intermediates.
22+
* - Provides an output port for the constructed lcmt_output message.
23+
* - Can be connected to an LCM publisher system for message transmission.
24+
*/
1725
class C3OutputPublisher : public drake::systems::LeafSystem<double> {
1826
public:
27+
/**
28+
* @brief Constructor. Declares input and output ports.
29+
*/
1930
C3OutputPublisher();
2031

32+
/**
33+
* @brief Returns the input port for the C3 solution vector.
34+
*/
2135
const drake::systems::InputPort<double>& get_input_port_c3_solution() const {
2236
return this->get_input_port(c3_solution_port_);
2337
}
2438

39+
/**
40+
* @brief Returns the input port for the C3 intermediates vector.
41+
*/
2542
const drake::systems::InputPort<double>& get_input_port_c3_intermediates()
2643
const {
2744
return this->get_input_port(c3_intermediates_port_);
2845
}
2946

47+
/**
48+
* @brief Returns the output port for the lcmt_output message.
49+
*/
3050
const drake::systems::OutputPort<double>& get_output_port_c3_output() const {
3151
return this->get_output_port(c3_output_port_);
3252
}
3353

54+
/**
55+
* @brief Adds an LCM publisher system to the given DiagramBuilder.
56+
* @param builder The diagram builder to add the publisher to.
57+
* @param solution_port Output port for the solution vector.
58+
* @param intermediates_port Output port for the intermediates vector.
59+
* @param channel LCM channel name.
60+
* @param lcm LCM interface pointer.
61+
* @param publish_triggers Set of triggers for publishing.
62+
* @param publish_period Period for periodic publishing (optional).
63+
* @param publish_offset Offset for periodic publishing (optional).
64+
* @return Pointer to the created LcmPublisherSystem.
65+
*/
3466
static drake::systems::lcm::LcmPublisherSystem* AddLcmPublisherToBuilder(
3567
drake::systems::DiagramBuilder<double>& builder,
3668
const drake::systems::OutputPort<double>& solution_port,
@@ -40,16 +72,22 @@ class C3OutputPublisher : public drake::systems::LeafSystem<double> {
4072
double publish_period = 0.0, double publish_offset = 0.0);
4173

4274
private:
75+
/**
76+
* @brief Calculates the lcmt_output message from the input ports.
77+
* @param context The system context.
78+
* @param output The output message to populate.
79+
*/
4380
void DoCalc(const drake::systems::Context<double>& context,
4481
c3::lcmt_output* output) const;
4582

46-
// void OutputNextC3Input(const drake::systems::Context<double>& context,
47-
// drake::systems::BasicVector<double>* u_next) const;
48-
49-
drake::systems::InputPortIndex c3_solution_port_;
50-
drake::systems::InputPortIndex c3_intermediates_port_;
51-
drake::systems::OutputPortIndex c3_output_port_;
83+
drake::systems::InputPortIndex
84+
c3_solution_port_; /**< Index for solution input port. */
85+
drake::systems::InputPortIndex
86+
c3_intermediates_port_; /**< Index for intermediates input port. */
87+
drake::systems::OutputPortIndex
88+
c3_output_port_; /**< Index for output port. */
5289
};
90+
5391
} // namespace publishers
5492
} // namespace systems
5593
} // namespace c3

0 commit comments

Comments
 (0)