Replace boost features with C++11 equivalents (#58)
* Replace boost features with C++11 equivalents Signed-off-by: Jacob Perron <jacob@openrobotics.org> * Include <cmath> in util.h Needed for std::abs Signed-off-by: Jacob Perron <jacob@openrobotics.org>
This commit is contained in:
parent
850b011a55
commit
2b9591f0f7
17 changed files with 95 additions and 87 deletions
|
@ -1,8 +1,7 @@
|
|||
#include <boost/bind.hpp>
|
||||
#include <boost/make_shared.hpp>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
#include <assert.h>
|
||||
|
||||
#include "create/create.h"
|
||||
|
@ -42,11 +41,11 @@ namespace create {
|
|||
poseCovar = Matrix(3, 3, 0.0);
|
||||
requestedLeftVel = 0;
|
||||
requestedRightVel = 0;
|
||||
data = boost::shared_ptr<Data>(new Data(model.getVersion()));
|
||||
data = std::shared_ptr<Data>(new Data(model.getVersion()));
|
||||
if (model.getVersion() == V_1) {
|
||||
serial = boost::make_shared<SerialQuery>(data);
|
||||
serial = std::make_shared<SerialQuery>(data);
|
||||
} else {
|
||||
serial = boost::make_shared<SerialStream>(data);
|
||||
serial = std::make_shared<SerialStream>(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -273,7 +272,7 @@ namespace create {
|
|||
float maxWait = 30; // seconds
|
||||
float retryInterval = 5; //seconds
|
||||
time(&start);
|
||||
while (!serial->connect(port, baud, boost::bind(&Create::onData, this)) && !timeout) {
|
||||
while (!serial->connect(port, baud, std::bind(&Create::onData, this)) && !timeout) {
|
||||
time(&now);
|
||||
if (difftime(now, start) > maxWait) {
|
||||
timeout = true;
|
||||
|
|
10
src/data.cpp
10
src/data.cpp
|
@ -1,6 +1,6 @@
|
|||
#include "create/data.h"
|
||||
|
||||
#define ADD_PACKET(id,nbytes,info,enabledVersion) if ((enabledVersion) & version) packets[id]=boost::make_shared<Packet>(nbytes,info)
|
||||
#define ADD_PACKET(id,nbytes,info,enabledVersion) if ((enabledVersion) & version) packets[id]=std::make_shared<Packet>(nbytes,info)
|
||||
|
||||
namespace create {
|
||||
|
||||
|
@ -45,7 +45,7 @@ namespace create {
|
|||
ADD_PACKET(ID_STASIS, 1, "stasis", V_3);
|
||||
|
||||
totalDataBytes = 0;
|
||||
for (std::map<uint8_t, boost::shared_ptr<Packet> >::iterator it = packets.begin();
|
||||
for (std::map<uint8_t, std::shared_ptr<Packet> >::iterator it = packets.begin();
|
||||
it != packets.end();
|
||||
++it) {
|
||||
ids.push_back(it->first);
|
||||
|
@ -62,15 +62,15 @@ namespace create {
|
|||
return false;
|
||||
}
|
||||
|
||||
boost::shared_ptr<Packet> Data::getPacket(uint8_t id) {
|
||||
std::shared_ptr<Packet> Data::getPacket(uint8_t id) {
|
||||
if (isValidPacketID(id)) {
|
||||
return packets[id];
|
||||
}
|
||||
return boost::shared_ptr<Packet>();
|
||||
return std::shared_ptr<Packet>();
|
||||
}
|
||||
|
||||
void Data::validateAll() {
|
||||
for (std::map<uint8_t, boost::shared_ptr<Packet> >::iterator it = packets.begin();
|
||||
for (std::map<uint8_t, std::shared_ptr<Packet> >::iterator it = packets.begin();
|
||||
it != packets.end();
|
||||
++it) {
|
||||
it->second->validate();
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#include <memory>
|
||||
|
||||
#include "create/packet.h"
|
||||
|
||||
namespace create {
|
||||
|
@ -11,22 +13,22 @@ namespace create {
|
|||
Packet::~Packet() { }
|
||||
|
||||
void Packet::setDataToValidate(const uint16_t& tmp) {
|
||||
boost::mutex::scoped_lock lock(tmpDataMutex);
|
||||
std::lock_guard<std::mutex> lock(tmpDataMutex);
|
||||
tmpData = tmp;
|
||||
}
|
||||
|
||||
void Packet::validate() {
|
||||
boost::mutex::scoped_lock lock(tmpDataMutex);
|
||||
std::lock_guard<std::mutex> lock(tmpDataMutex);
|
||||
setData(tmpData);
|
||||
}
|
||||
|
||||
void Packet::setData(const uint16_t& d) {
|
||||
boost::mutex::scoped_lock lock(dataMutex);
|
||||
std::lock_guard<std::mutex> lock(dataMutex);
|
||||
data = d;
|
||||
}
|
||||
|
||||
uint16_t Packet::getData() const {
|
||||
boost::mutex::scoped_lock lock(dataMutex);
|
||||
std::lock_guard<std::mutex> lock(dataMutex);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
#include "create/serial.h"
|
||||
|
@ -5,7 +7,7 @@
|
|||
|
||||
namespace create {
|
||||
|
||||
Serial::Serial(boost::shared_ptr<Data> d) :
|
||||
Serial::Serial(std::shared_ptr<Data> d) :
|
||||
signals(io, SIGINT, SIGTERM),
|
||||
port(io),
|
||||
dataReady(false),
|
||||
|
@ -31,7 +33,7 @@ namespace create {
|
|||
}
|
||||
}
|
||||
|
||||
bool Serial::connect(const std::string& portName, const int& baud, boost::function<void()> cb) {
|
||||
bool Serial::connect(const std::string& portName, const int& baud, std::function<void()> cb) {
|
||||
using namespace boost::asio;
|
||||
port.open(portName);
|
||||
port.set_option(serial_port::baud_rate(baud));
|
||||
|
@ -40,7 +42,7 @@ namespace create {
|
|||
port.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
|
||||
port.set_option(serial_port::flow_control(serial_port::flow_control::none));
|
||||
|
||||
signals.async_wait(boost::bind(&Serial::signalHandler, this, _1, _2));
|
||||
signals.async_wait(std::bind(&Serial::signalHandler, this, std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
usleep(1000000);
|
||||
|
||||
|
@ -90,17 +92,22 @@ namespace create {
|
|||
// Start continuously reading one byte at a time
|
||||
boost::asio::async_read(port,
|
||||
boost::asio::buffer(&byteRead, 1),
|
||||
boost::bind(&Serial::onData, shared_from_this(), _1, _2));
|
||||
std::bind(&Serial::onData,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
|
||||
ioThread = boost::thread(boost::bind(&boost::asio::io_service::run, &io));
|
||||
ioThread = std::thread(std::bind(
|
||||
static_cast<std::size_t(boost::asio::io_service::*)(void)>(
|
||||
&boost::asio::io_service::run), &io));
|
||||
|
||||
// Wait for first complete read to finish
|
||||
boost::unique_lock<boost::mutex> lock(dataReadyMut);
|
||||
std::unique_lock<std::mutex> lock(dataReadyMut);
|
||||
|
||||
int attempts = 1;
|
||||
int maxAttempts = 10;
|
||||
while (!dataReady) {
|
||||
if (!dataReadyCond.timed_wait(lock, boost::get_system_time() + boost::posix_time::milliseconds(500))) {
|
||||
if (dataReadyCond.wait_for(lock, std::chrono::milliseconds(500)) == std::cv_status::timeout) {
|
||||
if (attempts >= maxAttempts) {
|
||||
CERR("[create::Serial] ", "failed to receive data from Create. Check if robot is powered!");
|
||||
io.stop();
|
||||
|
@ -125,7 +132,7 @@ namespace create {
|
|||
ioThread.join();
|
||||
isReading = false;
|
||||
{
|
||||
boost::lock_guard<boost::mutex> lock(dataReadyMut);
|
||||
std::lock_guard<std::mutex> lock(dataReadyMut);
|
||||
dataReady = false;
|
||||
}
|
||||
}
|
||||
|
@ -138,7 +145,7 @@ namespace create {
|
|||
|
||||
// Notify first data packets ready
|
||||
{
|
||||
boost::lock_guard<boost::mutex> lock(dataReadyMut);
|
||||
std::lock_guard<std::mutex> lock(dataReadyMut);
|
||||
if (!dataReady) {
|
||||
dataReady = true;
|
||||
dataReadyCond.notify_one();
|
||||
|
@ -163,7 +170,10 @@ namespace create {
|
|||
// Read the next byte
|
||||
boost::asio::async_read(port,
|
||||
boost::asio::buffer(&byteRead, 1),
|
||||
boost::bind(&Serial::onData, shared_from_this(), _1, _2));
|
||||
std::bind(&Serial::onData,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
bool Serial::send(const uint8_t* bytes, unsigned int numBytes) {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "create/serial_query.h"
|
||||
#include "create/types.h"
|
||||
|
@ -7,7 +8,7 @@
|
|||
|
||||
namespace create {
|
||||
|
||||
SerialQuery::SerialQuery(boost::shared_ptr<Data> d) : Serial(d),
|
||||
SerialQuery::SerialQuery(std::shared_ptr<Data> d) : Serial(d),
|
||||
streamRecoveryTimer(io),
|
||||
packetID(ID_BUMP_WHEELDROP),
|
||||
packetByte(0),
|
||||
|
@ -30,7 +31,8 @@ namespace create {
|
|||
send(requestPacket, 2);
|
||||
// Automatically resend request if no response is received
|
||||
streamRecoveryTimer.expires_from_now(boost::posix_time::milliseconds(50));
|
||||
streamRecoveryTimer.async_wait(boost::bind(&SerialQuery::restartSensorStream, this, _1));
|
||||
streamRecoveryTimer.async_wait(
|
||||
std::bind(&SerialQuery::restartSensorStream, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void SerialQuery::restartSensorStream(const boost::system::error_code& err) {
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "create/serial_stream.h"
|
||||
#include "create/types.h"
|
||||
|
||||
namespace create {
|
||||
|
||||
SerialStream::SerialStream(boost::shared_ptr<Data> d, const uint8_t& header) : Serial(d), readState(READ_HEADER), headerByte(header) {
|
||||
SerialStream::SerialStream(std::shared_ptr<Data> d, const uint8_t& header) : Serial(d), readState(READ_HEADER), headerByte(header) {
|
||||
}
|
||||
|
||||
bool SerialStream::startSensorStream() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue