Skip to content

Python API Reference

sirojudin munir edited this page Aug 16, 2026 · 4 revisions

The sf-motion Python API provides a simple interface for configuring, controlling, and monitoring the FOC motor controller. All motor commands are exposed as Python methods.

Plotter API

sf-motion provides plotter channels that can be used to monitor motor variables.

The available channel names are:

Channel Description
ia Phase A current
ib Phase B current
ic Phase C current
i_alpha Alpha-axis current
i_beta Beta-axis current
id d-axis current
iq q-axis current
va Phase A voltage
vb Phase B voltage
vc Phase C voltage
v_alpha Alpha-axis voltage
v_beta Beta-axis voltage
vd d-axis voltage
vq q-axis voltage
e_rad Electrical angle
actual_rpm Actual motor speed
actual_angle Actual motor angle
Is_ref Current reference
rpm_ref Speed reference
pos_ref Position reference
m_angle_rad Mechanical angle
m_angle_rad_comp Compensated mechanical angle
v_bus DC bus voltage

plotter_add_line('channel_name')

Add a variable to the plotter. The channel_name parameter can be specified using a plotter channel name if supported by the Python API.

Example:

motor.plotter_add_line('iq')

plotter_remove_line('channel_name')

Remove a variable from the plotter.

Example:

motor.plotter_remove_line('iq')

Configuration

set_default_config()

Reset the motor controller configuration to the default configuration.

motor.set_default_config()

save_config()

Save the current configuration.

motor.save_config()

Control Mode

set_foc_motor_mode(<mode:uint8>)

Set the motor control mode.

motor.set_foc_motor_mode(mode)

get_foc_motor_mode()

Read the motor control mode.

mode = motor.get_foc_motor_mode()

The available control mode are:

Control Mode Description
0 current control
1 speed control
2 position control
5 openloop mode
6 disable

Motor Parameters

set_pole_pairs(<value:uint8>)

Set the motor pole-pair count.

motor.set_pole_pairs(value)

get_pole_pairs()

Read the configured pole-pair count.

pole_pairs = motor.get_pole_pairs()

set_kv(<value:float>)

Set the motor KV parameter.

motor.set_kv(value)

get_kv()

Read the motor KV parameter.

kv = motor.get_kv()

set_rs(<value:float>)

Set the motor stator resistance.

motor.set_rs(value)

get_rs()

Read the motor stator resistance.

Rs = motor.get_rs()

set_ld(<value:float>)

Set the motor d-axis inductance.

motor.set_ld(value)

get_ld()

Read the motor d-axis inductance.

Ld = motor.set_ld()

set_lq(<value:float>)

Set the motor q-axis inductance.

motor.set_lq(value)

get_lq()

Read the motor q-axis inductance.

Lq = motor.set_lq()

set_flux_linkage(<value:float>)

Set the motor flux linkage.

motor.set_flux_linkage(value)

get_flux_linkage(<value:float>)

Read the motor flux linkage.

flux_linkage = motor.get_flux_linkage()

PID Controllers

set_pid_id(<kp:float>, <ki:float>, <deadband:float>)

Set current d-Axis PI gain.

Example:

motor.set_pid_id(0.02, 12, 0)

get_pid_id()

Read current d-Axis PI gain.

Example:

pid_data = motor.get_pid_id()
kp = pid_data['kp']
ki = pid_data['ki']
db = pid_data['deadband']

set_pid_iq(<kp:float>, <ki:float>, <deadband:float>)

Set current q-Axis PI gain.

Example:

motor.set_pid_iq(0.02, 12, 0)

get_pid_iq()

Read current q-Axis PI gain.

Example:

pid_data = motor.get_pid_iq()
kp = pid_data['kp']
ki = pid_data['ki']
db = pid_data['deadband']

set_pid_speed(<kp:float>, <ki:float>, <out_max:float>, <deadband:float>)

Set speed PI controller gain and output limit.

Example:

motor.set_pid_speed(0.01, 0.1, 5, 0)

get_pid_speed()

Read speed PI controller gain and output limit.

Example:

pid_data = motor.get_pid_speed()
kp = pid_data['kp']
ki = pid_data['ki']
out_max = pid_data['out_max']
db = pid_data['deadband']

set_pid_position(<kp:float>, <ki:float>, <kd:float>, <out_max:float>, <deadband:float>, <d_fc:float>)

Set position PID controller gain.

Example:

motor.set_pid_position(4, 0, 0.2, 500, 0.01, 100)

get_pid_position()

Read position PID controller gain.

Example:

pid_data = motor.get_pid_position()
kp = pid_data['kp']
ki = pid_data['ki']
kd = pid_data['kd']
out_max = pid_data['out_max']
db = pid_data['deadband']
derivative_freq_cutoff = pid_data['d_fc']