Send HEARTBEAT mesage from Godot
This commit is contained in:
parent
7890c90380
commit
f46887d6cc
3 changed files with 47 additions and 1 deletions
|
|
@ -14,6 +14,7 @@ env = SConscript("modules/godot-cpp/SConstruct")
|
||||||
|
|
||||||
# tweak this if you want to use different folders, or more folders, to store your source code in.
|
# tweak this if you want to use different folders, or more folders, to store your source code in.
|
||||||
env.Append(CPPPATH=["src/"])
|
env.Append(CPPPATH=["src/"])
|
||||||
|
env.Append(CPPPATH=["include/"])
|
||||||
sources = Glob("src/*.cpp")
|
sources = Glob("src/*.cpp")
|
||||||
|
|
||||||
# Generated with local Godot installation
|
# Generated with local Godot installation
|
||||||
|
|
@ -48,5 +49,7 @@ else:
|
||||||
"project/bin/libmarshconnector{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
|
"project/bin/libmarshconnector{}{}".format(env["suffix"], env["SHLIBSUFFIX"]),
|
||||||
source=sources,
|
source=sources,
|
||||||
)
|
)
|
||||||
|
# Triggered a lot in MAVLink headers
|
||||||
|
env.Append(CXXFLAGS=["-Wno-address-of-packed-member"])
|
||||||
|
|
||||||
Default(library)
|
Default(library)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,16 @@
|
||||||
#include "marshconnector.h"
|
#include "marshconnector.h"
|
||||||
|
|
||||||
#include "godot_cpp/classes/engine.hpp"
|
#include "godot_cpp/classes/engine.hpp"
|
||||||
|
#include "godot_cpp/classes/font_file.hpp"
|
||||||
|
#include "godot_cpp/classes/packet_peer_udp.hpp"
|
||||||
#include "godot_cpp/core/memory.hpp"
|
#include "godot_cpp/core/memory.hpp"
|
||||||
#include "godot_cpp/core/print_string.hpp"
|
#include "godot_cpp/core/print_string.hpp"
|
||||||
|
#include "godot_cpp/variant/packed_byte_array.hpp"
|
||||||
|
#include "mavlink/all/mavlink.h" // IWYU pragma: keep; always include the mavlink.h file for selected dialect
|
||||||
|
#include "mavlink/mavlink_helpers.h"
|
||||||
|
|
||||||
|
// How much extra bytes are needed on top of message payload length
|
||||||
|
#define MAVLINK_OVERHEAD 12
|
||||||
|
|
||||||
using namespace godot;
|
using namespace godot;
|
||||||
|
|
||||||
|
|
@ -22,6 +30,8 @@ MarshConnector::MarshConnector() {
|
||||||
heartbeat_timer->set_autostart(true);
|
heartbeat_timer->set_autostart(true);
|
||||||
heartbeat_timer->connect("timeout", Callable(this, "send_heartbeat"));
|
heartbeat_timer->connect("timeout", Callable(this, "send_heartbeat"));
|
||||||
|
|
||||||
|
socket = memnew(PacketPeerUDP);
|
||||||
|
|
||||||
if (Engine::get_singleton()->is_editor_hint()) {
|
if (Engine::get_singleton()->is_editor_hint()) {
|
||||||
// Don't run _process() in the editor
|
// Don't run _process() in the editor
|
||||||
set_process_mode(Node::ProcessMode::PROCESS_MODE_DISABLED);
|
set_process_mode(Node::ProcessMode::PROCESS_MODE_DISABLED);
|
||||||
|
|
@ -32,8 +42,38 @@ MarshConnector::~MarshConnector() {
|
||||||
// Free only manually managed member variables
|
// Free only manually managed member variables
|
||||||
}
|
}
|
||||||
|
|
||||||
void MarshConnector::_process(double delta) { time_passed += delta; }
|
void MarshConnector::_ready() { print_line("MarshConnector ready"); }
|
||||||
|
|
||||||
|
void MarshConnector::_process(double delta) {
|
||||||
|
time_passed += delta;
|
||||||
|
|
||||||
|
if (!socket->is_socket_connected()) {
|
||||||
|
socket->connect_to_host("127.0.0.1", 24400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MarshConnector::send_heartbeat() {
|
void MarshConnector::send_heartbeat() {
|
||||||
print_line("Sending HEARTBEAT at ", time_passed, " seconds");
|
print_line("Sending HEARTBEAT at ", time_passed, " seconds");
|
||||||
|
|
||||||
|
mavlink_heartbeat_t heartbeat;
|
||||||
|
heartbeat.type = MAV_TYPE_HELICOPTER;
|
||||||
|
heartbeat.autopilot = MAV_AUTOPILOT_INVALID;
|
||||||
|
heartbeat.base_mode = 0; // none of the flags applicable
|
||||||
|
heartbeat.custom_mode = 0; // not used so far
|
||||||
|
heartbeat.system_status = MAV_STATE_ACTIVE;
|
||||||
|
|
||||||
|
mavlink_message_t message;
|
||||||
|
mavlink_msg_heartbeat_encode_chan(1, MARSH_COMP_ID_VISUALISATION,
|
||||||
|
MAVLINK_COMM_0, &message, &heartbeat);
|
||||||
|
uint8_t send_buffer[MAVLINK_OVERHEAD + MAVLINK_MSG_ID_HEARTBEAT_LEN];
|
||||||
|
mavlink_msg_to_send_buffer(send_buffer, &message);
|
||||||
|
|
||||||
|
PackedByteArray array;
|
||||||
|
for (int i = 0; i < sizeof(send_buffer); i++) {
|
||||||
|
array.append(send_buffer[i]);
|
||||||
|
}
|
||||||
|
print_line("Data array ", array);
|
||||||
|
|
||||||
|
const auto result = socket->put_packet(array);
|
||||||
|
print_line("Send result ", result);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#define MARSHCONNECTOR_H
|
#define MARSHCONNECTOR_H
|
||||||
|
|
||||||
#include "godot_cpp/classes/node.hpp"
|
#include "godot_cpp/classes/node.hpp"
|
||||||
|
#include "godot_cpp/classes/packet_peer_udp.hpp"
|
||||||
#include "godot_cpp/classes/timer.hpp"
|
#include "godot_cpp/classes/timer.hpp"
|
||||||
|
|
||||||
namespace godot {
|
namespace godot {
|
||||||
|
|
@ -12,6 +13,7 @@ class MarshConnector : public Node {
|
||||||
private:
|
private:
|
||||||
double time_passed;
|
double time_passed;
|
||||||
Timer *heartbeat_timer;
|
Timer *heartbeat_timer;
|
||||||
|
PacketPeerUDP *socket;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
@ -20,6 +22,7 @@ public:
|
||||||
MarshConnector();
|
MarshConnector();
|
||||||
~MarshConnector();
|
~MarshConnector();
|
||||||
|
|
||||||
|
void _ready() override;
|
||||||
void _process(double delta) override;
|
void _process(double delta) override;
|
||||||
|
|
||||||
void send_heartbeat();
|
void send_heartbeat();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue