Skip to content

Instantly share code, notes, and snippets.

@naga-karupi
Last active November 23, 2023 14:11
Show Gist options
  • Save naga-karupi/cb6d6fe9356c9604ed36f95ce7cb2906 to your computer and use it in GitHub Desktop.
Save naga-karupi/cb6d6fe9356c9604ed36f95ce7cb2906 to your computer and use it in GitHub Desktop.
linux実行環境とコード
cmake_minimum_required(VERSION 3.12)
project(mpc CXX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR})
if (NOT CMAKE_CONFIGURATION_TYPES AND
NOT CMAKE_NO_BUILD_TYPE AND
NOT CMAKE_BUILD_TYPE AND
CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
message(STATUS "[!] Setting build type to 'Release' as none was specified.")
set(CMAKE_BUILD_TYPE Release)
endif()
add_executable(mpc_test
Main.cpp
)
find_package(Siv3D)
target_link_libraries(mpc_test PUBLIC Siv3D::Siv3D)
target_compile_features(mpc_test PRIVATE cxx_std_20)
.
├── CMakeCache.txt
├── CMakeFiles
│ ├── 3.28.0-rc5
│ │ ├── CMakeCCompiler.cmake
│ │ ├── CMakeCXXCompiler.cmake
│ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ ├── CMakeSystem.cmake
│ │ ├── CompilerIdC
│ │ │ ├── a.out
│ │ │ ├── CMakeCCompilerId.c
│ │ │ └── tmp
│ │ └── CompilerIdCXX
│ │ ├── a.out
│ │ ├── CMakeCXXCompilerId.cpp
│ │ └── tmp
│ ├── cmake.check_cache
│ ├── CMakeConfigureLog.yaml
│ ├── CMakeDirectoryInformation.cmake
│ ├── Makefile2
│ ├── Makefile.cmake
│ ├── mpc_test.dir
│ │ ├── build.make
│ │ ├── cmake_clean.cmake
│ │ ├── compiler_depend.internal
│ │ ├── compiler_depend.make
│ │ ├── compiler_depend.ts
│ │ ├── DependInfo.cmake
│ │ ├── depend.make
│ │ ├── flags.make
│ │ ├── link.txt
│ │ ├── Main.cpp.o
│ │ ├── Main.cpp.o.d
│ │ └── progress.make
│ ├── pkgRedirects
│ ├── progress.marks
│ └── TargetDirectories.txt
├── cmake_install.cmake
└── Makefile
8 directories, 31 files
naganaga@naga ~/P/c/s/m/build> cd ../
naganaga@naga ~/P/c/s/mpc-test> tree
.
├── CMakeLists.txt
├── Main.cpp
├── PID.cpp
├── PID.h
└── resources
└── engine
├── font
│ ├── fontawesome
│ │ ├── fontawesome-brands.otf.zstdcmp
│ │ ├── fontawesome-solid.otf.zstdcmp
│ │ └── LICENSE.txt
│ ├── materialdesignicons
│ │ ├── license.md
│ │ └── materialdesignicons-webfont.ttf.zstdcmp
│ ├── min
│ │ ├── LICENSE
│ │ └── siv3d-min.woff
│ ├── mplus
│ │ ├── LICENSE_E
│ │ ├── mplus-1p-black.ttf.zstdcmp
│ │ ├── mplus-1p-bold.ttf.zstdcmp
│ │ ├── mplus-1p-heavy.ttf.zstdcmp
│ │ ├── mplus-1p-light.ttf.zstdcmp
│ │ ├── mplus-1p-medium.ttf.zstdcmp
│ │ ├── mplus-1p-regular.ttf.zstdcmp
│ │ └── mplus-1p-thin.ttf.zstdcmp
│ ├── noto-cjk
│ │ ├── LICENSE
│ │ └── NotoSansCJK-Regular.ttc.zstdcmp
│ └── noto-emoji
│ ├── LICENSE
│ ├── NotoColorEmoji.ttf.zstdcmp
│ └── NotoEmoji-Regular.ttf.zstdcmp
├── shader
│ ├── essl
│ │ ├── apply_srgb_curve.frag
│ │ ├── bitmapfont.frag
│ │ ├── copy.frag
│ │ ├── forward3d.frag
│ │ ├── forward3d.vert
│ │ ├── fullscreen_triangle.frag
│ │ ├── fullscreen_triangle.vert
│ │ ├── gaussian_blur_9.frag
│ │ ├── line3d.frag
│ │ ├── line3d.vert
│ │ ├── msdffont.frag
│ │ ├── msdffont_outline.frag
│ │ ├── msdffont_outlineshadow.frag
│ │ ├── msdffont_shadow.frag
│ │ ├── msdfprint.frag
│ │ ├── round_dot.frag
│ │ ├── sdffont.frag
│ │ ├── sdffont_outline.frag
│ │ ├── sdffont_outlineshadow.frag
│ │ ├── sdffont_shadow.frag
│ │ ├── shape.frag
│ │ ├── sky.frag
│ │ ├── sprite.vert
│ │ ├── square_dot.frag
│ │ └── texture.frag
│ └── glsl
│ ├── apply_srgb_curve.frag
│ ├── bitmapfont.frag
│ ├── copy.frag
│ ├── forward3d.frag
│ ├── forward3d.vert
│ ├── fullscreen_triangle.frag
│ ├── fullscreen_triangle.vert
│ ├── gaussian_blur_9.frag
│ ├── line3d.frag
│ ├── line3d.vert
│ ├── msdffont.frag
│ ├── msdffont_outline.frag
│ ├── msdffont_outlineshadow.frag
│ ├── msdffont_shadow.frag
│ ├── msdfprint.frag
│ ├── round_dot.frag
│ ├── sdffont.frag
│ ├── sdffont_outline.frag
│ ├── sdffont_outlineshadow.frag
│ ├── sdffont_shadow.frag
│ ├── shape.frag
│ ├── sky.frag
│ ├── sprite.vert
│ ├── square_dot.frag
│ └── texture.frag
├── soundfont
│ ├── GMGSx.sf2.txt
│ └── GMGSx.sf2.zstdcmp
└── texture
└── box-shadow
├── 128.png
├── 16.png
├── 256.png
├── 32.png
├── 64.png
└── 8.png
OpenSiv3D for Linux
0: [info] ℹ️ 2023-11-23 23:01:26
0: [info] ℹ️ Siv3D Engine (0.6.11) Linux [Release build]
0: [info] ℹ️ OS: Ubuntu 20.04.6 LTS (20.4.0.0)
17: [info] ℹ️ CPU: GenuineIntel 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz 6.140.1 (CPU packages: 1, Physical CPUs: 8, Logical CPUs: 8)
17: [info] ℹ️ Memory: [Physical Available: 9.14GiB Total: 15.3GiB] [Virtual: Available: 32TiB Total: 32TiB]
17: [info] ℹ️ Connected displays: 0
17: [info] ⌛ Preparing for setup...
17: [info] soundfontCacheDirectory: /home/naganaga/.cache/Siv3D/0.6.11/soundfont/
17: [info] ℹ️ Engine font `GMGSx.sf2` found in the user cache directory
17: [info] libmpg123 is available
99: [info] 🎧 Audio backend: MiniAudio (channel count: 2, sample rate: 44100, buffer size: 360)
101: [info] ℹ️ SoundTouch is available
101: [info] Default Screenshot directory: "/home/naganaga/Project/cpp/siv3d/mpc-test/build/Screenshot/"
101: [info] fontCacheDirectory: /home/naganaga/.cache/Siv3D/0.6.11/font/
101: [info] ℹ️ Engine font `noto-cjk/NotoSansCJK-Regular.ttc` found in the user cache directory
101: [info] ℹ️ Engine font `noto-emoji/NotoEmoji-Regular.ttf` found in the user cache directory
101: [info] ℹ️ Engine font `noto-emoji/NotoColorEmoji.ttf` found in the user cache directory
101: [info] ℹ️ Engine font `mplus/mplus-1p-thin.ttf` found in the user cache directory
101: [info] ℹ️ Engine font `mplus/mplus-1p-light.ttf` found in the user cache directory
101: [info] ℹ️ Engine font `mplus/mplus-1p-regular.ttf` found in the user cache directory
102: [info] ℹ️ Engine font `mplus/mplus-1p-medium.ttf` found in the user cache directory
102: [info] ℹ️ Engine font `mplus/mplus-1p-bold.ttf` found in the user cache directory
102: [info] ℹ️ Engine font `mplus/mplus-1p-heavy.ttf` found in the user cache directory
102: [info] ℹ️ Engine font `mplus/mplus-1p-black.ttf` found in the user cache directory
102: [info] ℹ️ Engine font `fontawesome/fontawesome-solid.otf` found in the user cache directory
102: [info] ℹ️ Engine font `fontawesome/fontawesome-brands.otf` found in the user cache directory
102: [info] ℹ️ Engine font `materialdesignicons/materialdesignicons-webfont.ttf` found in the user cache directory
#include <Siv3D/Siv3D.hpp>
#include "PID.h"
SIV3D_SET(EngineOption::Renderer::Headless) // Force non-graphical mode
using namespace s3d;
template<size_t s>
class Delayer
{
Array<Vec2> m_buffer{ s };
size_t m_counter = 0;
public:
Delayer() = default;
Vec2 operator()(Vec2 input)
{
m_buffer[m_counter] = input;
auto output = m_buffer[(s + m_counter - 1) % s];
m_counter++;
m_counter %= s;
return output;
}
};
class Robot
{
Vec2 m_accelerate{ 0.0, 0.0 };
Vec2 m_speed{ 0.0, 0.0 };
Vec2 m_position{ 0.0, 0.0 };
Vec2 m_jerk{ 0.0, 0.0 };
const double m_accel_limit;
const double m_speed_limit;
void CheckLimit(Vec2& accelerate, double t)
{
auto a_size = accelerate.length();
if (a_size > m_accel_limit)
{
accelerate = accelerate.normalized() * m_accel_limit;
}
auto v = m_speed - accelerate * t;
auto v_size = v.length();
if (v_size > m_speed_limit)
{
accelerate = Vec2{ 0, 0 };
}
}
public:
Robot(double accel_limit = 800.0, double speed_limit = 10.0)
: m_accel_limit(accel_limit), m_speed_limit(speed_limit)
{
}
~Robot() = default;
void Update(Vec2 accelerate, double t = 0.001)
{
this->m_jerk = (accelerate - this->m_accelerate) / t;
this->m_position += this->m_speed * t + 0.5 * this->m_accelerate * t * t;
this->m_speed += accelerate * t;
this->m_accelerate = accelerate;
if (this->m_speed.length() > this->m_speed_limit)
{
this->m_speed *= this->m_speed_limit / m_speed.length();
}
}
auto GetPosition() const noexcept -> Vec2
{
return m_position;
}
};
namespace globals
{
uint32 counter = 0;
Stopwatch sw;
Delayer<1> d;
Robot rbt;
Array<Vec2> target(0);
PID pid(50, 15, 1, 3);
}
void CalcRobot(std::atomic_bool& abort)
{
if (not globals::sw.isRunning())
{
globals::sw.start();
}
while (1)
{
if (abort)
{
return;
}
globals::counter += 10;
globals::counter %= 8000;
auto error = globals::target[globals::counter] - globals::rbt.GetPosition();
auto accel = error.normalized() * globals::pid(error.length());
globals::rbt.Update(globals::d(accel), 0.02);
System::Sleep(20ms);
}
}
void ToLineString(const Array<double>& values_x, const Array<double>& values_y, const Vec2& start, LineString& ls, double scale)
{
ls.resize(values_x.size());
for (size_t i = 0; i < values_x.size(); ++i)
{
ls[i] = (start + Vec2{ values_x[i] * scale, (values_y[i] * -scale)});
}
}
void Main()
{
Scene::SetBackground(Palette::White);
constexpr size_t N = 800;
constexpr size_t N_point = 8000;
globals::target.resize(N_point + 1);
constexpr double theta_start = 0.0;
constexpr double theta_end = 2.0 * Math::Pi;
constexpr double theta_step = (theta_end - theta_start) / N;
Array<double> values_x(N + 1);
Array<double> values_y(N + 1);
for (size_t i = 0; i < N + 1; i++)
{
const double theta = theta_start + theta_step * i;
values_x.at(i) = 4 * Math::Sin(3 * theta);
values_y.at(i) = 4 * Math::Sin(4 * theta);
}
constexpr double theta_step_point = (theta_end - theta_start) / N_point;
for (size_t i = 0; i < N_point; i++)
{
const double theta = theta_start + theta_step_point * i;
globals::target.at(i).x = 4 * Math::Sin(3 * theta);
globals::target.at(i).y = 4 * Math::Sin(4 * theta);
}
LineString ls(N + 1);
ToLineString(values_x, values_y, Vec2{400, 300}, ls, 50);
AsyncTask<void> task;
std::atomic_bool abort{ false };
task = Async(CalcRobot, std::ref(abort));
while (System::Update())
{
ls.draw(Palette::Blue);
Circle(globals::target.at(globals::counter).x * 50 + 400, globals::target.at(globals::counter).y * 50 + 300, 5).draw(Palette::Red);
Circle(globals::rbt.GetPosition().x * 50 + 400, globals::rbt.GetPosition().y * 50 + 300, 5).draw(Palette::Green);
}
abort = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment