Skip to content

Commit d346e02

Browse files
mpowelsonLevi-Armstrong
authored andcommitted
Add ProfileSwitchProcessGenerator
This generator simply returns a value specified in the composite profile. This can be used to switch execution based on the profile
1 parent 4e8e445 commit d346e02

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
lines changed

tesseract/tesseract_planning/tesseract_motion_planners/include/tesseract_motion_planners/planner_utils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
3131
#include <Eigen/Geometry>
3232
#include <console_bridge/console.h>
3333
TESSERACT_COMMON_IGNORE_WARNINGS_POP
34+
35+
#include <tesseract_command_language/constants.h>
36+
#include <tesseract_kinematics/core/forward_kinematics.h>
3437
#include <tesseract_motion_planners/robot_config.h>
3538
#include <tesseract_motion_planners/core/types.h>
3639

@@ -115,7 +118,7 @@ bool isValidState(const tesseract_kinematics::ForwardKinematics::ConstPtr& robot
115118
inline std::string getProfileString(const std::string& profile,
116119
const std::string& planner_name,
117120
const PlannerProfileRemapping& profile_remapping,
118-
std::string default_profile = "DEFAULT")
121+
std::string default_profile = DEFAULT_PROFILE_KEY)
119122
{
120123
std::string results = profile;
121124
if (profile.empty())

tesseract/tesseract_planning/tesseract_process_managers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ add_library(${PROJECT_NAME}
4646
src/process_generators/fix_state_collision_process_generator.cpp
4747
src/process_generators/iterative_spline_parameterization_process_generator.cpp
4848
src/process_generators/motion_planner_process_generator.cpp
49+
src/process_generators/profile_switch_process_generator.cpp
4950
src/process_managers/raster_process_manager.cpp
5051
src/process_managers/raster_dt_process_manager.cpp
5152
src/process_managers/raster_waad_process_manager.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @file profile_switch_process_generator.h
3+
* @brief Process generator that returns a value based on the profile
4+
*
5+
* @author Matthew Powelson
6+
* @date October 26. 2020
7+
* @version TODO
8+
* @bug No known bugs
9+
*
10+
* @par License
11+
* Software License Agreement (Apache License)
12+
* @par
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
* @par
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
#ifndef TESSERACT_PROCESS_MANAGERS_PROFILE_SWITCH_PROCESS_GENERATOR_H
25+
#define TESSERACT_PROCESS_MANAGERS_PROFILE_SWITCH_PROCESS_GENERATOR_H
26+
27+
#include <tesseract_common/macros.h>
28+
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
29+
#include <atomic>
30+
TESSERACT_COMMON_IGNORE_WARNINGS_POP
31+
32+
#include <tesseract_process_managers/process_generator.h>
33+
34+
namespace tesseract_planning
35+
{
36+
struct ProfileSwitchProfile
37+
{
38+
ProfileSwitchProfile(const int& return_value = 1);
39+
40+
using Ptr = std::shared_ptr<ProfileSwitchProfile>;
41+
using ConstPtr = std::shared_ptr<const ProfileSwitchProfile>;
42+
43+
int return_value;
44+
};
45+
using ProfileSwitchProfileMap = std::unordered_map<std::string, ProfileSwitchProfile::Ptr>;
46+
47+
/**
48+
* @brief This generator simply returns a value specified in the composite profile. This can be used to switch execution
49+
* based on the profile
50+
*/
51+
class ProfileSwitchProcessGenerator : public ProcessGenerator
52+
{
53+
public:
54+
using UPtr = std::unique_ptr<ProfileSwitchProcessGenerator>;
55+
56+
ProfileSwitchProcessGenerator(std::string name = "Profile Switch");
57+
58+
~ProfileSwitchProcessGenerator() override = default;
59+
ProfileSwitchProcessGenerator(const ProfileSwitchProcessGenerator&) = delete;
60+
ProfileSwitchProcessGenerator& operator=(const ProfileSwitchProcessGenerator&) = delete;
61+
ProfileSwitchProcessGenerator(ProfileSwitchProcessGenerator&&) = delete;
62+
ProfileSwitchProcessGenerator& operator=(ProfileSwitchProcessGenerator&&) = delete;
63+
64+
const std::string& getName() const override;
65+
66+
std::function<void()> generateTask(ProcessInput input) override;
67+
68+
std::function<int()> generateConditionalTask(ProcessInput input) override;
69+
70+
bool getAbort() const override;
71+
72+
void setAbort(bool abort) override;
73+
74+
ProfileSwitchProfileMap composite_profiles;
75+
76+
private:
77+
/** @brief If true, all tasks return immediately. Workaround for https://github.com/taskflow/taskflow/issues/201 */
78+
std::atomic<bool> abort_{ false };
79+
80+
std::string name_;
81+
82+
int conditionalProcess(ProcessInput input) const;
83+
84+
void process(ProcessInput input) const;
85+
};
86+
} // namespace tesseract_planning
87+
#endif // TESSERACT_PROCESS_MANAGERS_PROFILE_SWITCH_PROCESS_GENERATOR_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @file profile_switch_process_generator.h
3+
* @brief Process generator that returns a value based on the profile
4+
*
5+
* @author Matthew Powelson
6+
* @date October 26. 2020
7+
* @version TODO
8+
* @bug No known bugs
9+
*
10+
* @par License
11+
* Software License Agreement (Apache License)
12+
* @par
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
* @par
18+
* Unless required by applicable law or agreed to in writing, software
19+
* distributed under the License is distributed on an "AS IS" BASIS,
20+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21+
* See the License for the specific language governing permissions and
22+
* limitations under the License.
23+
*/
24+
25+
#include <tesseract_common/macros.h>
26+
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
27+
#include <console_bridge/console.h>
28+
TESSERACT_COMMON_IGNORE_WARNINGS_POP
29+
30+
#include <tesseract_process_managers/process_generators/profile_switch_process_generator.h>
31+
#include <tesseract_command_language/constants.h>
32+
#include <tesseract_command_language/utils/utils.h>
33+
#include <tesseract_motion_planners/planner_utils.h>
34+
35+
namespace tesseract_planning
36+
{
37+
ProfileSwitchProfile::ProfileSwitchProfile(const int& return_value) : return_value(return_value) {}
38+
39+
ProfileSwitchProcessGenerator::ProfileSwitchProcessGenerator(std::string name) : name_(std::move(name))
40+
{
41+
// Register default profile
42+
composite_profiles[DEFAULT_PROFILE_KEY] = std::make_shared<ProfileSwitchProfile>();
43+
}
44+
45+
const std::string& ProfileSwitchProcessGenerator::getName() const { return name_; }
46+
47+
std::function<void()> ProfileSwitchProcessGenerator::generateTask(ProcessInput input)
48+
{
49+
return [=]() { process(input); };
50+
}
51+
52+
std::function<int()> ProfileSwitchProcessGenerator::generateConditionalTask(ProcessInput input)
53+
{
54+
return [=]() { return conditionalProcess(input); };
55+
}
56+
57+
int ProfileSwitchProcessGenerator::conditionalProcess(ProcessInput input) const
58+
{
59+
if (abort_)
60+
return 0;
61+
62+
// --------------------
63+
// Check that inputs are valid
64+
// --------------------
65+
const Instruction* input_instruction = input.getInstruction();
66+
if (!isCompositeInstruction(*input_instruction))
67+
{
68+
CONSOLE_BRIDGE_logError("Input instruction to ProfileSwitch must be a composite instruction. Returning 0");
69+
return 0;
70+
}
71+
72+
const auto* ci = input_instruction->cast_const<CompositeInstruction>();
73+
74+
// Get the profile
75+
std::string profile = getProfileString(ci->getProfile(), name_, input.composite_profile_remapping);
76+
ProfileSwitchProfile::Ptr cur_composite_profile =
77+
getProfile<ProfileSwitchProfile>(profile, composite_profiles, std::make_shared<ProfileSwitchProfile>());
78+
if (!cur_composite_profile)
79+
{
80+
CONSOLE_BRIDGE_logWarn("ProfileSwitchProfile invalid. Returning 1");
81+
return 1;
82+
}
83+
84+
// Return the value specified in the profile
85+
CONSOLE_BRIDGE_logDebug("ProfileSwitchProfile returning %d", cur_composite_profile->return_value);
86+
return cur_composite_profile->return_value;
87+
}
88+
89+
void ProfileSwitchProcessGenerator::process(ProcessInput input) const { conditionalProcess(input); }
90+
91+
bool ProfileSwitchProcessGenerator::getAbort() const { return abort_; }
92+
void ProfileSwitchProcessGenerator::setAbort(bool abort) { abort_ = abort; }
93+
94+
} // namespace tesseract_planning

0 commit comments

Comments
 (0)