SMACC2
Loading...
Searching...
No Matches
cl_moveit2z::CpTrajectoryExecutor Class Reference

Component for centralized trajectory execution. More...

#include <cp_trajectory_executor.hpp>

Inheritance diagram for cl_moveit2z::CpTrajectoryExecutor:
Inheritance graph
Collaboration diagram for cl_moveit2z::CpTrajectoryExecutor:
Collaboration graph

Public Member Functions

 CpTrajectoryExecutor ()=default
 
virtual ~CpTrajectoryExecutor ()=default
 
void onInitialize () override
 Initialize the component and get references to required components/clients.
 
ExecutionResult execute (const moveit_msgs::msg::RobotTrajectory &trajectory, const ExecutionOptions &options={})
 Execute a trajectory synchronously.
 
ExecutionResult executePlan (const moveit::planning_interface::MoveGroupInterface::Plan &plan, const ExecutionOptions &options={})
 Execute a motion plan synchronously.
 
void cancel ()
 Cancel ongoing execution.
 
CpTrajectoryHistorygetTrajectoryHistory ()
 Get the trajectory history component.
 
- Public Member Functions inherited from smacc2::ISmaccComponent
 ISmaccComponent ()
 
virtual ~ISmaccComponent ()
 
virtual std::string getName () const
 

Private Member Functions

void applyExecutionOptions (moveit::planning_interface::MoveGroupInterface &moveGroup, const ExecutionOptions &options)
 Apply execution options to MoveGroupInterface.
 
void recordToHistory (const moveit_msgs::msg::RobotTrajectory &trajectory, const moveit_msgs::msg::MoveItErrorCodes &errorCode, const std::string &name)
 Record trajectory to history component.
 

Private Attributes

ClMoveit2zmoveit2zClient_ = nullptr
 
CpTrajectoryHistorytrajectoryHistory_ = nullptr
 

Additional Inherited Members

- Protected Member Functions inherited from smacc2::ISmaccComponent
template<typename TOrthogonal , typename TClient >
void onComponentInitialization ()
 
template<typename EventType >
void postEvent (const EventType &ev)
 
template<typename EventType >
void postEvent ()
 
template<typename TOrthogonal , typename TSourceObject >
void onOrthogonalAllocation ()
 
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
template<typename TComponent >
void requiresComponent (TComponent *&requiredComponentStorage, bool throwExceptionIfNotExist)
 
template<typename TComponent >
void requiresComponent (std::string name, TComponent *&requiredComponentStorage, bool throwExceptionIfNotExist)
 
template<typename TComponent >
void requiresComponent (TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename TComponent >
void requiresComponent (std::string name, TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename TClient >
void requiresClient (TClient *&requiredClientStorage)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createSiblingComponent (TArgs... targs)
 
template<typename SmaccComponentType , typename TOrthogonal , typename TClient , typename... TArgs>
SmaccComponentType * createSiblingNamedComponent (std::string name, TArgs... targs)
 
rclcpp::Node::SharedPtr getNode ()
 
rclcpp::Logger getLogger () const
 
ISmaccStateMachinegetStateMachine ()
 
- Protected Attributes inherited from smacc2::ISmaccComponent
ISmaccStateMachinestateMachine_
 
ISmaccClientowner_
 

Detailed Description

Component for centralized trajectory execution.

This component provides a unified interface for executing trajectories with:

  • Automatic recording to CpTrajectoryHistory
  • Consistent error handling and event posting
  • Execution options (velocity/acceleration scaling)
  • Execution time tracking

Pattern: Executor with signals (like action clients)

Definition at line 76 of file cp_trajectory_executor.hpp.

Constructor & Destructor Documentation

◆ CpTrajectoryExecutor()

cl_moveit2z::CpTrajectoryExecutor::CpTrajectoryExecutor ( )
default

◆ ~CpTrajectoryExecutor()

virtual cl_moveit2z::CpTrajectoryExecutor::~CpTrajectoryExecutor ( )
virtualdefault

Member Function Documentation

◆ applyExecutionOptions()

void cl_moveit2z::CpTrajectoryExecutor::applyExecutionOptions ( moveit::planning_interface::MoveGroupInterface & moveGroup,
const ExecutionOptions & options )
inlineprivate

Apply execution options to MoveGroupInterface.

Parameters
moveGroupReference to MoveGroupInterface
optionsExecution options to apply

Definition at line 238 of file cp_trajectory_executor.hpp.

240 {
241 if (options.maxVelocityScaling)
242 {
243 moveGroup.setMaxVelocityScalingFactor(*options.maxVelocityScaling);
244 }
245
246 if (options.maxAccelerationScaling)
247 {
248 moveGroup.setMaxAccelerationScalingFactor(*options.maxAccelerationScaling);
249 }
250 }

References cl_moveit2z::ExecutionOptions::maxAccelerationScaling, and cl_moveit2z::ExecutionOptions::maxVelocityScaling.

◆ cancel()

void cl_moveit2z::CpTrajectoryExecutor::cancel ( )
inline

Cancel ongoing execution.

Note: This is a best-effort cancellation and may not immediately stop the robot

Definition at line 199 of file cp_trajectory_executor.hpp.

200 {
201 if (!moveit2zClient_)
202 {
203 RCLCPP_ERROR(getLogger(), "[CpTrajectoryExecutor] Cannot cancel: client not available");
204 return;
205 }
206
207 auto & moveGroup = moveit2zClient_->moveGroupClientInterface;
208
209 try
210 {
211 moveGroup->stop();
212 RCLCPP_INFO(getLogger(), "[CpTrajectoryExecutor] Trajectory execution cancelled");
213 }
214 catch (const std::exception & e)
215 {
216 RCLCPP_ERROR(
217 getLogger(), "[CpTrajectoryExecutor] Exception during cancellation: %s", e.what());
218 }
219 }
std::shared_ptr< moveit::planning_interface::MoveGroupInterface > moveGroupClientInterface
rclcpp::Logger getLogger() const

References smacc2::ISmaccComponent::getLogger(), cl_moveit2z::ClMoveit2z::moveGroupClientInterface, and moveit2zClient_.

Here is the call graph for this function:

◆ execute()

ExecutionResult cl_moveit2z::CpTrajectoryExecutor::execute ( const moveit_msgs::msg::RobotTrajectory & trajectory,
const ExecutionOptions & options = {} )
inline

Execute a trajectory synchronously.

Parameters
trajectoryThe robot trajectory to execute
optionsExecution configuration options
Returns
ExecutionResult with success status and error information

Definition at line 114 of file cp_trajectory_executor.hpp.

115 {})
116 {
117 ExecutionResult result;
118
119 if (!moveit2zClient_)
120 {
121 result.errorMessage = "ClMoveit2z client not available";
122 RCLCPP_ERROR(getLogger(), "[CpTrajectoryExecutor] %s", result.errorMessage.c_str());
123 return result;
124 }
125
126 auto & moveGroup = moveit2zClient_->moveGroupClientInterface;
127
128 try
129 {
130 // Apply execution options
131 applyExecutionOptions(*moveGroup, options);
132
133 RCLCPP_INFO(
134 getLogger(), "[CpTrajectoryExecutor] Executing trajectory with %lu points...",
135 trajectory.joint_trajectory.points.size());
136
137 // Track execution time
138 auto startTime = std::chrono::steady_clock::now();
139
140 // Execute trajectory
141 result.errorCode = moveGroup->execute(trajectory);
142
143 auto endTime = std::chrono::steady_clock::now();
144 result.executionTime = std::chrono::duration<double>(endTime - startTime);
145
146 result.success = (result.errorCode.val == moveit_msgs::msg::MoveItErrorCodes::SUCCESS);
147
148 if (result.success)
149 {
150 RCLCPP_INFO(
151 getLogger(), "[CpTrajectoryExecutor] Trajectory execution succeeded (%.2f seconds)",
152 result.executionTime.count());
153 }
154 else
155 {
156 result.errorMessage =
157 "Trajectory execution failed with error code: " + std::to_string(result.errorCode.val);
158 RCLCPP_WARN(getLogger(), "[CpTrajectoryExecutor] %s", result.errorMessage.c_str());
159 }
160
161 // Record to history if enabled
162 if (options.recordToHistory && trajectoryHistory_)
163 {
164 std::string trajName = options.trajectoryName.value_or("unnamed_trajectory");
165 recordToHistory(trajectory, result.errorCode, trajName);
166 }
167 }
168 catch (const std::exception & e)
169 {
170 result.success = false;
171 result.errorMessage = std::string("Exception during trajectory execution: ") + e.what();
172 RCLCPP_ERROR(getLogger(), "[CpTrajectoryExecutor] %s", result.errorMessage.c_str());
173 }
174
175 return result;
176 }
void recordToHistory(const moveit_msgs::msg::RobotTrajectory &trajectory, const moveit_msgs::msg::MoveItErrorCodes &errorCode, const std::string &name)
Record trajectory to history component.
void applyExecutionOptions(moveit::planning_interface::MoveGroupInterface &moveGroup, const ExecutionOptions &options)
Apply execution options to MoveGroupInterface.

Referenced by cl_moveit2z::CbMoveEndEffectorTrajectory::executeJointSpaceTrajectory().

Here is the caller graph for this function:

◆ executePlan()

ExecutionResult cl_moveit2z::CpTrajectoryExecutor::executePlan ( const moveit::planning_interface::MoveGroupInterface::Plan & plan,
const ExecutionOptions & options = {} )
inline

Execute a motion plan synchronously.

Convenience method that accepts a MoveGroupInterface::Plan directly

Parameters
planThe motion plan containing the trajectory
optionsExecution configuration options
Returns
ExecutionResult with success status and error information

Definition at line 187 of file cp_trajectory_executor.hpp.

189 {})
190 {
191 return execute(plan.trajectory_, options);
192 }
ExecutionResult execute(const moveit_msgs::msg::RobotTrajectory &trajectory, const ExecutionOptions &options={})
Execute a trajectory synchronously.

