To simulate in octave

I was helped by chat gpt to simulate with odrive v3.6 and Dual Shaft Motor - D5065 270kv in octave.

Is this a good way to proceed?
Or if you have better code, please let me know.

Thank you!

pkg load control  # Load control system toolbox

# Define motor and robot parameters
R = 0.5;  # Motor resistance (ohms)
L = 0.0015;  # Motor inductance (henry)
Ke = 0.01;  # Back-EMF constant (V/rad/s)
Kt = 0.1;  # Torque constant (Nm/A)
J_robot = 0.01;  # Inertia of the robot (kg*m^2), considering robot and wheels

# Calculate the total inertia based on robot weight and wheel size (15kg robot, wheel radius R = 0.05m)
M_robot = 15;  # Robot mass (kg)
R_wheel = 0.09;  # Wheel radius (m)
J = 0.5 * M_robot * R_wheel^2 + J_robot;  # Total inertia of the robot and wheels

B = 0.001;  # Friction coefficient (Ns/m)
V_max = 56;  # Maximum voltage (V)

# Transfer function of the motor (electrical and mechanical coupling model)
s = tf('s');
motor_tf = Kt / ((L * s + R) * (J * s + B) + Kt * Ke);

# PI controller parameters
Kp = 1.2;  # Proportional gain
Ki = 2;  # Integral gain
pi_controller = Kp + Ki/s;

# Closed-loop transfer function (current control loop)
closed_loop_system = feedback(pi_controller * motor_tf, 1);

# Simulation settings
t = 0:0.01:5;  # Simulate for 5 seconds
target_speed = 0.7;  # Target speed of 0.7 rad/s
input_signal = target_speed * ones(size(t));  # Input signal corresponding to the target speed

# Compute system response (response to the target speed of 0.7 rad/s)
[response, t] = lsim(closed_loop_system, input_signal, t);

# Plot the response
figure;
plot(t, response);
title("Motor Response with PI Controller (Target Speed = 0.7 rad/s)");
xlabel("Time (s)");
ylabel("Speed (rad/s)");
grid on;

# Check for speed and current overshoot
[peak_value, peak_time] = max(response);
disp(['Maximum speed response: ', num2str(peak_value), ' rad/s']);
disp(['Overshoot time: ', num2str(peak_time), ' seconds']);

Sure, you could use Octave pretty effectively. But I definitely don’t think those motor constants are accurate – you can check them here: ODrive motor guide - Google Sheets

I think the 270kV has about 15uH of inductance, for the record.