Skip to content

Commit 0202411

Browse files
committed
WIP positional PID controller
Needs tuning
1 parent 492987c commit 0202411

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

src/Primitives/Mindstorms/Motor.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,35 @@ void Motor::drive_to_target(int32_t _s) {
118118

119119
// ev3 has rpm of 160-170
120120
// so maximum around a 1000 degrees per second
121-
float max_speed = 250.0f; // Degrees per second
121+
float max_speed = 350.0f; // Degrees per second
122122
float target_speed = max_speed; // Degrees per second
123123

124-
PID pid(1.0f, 0.05f, 0.2f);
125-
PID position_pid(1.0f, 0.05f, 0.2f);
124+
PID speed_pid(1.0f, 0.05f, 0.2f);
125+
PID position_pid(2.5f, 0.001f, 0.09f);
126126

127127
while (true) {
128128
// Control speed.
129129
float encoder_speed = encoder->get_speed();
130130
float error = target_speed - encoder_speed;
131131

132-
float output = pid.update(error);
132+
float output = speed_pid.update(error);
133133

134134
float speed = clamp(output, -10000.0f, 10000.0f);
135135
float normalized_speed = speed / 10000.0f;
136136
set_speed(normalized_speed);
137137

138138
// Control position.
139-
/*error = static_cast<float>(get_drift());
139+
error = static_cast<float>(get_drift());
140140
output = position_pid.update(error);
141-
target_speed = clamp(output, -max_speed, -max_speed);*/
141+
target_speed = clamp(output, -max_speed, max_speed);
142+
143+
printf("position error = %f\n", error);
142144

143145
//printf("error = %f, speed = %f, integral = %f, derivative = %f, encoder_speed = %f\n", error, speed, integral, derivative, encoder_speed);
146+
147+
if (error == 0.0f && position_pid.prev_error == 0.0f) {
148+
break;
149+
}
144150
}
145151
halt();
146152

src/Primitives/zephyr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ void motor_timer_func(struct k_timer *timer_id) {
318318
//for (int motor_index = 0; motor_index < 4; motor_index++) {
319319
for (int motor_index = 0; motor_index < 1; motor_index++) {
320320
if (auto motor = get_motor(motor_index)) {
321-
printf("Timer, ticks = %d motor = %d, speed = %f\n", motor.value().encoder->ticks, motor_index, motor.value().encoder->speed);
321+
//printf("Timer, ticks = %d motor = %d, speed = %f\n", motor.value().encoder->ticks, motor_index, motor.value().encoder->speed);
322322
float current_speed = motor.value().encoder->speed;
323323
motor.value().encoder->speed = current_speed * 0.2f + 0.8f * ((float) (motor.value().encoder->ticks) / 0.005f); // Convert to degrees per second
324324
motor.value().encoder->ticks = 0;

0 commit comments

Comments
 (0)