C++ tutorial

Install dependencies

In this tutorial, we use CMake to build the program.

Build first program

First, open a terminal and prepare a directory for the sample.

mkdir autd3-sample
cd autd3-sample

Then, make CMakeLists.txt and main.cpp files.

└─autd3-sample
        CMakeLists.txt
        main.cpp

Next, edit CMakeLists.txt as follows,

cmake_minimum_required(VERSION 3.21)

project(autd3-sample)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24.0") 
  cmake_policy(SET CMP0135 NEW)
endif()

include(FetchContent)
if(WIN32)
  if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "AMD64")
    FetchContent_Declare(
      autd3
      URL https://github.com/shinolab/autd3-cpp/releases/download/v29.0.0-rc.16/autd3-v29.0.0-rc.16-win-x64.zip
    )
    FetchContent_Declare(
      autd3-link-soem
      URL https://github.com/shinolab/autd3-cpp-link-soem/releases/download/v29.0.0-rc.16/autd3-link-soem-v29.0.0-rc.16-win-x64.zip
    )
  elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "ARM64")
    FetchContent_Declare(
      autd3
      URL https://github.com/shinolab/autd3-cpp/releases/download/v29.0.0-rc.16/autd3-v29.0.0-rc.16-win-arm.zip
    )
    FetchContent_Declare(
      autd3-link-soem
      URL https://github.com/shinolab/autd3-cpp-link-soem/releases/download/v29.0.0-rc.16/autd3-link-soem-v29.0.0-rc.16-win-arm.zip
    )
  else()
      message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_PROCESSOR}")
  endif()
elseif(APPLE)
  FetchContent_Declare(
    autd3
    URL https://github.com/shinolab/autd3-cpp/releases/download/v29.0.0-rc.16/autd3-v29.0.0-rc.16-macos-aarch64.tar.gz
  )
  FetchContent_Declare(
    autd3-link-soem
    URL https://github.com/shinolab/autd3-cpp-link-soem/releases/download/v29.0.0-rc.16/autd3-link-soem-v29.0.0-rc.16-macos-aarch64.tar.gz
  )
else()
  FetchContent_Declare(
    autd3
    URL https://github.com/shinolab/autd3-cpp/releases/download/v29.0.0-rc.16/autd3-v29.0.0-rc.16-linux-x64.tar.gz
  )
  FetchContent_Declare(
    autd3-link-soem
    URL https://github.com/shinolab/autd3-cpp-link-soem/releases/download/v29.0.0-rc.16/autd3-link-soem-v29.0.0-rc.16-linux-x64.tar.gz
  )
endif()
set(USE_SYSTEM_EIGEN OFF)
FetchContent_MakeAvailable(autd3 autd3-link-soem)

add_executable(main main.cpp)

target_link_libraries(main PRIVATE autd3::autd3 autd3::link::soem)

if (CMAKE_GENERATOR MATCHES "Visual Studio")
    set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT main)
endif()

And, edit main.cpp as follows. This is the source code for generating a focus with AM modulation.

#include <autd3_link_soem.hpp>
#include <iostream>

#include "autd3.hpp"

int main() try {
  auto autd =
      autd3::ControllerBuilder({autd3::AUTD3(autd3::Point3::origin())})
          .open(autd3::link::SOEM::builder().with_err_handler(
              [](const uint16_t slave, const autd3::link::Status status) {
                std::cout << "slave [" << slave << "]: " << status << std::endl;
                if (status == autd3::link::Status::Lost()) exit(-1);
              }));

  const auto firm_version = autd.firmware_version();
  std::copy(firm_version.begin(), firm_version.end(),
            std::ostream_iterator<autd3::FirmwareVersion>(std::cout, "\n"));

  autd.send(autd3::Silencer());

  const autd3::Point3 focus = autd.center() + autd3::Vector3(0, 0, 150);
  autd3::gain::Focus g(focus);

  autd3::modulation::Sine m(150 * autd3::Hz);

  autd.send((m, g));

  std::cout << "press enter to finish..." << std::endl;
  std::cin.ignore();

  autd.close();

  return 0;
} catch (std::exception& ex) {
  std::cerr << ex.what() << std::endl;
}

Then, build with CMake.

mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . --config Release

Finally, run the program.

.\Release\main.exe
sudo ./main