forked from abuchan/mbed_wiggle_jig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAxis.cpp
215 lines (174 loc) · 5.37 KB
/
Axis.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "Axis.h"
#define PWM_MAX 0.8
#define VEL_ALPHA 0.2
Axis::Axis(
PinName enc_a, PinName enc_b, PinName enc_i,
float mot_dir,
PinName mot_in1, PinName mot_in2, PinName mot_pwm, PinName mot_fb,
Timer* timer, float pid_rate,
float pos_pid_p, float pos_pid_i, float pos_pid_d,
float vel_pid_p, float vel_pid_i, float vel_pid_d,
float pos_min, float pos_max, float vel_max,
float curr_max, float home_pwm
):
qei_(enc_a, enc_b, enc_i, REV_TO_TICKS, timer),
mot_dir_(mot_dir),
mot_in1_(mot_in1), mot_in2_(mot_in2), mot_pwm_(mot_pwm), mot_fb_(mot_fb),
timer_(timer), pid_rate_(pid_rate),
pos_pid_(pos_pid_p, pos_pid_i, pos_pid_d, pid_rate),
vel_pid_(vel_pid_p, vel_pid_i, vel_pid_d, pid_rate),
pos_min_(pos_min), pos_max_(pos_max), vel_max_(vel_max),
curr_max_(curr_max), home_pwm_(home_pwm),
zero_ticks_(0), mode_(MODE_NONE), last_time_us_(-1), last_pos_(0.0), last_cmd_(0.0) {
mot_pwm_.period_us(2000);
pos_pid_.setInputLimits(-4.0 * PI, 4.0 * PI);
pos_pid_.setOutputLimits(-PWM_MAX, PWM_MAX);
pos_pid_.setBias(0.0);
vel_pid_.setInputLimits(-1.5*vel_max_, 1.5*vel_max_);
vel_pid_.setOutputLimits(-PWM_MAX, PWM_MAX);
vel_pid_.setBias(0.0);
}
Axis::AxisMode Axis::get_mode(void) {
return mode_;
}
int Axis::get_ticks(void) {
return qei_.getPulses();
}
bool Axis::homed(void) {
return qei_.homed();
}
float Axis::get_position(void) {
return ((float)(get_ticks() - zero_ticks_) * TICKS_TO_RADIANS) /*+ (last_vel_ * (timer_->read_us() - last_time_us_) / 1000000.0)*/;
}
float Axis::get_last_position(void) {
return last_pos_;
}
float Axis::get_velocity(void) {
return TICKS_TO_RADIANS * qei_.getPulseFreq();
}
float Axis::get_last_velocity(void) {
return last_vel_;
}
float Axis::get_current(void) {
return mot_fb_.read() * FB_TO_CURRENT;
}
float Axis::get_command(void) {
return last_cmd_;
}
float Axis::get_err_curr(void) {
return err_curr_;
}
void Axis::get_pid_state(float* state) {
pos_pid_.get_state(state);
}
void Axis::set_mode(AxisMode mode) {
if (mode != MODE_NO_CHANGE)
mode_ = mode;
}
void Axis::set_mode_command(AxisMode mode, float command) {
set_mode(mode);
switch(mode) {
case MODE_PWM:
set_motor(command);
break;
case MODE_POS:
set_position(command);
break;
case MODE_VEL:
set_velocity(command);
break;
}
}
void Axis::set_motor(float motor) {
motor = motor * mot_dir_;
if (motor > 1.0)
motor = 1.0;
else if (motor < -1.0)
motor = -1.0;
if (abs(motor) < 0.001) {
mot_in1_.write(0);
mot_in2_.write(0);
mot_pwm_.write(0.0);
} else {
if (motor > 0.0) {
mot_in1_.write(0);
mot_in2_.write(1);
mot_pwm_.write(motor);
} else {
mot_in2_.write(0);
mot_in1_.write(1);
mot_pwm_.write(-motor);
}
}
last_cmd_ = motor;
}
void Axis::set_zero(int zero_ticks) {
zero_ticks_ = zero_ticks;
}
void Axis::set_position(float position) {
if (position < pos_min_)
position = pos_min_;
if (position > pos_max_)
position = pos_max_;
pos_pid_.setSetPoint(position);
}
void Axis::set_velocity(float velocity) {
if (abs(velocity) > vel_max_)
velocity = velocity > 0.0 ? vel_max_ : - vel_max_;
vel_pid_.setSetPoint(velocity);
}
void Axis::update(void) {
float cmd = 0.0;
float curr = get_current();
if (curr > curr_max_) {
mode_ = MODE_ERR;
err_curr_ = curr;
}
float pos = get_position();
float vel = get_velocity();
last_vel_ = VEL_ALPHA * vel + (1.0-VEL_ALPHA) * last_vel_;
switch (mode_) {
case (MODE_NONE):
set_motor(0.0);
break;
case (MODE_HOME):
if (qei_.homed())
mode_ = MODE_NONE;
else {
cmd = home_pwm_;
set_motor(cmd);
}
break;
case (MODE_PWM):
break;
case (MODE_POS):
if (abs(pos - pos_pid_.getSetPoint()) < 0.01) {
cmd = 0.0;
} else {
pos_pid_.setProcessValue(pos);
cmd = pos_pid_.compute();
}
set_motor(cmd);
break;
case (MODE_VEL):
vel_pid_.setProcessValue(last_vel_);
cmd = vel_pid_.compute();
if (cmd > 0.1)
cmd += 0.2;
else if (cmd < -0.1)
cmd -= 0.2;
set_motor(cmd);
break;
case (MODE_ERR):
set_motor(0.0);
break;
default:
mode_ = MODE_NONE;
set_motor(0.0);
break;
}
if (mode_ != MODE_ERR)
last_cmd_ = cmd;
last_pos_ = pos;
last_time_us_ = timer_->read_us();
}