PID motor controll for the biorobotics project.

Dependencies:   FastPWM QEI

Dependents:   PID_example Motor_calibration Demo_mode Demo_mode ... more

motor.h

Committer:
brass_phoenix
Date:
2018-10-31
Revision:
10:d50c4957a193
Parent:
7:eb8787e7a5f5
Child:
12:2c65bcbebf8a

File content as of revision 10:d50c4957a193:

#pragma once

#include "mbed.h"
#include "FastPWM.h"
#include "MODSERIAL.h"
#include "QEI.h"

#include "pid.h"

// Amount of motor encoder pulses per rotation. When using X4 encoding.
const int PULSES_PER_ROTATION = 6533;

const double MOTOR_THRESHOLD_RPS = 0.1; // Rad/s under which we send 0 to the motor, to prevent it from jittering around.
const double MOTOR_STALL_PWM = 0.45; // PWM fraction above which the motor starts to move.

class Motor {
    /// Rotates a motor to a given angle using PID control.
    /// Sets the pwm frequency to 60 HZ.
private:
    const float PI = 3.14159265359;

    Ticker motor_ticker;

    FastPWM pwm_out;
    DigitalOut dir_out;
    QEI encoder;
    PID pid;
    
    // Reduction ratio of an extra gear box.
    // On top of the gear box in the motor.
    double extra_reduction_ratio;
    
    // How much our rotation frame is turned with respect to the "0" value
    // of the encoder.
    double rotation_frame_offset;
    
    double target_angle;
    
    // For debugging purposes;
    Serial* pc;
    bool serial_debugging;
    int printcount;
    float pid_period;
public:
    // Initializes the motor with the two driver pins, and the two encoder pins.
    Motor(PinName pwm_pin, PinName dir_pin, PinName encoder_a, PinName encoder_b, Serial* pc);
    // Initialize the motor without serial output.
    Motor(PinName pwm_pin, PinName dir_pin, PinName encoder_a, PinName encoder_b);
    // Set the angle the motor needs to rotate to. In radians.
    void set_target_angle(double angle);
    void set_pid_k_values(double k_p, double k_i, double k_d);
    // Reduction ratio of an extra gear box.
    // On top of the gear box in the motor.
    void set_extra_reduction_ratio(double ratio);
    
    // Starts the motor controller with the specified update period.
    void start(float period);
    // Stops the motor immediately.
    void stop();
    
    // Defines the current angle to be the given amount of radians.
    void define_current_angle_as_x_radians(double radians);
    
    // Returns the current angle of the motor.
    double get_current_angle();
    
private:
    void update();
    // Updates the motor with the given speed.
    // The speed can be both positive and negative, in the range [-1, 1].
    void update_motor_speed(double speed);
    double encoder_pulses_to_radians(int pulses);
    // Converts radians/s values into PWM values for motor controll.
    // Both positive and negative values.
    double radians_per_second_to_pwm(double rps);
};