-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
app/other/behaviour_tree_car/include/behaviour_tree/node/custom/action/Drive.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#ifndef BEHAVIOUR_TREE_DRIVE_HPP | ||
#define BEHAVIOUR_TREE_DRIVE_HPP | ||
|
||
#pragma once | ||
|
||
#include "behaviour_tree/node/custom/CustomNode.hpp" | ||
|
||
#include "behaviour_tree/Context.h" | ||
#ifndef BEHAVIOUR_TREE_DISABLE_RUN | ||
#include "behaviour_tree/CarContext.hpp" | ||
#endif // !BEHAVIOUR_TREE_DISABLE_RUN | ||
|
||
#include "enum/DirectionType.hpp" | ||
|
||
namespace behaviour_tree::node::custom::action | ||
{ | ||
class Drive final : public CustomNode | ||
{ | ||
public: | ||
Drive(const std::string &name, const int speed, const DirectionType &direction_type) : CustomNode(name), speed(speed), direction_type(direction_type) | ||
{ | ||
} | ||
|
||
const Status run(const int tick_count, std::shared_ptr<Context> context) final override | ||
{ | ||
#ifndef BEHAVIOUR_TREE_DISABLE_RUN | ||
std::shared_ptr<CarContext> car_context = std::dynamic_pointer_cast<CarContext>(context); | ||
auto car_system = car_context->getCarSystem(); | ||
const MovementSystem *movement_system = car_system->getMovementSystem(); | ||
switch (this->getDirectionType()) | ||
{ | ||
case DirectionType::Backward: | ||
{ | ||
movement_system->setRearWheelsDirectionToBackward(); | ||
break; | ||
} | ||
case DirectionType::Forward: | ||
{ | ||
movement_system->setRearWheelsDirectionToForward(); | ||
break; | ||
} | ||
} | ||
movement_system->setRearWheelsSpeed(this->getSpeed()); | ||
#endif // !BEHAVIOUR_TREE_DISABLE_RUN | ||
return Status::Success; | ||
} | ||
|
||
const std::string toString() const final override | ||
{ | ||
const std::string &name = this->getName(); | ||
std::string direction_type; | ||
switch (this->getDirectionType()) | ||
{ | ||
case DirectionType::Forward: | ||
direction_type = "Forward"; | ||
break; | ||
case DirectionType::Backward: | ||
direction_type = "Backward"; | ||
break; | ||
} | ||
if (name != "") | ||
return fmt::format(R"(<Action:Drive name='{}' speed='{}' direction_type='{}'/>)", name, this->getSpeed()); | ||
else | ||
return fmt::format(R"(<Action:Drive speed='{}' direction_type='{}'/>)", this->getSpeed()); | ||
} | ||
|
||
const int getSpeed() const | ||
{ | ||
return this->speed; | ||
} | ||
|
||
const DirectionType &getDirectionType() const | ||
{ | ||
return this->direction_type; | ||
} | ||
|
||
private: | ||
const int speed; | ||
const DirectionType direction_type; | ||
}; | ||
} | ||
|
||
#endif |