Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #45 from forno/upgrade/1.4.0
Browse files Browse the repository at this point in the history
Upgrade version 1.4.0
  • Loading branch information
forno authored Jan 27, 2017
2 parents 5b9ec8e + eb30794 commit b8965f5
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 64 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cmake_minimum_required(VERSION 2.8.12.1)
project(ics3 C CXX)

set(PROJECT_VER_MAJOR 1)
set(PROJECT_VER_MINOR 3)
set(PROJECT_VER_PATCH 2)
set(PROJECT_VER_MINOR 4)
set(PROJECT_VER_PATCH 0)
set(PROJECT_VER "${PROJECT_VER_MAJOR}.${PROJECT_VER_MINOR}.${PROJECT_VER_PATCH}")
set(PROJECT_APIVER "${PROJECT_VER_MAJOR}.${PROJECT_VER_MINOR}")

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ that suport ICS3.0 or newer serial communication.
ex)[KRS series servo moters produced by KONDO KAGAKU co., ltd.](http://kondo-robot.com/product-category/servomotor/krs)

## Caution
1. This software require c++11 compiler. In ubuntu 14.04 or after version have compiler support c++11.
1. This software require c++11 compiler. In ubuntu 14.04 or newer version have compiler support c++11.
2. This software only support serial mode. You should lock the serial mode for never turn on PWM mode.
3. Linux need serial device that mounted for use it. `script/setup.sh` will mount it to `/dev/ttyUSB0`.

Expand Down
4 changes: 3 additions & 1 deletion include/core.h → include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ class Core
using Container = std::vector<value_type>;
using IDContainerTx = std::array<value_type, 4>;
using IDContainerRx = std::array<value_type, 5>;

explicit Core(const std::string&, speed_t); // touch by only libics3
~Core() noexcept;
Core(const Core&) = delete;
Core& operator=(const Core&) = delete;
Core(Core&&) noexcept;
Core& operator=(Core&&) noexcept;

static std::shared_ptr<Core> getCore(const std::string&, speed_t);
static std::unique_ptr<Core> getCore(const std::string&, speed_t);

void communicate(const Container&, Container&);
void communicateID(const IDContainerTx&, IDContainerRx&);
private:
Expand Down
10 changes: 7 additions & 3 deletions include/ics3/angle.h → include/ics3/angle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef LIBICS3_ICS3_ANGLE_H
#define LIBICS3_ICS3_ANGLE_H

#include "ics3/check_invalid.h"
#include "ics3/check_invalid.hpp"

namespace ics
{
class ICS3;

class Angle
{
friend ICS3; // for touch rawData
public:
using rawType = uint16_t;
using type = double;

static constexpr Angle newDegree(type = 0.0);
static constexpr Angle newRadian(type = 0.0);
static constexpr Angle newSameUnit(const Angle&, type = 0.0);
Expand All @@ -49,6 +51,7 @@ class Angle

Angle(const Angle&) noexcept = default;
Angle& operator=(const Angle&) noexcept;

constexpr type get() const noexcept;
constexpr operator type() const noexcept;
void set(type);
Expand All @@ -57,6 +60,7 @@ class Angle
void setRaw(rawType);
private:
constexpr Angle(type, type); // non explicit, user cannot touch this

static constexpr rawType castToRaw(type, type) noexcept;
static constexpr rawType checkValidAngle(rawType);

Expand Down Expand Up @@ -122,8 +126,8 @@ inline void Angle::setRaw(rawType raw)
}

constexpr Angle::Angle(type calibration, type angle)
: rawData {checkValidAngle(castToRaw(calibration, angle))}, // throw std::out_of_range
rawCalibration {calibration}
: rawData {checkValidAngle(castToRaw(calibration, angle))}, // throw std::out_of_range
rawCalibration {calibration}
{
}

Expand Down
6 changes: 4 additions & 2 deletions include/ics3/baudrate.h → include/ics3/baudrate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ class Baudrate
{
public:
using type = uint8_t;

static constexpr Baudrate RATE115200() noexcept;
//static constexpr Baudrate RATE625000() noexcept;
//static constexpr Baudrate RATE1250000() noexcept;

constexpr type get() const noexcept;
constexpr operator type() const noexcept;
constexpr speed_t getSpeed() const noexcept;
Expand Down Expand Up @@ -69,8 +71,8 @@ constexpr speed_t Baudrate::getSpeed() const noexcept
}

constexpr Baudrate::Baudrate(type romdata, speed_t baudrate) noexcept
: romdata {romdata},
baudrate {baudrate}
: romdata {romdata},
baudrate {baudrate}
{
}
}
Expand Down
File renamed without changes.
30 changes: 16 additions & 14 deletions include/ics3/eepparam.h → include/ics3/eepparam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <array>

#include "ics3/baudrate.h"
#include "ics3/check_invalid.h"
#include "ics3/baudrate.hpp"
#include "ics3/check_invalid.hpp"

namespace ics
{
Expand All @@ -41,6 +41,7 @@ class EepParam
using TargetContainer = std::array<uint8_t, 64>;
using size_type = TargetContainer::size_type;
using ValidChecker = type (&)(type, type, type);

enum Flag : type
{
REVERSE = 0x01,
Expand Down Expand Up @@ -86,6 +87,7 @@ class EepParam
type,
ValidChecker,
type);

static constexpr type checkValidRange(type, type, type);
static constexpr type checkValidEvenRange(type, type, type);
static constexpr type checkValidFlag(type, type, type);
Expand Down Expand Up @@ -237,18 +239,18 @@ inline void EepParam::write(TargetContainer& dest) const noexcept
}

constexpr EepParam::EepParam(
size_type offset,
size_type length,
type min,
type max,
ValidChecker setFunc,
type data)
: offset(offset),
length(length),
min(min),
max(max),
setFunc(setFunc),
data(setFunc(data, min, max)) // throw std::invalid_argument, std::out_of_range
size_type offset,
size_type length,
type min,
type max,
ValidChecker setFunc,
type data)
: offset(offset),
length(length),
min(min),
max(max),
setFunc(setFunc),
data(setFunc(data, min, max)) // throw std::invalid_argument, std::out_of_range
{
}

Expand Down
4 changes: 2 additions & 2 deletions include/ics3/eeprom.h → include/ics3/eeprom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <algorithm>

#include "ics3/eepparam.h"
#include "ics3/eepparam.hpp"

namespace ics
{
Expand Down Expand Up @@ -69,7 +69,7 @@ inline void EepRom::write(Iter&& dest) const
}

inline EepRom::EepRom(const Container& src)
: data(src) // for Ubuntu14.04 compiler
: data(src) // for Ubuntu14.04 compiler
{
}
}
Expand Down
16 changes: 8 additions & 8 deletions include/ics3/ics
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef LIBICS3_ICS3_ICS_H
#define LIBICS3_ICS3_ICS_H

#include "ics3/angle.h"
#include "ics3/baudrate.h"
#include "ics3/check_invalid.h"
#include "ics3/eepparam.h"
#include "ics3/eeprom.h"
#include "ics3/ics3.h"
#include "ics3/id.h"
#include "ics3/parameter.h"
#include "ics3/angle.hpp"
#include "ics3/baudrate.hpp"
#include "ics3/check_invalid.hpp"
#include "ics3/eepparam.hpp"
#include "ics3/eeprom.hpp"
#include "ics3/ics3.hpp"
#include "ics3/id.hpp"
#include "ics3/parameter.hpp"

#endif // LIBICS3_ICS3_ICS_H
10 changes: 6 additions & 4 deletions include/ics3/ics3.h → include/ics3/ics3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <memory>
#include <string>

#include "ics3/angle.h"
#include "ics3/baudrate.h"
#include "ics3/angle.hpp"
#include "ics3/baudrate.hpp"

namespace ics
{
// Forward declaration
class Core;
class Parameter;
class EepRom;
class ID;
class Parameter;

class ICS3
{
public:
explicit ICS3(const std::string&, const Baudrate& = Baudrate::RATE115200());
~ICS3() noexcept;

Angle move(const ID&, Angle);
Angle free(const ID&, Angle = Angle::newRadian());
Parameter get(const ID&, const Parameter&);
Expand All @@ -54,7 +56,7 @@ class ICS3
ID getID();
void setID(const ID&);
private:
std::shared_ptr<Core> core;
std::unique_ptr<Core> core;
};
}

Expand Down
4 changes: 3 additions & 1 deletion include/ics3/id.h → include/ics3/id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ class ID
{
public:
using type = uint8_t;

constexpr ID(type); // ID is non explicit constructor because only do check limit

constexpr type get() const noexcept;
constexpr operator type() const noexcept;
private:
const type data;
};

constexpr ID::ID(type id)
: data {id < 32 ? id : throw std::invalid_argument {"invalid ID: must be 0 <= id <= 31"}}
: data {id < 32 ? id : throw std::invalid_argument {"invalid ID: must be 0 <= id <= 31"}}
{
}

Expand Down
11 changes: 6 additions & 5 deletions include/ics3/parameter.h → include/ics3/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef LIBICS3_ICS3_PARAMETER_H
#define LIBICS3_ICS3_PARAMETER_H

#include "ics3/check_invalid.h"
#include "ics3/check_invalid.hpp"

namespace ics
{
class Parameter
{
public:
using type = uint8_t;

static constexpr Parameter stretch(type = 30);
static constexpr Parameter speed(type = 127);
static constexpr Parameter current(type = 63);
Expand Down Expand Up @@ -107,10 +108,10 @@ constexpr Parameter::type Parameter::getSubcommand() const noexcept
}

constexpr Parameter::Parameter(type sc, type min, type max, type defaultData)
: sc {sc},
min {min},
max {max},
data {checkValidRange(defaultData, min, max)}
: sc {sc},
min {min},
max {max},
data {checkValidRange(defaultData, min, max)}
{
}
}
Expand Down
27 changes: 13 additions & 14 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <fcntl.h> // for open FLAGS
#include <unistd.h> // for tty checks

#include "core.h"
#include "core.hpp"

template<typename T, typename... Args>
inline std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

ics::Core::Core(const std::string& path, speed_t baudrate)
: fd {open(path.c_str(), O_RDWR | O_NOCTTY)},
oldTio {}
: fd {open(path.c_str(), O_RDWR | O_NOCTTY)},
oldTio {}
{
if (fd < 0)
throw std::runtime_error {"Cannot open deveice"};
Expand Down Expand Up @@ -64,8 +70,8 @@ ics::Core::~Core() noexcept
}

ics::Core::Core(Core&& rhs) noexcept
: fd {rhs.fd},
oldTio(rhs.oldTio) // for Ubuntu14.04 compiler
: fd {rhs.fd},
oldTio(rhs.oldTio) // for Ubuntu14.04 compiler
{
rhs.fd = -1;
}
Expand All @@ -81,16 +87,9 @@ ics::Core& ics::Core::operator=(Core&& rhs) noexcept
return *this;
}

std::shared_ptr<ics::Core> ics::Core::getCore(const std::string& path, speed_t baudrate)
std::unique_ptr<ics::Core> ics::Core::getCore(const std::string& path, speed_t baudrate)
{
static std::unordered_map<std::string, std::weak_ptr<Core>> cache;
auto objPtr = cache[path].lock(); // try get
// for (const auto& data : cache) if (data.second.expired()) cache.erase(data.first); // clean cashe //FIXME error code
if (!objPtr) { // get failed
objPtr = std::make_shared<Core>(path, baudrate);
cache[path] = objPtr;
}
return objPtr;
return make_unique<Core>(path, baudrate);
}

void ics::Core::communicate(const Container& tx, Container& rx)
Expand Down
16 changes: 10 additions & 6 deletions src/ics3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ics3/ics3.h"
#include "ics3/ics3.hpp"

#include "core.h"
#include "ics3/eeprom.h"
#include "ics3/id.h"
#include "ics3/parameter.h"
#include "core.hpp"
#include "ics3/eeprom.hpp"
#include "ics3/id.hpp"
#include "ics3/parameter.hpp"

static inline ics::Angle::rawType getReceiveAngle(const ics::Core::Container& rx) noexcept
{
Expand All @@ -42,7 +42,11 @@ static inline ics::Core::value_type getCmd(const ics::Core::value_type head, con
}

ics::ICS3::ICS3(const std::string& path, const Baudrate& baudrate)
: core {Core::getCore(path, baudrate.getSpeed())} // throw std::invalid_argument, std::runtime_error
: core {Core::getCore(path, baudrate.getSpeed())} // throw std::invalid_argument, std::runtime_error
{
}

ics::ICS3::~ICS3() noexcept
{
}

Expand Down
2 changes: 1 addition & 1 deletion test/integrate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ TEST(AllTestInIntegrate, Parameter) {
TEST(AllTestInIntegrate, ICS3) {
constexpr auto path = "/dev/ttyUSB0";
constexpr auto baudrate = ics::Baudrate::RATE115200();
constexpr ics::ID id {2};
constexpr ics::ID id {0};
try {
ics::ICS3 ics {path, baudrate};
testIcsMove(ics, id);
Expand Down

0 comments on commit b8965f5

Please sign in to comment.