Skip to content

Created estop #10

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ install(
INCLUDES DESTINATION include/${PROJECT_NAME}
)

add_executable(estop src/estop.cpp)
ament_target_dependencies(estop rclcpp px4_msgs geometry_msgs tf2 tf2_ros px4_ros_com)
target_link_libraries(estop dascBots)
target_include_directories(estop PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/${PROJECT_NAME}>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
)
install(TARGETS
estop
DESTINATION lib/${PROJECT_NAME}
)

add_executable(example_control src/example_control.cpp)
ament_target_dependencies(example_control rclcpp px4_msgs geometry_msgs tf2 tf2_ros px4_ros_com)
target_link_libraries(example_control dascBots)
Expand Down
87 changes: 87 additions & 0 deletions src/estop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

#include <chrono>
#include <functional>
#include <memory>
#include "rclcpp/rclcpp.hpp"

#include "px4_msgs/msg/vehicle_command.hpp"

using namespace std::chrono_literals;

class Estop : public rclcpp::Node {

public:
Estop()
: Node("estop")
{
for (int i=0; i < maxN; i++){
std::string topic_name = "/drone" + std::to_string(i) + "/fmu/vehicle_command/in";
drone_publishers_.push_back(
this->create_publisher<px4_msgs::msg::VehicleCommand>(topic_name,10)
);
}

for (int i=0; i < maxN; i++){
std::string topic_name = "/rover" + std::to_string(i) + "/fmu/vehicle_command/in";
rover_publishers_.push_back(
this->create_publisher<px4_msgs::msg::VehicleCommand>(topic_name,10)
);
}

timer_ = this->create_wall_timer(
200ms, std::bind(&Estop::timer_callback, this));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void estop() {
this->estop = true;
}

private:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool estop = false;

const int maxN = 25;

rclcpp::TimerBase::SharedPtr timer_;
std::vector<rclcpp::Publisher<px4_msgs::msg::VehicleCommand>::SharedPtr> drone_publishers_;
std::vector<rclcpp::Publisher<px4_msgs::msg::VehicleCommand>::SharedPtr> rover_publishers_;


void timer_callback() {

auto msg = px4_msgs::msg::VehicleCommand();
msg.timestamp = 0;
msg.param1 = 0.0;
msg.param2 = 21196; // force disarm
msg.command = px4_msgs::msg::VehicleCommand::VEHICLE_CMD_COMPONENT_ARM_DISARM;
msg.target_component = 1;
msg.source_system = 1;
msg.source_component = 1;
msg.from_external = true;

// drone stop command
for (int i=0; i< maxN; i++){
msg.target_system = i;
drone_publishers_[i]->publish(msg);
}

// rover stop command
for (int i=0; i< maxN; i++){
msg.target_system = i;
rover_publishers_[i]->publish(msg);
}

RCLCPP_WARN(this->get_logger(), "sending e stop to drones with id less than %d", maxN);
}



}; // class Estop


int main(int argc, char* argv[]) {

std::cout << "Sending ESTOP" << std::endl;
setvbuf(stdout, NULL, _IONBF, BUFSIZ);

rclcpp::init(argc, argv);

rclcpp::spin(std::make_shared<Estop>());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to

    auto estop_node = std::make_shared<Estop>();
    rclcpp::executors::MultiThreadedExecutor server_exec;
    server_exec.add_node(estop_node);
    
    auto server_spin_exec = [&server_exec]() {
        server_exec.spin();
    };

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok good idea, ill make these changes, but we should wait only merge them after weve tested to see that it works.


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Block by

int n;
std::cin >> n;
estop_node.estop();
while(rclcpp::ok()) {
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok we can do something like this. are you sure the std::cin trick works in multithreaded?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it should work.

rclcpp::shutdown();
return 0;
}