Skip to content

Instantly share code, notes, and snippets.

@hsnks100
Created July 27, 2018 00:37
Show Gist options
  • Save hsnks100/c54cbef7d91e3a8d77e443c55aaf64c1 to your computer and use it in GitHub Desktop.
Save hsnks100/c54cbef7d91e3a8d77e443c55aaf64c1 to your computer and use it in GitHub Desktop.
가감속
std::shared_ptr<boost::asio::deadline_timer> m_motorTimer;
FromToWithDuration2<float> fromLeft(현재값, 목표값, 1000);
auto self = shared_from_this();
m_motorTimer = createSchedule(m_ioService, 100, [this, self, 목표값, 증감값(=맥스출력/10)]()->bool{
bool res = fromLeft.stepValue(증감값);
if(res == false)
return res;
m_leftMotorValue = fromLeft.getValue();
모터돌리기(
m_leftMotorValue);
return true;
});
#include "scheduler.h"
#include <iostream>
#include <boost/asio.hpp>
using namespace boost;
void scheduler(std::shared_ptr<asio::deadline_timer> t, std::function<bool()> task, long delta) {
t->expires_from_now(posix_time::milliseconds(delta));
t->async_wait([t, task, delta](const boost::system::error_code& error) {
if (!error)
{
auto res = task();
if(res) {
scheduler(t, task, delta);
}
}
});
}
std::shared_ptr<asio::deadline_timer> createSchedule(asio::io_service& ios, long delta, std::function<bool()> task) {
auto res = task();
auto t = std::make_shared<asio::deadline_timer>(ios);
if(res) {
scheduler(t, task, delta);
}
return t;
}
std::shared_ptr<asio::deadline_timer> createDelay(asio::io_service& ios, long delta, std::function<void()> task) {
auto t = std::make_shared<asio::deadline_timer>(ios);
t->expires_from_now(posix_time::milliseconds(delta));
t->async_wait([t, task, delta](const boost::system::error_code& error) {
if (!error)
{
task();
}
});
return t;
}
#pragma once
#include <iostream>
#include <boost/asio.hpp>
using namespace boost;
void scheduler(std::shared_ptr<asio::deadline_timer> t, std::function<bool()> task, long delta);
std::shared_ptr<asio::deadline_timer> createSchedule(asio::io_service& ios, long delta, std::function<bool()> task);
std::shared_ptr<asio::deadline_timer> createDelay(asio::io_service& ios, long delta, std::function<void()> task);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment