Library containing Crazyflie 2.0 controller classes: - Attitude estimator - Horizontal estimator - Vertical estimator - Attitude controller - Horizontal controller - Vertical controller - Mixer
Mixer/Mixer.cpp@5:b9947e3d20cf, 2018-09-27 (annotated)
- Committer:
- fbob
- Date:
- Thu Sep 27 13:51:54 2018 +0000
- Revision:
- 5:b9947e3d20cf
- Parent:
- 0:b1f2c9e88e32
- Child:
- 6:3188b00263e8
Included parameters.h;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fbob | 0:b1f2c9e88e32 | 1 | #include "mbed.h" |
fbob | 0:b1f2c9e88e32 | 2 | #include "Mixer.h" |
fbob | 0:b1f2c9e88e32 | 3 | |
fbob | 0:b1f2c9e88e32 | 4 | // Class constructor |
fbob | 0:b1f2c9e88e32 | 5 | Mixer::Mixer() : motor_1(PA_1), motor_2(PB_11), motor_3(PA_15), motor_4(PB_9_ALT1) |
fbob | 0:b1f2c9e88e32 | 6 | { |
fbob | 0:b1f2c9e88e32 | 7 | } |
fbob | 0:b1f2c9e88e32 | 8 | |
fbob | 5:b9947e3d20cf | 9 | // Actuate motors with desired total trust force (N) and torques (N.m) |
fbob | 0:b1f2c9e88e32 | 10 | void Mixer::actuate(float f_t, float tau_phi, float tau_theta, float tau_psi) |
fbob | 0:b1f2c9e88e32 | 11 | { |
fbob | 0:b1f2c9e88e32 | 12 | force_and_torques_to_angular_velocities(f_t,tau_phi,tau_theta,tau_psi); |
fbob | 0:b1f2c9e88e32 | 13 | motor_1 = angular_velocity_to_pwm(omega_1); |
fbob | 0:b1f2c9e88e32 | 14 | motor_2 = angular_velocity_to_pwm(omega_2); |
fbob | 0:b1f2c9e88e32 | 15 | motor_3 = angular_velocity_to_pwm(omega_3); |
fbob | 0:b1f2c9e88e32 | 16 | motor_4 = angular_velocity_to_pwm(omega_4); |
fbob | 0:b1f2c9e88e32 | 17 | } |
fbob | 0:b1f2c9e88e32 | 18 | |
fbob | 5:b9947e3d20cf | 19 | // Converts total trust force (N) and torques (N.m) to angular velocities (rad/s) |
fbob | 0:b1f2c9e88e32 | 20 | void Mixer::force_and_torques_to_angular_velocities(float f_t, float tau_phi, float tau_theta, float tau_psi) |
fbob | 0:b1f2c9e88e32 | 21 | { |
fbob | 0:b1f2c9e88e32 | 22 | float omega_1_squared = (1.0f/4.0f)*(f_t/kl-tau_phi/(kl*l)-tau_theta/(kl*l)-tau_psi/kd); |
fbob | 0:b1f2c9e88e32 | 23 | float omega_2_squared = (1.0f/4.0f)*(f_t/kl-tau_phi/(kl*l)+tau_theta/(kl*l)+tau_psi/kd); |
fbob | 0:b1f2c9e88e32 | 24 | float omega_3_squared = (1.0f/4.0f)*(f_t/kl+tau_phi/(kl*l)+tau_theta/(kl*l)-tau_psi/kd); |
fbob | 0:b1f2c9e88e32 | 25 | float omega_4_squared = (1.0f/4.0f)*(f_t/kl+tau_phi/(kl*l)-tau_theta/(kl*l)+tau_psi/kd); |
fbob | 0:b1f2c9e88e32 | 26 | if(omega_1_squared>=0) { |
fbob | 0:b1f2c9e88e32 | 27 | omega_1 = sqrt(omega_1_squared); |
fbob | 0:b1f2c9e88e32 | 28 | } else { |
fbob | 0:b1f2c9e88e32 | 29 | omega_1 = 0.0f; |
fbob | 0:b1f2c9e88e32 | 30 | } |
fbob | 0:b1f2c9e88e32 | 31 | if(omega_2_squared>=0) { |
fbob | 0:b1f2c9e88e32 | 32 | omega_2 = sqrt(omega_2_squared); |
fbob | 0:b1f2c9e88e32 | 33 | } else { |
fbob | 0:b1f2c9e88e32 | 34 | omega_2 = 0.0f; |
fbob | 0:b1f2c9e88e32 | 35 | } |
fbob | 0:b1f2c9e88e32 | 36 | if(omega_3_squared>=0) { |
fbob | 0:b1f2c9e88e32 | 37 | omega_3 = sqrt(omega_3_squared); |
fbob | 0:b1f2c9e88e32 | 38 | } else { |
fbob | 0:b1f2c9e88e32 | 39 | omega_3 = 0.0f; |
fbob | 0:b1f2c9e88e32 | 40 | } |
fbob | 0:b1f2c9e88e32 | 41 | if(omega_4_squared>=0) { |
fbob | 0:b1f2c9e88e32 | 42 | omega_4 = sqrt(omega_4_squared); |
fbob | 0:b1f2c9e88e32 | 43 | } else { |
fbob | 0:b1f2c9e88e32 | 44 | omega_4 = 0.0f; |
fbob | 0:b1f2c9e88e32 | 45 | } |
fbob | 0:b1f2c9e88e32 | 46 | } |
fbob | 0:b1f2c9e88e32 | 47 | |
fbob | 5:b9947e3d20cf | 48 | // Converts desired angular velocity (rad/s) to PWM signal (%) |
fbob | 0:b1f2c9e88e32 | 49 | float Mixer::angular_velocity_to_pwm(float omega) |
fbob | 0:b1f2c9e88e32 | 50 | { |
fbob | 0:b1f2c9e88e32 | 51 | float pwm = alpha*pow(omega,2)+beta*omega; |
fbob | 0:b1f2c9e88e32 | 52 | return pwm; |
fbob | 0:b1f2c9e88e32 | 53 | } |