Foxglove SDK
The Foxglove SDK allows you to quickly and easily:
- Stream and visualize your robot data live in Foxglove
- Log your data to MCAP files
The SDK is available for C++, Python, and Rust under the MIT license.
This documentation provides an overview of the SDK to help you get started. For more detailed information, see the additional resources for each language below.
Installation
- Rust
- Python
- C++
Install foxglove from crates.io: https://crates.io/crates/foxglove
cargo add foxglove
Install foxglove-sdk from PyPI: https://pypi.org/project/foxglove-sdk
pip install foxglove-sdk
The C++ SDK is a wrapper around a C library. To build it, you will need to link that library and compile the SDK source as part of your build process. The SDK assumes C++17 or newer.
Download the library, source, and header files for your platform from the SDK release assets. The SDK release ships a CMake package config (under lib/cmake/foxglove-sdk/) that you wire up with find_package.
If you're using CMake, you can use the following as a starting point. For more detail, see the quickstart example.
cmake_minimum_required(VERSION 3.20)
# TODO: replace "my_program" throughout with the name of your project
project(my_program LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
# Fetch the Foxglove SDK release archive.
include(FetchContent)
FetchContent_Declare(
foxglove_sdk
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
# TODO: Copy the URL and SHA for the appropriate .zip file from here:
# https://github.com/foxglove/foxglove-sdk/releases?q=sdk%2F&expanded=true
URL <download URL from the GitHub release>
URL_HASH SHA256=<sha from the GitHub release>
)
FetchContent_MakeAvailable(foxglove_sdk)
# Load the SDK's CMake package config, which defines IMPORTED targets for the
# prebuilt C libraries and the foxglove_sdk_add_cpp_library() helper.
find_package(foxglove-sdk CONFIG REQUIRED HINTS "${foxglove_sdk_SOURCE_DIR}")
# Compile the SDK's C++ wrapper sources against your toolchain and link the
# appropriate C library.
foxglove_sdk_add_cpp_library(foxglove_cpp TYPE STATIC)
# Assuming the project consists of a single source file, main.cpp.
add_executable(my_program main.cpp)
target_link_libraries(my_program PRIVATE foxglove_cpp)
Getting started
Here's a minimal example that sends Log messages to the Foxglove app.
When you run this example and open a Log panel in the Foxglove app, you'll see messages logged a few times per second.
- Rust
- Python
- C++
use foxglove::{
WebSocketServer, log,
messages::{Log, Timestamp, log::Level},
};
use std::{thread, time::Duration};
fn main() {
WebSocketServer::new()
.start_blocking()
.expect("Server failed to start");
loop {
log!(
"/hello",
Log {
level: Level::Info.into(),
timestamp: Some(Timestamp::now()),
message: "Hello, Foxglove!".to_string(),
..Default::default()
}
);
thread::sleep(Duration::from_millis(100));
}
}
import foxglove
import time
from foxglove.messages import Log, LogLevel, Timestamp
server = foxglove.start_server()
while True:
foxglove.log(
"/hello",
Log(
timestamp=Timestamp.now(),
level=LogLevel.Info,
message="Hello, Foxglove!",
)
)
time.sleep(0.033)
#include <foxglove/server.hpp>
#include <foxglove/messages.hpp>
#include <thread>
int main(int argc, const char *argv[])
{
foxglove::WebSocketServerOptions ws_options;
auto server = foxglove::WebSocketServer::create(std::move(ws_options)).value();
auto channel = foxglove::messages::LogChannel::create("/hello").value();
while (true)
{
const auto now = std::chrono::system_clock::now();
const auto nanos_since_epoch = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
const auto seconds_since_epoch = nanos_since_epoch / 1000000000;
const auto remaining_nanos = nanos_since_epoch % 1000000000;
foxglove::messages::Log log;
log.level = foxglove::messages::Log::LogLevel::INFO;
log.message = "Hello, Foxglove!";
log.timestamp = foxglove::Timestamp{
static_cast<uint32_t>(seconds_since_epoch),
static_cast<uint32_t>(remaining_nanos)};
channel.log(log);
std::this_thread::sleep_for(std::chrono::milliseconds(33));
}
return 0;
}
Reference
Reference API documentation, additional examples, and other resources for each SDK language.
- Rust
- Python
- C++