Skip to content

Commit

Permalink
Merge pull request #17 from julianoes/pr-flush
Browse files Browse the repository at this point in the history
camera-manager: logging improvements
  • Loading branch information
julianoes authored Mar 21, 2024
2 parents 236826f + a5bd150 commit e6f8875
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 37 deletions.
45 changes: 38 additions & 7 deletions camera-manager/camera_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <thread>
#include <filesystem>
#include <mavsdk/mavsdk.h>
#include <mavsdk/log_callback.h>
#include <mavsdk/plugins/camera_server/camera_server.h>
#include <mavsdk/plugins/ftp_server/ftp_server.h>
#include <mavsdk/plugins/param_server/param_server.h>
Expand All @@ -16,7 +17,7 @@ int main(int argc, char* argv[])
<< "\n"
<< "Usage: " << argv[0] << " <mavsdk connection url> <ground station connection url> <our ip>\n"
<< "\n"
<< "E.g. " << argv[0] << " serial:///dev/ttyUSB0:57600 udp://192.168.1.51:14550 rtsp://192.168.1.45:8554/live\n";
<< "E.g. " << argv[0] << " serial:///dev/ttyUSB0:57600 udp://192.168.1.51:14550 rtsp://192.168.1.45:8554/live" << std::endl;
return 1;
}

Expand All @@ -39,6 +40,36 @@ int main(int argc, char* argv[])
// MAVSDK setup second
mavsdk::Mavsdk mavsdk{mavsdk::Mavsdk::Configuration{mavsdk::Mavsdk::ComponentType::Camera}};

// We overwrite the mavsdk logs to prepend "Mavsdk:" and to make sure we flush it after every
// line using std::endl. Otherwise, it gets buffered by systemctl and logs appear delayed.

mavsdk::log::subscribe([](mavsdk::log::Level level, // message severity level
const std::string& message, // message text
const std::string& file, // source file from which the message was sent
int line) { // line number in the source file

std::cout << "Mavsdk ";
// process the log message in a way you like
switch (level) {
case mavsdk::log::Level::Debug:
std::cout << "debug: ";
break;
case mavsdk::log::Level::Info:
std::cout << "info: ";
break;
case mavsdk::log::Level::Warn:
std::cout << "warning: ";
break;
case mavsdk::log::Level::Err:
std::cout << "error: ";
}
std::cout << message;
std::cout << " (" << file << ":" << line << ")" << std::endl;

// returning true from the callback disables default printing
return true;
});

auto result = mavsdk.add_any_connection(mavsdk_connection_url, mavsdk::ForwardingOption::ForwardingOn);
if (result != mavsdk::ConnectionResult::Success) {
std::cerr << "Could not establish autopilot connection: " << result << std::endl;
Expand Down Expand Up @@ -82,7 +113,7 @@ int main(int argc, char* argv[])
stream_res = 1;
break;
default:
std::cerr << "Unexpected stream resolution\n";
std::cerr << "Unexpected stream resolution" << std::endl;
break;
}

