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:
Jacob Perron 2020-11-01 18:05:17 -08:00 committed by GitHub
parent 850b011a55
commit 2b9591f0f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 95 additions and 87 deletions

View file

@ -32,9 +32,9 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef CREATE_H
#define CREATE_H
#include <boost/shared_ptr.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <chrono>
#include <memory>
#include <string>
#include <unistd.h>
@ -96,8 +96,8 @@ namespace create {
bool updateLEDs();
protected:
boost::shared_ptr<create::Data> data;
boost::shared_ptr<create::Serial> serial;
std::shared_ptr<create::Data> data;
std::shared_ptr<create::Serial> serial;
public:
/**

View file

@ -32,9 +32,8 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef CREATE_DATA_H
#define CREATE_DATA_H
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <map>
#include <memory>
#include <vector>
#include "create/packet.h"
@ -43,7 +42,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace create {
class Data {
private:
std::map<uint8_t, boost::shared_ptr<Packet> > packets;
std::map<uint8_t, std::shared_ptr<Packet> > packets;
uint32_t totalDataBytes;
std::vector<uint8_t> ids;
@ -52,7 +51,7 @@ namespace create {
~Data();
bool isValidPacketID(const uint8_t id) const;
boost::shared_ptr<Packet> getPacket(const uint8_t id);
std::shared_ptr<Packet> getPacket(const uint8_t id);
void validateAll();
uint32_t getTotalDataBytes() const;
uint8_t getNumPackets() const;

View file

@ -31,15 +31,15 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef CREATE_PACKET_H
#define CREATE_PACKET_H
#include <boost/thread/mutex.hpp>
#include <mutex>
namespace create {
class Packet {
private:
uint16_t data;
uint16_t tmpData;
mutable boost::mutex dataMutex;
mutable boost::mutex tmpDataMutex;
mutable std::mutex dataMutex;
mutable std::mutex tmpDataMutex;
protected:
// Thread safe

View file

@ -35,19 +35,20 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef CREATE_SERIAL_H
#define CREATE_SERIAL_H
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "create/data.h"
#include "create/types.h"
#include "create/util.h"
namespace create {
class Serial : public boost::enable_shared_from_this<Serial> {
class Serial : public std::enable_shared_from_this<Serial> {
protected:
boost::asio::io_service io;
@ -55,9 +56,9 @@ namespace create {
boost::asio::serial_port port;
private:
boost::thread ioThread;
boost::condition_variable dataReadyCond;
boost::mutex dataReadyMut;
std::thread ioThread;
std::condition_variable dataReadyCond;
std::mutex dataReadyMut;
bool dataReady;
bool isReading;
bool firstRead;
@ -66,13 +67,13 @@ namespace create {
// Callback executed when data arrives from Create
void onData(const boost::system::error_code& e, const std::size_t& size);
// Callback to execute once data arrives
boost::function<void()> callback;
std::function<void()> callback;
// Start and stop reading data from Create
bool startReading();
void stopReading();
protected:
boost::shared_ptr<Data> data;
std::shared_ptr<Data> data;
// These are for possible diagnostics
uint64_t corruptPackets;
uint64_t totalPackets;
@ -85,9 +86,9 @@ namespace create {
void notifyDataReady();
public:
Serial(boost::shared_ptr<Data> data);
Serial(std::shared_ptr<Data> data);
~Serial();
bool connect(const std::string& port, const int& baud = 115200, boost::function<void()> cb = 0);
bool connect(const std::string& port, const int& baud = 115200, std::function<void()> cb = 0);
void disconnect();
inline bool connected() const { return port.is_open(); };
bool send(const uint8_t* bytes, const uint32_t numBytes);

View file

@ -36,11 +36,9 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef CREATE_SERIAL_QUERY_H
#define CREATE_SERIAL_QUERY_H
#include <memory>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include "create/data.h"
#include "create/types.h"
@ -69,7 +67,7 @@ namespace create {
void processByte(uint8_t byteRead);
public:
SerialQuery(boost::shared_ptr<Data> data);
SerialQuery(std::shared_ptr<Data> data);
};
} // namespace create

View file

@ -35,11 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef CREATE_SERIAL_STREAM_H
#define CREATE_SERIAL_STREAM_H
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#include <memory>
#include "create/data.h"
#include "create/types.h"
@ -73,7 +69,7 @@ namespace create {
void processByte(uint8_t byteRead);
public:
SerialStream(boost::shared_ptr<Data> data, const uint8_t& header = create::util::STREAM_HEADER);
SerialStream(std::shared_ptr<Data> data, const uint8_t& header = create::util::STREAM_HEADER);
};
} // namespace create

View file

@ -32,6 +32,8 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef CREATE_UTIL_H
#define CREATE_UTIL_H
#include <cmath>
#define COUT(prefix,msg) (std::cout<<prefix<<msg<<std::endl)
#define CERR(prefix,msg) (std::cerr<<prefix<<msg<<std::endl)

View file

@ -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;

View file

@ -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();

View file

@ -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;
}

View file

@ -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) {

View file

@ -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) {

View file

@ -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() {

View file

@ -33,8 +33,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include <boost/shared_ptr.hpp>
TEST(CreateTest, ConstructorSingleParam)
{
create::Create create_default;

View file

@ -34,7 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include <boost/shared_ptr.hpp>
#include <memory>
TEST(DataTest, Constructor)
{
@ -76,31 +76,31 @@ TEST(DataTest, GetPacket)
{
// Get a packet exclusive to V_1
create::Data data_v_1(create::V_1);
boost::shared_ptr<create::Packet> v_1_packet_ptr = data_v_1.getPacket(create::ID_OVERCURRENTS);
EXPECT_NE(v_1_packet_ptr, boost::shared_ptr<create::Packet>())
std::shared_ptr<create::Packet> v_1_packet_ptr = data_v_1.getPacket(create::ID_OVERCURRENTS);
EXPECT_NE(v_1_packet_ptr, std::shared_ptr<create::Packet>())
<< "ID_OVERCURRENTS packet not found for protocol V_1";
EXPECT_EQ(static_cast<int>(v_1_packet_ptr->nbytes), 1);
EXPECT_EQ(v_1_packet_ptr->info, std::string("overcurrents"));
// Get a packet for V_2
create::Data data_v_2(create::V_2);
boost::shared_ptr<create::Packet> v_2_packet_ptr = data_v_2.getPacket(create::ID_DISTANCE);
EXPECT_NE(v_2_packet_ptr, boost::shared_ptr<create::Packet>())
std::shared_ptr<create::Packet> v_2_packet_ptr = data_v_2.getPacket(create::ID_DISTANCE);
EXPECT_NE(v_2_packet_ptr, std::shared_ptr<create::Packet>())
<< "ID_DISTANCE packet not found for protocol V_2";
EXPECT_EQ(static_cast<int>(v_2_packet_ptr->nbytes), 2);
EXPECT_EQ(v_2_packet_ptr->info, std::string("distance"));
// Get a packet exclusive to V_3
create::Data data_v_3(create::V_3);
boost::shared_ptr<create::Packet> v_3_packet_ptr = data_v_3.getPacket(create::ID_LIGHT_FRONT_RIGHT);
EXPECT_NE(v_3_packet_ptr, boost::shared_ptr<create::Packet>())
std::shared_ptr<create::Packet> v_3_packet_ptr = data_v_3.getPacket(create::ID_LIGHT_FRONT_RIGHT);
EXPECT_NE(v_3_packet_ptr, std::shared_ptr<create::Packet>())
<< "ID_LIGHT_FRONT_RIGHT packet not found for protocol V_3";
EXPECT_EQ(static_cast<int>(v_3_packet_ptr->nbytes), 2);
EXPECT_EQ(v_3_packet_ptr->info, std::string("light_bumper_front_right"));
// Get a non-existent packet
boost::shared_ptr<create::Packet> not_a_packet_ptr = data_v_3.getPacket(60);
EXPECT_EQ(not_a_packet_ptr, boost::shared_ptr<create::Packet>());
std::shared_ptr<create::Packet> not_a_packet_ptr = data_v_3.getPacket(60);
EXPECT_EQ(not_a_packet_ptr, std::shared_ptr<create::Packet>());
}
TEST(DataTest, GetPacketIDs)

View file

@ -33,17 +33,17 @@ POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include <boost/shared_ptr.hpp>
#include <memory>
TEST(SerialQueryTest, Constructor)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialQuery serial_query(data_ptr);
}
TEST(SerialQueryTest, Connected)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialQuery serial_query(data_ptr);
// Did not call connect and nothing to connect to, so expect false
@ -52,7 +52,7 @@ TEST(SerialQueryTest, Connected)
TEST(SerialQueryTest, Disconnect)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialQuery serial_query(data_ptr);
// Not connected, but should not fail
@ -61,7 +61,7 @@ TEST(SerialQueryTest, Disconnect)
TEST(SerialQueryTest, NumPackets)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialQuery serial_query(data_ptr);
// Not connected, so zero packets should have been received
@ -71,7 +71,7 @@ TEST(SerialQueryTest, NumPackets)
TEST(SerialQueryTest, Send)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialQuery serial_query(data_ptr);
// Some bytes to send (to set date)
@ -82,7 +82,7 @@ TEST(SerialQueryTest, Send)
TEST(SerialQueryTest, SendOpcode)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialQuery serial_query(data_ptr);
// Not connected, so failure expected

View file

@ -33,17 +33,17 @@ POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include <boost/shared_ptr.hpp>
#include <memory>
TEST(SerialStreamTest, Constructor)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialStream serial_stream(data_ptr);
}
TEST(SerialStreamTest, Connected)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialStream serial_stream(data_ptr);
// Did not call connect and nothing to connect to, so expect false
@ -52,7 +52,7 @@ TEST(SerialStreamTest, Connected)
TEST(SerialStreamTest, Disconnect)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialStream serial_stream(data_ptr);
// Not connected, but should not fail
@ -61,7 +61,7 @@ TEST(SerialStreamTest, Disconnect)
TEST(SerialStreamTest, NumPackets)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialStream serial_stream(data_ptr);
// Not connected, so zero packets should have been received
@ -71,7 +71,7 @@ TEST(SerialStreamTest, NumPackets)
TEST(SerialStreamTest, Send)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialStream serial_stream(data_ptr);
// Some bytes to send (to set date)
@ -82,7 +82,7 @@ TEST(SerialStreamTest, Send)
TEST(SerialStreamTest, SendOpcode)
{
boost::shared_ptr<create::Data> data_ptr = boost::make_shared<create::Data>();
std::shared_ptr<create::Data> data_ptr = std::make_shared<create::Data>();
create::SerialStream serial_stream(data_ptr);
// Not connected, so failure expected