Referenced by cl_moveit2z::CbMoveJoints::moveJoints(), and cl_moveit2z::CbMoveEndEffector::moveToAbsolutePose().

Here is the caller graph for this function:

◆ getTrajectoryHistory()

CpTrajectoryHistory * cl_moveit2z::CpTrajectoryExecutor::getTrajectoryHistory ( )
inline

Get the trajectory history component.

Returns
Pointer to CpTrajectoryHistory, or nullptr if not available

Definition at line 226 of file cp_trajectory_executor.hpp.

226{ return trajectoryHistory_; }

References trajectoryHistory_.

◆ onInitialize()

void cl_moveit2z::CpTrajectoryExecutor::onInitialize ( )
inlineoverridevirtual

Initialize the component and get references to required components/clients.

Reimplemented from smacc2::ISmaccComponent.

Definition at line 85 of file cp_trajectory_executor.hpp.

86 {
88
89 // CpTrajectoryHistory is optional but recommended
91
93 {
94 RCLCPP_INFO(
95 getLogger(),
96 "[CpTrajectoryExecutor] Component initialized with trajectory history recording enabled");
97 }
98 else
99 {
100 RCLCPP_WARN(
101 getLogger(),
102 "[CpTrajectoryExecutor] Component initialized without CpTrajectoryHistory "
103 "(trajectory history will not be recorded)");
104 }
105 }
void requiresClient(TClient *&requiredClientStorage)
void requiresComponent(TComponent *&requiredComponentStorage, bool throwExceptionIfNotExist)

References smacc2::ISmaccComponent::getLogger(), moveit2zClient_, smacc2::ISmaccComponent::requiresClient(), smacc2::ISmaccComponent::requiresComponent(), and trajectoryHistory_.

Here is the call graph for this function:

◆ recordToHistory()

void cl_moveit2z::CpTrajectoryExecutor::recordToHistory ( const moveit_msgs::msg::RobotTrajectory & trajectory,
const moveit_msgs::msg::MoveItErrorCodes & errorCode,
const std::string & name )
inlineprivate

Record trajectory to history component.

Parameters
trajectoryThe executed trajectory
errorCodeExecution result error code
nameName for this trajectory entry

Definition at line 259 of file cp_trajectory_executor.hpp.

262 {
264 {
265 return;
266 }
267
268 try
269 {
270 trajectoryHistory_->pushTrajectory(name, trajectory, errorCode);
271 RCLCPP_DEBUG(
272 getLogger(), "[CpTrajectoryExecutor] Recorded trajectory '%s' to history", name.c_str());
273 }
274 catch (const std::exception & e)
275 {
276 RCLCPP_WARN(
277 getLogger(), "[CpTrajectoryExecutor] Failed to record trajectory to history: %s", e.what());
278 }
279 }
void pushTrajectory(std::string name, const moveit_msgs::msg::RobotTrajectory &trajectory, moveit_msgs::msg::MoveItErrorCodes result)

References smacc2::ISmaccComponent::getLogger(), cl_moveit2z::CpTrajectoryHistory::pushTrajectory(), and trajectoryHistory_.

Here is the call graph for this function:

Member Data Documentation

◆ moveit2zClient_

ClMoveit2z* cl_moveit2z::CpTrajectoryExecutor::moveit2zClient_ = nullptr
private

Definition at line 229 of file cp_trajectory_executor.hpp.

Referenced by cancel(), and onInitialize().

◆ trajectoryHistory_

CpTrajectoryHistory* cl_moveit2z::CpTrajectoryExecutor::trajectoryHistory_ = nullptr
private

Definition at line 230 of file cp_trajectory_executor.hpp.

Referenced by getTrajectoryHistory(), onInitialize(), and recordToHistory().


The documentation for this class was generated from the following file: