PID motor controll for the biorobotics project.
Dependents: PID_example Motor_calibration Demo_mode Demo_mode ... more
Diff: motor.cpp
- Revision:
- 2:b30a467e90d3
- Parent:
- 0:009e84d7af32
- Child:
- 4:5353c5d0d2ed
diff -r 0bb9adb89005 -r b30a467e90d3 motor.cpp --- a/motor.cpp Fri Oct 19 11:01:52 2018 +0000 +++ b/motor.cpp Mon Oct 29 14:15:57 2018 +0000 @@ -1,5 +1,22 @@ #include "motor.h" +Motor::Motor(PinName pwm_pin, PinName dir_pin, PinName encoder_a, PinName encoder_b): + pwm_out(pwm_pin), + dir_out(dir_pin), + encoder(encoder_a, encoder_b, NC, PULSES_PER_ROTATION, QEI::X4_ENCODING) + { + pid = PID(); + + target_angle = 0; + + printcount = 0; + pid_period = 0; + serial_debugging = false; + + // 60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to be done once) + pwm_out.period_us(60.0); +} + Motor::Motor(PinName pwm_pin, PinName dir_pin, PinName encoder_a, PinName encoder_b, Serial* pc): pwm_out(pwm_pin), dir_out(dir_pin), @@ -12,6 +29,7 @@ printcount = 0; pid_period = 0; this->pc = pc; + serial_debugging = true; // 60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to be done once) pwm_out.period_us(60.0); @@ -45,10 +63,13 @@ double speed_pwm = radians_per_second_to_pwm(speed_rps); - printcount++; - if (printcount >= 0.1L/pid_period) { - pc->printf("c_angle: %f, d_angle: %f, error: %f, rps: %f, speed: %f\n", current_angle, target_angle, error, speed_rps, speed_pwm); - printcount = 0; + if (serial_debugging) { + + printcount++; + if (printcount >= 0.1L/pid_period) { + pc->printf("c_angle: %f, d_angle: %f, error: %f, rps: %f, speed: %f\n", current_angle, target_angle, error, speed_rps, speed_pwm); + printcount = 0; + } } update_motor_speed(speed_pwm);