SMACC2
Loading...
Searching...
No Matches
cl_keyboard::CbKeyboardTwistTeleop Class Reference

#include <cb_keyboard_twist_teleop.hpp>

Inheritance diagram for cl_keyboard::CbKeyboardTwistTeleop:
Inheritance graph
Collaboration diagram for cl_keyboard::CbKeyboardTwistTeleop:
Collaboration graph

Public Member Functions

 CbKeyboardTwistTeleop (float linearStep=0.15f, float angularStep=0.4f, geometry_msgs::msg::Twist idleTwist=geometry_msgs::msg::Twist())
 
void onEntry () override
 
void onExit () override
 
void update () override
 
- Public Member Functions inherited from smacc2::SmaccClientBehavior
virtual ~SmaccClientBehavior ()
 
void onEntry () override
 
void onExit () override
 
- 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)
 
template<typename SmaccComponentType >
void requiresComponent (std::string name, SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
virtual void executeOnEntry ()
 
virtual void executeOnExit ()
 
- Public Member Functions inherited from smacc2::ISmaccUpdatable
 ISmaccUpdatable ()
 
 ISmaccUpdatable (rclcpp::Duration duration)
 
void executeUpdate (rclcpp::Node::SharedPtr node)
 
void setUpdatePeriod (rclcpp::Duration duration)
 

Private Member Functions

void onArrowPress (ArrowKey arrow)
 

Private Attributes

components::CpKeyboardListener1keyboardListener_ = nullptr
 
smacc2::client_core_components::CpTopicPublisher< geometry_msgs::msg::Twist > * twistPublisher_
 
float linearStep_
 
float angularStep_
 
geometry_msgs::msg::Twist idleTwist_
 
std::mutex mutex_
 
geometry_msgs::msg::Twist heldTwist_
 
std::optional< std::chrono::steady_clock::time_point > lastArrowTime_
 
std::chrono::milliseconds deadman_ {350}
 

Additional Inherited Members

- 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 void dispose ()
 
virtual rclcpp::Node::SharedPtr getNode () const
 
virtual rclcpp::Logger getLogger () const
 
- Protected Member Functions inherited from smacc2::ISmaccUpdatable

Detailed Description

Definition at line 39 of file cb_keyboard_twist_teleop.hpp.

Constructor & Destructor Documentation

◆ CbKeyboardTwistTeleop()

cl_keyboard::CbKeyboardTwistTeleop::CbKeyboardTwistTeleop ( float linearStep = 0.15f,
float angularStep = 0.4f,
geometry_msgs::msg::Twist idleTwist = geometry_msgs::msg::Twist() )
inline

Member Function Documentation

◆ onArrowPress()

void cl_keyboard::CbKeyboardTwistTeleop::onArrowPress ( ArrowKey arrow)
inlineprivate

Definition at line 106 of file cb_keyboard_twist_teleop.hpp.

107 {
108 std::lock_guard<std::mutex> lock(mutex_);
109 heldTwist_ = geometry_msgs::msg::Twist();
110 switch (arrow)
111 {
112 case ArrowKey::Up:
113 heldTwist_.linear.x = linearStep_;
114 break;
115 case ArrowKey::Down:
116 heldTwist_.linear.x = -linearStep_;
117 break;
118 case ArrowKey::Left:
119 heldTwist_.angular.z = angularStep_;
120 break;
121 case ArrowKey::Right:
122 heldTwist_.angular.z = -angularStep_;
123 break;
124 }
125 lastArrowTime_ = std::chrono::steady_clock::now();
126 }
std::optional< std::chrono::steady_clock::time_point > lastArrowTime_

References angularStep_, cl_keyboard::Down, heldTwist_, lastArrowTime_, cl_keyboard::Left, linearStep_, mutex_, cl_keyboard::Right, and cl_keyboard::Up.

Referenced by onEntry().

Here is the caller graph for this function:

◆ onEntry()

void cl_keyboard::CbKeyboardTwistTeleop::onEntry ( )
inlineoverridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 49 of file cb_keyboard_twist_teleop.hpp.

50 {
51 // sync behavior: onEntry runs on the state machine thread, so component
52 // resolution and signal wiring are safe here
55
56 if (keyboardListener_ != nullptr)
57 {
59 }
60 else
61 {
62 RCLCPP_ERROR(getLogger(), "[%s] CpKeyboardListener1 not found", getName().c_str());
63 }
64
65 if (twistPublisher_ == nullptr)
66 {
67 RCLCPP_ERROR(
68 getLogger(), "[%s] CpTopicPublisher<Twist> not found - create it in the orthogonal",
69 getName().c_str());
70 }
71 }
smacc2::client_core_components::CpTopicPublisher< geometry_msgs::msg::Twist > * twistPublisher_
components::CpKeyboardListener1 * keyboardListener_
void OnArrowPress(void(T::*callback)(ArrowKey arrow), T *object)
virtual rclcpp::Logger getLogger() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)

References smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), keyboardListener_, cl_keyboard::components::CpKeyboardListener1::OnArrowPress(), onArrowPress(), smacc2::ISmaccClientBehavior::requiresComponent(), and twistPublisher_.

Here is the call graph for this function:

◆ onExit()

void cl_keyboard::CbKeyboardTwistTeleop::onExit ( )
inlineoverridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 73 of file cb_keyboard_twist_teleop.hpp.

74 {
75 // stop pushing: leave the topic at zero for whatever runs next
76 if (twistPublisher_ != nullptr)
77 {
78 twistPublisher_->publish(geometry_msgs::msg::Twist());
79 }
80 }

References smacc2::client_core_components::CpTopicPublisher< MessageType >::publish(), and twistPublisher_.

Here is the call graph for this function:

◆ update()

void cl_keyboard::CbKeyboardTwistTeleop::update ( )
inlineoverridevirtual

Implements smacc2::ISmaccUpdatable.

Definition at line 82 of file cb_keyboard_twist_teleop.hpp.

83 {
84 if (twistPublisher_ == nullptr)
85 {
86 return;
87 }
88
89 geometry_msgs::msg::Twist out;
90 bool held;
91 {
92 std::lock_guard<std::mutex> lock(mutex_);
93 auto now = std::chrono::steady_clock::now();
94 held = lastArrowTime_ && (now - *lastArrowTime_) < deadman_;
95 out = held ? heldTwist_ : idleTwist_;
96 }
98
99 RCLCPP_INFO_THROTTLE(
100 getLogger(), *getNode()->get_clock(), 2000,
101 "[%s] publishing %s twist: linear=%.2f angular=%.2f", getName().c_str(),
102 held ? "held" : "idle", out.linear.x, out.angular.z);
103 }
virtual rclcpp::Node::SharedPtr getNode() const

References deadman_, smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::getNode(), heldTwist_, idleTwist_, lastArrowTime_, mutex_, smacc2::client_core_components::CpTopicPublisher< MessageType >::publish(), and twistPublisher_.

Here is the call graph for this function:

Member Data Documentation

◆ angularStep_

float cl_keyboard::CbKeyboardTwistTeleop::angularStep_
private

Definition at line 133 of file cb_keyboard_twist_teleop.hpp.

Referenced by onArrowPress().

◆ deadman_

std::chrono::milliseconds cl_keyboard::CbKeyboardTwistTeleop::deadman_ {350}
private

Definition at line 145 of file cb_keyboard_twist_teleop.hpp.

145{350};

Referenced by update().

◆ heldTwist_

geometry_msgs::msg::Twist cl_keyboard::CbKeyboardTwistTeleop::heldTwist_
private

Definition at line 137 of file cb_keyboard_twist_teleop.hpp.

Referenced by onArrowPress(), and update().

◆ idleTwist_

geometry_msgs::msg::Twist cl_keyboard::CbKeyboardTwistTeleop::idleTwist_
private

Definition at line 134 of file cb_keyboard_twist_teleop.hpp.

Referenced by update().

◆ keyboardListener_

components::CpKeyboardListener1* cl_keyboard::CbKeyboardTwistTeleop::keyboardListener_ = nullptr
private

Definition at line 128 of file cb_keyboard_twist_teleop.hpp.

Referenced by onEntry().

◆ lastArrowTime_

std::optional<std::chrono::steady_clock::time_point> cl_keyboard::CbKeyboardTwistTeleop::lastArrowTime_
private

Definition at line 138 of file cb_keyboard_twist_teleop.hpp.

Referenced by onArrowPress(), and update().

◆ linearStep_

float cl_keyboard::CbKeyboardTwistTeleop::linearStep_
private

Definition at line 132 of file cb_keyboard_twist_teleop.hpp.

Referenced by onArrowPress().

◆ mutex_

std::mutex cl_keyboard::CbKeyboardTwistTeleop::mutex_
private

Definition at line 136 of file cb_keyboard_twist_teleop.hpp.

Referenced by onArrowPress(), and update().

◆ twistPublisher_

smacc2::client_core_components::CpTopicPublisher<geometry_msgs::msg::Twist>* cl_keyboard::CbKeyboardTwistTeleop::twistPublisher_
private
Initial value:
=
nullptr

Definition at line 129 of file cb_keyboard_twist_teleop.hpp.

Referenced by onEntry(), onExit(), and update().


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