Expand Down Expand Up @@ -203,14 +234,14 @@ int main(int argc, char* argv[])
camera_server.subscribe_start_video([&](int32_t) {

if (recording) {
std::cout << "Video already started\n";
std::cout << "Video already started" << std::endl;
camera_server.respond_start_video(
mavsdk::CameraServer::CameraFeedback::Failed);

} else {
std::cout << "Start video\n";
std::cout << "Start video" << std::endl;
siyi_messager.send(siyi_serializer.assemble_message(siyi::ToggleRecording{}));
std::cout << "Video started\n";
std::cout << "Video started" << std::endl;
siyi_messager.send(siyi_serializer.assemble_message(siyi::ToggleRecording{}));
recording = true;
recording_start_time = std::chrono::steady_clock::now();
Expand All @@ -222,12 +253,12 @@ int main(int argc, char* argv[])
camera_server.subscribe_stop_video([&](int32_t) {

if (!recording) {
std::cout << "Video not started\n";
std::cout << "Video not started" << std::endl;
camera_server.respond_stop_video(
mavsdk::CameraServer::CameraFeedback::Failed);

} else {
std::cout << "Stop video\n";
std::cout << "Stop video" << std::endl;
siyi_messager.send(siyi_serializer.assemble_message(siyi::ToggleRecording{}));
recording = false;
camera_server.respond_stop_video(
Expand Down
18 changes: 9 additions & 9 deletions camera-manager/siyi_camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Camera {
} else if (_stream_settings.resolution_h == 720 && _stream_settings.resolution_l == 1280) {
return Resolution::Res1280x720;
} else {
std::cerr << "resolution invalid\n";
std::cerr << "resolution invalid" << std::endl;
assert(false);
return Resolution::Res1280x720;
}
Expand Down Expand Up @@ -138,7 +138,7 @@ class Camera {
set_stream_settings.resolution_l = 3840;
set_stream_settings.resolution_h = 2160;
} else {
std::cerr << "resolution invalid\n";
std::cerr << "resolution invalid" << std::endl;
return false;
}

Expand All @@ -150,7 +150,7 @@ class Camera {
_deserializer.disassemble_message<siyi::AckSetStreamSettings>(_messager.receive());

if (!maybe_ack_set_stream_settings || maybe_ack_set_stream_settings.value().result != 1) {
std::cerr << "setting stream settings failed\n";
std::cerr << "setting stream settings failed" << std::endl;
return false;
}

Expand Down Expand Up @@ -184,7 +184,7 @@ class Camera {
} else if (settings.video_enc_type == 2) {
return Codec::H265;
} else {
std::cerr << "codec invalid\n";
std::cerr << "codec invalid" << std::endl;
assert(false);
return Codec::H264;
}
Expand All @@ -206,7 +206,7 @@ class Camera {
} else if (codec == Codec::H265) {
set_stream_settings.video_enc_type = 2;
} else {
std::cerr << "codec invalid\n";
std::cerr << "codec invalid" << std::endl;
return false;
}
set_stream_settings.video_bitrate_kbps = _stream_settings.video_bitrate_kbps;
Expand All @@ -218,7 +218,7 @@ class Camera {
_deserializer.disassemble_message<siyi::AckSetStreamSettings>(_messager.receive());

if (!maybe_ack_set_stream_settings || maybe_ack_set_stream_settings.value().result != 1) {
std::cerr << "setting stream settings failed\n";
std::cerr << "setting stream settings failed" << std::endl;
return false;
}
auto get_stream_settings = siyi::GetStreamSettings{};
Expand Down Expand Up @@ -265,7 +265,7 @@ class Camera {
_deserializer.disassemble_message<siyi::AckSetStreamSettings>(_messager.receive());

if (!maybe_ack_set_stream_settings || maybe_ack_set_stream_settings.value().result != 1) {
std::cerr << "setting stream settings failed\n";
std::cerr << "setting stream settings failed" << std::endl;
return false;
}

Expand Down Expand Up @@ -317,7 +317,7 @@ class Camera {
// _deserializer.disassemble_message<siyi::AckManualZoom>(_messager.receive());

//if (maybe_ack_manual_zoom) {
// std::cerr << "current zoom: " << maybe_ack_manual_zoom.value().zoom_multiple << '\n';
// std::cerr << "current zoom: " << maybe_ack_manual_zoom.value().zoom_multiple << std::endl;
// _ack_manual_zoom = maybe_ack_manual_zoom.value();
// return false;
//}
Expand Down Expand Up @@ -354,7 +354,7 @@ class Camera {
// _deserializer.disassemble_message<siyi::AckManualZoom>(_messager.receive());

//if (maybe_ack_manual_zoom) {
// std::cerr << "current zoom: " << maybe_ack_manual_zoom.value().zoom_multiple << '\n';
// std::cerr << "current zoom: " << maybe_ack_manual_zoom.value().zoom_multiple << std::endl;
// _ack_manual_zoom = maybe_ack_manual_zoom.value();
// return false;
//}
Expand Down
32 changes: 16 additions & 16 deletions camera-manager/siyi_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void print_usage(const std::string_view& bin_name)
<< " Options:\n"
<< " - h264 (for H264)\n"
<< " - h265 (for H265/HVEC)\n"
<< "\n";
<< std::endl;
}

int main(int argc, char* argv[])
Expand All @@ -59,7 +59,7 @@ int main(int argc, char* argv[])
}

if (argc == 1 ) {
std::cout << "No argument supplied.\n";
std::cout << "No argument supplied." << std::endl;
print_usage(argv[0]);
return 0;
}
Expand All @@ -73,35 +73,35 @@ int main(int argc, char* argv[])
siyi_camera.print_version();

} else if (action == "take_picture") {
std::cout << "Take picture\n";
std::cout << "Take picture" << std::endl;
siyi_messager.send(siyi_serializer.assemble_message(siyi::TakePicture{}));
(void)siyi_messager.receive();

} else if (action == "toggle_recording") {
std::cout << "Toggle recording\n";
std::cout << "Toggle recording" << std::endl;
siyi_messager.send(siyi_serializer.assemble_message(siyi::ToggleRecording{}));
(void)siyi_messager.receive();

} else if (action == "gimbal") {
if (argc >= 3) {
const std::string_view command {argv[2]};
if (command == "neutral") {
std::cout << "Set gimbal neutral\n";
std::cout << "Set gimbal neutral" << std::endl;
siyi_messager.send(siyi_serializer.assemble_message(siyi::GimbalCenter{}));
(void)siyi_messager.receive();
} else if (command == "angle") {
if (argc >= 5) {
auto pitch = std::strtol(argv[3], nullptr, 10);
auto yaw = std::strtol(argv[4], nullptr, 10);
std::cout << "Set gimbal to " << pitch << " deg and yaw " << yaw << "\n";
std::cout << "Set gimbal to " << pitch << " deg and yaw " << yaw << std::endl;
siyi::SetGimbalAttitude set_gimbal_attitude{};
set_gimbal_attitude.pitch_t10 = static_cast<std::int16_t>(pitch*10);
set_gimbal_attitude.yaw_t10 = static_cast<std::int16_t>(yaw*10);
siyi_messager.send(siyi_serializer.assemble_message(set_gimbal_attitude));
(void)siyi_messager.receive();

} else {
std::cout << "Not enough arguments\n";
std::cout << "Not enough arguments" << std::endl;
print_usage(argv[0]);
return 1;
}
Expand All @@ -117,14 +117,14 @@ int main(int argc, char* argv[])
} else if (type_str == "recording") {
type = siyi::Camera::Type::Recording;
} else {
std::cout << "Invalid type\n";
std::cout << "Invalid type" << std::endl;
print_usage(argv[0]);
return 1;
}

siyi_camera.print_settings(type);
} else {
std::cout << "Not enough arguments\n";
std::cout << "Not enough arguments" << std::endl;
print_usage(argv[0]);
return 1;
}
Expand All @@ -141,7 +141,7 @@ int main(int argc, char* argv[])
} else if (type_str == "recording") {
type = siyi::Camera::Type::Recording;
} else {
std::cout << "Invalid type\n";
std::cout << "Invalid type" << std::endl;
print_usage(argv[0]);
return 1;
}
Expand Down Expand Up @@ -247,16 +247,16 @@ int main(int argc, char* argv[])
}

} else {
std::cout << "Unknown setting\n";
std::cout << "Unknown setting" << std::endl;
print_usage(argv[0]);
return 1;
}

std::cout << "New " << type_str << " settings:\n";
std::cout << "New " << type_str << " settings:" << std::endl;
siyi_camera.print_settings(type);

} else {
std::cout << "Not enough arguments\n";
std::cout << "Not enough arguments" << std::endl;
print_usage(argv[0]);
return 1;
}
Expand Down Expand Up @@ -300,7 +300,7 @@ int main(int argc, char* argv[])
siyi_camera.absolute_zoom(factor);
}
} else {
std::cout << "Not enough arguments\n";
std::cout << "Not enough arguments" << std::endl;
print_usage(argv[0]);
return 1;
}
Expand All @@ -309,13 +309,13 @@ int main(int argc, char* argv[])
if (argc >= 3) {

} else {
std::cout << "Not enough arguments\n";
std::cout << "Not enough arguments" << std::endl;
print_usage(argv[0]);
return 1;
}

} else {
std::cout << "Unknown command\n";
std::cout << "Unknown command" << std::endl;
print_usage(argv[0]);
return 2;
}
Expand Down
10 changes: 5 additions & 5 deletions camera-manager/siyi_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class AckFirmwareVersion : public AckPayload<AckFirmwareVersion> {
bool fill_impl(const std::vector<std::uint8_t>& bytes) {

if (bytes.size() != len) {
std::cerr << "Length wrong: " << bytes.size() << " instead of " << len << '\n';
std::cerr << "Length wrong: " << bytes.size() << " instead of " << len << std::endl;
return false;
}

Expand All @@ -318,7 +318,7 @@ class AckFirmwareVersion : public AckPayload<AckFirmwareVersion> {
<< "Gimbal version: "
<< int(self.gimbal_firmware_ver_major) << '.'
<< int(self.gimbal_firmware_ver_minor) << '.'
<< int(self.gimbal_firmware_ver_patch) << '\n';
<< int(self.gimbal_firmware_ver_patch) << std::endl;
return str;
}

Expand All @@ -339,7 +339,7 @@ class AckGetStreamResolution : public AckPayload<AckGetStreamResolution> {
bool fill_impl(const std::vector<std::uint8_t>& bytes) {

if (bytes.size() != len) {
std::cerr << "Length wrong: " << bytes.size() << " instead of " << len << '\n';
std::cerr << "Length wrong: " << bytes.size() << " instead of " << len << std::endl;
return false;
}

Expand All @@ -361,7 +361,7 @@ class AckGetStreamResolution : public AckPayload<AckGetStreamResolution> {
friend std::ostream& operator<<(std::ostream& str, const AckGetStreamResolution& self) {
str << "Resolution: " << self.resolution_l << "x" << self.resolution_h << '\n'
<< "Bitrate: " << self.video_bitrate_kbps << " kbps\n"
<< "Codec: " << (self.video_enc_type == 1 ? "H264" : "H265") << '\n';
<< "Codec: " << (self.video_enc_type == 1 ? "H264" : "H265") << std::endl;
return str;
}

Expand All @@ -388,7 +388,7 @@ class AckSetStreamSettings : public AckPayload<AckSetStreamSettings> {
bool fill_impl(const std::vector<std::uint8_t>& bytes) {

if (bytes.size() != len) {
std::cerr << "Length wrong: " << bytes.size() << " instead of " << len << '\n';
std::cerr << "Length wrong: " << bytes.size() << " instead of " << len << std::endl;
return false;
}

Expand Down

0 comments on commit e6f8875

Please sign in to comment.