Configuration of 3-Phase Motors and Stepper motors

Configure a 3-Phase Motor

[source code]

import time
import pilib

# make a connection to the PiMotion
pm = pilib.Pidev('192.168.1.201')

poles = 4
phases = 3
encoder_cnts = 512*4
elec_cycle_div = 0.01 * pilib.DEFAULT_CLK_PIMOTION

# configure the PiMotion to a motor type 3-PHASE
pm.mtr_set_mtr_type(pilib.MTR_TYPE_3PHASE)
pm.mtr_set_poles(poles)
pm.mtr_set_enc_counts_per_rot(encoder_cnts)
pm.mtr_set_enc_polarity(0)
pm.mtr_set_use_aux_enc_for_servo(0)
pm.mtr_set_output_polarity(0)

# set the pid and trajectory generator update rate
pm.mtr_set_elec_cycle_div(elec_cycle_div)

# set the pid gains (proportional, integral, derivitive)
pm.mtr_set_kp(20.0)
pm.mtr_set_ki(0)
pm.mtr_set_kd(15.0)
pm.mtr_set_ilim(0)

# motor limits and position error
pm.mtr_set_max_pid_output(30000)
pm.mtr_set_max_pos_error(4*encoder_cnts)  # four rotations

# initial phase angle configuration parameters 
pm.mtr_set_reset_current(28000)
pm.mtr_set_reset_wait_time_ms(2000)

# reset the motor
if pm.mtr_get_was_reset() == 0:
    print('Resetting motor...')
    pm.mtr_write_phase_mem_default(phases, poles, encoder_cnts)
    pm.mtr_reset()
    while pm.mtr_get_reset_state():
        time.sleep(0.1)
    print('Finished resetting.')

Configure a Stepper Motor

[source code]

import time
import pilib

# make a connection to the PiMotion
pm = pilib.Pidev('192.168.1.200')

elec_cycle_div = 0.00001 * pilib.DEFAULT_CLK_PIMOTION # 10 microseconds
enc_counts_per_rot = 10000          # number of encoder counts per rotation
                                    # of the encoder (if present)
encoder_polarity = 0                # polarity of the encoder (0 normal)
drive_current = 3000                # current to drive the stepper
microstep_mult = 1024               # the level of microstepping for the motor
stepper_step_angle = 1.8            # stepper full step angle

# calculate the steps per rotation based on microstep_mult and stepper type
steps_per_rotation = (360.0/stepper_step_angle*float(microstep_mult))

# configure the PiMotion to a motor type 3-PHASE
pm.mtr_set_mtr_type(pilib.MTR_TYPE_STEPPER)

# configure the update rate of the trajectory generator
# as a divisor of the master clock (50 MHz)
pm.mtr_set_elec_cycle_div(elec_cycle_div)

# configure the encoder
pm.mtr_set_enc_polarity(0)
pm.mtr_set_enc_counts_per_rot(enc_counts_per_rot)

# setup the constant current to drive the stepper motor
pm.mtr_set_open_loop_drive_current(drive_current)

# reset the motor
print('Resetting motor...')

# configure the motor phase waveforms
pm.mtr_write_phase_mem_stepper(microstep_mult)

# start reset
pm.mtr_reset()

# wait for the reset to complete
while pm.mtr_get_reset_state():
    time.sleep(0.1)
    
print('Finished resetting.')