forked from glumb/mrc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVarSpeedServo.cpp
More file actions
188 lines (145 loc) Β· 5.07 KB
/
Copy pathVarSpeedServo.cpp
File metadata and controls
188 lines (145 loc) Β· 5.07 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "VarSpeedServo.h"
#include <util/atomic.h>
#include "Logger.h"
#include "Logger.h"
#include "Arduino.h"
namespace {
Logger logger("VarSpeedServo");
}
/**
*
*/
VarSpeedServo::VarSpeedServo(int pinNumber,
float maxAngleVelocity,
unsigned int minFreq,
unsigned int maxFreq,
float minRadAngle,
float maxRadAngle,
float homeRadAngle) {
this->pinNumber = pinNumber;
this->maxAngleVelocity = maxAngleVelocity; // per s
this->currentAngleVelocity = maxAngleVelocity;
this->lastUpdate = micros();
this->minRadAngle = minRadAngle;
this->maxRadAngle = maxRadAngle;
if (minRadAngle > maxRadAngle) {
logger.error("minAngle must be smaller than maxAngle on servo " + String(pinNumber) + " initialization (min: " + String(
minRadAngle / PI * 180) + " max: " + String(maxRadAngle / PI * 180) + ")");
}
if (minRadAngle > maxRadAngle) {
logger.error("minRadAngle must be smaller than maxRadAngle. Servo pin number: " + String(pinNumber));
}
this->startAngle = homeRadAngle;
this->currentAngle = homeRadAngle;
this->targetAngle = homeRadAngle;
this->homeAngle = homeRadAngle;
this->minFreq = minFreq;
this->maxFreq = maxFreq;
logger.info("Servo ");
logger.info(pinNumber);
if (this->pinNumber < 0) this->virtualServo = true;
if (!this->virtualServo) {
this->servo.attach(pinNumber);
}
this->move(); // drive to 0 position according to min max angle
}
float VarSpeedServo::getHomeRadAngle() {
return this->homeAngle;
}
int VarSpeedServo::getPinNumber() {
return this->pinNumber;
}
void VarSpeedServo::setAngleLimits(float minRadAngle, float maxRadAngle) {
if (minRadAngle > maxRadAngle) {
logger.error("minAngle must be smaller than maxAngle on servo_num: " + String(this->pinNumber) + " (min: " + String(
minRadAngle / PI * 180) + " max: " + String(maxRadAngle / PI * 180) + ")");
}
this->minRadAngle = minRadAngle;
this->maxRadAngle = maxRadAngle;
}
float VarSpeedServo::getMinRadAngle() {
return this->minRadAngle;
}
float VarSpeedServo::getMaxRadAngle() {
return this->maxRadAngle;
}
void VarSpeedServo::setCalibrationFreq(unsigned int minFreq, unsigned int maxFreq) {
this->minFreq = minFreq;
this->maxFreq = maxFreq;
}
unsigned int VarSpeedServo::getMinFreq() {
return this->minFreq;
}
unsigned int VarSpeedServo::getMaxFreq() {
return this->maxFreq;
}
float VarSpeedServo::getCurrentAngle() { // physical angle
float result;
// std::cout << "getCurrentAngle "<<currentAngle << '\n';
// current angle may change whe process interrupt kicks in
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
result = this->currentAngle;
}
return result;
}
void VarSpeedServo::setTargetRadAngle(float angleRad) {
this->startAngle = this->currentAngle;
this->elapsedTime = 0;
this->targetAngle = angleRad;
}
void VarSpeedServo::setFreqency(long microseconds) {
if (!this->virtualServo) this->servo.writeMicroseconds(microseconds);
}
float VarSpeedServo::getTargetRadAngle() {
return this->targetAngle;
}
void VarSpeedServo::setCurrentAngleVelocity(float angleRadVelocity) {
if (angleRadVelocity > this->maxAngleVelocity) {
this->currentAngleVelocity = this->maxAngleVelocity;
} else if (angleRadVelocity <= 0) {
this->currentAngleVelocity = 0.1;
} else {
this->currentAngleVelocity = angleRadVelocity;
}
}
float VarSpeedServo::getCurrentAngleVelocity() {
return this->currentAngleVelocity;
}
float VarSpeedServo::getMaxAngleVelocity() {
return this->maxAngleVelocity;
}
void VarSpeedServo::process(unsigned int deltaT) {
// v = s/t
this->elapsedTime += deltaT;
float deltaAngle = this->currentAngleVelocity * this->elapsedTime / 1000.0;
// Serial.println(deltaAngle);
if (fabs(deltaAngle) > fabs(this->targetAngle - this->startAngle)) {
this->currentAngle = this->targetAngle;
// set start to target to not move again on elapsed time. overflow ~50 days?
this->startAngle = this->targetAngle;
} else {
if (this->targetAngle > this->startAngle) {
this->currentAngle = this->startAngle + deltaAngle;
} else {
this->currentAngle = this->startAngle - deltaAngle;
}
}
this->move();
}
bool VarSpeedServo::atTargetAngle() {
return fabs(this->currentAngle - this->targetAngle) < 0.000001;
}
void VarSpeedServo::move() {
unsigned int freq = int(
this->map_float(this->currentAngle, this->minRadAngle, this->maxRadAngle, this->minFreq, this->maxFreq)
);
if (!this->virtualServo) this->servo.writeMicroseconds(freq);
}
float VarSpeedServo::map_float(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
bool VarSpeedServo::getOutOfRange() {
return this->outOfRange;
}