Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/humble isaac improvements #532

Merged
merged 10 commits into from
Feb 22, 2024
20 changes: 13 additions & 7 deletions smacc2/include/smacc2/client_bases/smacc_action_client_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ class SmaccActionClientBase : public ISmaccActionClient
//client_->wait_for_action_server();
}

void waitForServer()
{
RCLCPP_INFO_STREAM(
this->getLogger(),
"Waiting for action server '" << name_ << "' of type: " << demangledTypeName<ActionType>());
client_->wait_for_action_server();
}

static std::string getEventLabel()
{
auto type = TypeInfo::getTypeInfoFromType<ActionType>();
Expand Down Expand Up @@ -327,13 +335,11 @@ class SmaccActionClientBase : public ISmaccActionClient
this->lastRequest_ = lastRequest;

RCLCPP_INFO_STREAM(
getLogger(),
"["
<< getName()
<< "] Action request "
// << rclcpp_action::to_string(this->goalHandle_->get_goal_id()) <<". Goal sent to " << this->action_endpoint_
<< "\": " << std::endl
<< goal);
getLogger(), "[" << getName() << "] Action request "
// << rclcpp_action::to_string(this->goalHandle_->get_goal_id()) <<". Goal sent to " << this->action_endpoint_
// << "\": " << std::endl
// << goal
);

// if (client_->isServerConnected())
// {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class ClRosLaunch : public ISmaccClient
protected:
std::future<std::string> result_;

typedef std::function<void> cancelCallback;

static std::map<std::future<void>, cancelCallback> detached_futures_;

std::atomic<bool> cancellationToken_ = ATOMIC_VAR_INIT(false);
};
} // namespace client_bases
Expand Down
80 changes: 80 additions & 0 deletions smacc2/include/smacc2/client_bases/smacc_ros_launch_client_2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2021 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
* Authors: Pablo Inigo Blasco, Brett Aldrich
*
******************************************************************************************************************/

#pragma once

#include <smacc2/smacc_client.hpp>
#include <smacc2/smacc_state_machine.hpp>

#include <rclcpp_action/client.hpp>
#include <thread>

namespace smacc2
{
namespace client_bases
{
struct ProcessInfo
{
pid_t pid; // PID del proceso hijo
FILE * pipe; // Pipe para la salida del proceso hijo
};

ProcessInfo runProcess(const char * command);
void killGrandchildren(pid_t originalPid);
void killProcessesRecursive(pid_t pid);

class ClRosLaunch2 : public ISmaccClient
{
public:
ClRosLaunch2();

ClRosLaunch2(std::string packageName, std::string launchFilename);

virtual ~ClRosLaunch2();

void launch();

void stop();

static std::future<std::string> executeRosLaunch(
std::string packageName, std::string launchFilename, std::function<bool()> cancelCondition,
ClRosLaunch2 * client = nullptr);

// static std::string executeRosLaunch(
// std::string packageName, std::string launchFilename, std::function<bool()> cancelCondition);

std::string packageName_;

std::string launchFileName_;

pid_t launchPid_;

protected:
// std::future<std::string> result_;
std::future<std::string> result_;

typedef std::function<void> cancelCallback;

static std::map<std::future<void>, cancelCallback> detached_futures_;

std::atomic<bool> cancellationToken_ = ATOMIC_VAR_INIT(false);
};
} // namespace client_bases
} // namespace smacc2
17 changes: 10 additions & 7 deletions smacc2/include/smacc2/client_behaviors/cb_call_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,41 @@ class CbServiceCall : public smacc2::SmaccAsyncClientBehavior

void onEntry() override
{
RCLCPP_DEBUG_STREAM(
RCLCPP_INFO_STREAM(
getLogger(), "[" << this->getName() << "] creating ros service client: " << serviceName_);

client_ = getNode()->create_client<ServiceType>(serviceName_);

RCLCPP_DEBUG_STREAM(
RCLCPP_INFO_STREAM(
getLogger(), "[" << this->getName() << "] making service request to " << serviceName_);

resultFuture_ = client_->async_send_request(request_).future.share();

std::future_status status = resultFuture_.wait_for(0s);

RCLCPP_DEBUG_STREAM(
RCLCPP_INFO_STREAM(
getLogger(), "thread state: " << (int)status << " ok " << rclcpp::ok() << " shutdown "
<< this->isShutdownRequested() << "");

while (status != std::future_status::ready && rclcpp::ok() && !this->isShutdownRequested())
{
RCLCPP_DEBUG_STREAM(getLogger(), "[" << this->getName() << "] waiting response ");
RCLCPP_INFO_STREAM_THROTTLE(
getLogger(), *getNode()->get_clock(), 1000,
"[" << this->getName() << "] waiting response ");
rclcpp::sleep_for(pollRate_);
status = resultFuture_.wait_for(0s);
}

if (status == std::future_status::ready)
{
RCLCPP_DEBUG_STREAM(getLogger(), "[" << this->getName() << "] response received ");
RCLCPP_INFO_STREAM(getLogger(), "[" << this->getName() << "] response received ");
result_ = resultFuture_.get();
onServiceResponse(result_);
this->postSuccessEvent();
}
else
{
RCLCPP_DEBUG_STREAM(getLogger(), "[" << this->getName() << "] response not received ");
RCLCPP_INFO_STREAM(getLogger(), "[" << this->getName() << "] response not received ");
this->postFailureEvent();
}
}
Expand All @@ -94,7 +97,7 @@ class CbServiceCall : public smacc2::SmaccAsyncClientBehavior

virtual void onServiceResponse(std::shared_ptr<typename ServiceType::Response> /*result*/)
{
RCLCPP_DEBUG_STREAM(getLogger(), "[" << this->getName() << "] response received ");
RCLCPP_INFO_STREAM(getLogger(), "[" << this->getName() << "] response received ");
}
};

Expand Down
15 changes: 15 additions & 0 deletions smacc2/include/smacc2/client_behaviors/cb_ros_launch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ namespace smacc2
{
namespace client_behaviors
{
enum class RosLaunchMode
{
LAUNCH_DETTACHED,
LAUNCH_CLIENT_BEHAVIOR_LIFETIME
};

class CbRosLaunch : public smacc2::SmaccAsyncClientBehavior
{
private:
static std::vector<std::future<std::string>> detached_futures_;

public:
CbRosLaunch();

CbRosLaunch(std::string package, std::string launchfile, RosLaunchMode);

// CbRosLaunch(std::string packageName, std::string launchFileName);

virtual ~CbRosLaunch();
Expand All @@ -46,10 +57,14 @@ class CbRosLaunch : public smacc2::SmaccAsyncClientBehavior
std::optional<std::string> packageName_;
std::optional<std::string> launchFileName_;

RosLaunchMode launchMode_;

protected:
std::string result_;

smacc2::client_bases::ClRosLaunch * client_;

std::future<std::string> future_;
};
} // namespace client_behaviors
} // namespace smacc2
70 changes: 70 additions & 0 deletions smacc2/include/smacc2/client_behaviors/cb_ros_launch_2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2021 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
* Authors: Pablo Inigo Blasco, Brett Aldrich
*
******************************************************************************************************************/
#pragma once

#include <smacc2/client_bases/smacc_ros_launch_client_2.hpp>
#include <smacc2/smacc_asynchronous_client_behavior.hpp>

namespace smacc2
{
namespace client_behaviors
{
enum class RosLaunchMode
{
LAUNCH_DETTACHED,
LAUNCH_CLIENT_BEHAVIOR_LIFETIME
};

class CbRosLaunch2 : public smacc2::SmaccAsyncClientBehavior
{
private:
static std::vector<std::future<std::string>> detached_futures_;

public:
CbRosLaunch2();

CbRosLaunch2(std::string package, std::string launchfile, RosLaunchMode);

// CbRosLaunch2(std::string packageName, std::string launchFileName);

virtual ~CbRosLaunch2();

template <typename TOrthogonal, typename TSourceObject>
void onOrthogonalAllocation()
{
smacc2::SmaccAsyncClientBehavior::onOrthogonalAllocation<TOrthogonal, TSourceObject>();
}

void onEntry() override;

std::optional<std::string> packageName_;
std::optional<std::string> launchFileName_;

RosLaunchMode launchMode_;

protected:
std::future<std::string> result_;

smacc2::client_bases::ClRosLaunch2 * client_;

std::future<std::string> future_;
};
} // namespace client_behaviors
} // namespace smacc2
62 changes: 62 additions & 0 deletions smacc2/include/smacc2/client_behaviors/cb_ros_stop_2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2021 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
* Authors: Pablo Inigo Blasco, Brett Aldrich
*
******************************************************************************************************************/
#pragma once

#include <smacc2/client_bases/smacc_ros_launch_client_2.hpp>
#include <smacc2/smacc_asynchronous_client_behavior.hpp>

namespace smacc2
{
namespace client_behaviors
{
class CbRosStop2 : public smacc2::SmaccAsyncClientBehavior
{
private:
static std::vector<std::future<std::string>> detached_futures_;

public:
CbRosStop2();

CbRosStop2(pid_t launchPid);

// CbRosStop2(std::string packageName, std::string launchFileName);

virtual ~CbRosStop2();

template <typename TOrthogonal, typename TSourceObject>
void onOrthogonalAllocation()
{
smacc2::SmaccAsyncClientBehavior::onOrthogonalAllocation<TOrthogonal, TSourceObject>();
}

void onEntry() override;

std::optional<std::string> packageName_;
std::optional<std::string> launchFileName_;

protected:
std::future<std::string> result_;

smacc2::client_bases::ClRosLaunch2 * client_;

std::future<std::string> future_;
};
} // namespace client_behaviors
} // namespace smacc2
8 changes: 7 additions & 1 deletion smacc2/include/smacc2/client_behaviors/cb_sleep_for.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ class CbSleepFor : public smacc2::SmaccAsyncClientBehavior

void onEntry() override
{
rclcpp::sleep_for(std::chrono::nanoseconds(sleeptime_.nanoseconds()));
auto starttime = getNode()->now();
while (!this->isShutdownRequested() && (getNode()->now() - starttime) < sleeptime_)
{
rclcpp::sleep_for(10ms);
}

//rclcpp::sleep_for(std::chrono::nanoseconds(sleeptime_.nanoseconds()));
this->postSuccessEvent();
}

Expand Down
Loading
Loading