Skip to content

Commit b22ff73

Browse files
Add Set Digital Instruction
1 parent 9ffe779 commit b22ff73

File tree

7 files changed

+310
-0
lines changed

7 files changed

+310
-0
lines changed

tesseract_command_language/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ add_library(
5151
src/poly/waypoint_poly.cpp
5252
src/move_instruction.cpp
5353
src/set_analog_instruction.cpp
54+
src/set_digital_instruction.cpp
5455
src/set_tool_instruction.cpp
5556
src/timer_instruction.cpp
5657
src/wait_instruction.cpp

tesseract_command_language/include/tesseract_command_language/fwd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum class CompositeInstructionOrder : std::uint8_t;
3232
class CompositeInstruction;
3333
class MoveInstruction;
3434
class SetAnalogInstruction;
35+
class SetDigitalInstruction;
3536
class SetToolInstruction;
3637
enum class TimerInstructionType : std::uint8_t;
3738
class TimerInstruction;

tesseract_command_language/include/tesseract_command_language/instruction_type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class InstructionPoly;
3232

3333
bool isSetAnalogInstruction(const InstructionPoly& instruction);
3434

35+
bool isSetDigitalInstruction(const InstructionPoly& instruction);
36+
3537
bool isSetToolInstruction(const InstructionPoly& instruction);
3638

3739
bool isTimerInstruction(const InstructionPoly& instruction);
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* @file set_digital_instruction.h
3+
* @brief Set Digital Instruction
4+
*
5+
* @author Levi Armstrong
6+
* @date March 23, 2025
7+
* @version TODO
8+
* @bug No known bugs
9+
*
10+
* @copyright Copyright (c) 2025, Southwest Research Institute
11+
*
12+
* @par License
13+
* Software License Agreement (Apache License)
14+
* @par
15+
* Licensed under the Apache License, Version 2.0 (the "License");
16+
* you may not use this file except in compliance with the License.
17+
* You may obtain a copy of the License at
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
* @par
20+
* Unless required by applicable law or agreed to in writing, software
21+
* distributed under the License is distributed on an "AS IS" BASIS,
22+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23+
* See the License for the specific language governing permissions and
24+
* limitations under the License.
25+
*/
26+
#ifndef TESSERACT_COMMAND_LANGUAGE_SET_DIGITAL_INSTRUCTION_H
27+
#define TESSERACT_COMMAND_LANGUAGE_SET_DIGITAL_INSTRUCTION_H
28+
29+
#include <tesseract_common/macros.h>
30+
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
31+
#include <string>
32+
#include <boost/uuid/uuid.hpp>
33+
TESSERACT_COMMON_IGNORE_WARNINGS_POP
34+
35+
#include <tesseract_command_language/poly/instruction_poly.h>
36+
37+
namespace tesseract_planning
38+
{
39+
class SetDigitalInstruction final : public InstructionInterface
40+
{
41+
public:
42+
SetDigitalInstruction() = default; // Required for boost serialization do not use
43+
SetDigitalInstruction(std::string key, int index, bool value);
44+
45+
// Instruction
46+
47+
/**
48+
* @brief Get the UUID
49+
* @return The UUID
50+
*/
51+
const boost::uuids::uuid& getUUID() const override final;
52+
/**
53+
* @brief Set the UUID
54+
* @param uuid The UUID
55+
*/
56+
void setUUID(const boost::uuids::uuid& uuid) override final;
57+
/**
58+
* @brief Regenerate the UUID
59+
*/
60+
void regenerateUUID() override final;
61+
62+
/**
63+
* @brief Get the parent UUID
64+
* @return The parent UUID
65+
*/
66+
const boost::uuids::uuid& getParentUUID() const override final;
67+
/**
68+
* @brief Set the parent UUID
69+
* @param uuid The parent UUID
70+
*/
71+
void setParentUUID(const boost::uuids::uuid& uuid) override final;
72+
73+
/**
74+
* @brief Get the description
75+
* @return The description
76+
*/
77+
const std::string& getDescription() const override final;
78+
/**
79+
* @brief Set the description
80+
* @param description The description
81+
*/
82+
void setDescription(const std::string& description) override final;
83+
84+
/**
85+
* @brief Output the contents to std::cout
86+
* @param prefix The prefix to add to each variable
87+
*/
88+
void print(const std::string& prefix = "") const override final; // NOLINT
89+
90+
/**
91+
* @brief Make a deep copy of the object
92+
* @return A deep copy
93+
*/
94+
std::unique_ptr<InstructionInterface> clone() const override final;
95+
96+
// SetDigitalInstruction
97+
98+
/** @brief Get the digital key */
99+
std::string getKey() const;
100+
101+
/** @brief Get the digital index */
102+
int getIndex() const;
103+
104+
/** @brief Get the digital value */
105+
bool getValue() const;
106+
107+
private:
108+
/** @brief The instructions UUID */
109+
boost::uuids::uuid uuid_{};
110+
/** @brief The parent UUID if created from createChild */
111+
boost::uuids::uuid parent_uuid_{};
112+
/** @brief The description of the instruction */
113+
std::string description_{ "Tesseract Set Digital Instruction" };
114+
/** @brief The key is used to identify which type of digital to set */
115+
std::string key_;
116+
/** @brief The digital index to set */
117+
int index_{ 0 };
118+
/** @brief The digital value */
119+
bool value_{ false };
120+
121+
/**
122+
* @brief Check if two objects are equal
123+
* @param other The other object to compare with
124+
* @return True if equal, otherwise false
125+
*/
126+
bool equals(const InstructionInterface& other) const override final;
127+
128+
friend class boost::serialization::access;
129+
friend struct tesseract_common::Serialization;
130+
template <class Archive>
131+
void serialize(Archive& ar, const unsigned int version); // NOLINT
132+
};
133+
} // namespace tesseract_planning
134+
135+
BOOST_CLASS_EXPORT_KEY(tesseract_planning::SetDigitalInstruction)
136+
BOOST_CLASS_TRACKING(tesseract_planning::SetDigitalInstruction, boost::serialization::track_never)
137+
138+
#endif // TESSERACT_COMMAND_LANGUAGE_SET_DIGITAL_INSTRUCTION_H

tesseract_command_language/src/instruction_type.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include <tesseract_command_language/set_analog_instruction.h>
28+
#include <tesseract_command_language/set_digital_instruction.h>
2829
#include <tesseract_command_language/set_tool_instruction.h>
2930
#include <tesseract_command_language/timer_instruction.h>
3031
#include <tesseract_command_language/wait_instruction.h>
@@ -36,6 +37,11 @@ bool isSetAnalogInstruction(const InstructionPoly& instruction)
3637
return (instruction.getType() == std::type_index(typeid(SetAnalogInstruction)));
3738
}
3839

