SMACC2
Loading...
Searching...
No Matches
cl_nav2z::CbSpiralMotion Struct Reference

#include <cb_spiral_motion.hpp>

Inheritance diagram for cl_nav2z::CbSpiralMotion:
Inheritance graph
Collaboration diagram for cl_nav2z::CbSpiralMotion:
Collaboration graph

Public Member Functions

 CbSpiralMotion (std::optional< CbSpiralMotionOptions > options=std::nullopt)
 
void onEntry () override
 
void onExit () override
 
- Public Member Functions inherited from smacc2::SmaccAsyncClientBehavior
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
virtual ~SmaccAsyncClientBehavior ()
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onSuccess (TCallback callback, T *object)
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onFinished (TCallback callback, T *object)
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onFailure (TCallback callback, T *object)
 
void requestForceFinish ()
 
void executeOnEntry () override
 
void executeOnExit () override
 
void waitOnEntryThread (bool requestFinish)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onSuccess (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onFinished (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onFailure (TCallbackMethod callback, T *object)
 
- Public Member Functions inherited from smacc2::ISmaccClientBehavior
 ISmaccClientBehavior ()
 
virtual ~ISmaccClientBehavior ()
 
ISmaccStateMachinegetStateMachine ()
 
std::string getName () const
 
template<typename SmaccClientType >
void requiresClient (SmaccClientType *&storage)
 
template<typename SmaccComponentType >
void requiresComponent (SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 

Private Attributes

rclcpp::Publisher< geometry_msgs::msg::Twist >::SharedPtr cmdVelPub_
 
CbSpiralMotionOptions options_
 

Additional Inherited Members

- Protected Member Functions inherited from smacc2::SmaccAsyncClientBehavior
void postSuccessEvent ()
 
void postFailureEvent ()
 
virtual void dispose () override
 
bool isShutdownRequested ()
 onEntry is executed in a new thread. However the current state cannot be left until the onEntry thread finishes. This flag can be checked from the onEntry thread to force finishing the thread.
 
- Protected Member Functions inherited from smacc2::ISmaccClientBehavior
virtual void runtimeConfigure ()
 
template<typename EventType >
void postEvent (const EventType &ev)
 
template<typename EventType >
void postEvent ()
 
ISmaccStategetCurrentState ()
 
virtual rclcpp::Node::SharedPtr getNode () const
 
virtual rclcpp::Logger getLogger () const
 

Detailed Description

Definition at line 43 of file cb_spiral_motion.hpp.

Constructor & Destructor Documentation

◆ CbSpiralMotion()

cl_nav2z::CbSpiralMotion::CbSpiralMotion ( std::optional< CbSpiralMotionOptions > options = std::nullopt)

Definition at line 30 of file cb_spiral_motion.cpp.

31{
32 if (options)
33 {
34 options_ = *options;
35 }
36 else
37 {
38 // we use default options
39 options_ = CbSpiralMotionOptions();
40 }
41}
CbSpiralMotionOptions options_

References options_.

Member Function Documentation

◆ onEntry()

void cl_nav2z::CbSpiralMotion::onEntry ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 43 of file cb_spiral_motion.cpp.

44{
45 auto linearVelocity = *(options_.linearVelocity);
46 auto maxLinearVelocity = *(options_.maxLinearVelocity);
47 auto initialAngularVelocity = *(options_.initialAngularVelocity);
48 auto spiralMotionDuration = *(options_.spiralMotionDuration);
49 auto finalRadius = *(options_.finalRadius);
50
51 float rate = 20.0f;
52 rclcpp::Rate r(rate);
53 cmdVelPub_ = getNode()->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", rclcpp::QoS(1));
54
55 rclcpp::Duration linearRamp = rclcpp::Duration::from_seconds(spiralMotionDuration.seconds());
56 float linearAceleration = (maxLinearVelocity - linearVelocity) / linearRamp.seconds();
57 float dt = 1.0f / rate;
58
59 // we know final radious and the constant linear velocity
60 float finalAngularVelocity = maxLinearVelocity / finalRadius;
61
62 float angularAcceleration =
63 (initialAngularVelocity - finalAngularVelocity) / spiralMotionDuration.seconds();
64
65 geometry_msgs::msg::Twist cmd_vel;
66
67 cmd_vel.linear.x = linearVelocity;
68 cmd_vel.angular.z = initialAngularVelocity;
69 auto start_time = getNode()->now();
70
71 RCLCPP_INFO_STREAM(
72 getLogger(), "[CbSpiralMotion]: initialAngularVelocity: "
73 << initialAngularVelocity << ", finalAngularVelocity: " << finalAngularVelocity
74 << ", angularAcceleration: " << angularAcceleration);
75 RCLCPP_INFO_STREAM(
76 getLogger(), "[CbSpiralMotion]: linearAceleration: "
77 << linearAceleration << ", maxLinearVelocity: " << maxLinearVelocity);
78
79 bool end_condition = false;
80
81 while (!end_condition)
82 {
83 auto current_time = getNode()->now() - start_time;
84
85 cmd_vel.linear.x += static_cast<double>(linearAceleration) * dt;
86 if (cmd_vel.linear.x > maxLinearVelocity)
87 {
88 cmd_vel.linear.x = maxLinearVelocity;
89 }
90
91 float elapsedTimeFactor = current_time.seconds() / spiralMotionDuration.seconds();
92 cmd_vel.angular.z = initialAngularVelocity * (1.0f - elapsedTimeFactor) +
93 finalAngularVelocity * elapsedTimeFactor;
94
95 RCLCPP_INFO(
96 getLogger(), "[CbSpiralMotion] cmd_vel.linear.x = %f, cmd_vel.angular.z = %f",
97 cmd_vel.linear.x, cmd_vel.angular.z);
98
99 cmdVelPub_->publish(cmd_vel);
100 r.sleep();
101
102 auto now = getNode()->now();
103
104 rclcpp::Duration elapsed = now - start_time;
105 RCLCPP_INFO_STREAM(
106 getLogger(), "[CbSpiralMotion] elapsed time: " << elapsed.seconds() << ", total duration: "
107 << spiralMotionDuration.seconds());
108 if (elapsed > spiralMotionDuration)
109 {
110 RCLCPP_INFO_STREAM(getLogger(), "[CbSpiralMotion] spiralMotionDuration reached");
111 end_condition = true;
112 }
113 }
114
115 // asynchronous client behaviors usually post a success event when they are done
116 // that is used in states to transition to the next state
117 this->postSuccessEvent();
118}
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
std::optional< float > initialAngularVelocity
std::optional< float > linearVelocity
std::optional< rclcpp::Duration > spiralMotionDuration
std::optional< float > maxLinearVelocity
std::optional< float > finalRadius
rclcpp::Publisher< geometry_msgs::msg::Twist >::SharedPtr cmdVelPub_

References cmdVelPub_, cl_nav2z::CbSpiralMotionOptions::finalRadius, smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getNode(), cl_nav2z::CbSpiralMotionOptions::initialAngularVelocity, cl_nav2z::CbSpiralMotionOptions::linearVelocity, cl_nav2z::CbSpiralMotionOptions::maxLinearVelocity, options_, smacc2::SmaccAsyncClientBehavior::postSuccessEvent(), and cl_nav2z::CbSpiralMotionOptions::spiralMotionDuration.

Here is the call graph for this function:

◆ onExit()

void cl_nav2z::CbSpiralMotion::onExit ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 120 of file cb_spiral_motion.cpp.

120{}

Member Data Documentation

◆ cmdVelPub_

rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr cl_nav2z::CbSpiralMotion::cmdVelPub_
private

Definition at line 56 of file cb_spiral_motion.hpp.

Referenced by onEntry().

◆ options_

CbSpiralMotionOptions cl_nav2z::CbSpiralMotion::options_
private

Definition at line 59 of file cb_spiral_motion.hpp.

Referenced by CbSpiralMotion(), and onEntry().


The documentation for this struct was generated from the following files: