Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12e7303

Browse files
authored
Merge pull request #26 from MonoMotion/feature/offset
Feature/offset
2 parents a85ced9 + 983a01f commit 12e7303

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

src/include/servoarray/servoarray.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ class ServoArray {
3434
std::shared_ptr<Driver> driver_;
3535
ReadMode read_mode_ = ReadMode::Direct;
3636

37+
std::vector<double> offsets_;
3738
std::vector<double> cache_;
3839

3940
public:
40-
ServoArray(std::shared_ptr<Driver>);
41+
ServoArray(std::shared_ptr<Driver>, const std::vector<double>& offsets = {});
4142
ServoArray(const std::string& name = "", const DriverParams& = {}, DriverManager& = default_manager);
4243

4344
void write(std::size_t index, double rad);
@@ -48,6 +49,9 @@ class ServoArray {
4849
void set_read_mode(ReadMode);
4950
ReadMode read_mode() const;
5051

52+
double offset(std::size_t) const;
53+
void set_offset(std::size_t, double);
54+
5155
std::size_t size() const;
5256
};
5357

src/include/servoarray/user_config.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,22 @@ class MapConfig {
5353
MapConfig& merge(const MapConfig&);
5454
};
5555

56+
class OffsetConfig {
57+
std::unordered_map<std::size_t, double> offsets_;
58+
59+
friend class UserConfig;
60+
61+
public:
62+
const std::unordered_map<std::size_t, double>& offsets() const&;
63+
std::unordered_map<std::size_t, double> offsets() &&;
64+
65+
OffsetConfig& merge(const OffsetConfig&);
66+
};
67+
5668
class UserConfig {
5769
DriverConfig driver_;
5870
MapConfig mapping_;
71+
OffsetConfig offset_;
5972

6073
public:
6174
UserConfig() = default;
@@ -68,6 +81,9 @@ class UserConfig {
6881
const MapConfig& mapping() const&;
6982
MapConfig mapping() &&;
7083

84+
const OffsetConfig& offset() const&;
85+
OffsetConfig offset() &&;
86+
7187
UserConfig& merge(const UserConfig&);
7288
};
7389

src/lib/servoarray.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,37 @@
2020

2121
namespace ServoArray {
2222

23-
ServoArray::ServoArray(std::shared_ptr<Driver> driver) : driver_(driver) {
23+
ServoArray::ServoArray(std::shared_ptr<Driver> driver, const std::vector<double>& offsets) : driver_(driver), offsets_(offsets) {
2424
this->cache_.resize(this->size());
25+
this->offsets_.resize(this->size());
2526
}
2627

2728
ServoArray::ServoArray(const std::string& name, const DriverParams& params, DriverManager& manager) : driver_(manager.load(name, params)) {
2829
this->cache_.resize(this->size());
30+
this->offsets_.resize(this->size());
31+
32+
for (const auto& p : manager.config().offset().offsets()) {
33+
if (p.first >= this->size()) {
34+
// TODO: ignore this with warning
35+
throw std::runtime_error("Offset index out of range");
36+
}
37+
38+
this->offsets_[p.first] = p.second;
39+
}
2940
}
3041

3142
void ServoArray::write(std::size_t index, double rad) {
32-
this->driver_->write(index, rad);
33-
this->cache_[index] = rad;
43+
auto const value = this->offsets_[index] + rad;
44+
this->driver_->write(index, value);
45+
this->cache_[index] = value;
3446
}
3547

3648
double ServoArray::read(std::size_t index) {
3749
switch(this->read_mode_) {
3850
case ReadMode::Cached:
39-
return this->cache_[index];
51+
return this->cache_[index] - this->offsets_[index];
4052
case ReadMode::Direct:
41-
return this->driver_->read(index);
53+
return this->driver_->read(index) - this->offsets_[index];
4254
default:
4355
assert(false); // unreachable
4456
}
@@ -57,6 +69,14 @@ ReadMode ServoArray::read_mode() const {
5769
return this->read_mode_;
5870
}
5971

72+
double ServoArray::offset(std::size_t idx) const {
73+
return this->offsets_[idx];
74+
}
75+
76+
void ServoArray::set_offset(std::size_t idx, double value) {
77+
this->offsets_[idx] = value;
78+
}
79+
6080
std::size_t ServoArray::size() const {
6181
return this->driver_->size();
6282
}

src/lib/user_config.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// along with servoarray. If not, see <http://www.gnu.org/licenses/>.
1515

1616
#include <toml11/toml.hpp>
17+
#include <boost/lexical_cast.hpp>
1718

1819
#include "servoarray/user_config.h"
1920

@@ -53,10 +54,33 @@ MapConfig& MapConfig::merge(const MapConfig& other) {
5354
return *this;
5455
}
5556

57+
const std::unordered_map<std::size_t, double>& OffsetConfig::offsets() const& {
58+
return this->offsets_;
59+
}
60+
61+
std::unordered_map<std::size_t, double> OffsetConfig::offsets() && {
62+
return std::move(this->offsets_);
63+
}
64+
65+
OffsetConfig& OffsetConfig::merge(const OffsetConfig& other) {
66+
for (const auto& p : other.offsets_) {
67+
this->offsets_[p.first] = p.second;
68+
}
69+
70+
return *this;
71+
}
72+
5673
UserConfig::UserConfig(const std::string& path) {
5774
auto const config = toml::parse(path);
5875

5976
this->mapping_.names_ = toml::get_or<std::unordered_map<std::string, std::size_t>>(config, "mapping", {});
77+
{
78+
auto const offsets = toml::get_or<std::unordered_map<std::string, double>>(config, "offsets", {});
79+
for (auto const& p : offsets) {
80+
auto const idx = boost::lexical_cast<std::size_t>(p.first);
81+
this->offset_.offsets_[idx] = p.second;
82+
}
83+
}
6084

6185
// TODO: Seperate this into some function
6286
{
@@ -108,9 +132,13 @@ DriverConfig UserConfig::driver() && { return std::move(this->driver_); }
108132
const MapConfig& UserConfig::mapping() const& { return this->mapping_; }
109133
MapConfig UserConfig::mapping() && { return std::move(this->mapping_); }
110134

135+
const OffsetConfig& UserConfig::offset() const& { return this->offset_; }
136+
OffsetConfig UserConfig::offset() && { return std::move(this->offset_); }
137+
111138
UserConfig& UserConfig::merge(const UserConfig& other) {
112139
this->driver_.merge(other.driver());
113140
this->mapping_.merge(other.mapping());
141+
this->offset_.merge(other.offset());
114142

115143
return *this;
116144
}

src/python/binding.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class ServoArray {
106106

107107
void set_read_mode(::ServoArray::ReadMode mode) { this->sa.set_read_mode(mode); }
108108
::ServoArray::ReadMode read_mode() const { return this->sa.read_mode(); }
109+
110+
void set_offset(std::size_t index, double value) { this->sa.set_offset(index, value); }
111+
double offset(std::size_t index) const { return this->sa.offset(index); }
109112
};
110113

111114
}
@@ -134,6 +137,8 @@ PYBIND11_MODULE(servoarray, m) {
134137
.def("write", &Adaptor::ServoArray::write)
135138
.def("read", &Adaptor::ServoArray::read)
136139
.def_property("read_mode", &Adaptor::ServoArray::read_mode, &Adaptor::ServoArray::set_read_mode)
140+
.def("offset", &Adaptor::ServoArray::offset)
141+
.def("set_offset", &Adaptor::ServoArray::set_offset)
137142
.def("__len__", &Adaptor::ServoArray::size)
138143
.def("__setitem__", &Adaptor::ServoArray::write_slice)
139144
.def("__setitem__", &Adaptor::ServoArray::write)

0 commit comments

Comments
 (0)