40+
bool isSetDigitalInstruction(const InstructionPoly& instruction)
41+
{
42+
return (instruction.getType() == std::type_index(typeid(SetDigitalInstruction)));
43+
}
44+
3945
bool isSetToolInstruction(const InstructionPoly& instruction)
4046
{
4147
return (instruction.getType() == std::type_index(typeid(SetToolInstruction)));
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @file set_digital_instruction.cpp
3+
* @brief Set Digital Instruction
4+
*
5+
* @author Levi Armstrong
6+
* @date March 23, 2025
7+
* @version TODO
8+
* @bug No known bugs
9+
*
10+
* @copyright Copyright (c) 2025, Southwest Research Institute
11+
*
12+
* @par License
13+
* Software License Agreement (Apache License)
14+
* @par
15+
* Licensed under the Apache License, Version 2.0 (the "License");
16+
* you may not use this file except in compliance with the License.
17+
* You may obtain a copy of the License at
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
* @par
20+
* Unless required by applicable law or agreed to in writing, software
21+
* distributed under the License is distributed on an "AS IS" BASIS,
22+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23+
* See the License for the specific language governing permissions and
24+
* limitations under the License.
25+
*/
26+
#include <tesseract_common/macros.h>
27+
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
28+
#include <iostream>
29+
#include <boost/serialization/base_object.hpp>
30+
#include <boost/serialization/nvp.hpp>
31+
#include <boost/uuid/uuid_generators.hpp>
32+
#include <boost/uuid/uuid_io.hpp>
33+
#include <boost/uuid/uuid_serialize.hpp>
34+
TESSERACT_COMMON_IGNORE_WARNINGS_POP
35+
36+
#include <tesseract_command_language/set_digital_instruction.h>
37+
#include <tesseract_common/utils.h>
38+
39+
namespace tesseract_planning
40+
{
41+
SetDigitalInstruction::SetDigitalInstruction(std::string key, int index, bool value)
42+
: uuid_(boost::uuids::random_generator()()), key_(std::move(key)), index_(index), value_(value)
43+
{
44+
}
45+
46+
const boost::uuids::uuid& SetDigitalInstruction::getUUID() const { return uuid_; }
47+
void SetDigitalInstruction::setUUID(const boost::uuids::uuid& uuid)
48+
{
49+
if (uuid.is_nil())
50+
throw std::runtime_error("SetDigitalInstruction, tried to set uuid to null!");
51+
52+
uuid_ = uuid;
53+
}
54+
void SetDigitalInstruction::regenerateUUID() { uuid_ = boost::uuids::random_generator()(); }
55+
56+
const boost::uuids::uuid& SetDigitalInstruction::getParentUUID() const { return parent_uuid_; }
57+
void SetDigitalInstruction::setParentUUID(const boost::uuids::uuid& uuid) { parent_uuid_ = uuid; }
58+
59+
const std::string& SetDigitalInstruction::getDescription() const { return description_; }
60+
61+
void SetDigitalInstruction::setDescription(const std::string& description) { description_ = description; }
62+
63+
void SetDigitalInstruction::print(const std::string& prefix) const // NOLINT
64+
{
65+
std::cout << prefix + "Set Analog Instruction, Key: " << key_ << ", Index: " << index_ << ", Value: " << value_;
66+
std::cout << ", Description: " << getDescription() << "\n";
67+
}
68+
69+
std::unique_ptr<InstructionInterface> SetDigitalInstruction::clone() const
70+
{
71+
return std::make_unique<SetDigitalInstruction>(*this);
72+
}
73+
74+
std::string SetDigitalInstruction::getKey() const { return key_; }
75+
76+
int SetDigitalInstruction::getIndex() const { return index_; }
77+
78+
bool SetDigitalInstruction::getValue() const { return value_; }
79+
80+
bool SetDigitalInstruction::equals(const InstructionInterface& other) const
81+
{
82+
const auto* rhs = dynamic_cast<const SetDigitalInstruction*>(&other);
83+
if (rhs == nullptr)
84+
return false;
85+
86+
bool equal = true;
87+
equal &= (key_ == rhs->key_);
88+
equal &= (index_ == rhs->index_);
89+
equal &= (value_ == rhs->value_);
90+
return equal;
91+
}
92+
93+
template <class Archive>
94+
void SetDigitalInstruction::serialize(Archive& ar, const unsigned int /*version*/)
95+
{
96+
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(InstructionInterface);
97+
ar& boost::serialization::make_nvp("uuid", uuid_);
98+
ar& boost::serialization::make_nvp("parent_uuid", parent_uuid_);
99+
ar& boost::serialization::make_nvp("description", description_);
100+
ar& boost::serialization::make_nvp("key", key_);
101+
ar& boost::serialization::make_nvp("index", index_);
102+
ar& boost::serialization::make_nvp("value", value_);
103+
}
104+
105+
} // namespace tesseract_planning
106+
107+
TESSERACT_SERIALIZE_ARCHIVES_INSTANTIATE(tesseract_planning::SetDigitalInstruction)
108+
BOOST_CLASS_EXPORT_IMPLEMENT(tesseract_planning::SetDigitalInstruction)

tesseract_command_language/test/command_language_unit.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <tesseract_command_language/state_waypoint.h>
4141
#include <tesseract_command_language/move_instruction.h>
4242
#include <tesseract_command_language/set_analog_instruction.h>
43+
#include <tesseract_command_language/set_digital_instruction.h>
4344
#include <tesseract_command_language/set_tool_instruction.h>
4445
#include <tesseract_command_language/timer_instruction.h>
4546
#include <tesseract_command_language/wait_instruction.h>
@@ -216,6 +217,59 @@ TEST(TesseractCommandLanguageUnit, SetAnalogInstructionTests) // NOLINT
216217
}
217218
}
218219

