Repository navigation
Expand file tree
/
Copy pathservoarray.cpp
More file actions
84 lines (68 loc) · 2.48 KB
/
Copy pathservoarray.cpp
File metadata and controls
84 lines (68 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// This file is part of servoarray.
//
// servoarray is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// servoarray is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with servoarray. If not, see <http://www.gnu.org/licenses/>.
#include "servoarray/servoarray.h"
#include "servoarray/subscript_wrapper.h"
#include <cassert>
namespace ServoArray {
ServoArray::ServoArray(std::shared_ptr<Driver> driver, const std::vector<double>& offsets) : driver_(driver), offsets_(offsets) {
this->cache_.resize(this->size());
this->offsets_.resize(this->size());
}
ServoArray::ServoArray(const std::string& name, const DriverParams& params, DriverManager& manager) : driver_(manager.load(name, params)) {
this->cache_.resize(this->size());
this->offsets_.resize(this->size());
for (const auto& p : manager.config().offset().offsets()) {
if (p.first >= this->size()) {
// TODO: ignore this with warning
throw std::runtime_error("Offset index out of range");
}
this->offsets_[p.first] = p.second;
}
}
void ServoArray::write(std::size_t index, double rad) {
auto const value = this->offsets_[index] + rad;
this->driver_->write(index, value);
this->cache_[index] = value;
}
double ServoArray::read(std::size_t index) {
switch(this->read_mode_) {
case ReadMode::Cached:
return this->cache_[index] - this->offsets_[index];
case ReadMode::Direct:
return this->driver_->read(index) - this->offsets_[index];
default:
assert(false); // unreachable
}
}
SubscriptWrapper ServoArray::operator[](std::size_t index) {
return {this, index};
}
void ServoArray::set_read_mode(ReadMode mode) {
this->read_mode_ = mode;
// TODO: Print warning if mode == Direct and the driver doesn't support read
}
ReadMode ServoArray::read_mode() const {
return this->read_mode_;
}
double ServoArray::offset(std::size_t idx) const {
return this->offsets_[idx];
}
void ServoArray::set_offset(std::size_t idx, double value) {
this->offsets_[idx] = value;
}
std::size_t ServoArray::size() const {
return this->driver_->size();
}
}