220+
TEST(TesseractCommandLanguageUnit, SetDigitalInstructionTests) // NOLINT
221+
{
222+
using T = SetDigitalInstruction;
223+
test_suite::runInstructionInterfaceTest(InstructionPoly(T()));
224+
225+
{
226+
const std::string key{ "key" };
227+
const int index{ 5 };
228+
const bool value{ true };
229+
T instr(key, index, value);
230+
EXPECT_EQ(instr.getKey(), key);
231+
EXPECT_EQ(instr.getIndex(), index);
232+
EXPECT_EQ(instr.getValue(), value);
233+
234+
// Copy
235+
InstructionPoly poly{ instr };
236+
InstructionPoly copy(poly);
237+
EXPECT_EQ(copy.as<T>().getKey(), key);
238+
EXPECT_EQ(copy.as<T>().getIndex(), index);
239+
EXPECT_EQ(copy.as<T>().getValue(), value);
240+
EXPECT_TRUE(isSetDigitalInstruction(poly));
241+
EXPECT_TRUE(isSetDigitalInstruction(copy));
242+
test_suite::runInstructionSerializationTest(poly);
243+
test_suite::runInstructionSerializationTest(copy);
244+
245+
// Equal
246+
EXPECT_TRUE(poly == copy);
247+
EXPECT_TRUE(copy == poly);
248+
EXPECT_FALSE(copy != poly);
249+
250+
{ // Not Equal
251+
{
252+
InstructionPoly ne_poly{ T("ne", index, value) };
253+
EXPECT_FALSE(poly == ne_poly);
254+
EXPECT_FALSE(ne_poly == poly);
255+
EXPECT_TRUE(ne_poly != poly);
256+
}
257+
{
258+
InstructionPoly ne_poly{ T(key, 1, value) };
259+
EXPECT_FALSE(poly == ne_poly);
260+
EXPECT_FALSE(ne_poly == poly);
261+
EXPECT_TRUE(ne_poly != poly);
262+
}
263+
{
264+
InstructionPoly ne_poly{ T(key, index, false) };
265+
EXPECT_FALSE(poly == ne_poly);
266+
EXPECT_FALSE(ne_poly == poly);
267+
EXPECT_TRUE(ne_poly != poly);
268+
}
269+
}
270+
}
271+
}
272+
219273
TEST(TesseractCommandLanguageUnit, SetToolInstructionTests) // NOLINT
220274
{
221275
using T = SetToolInstruction;

0 commit comments

Comments
 (0)