AllenInstitute / pytic

PyTic - An Object-Oriented Python Wrapper for Pololu Tic Stepper Drivers
Other
10 stars 6 forks source link

How to update variables? #6

Open inregards2pluto opened 1 year ago

inregards2pluto commented 1 year ago

I'm working with a Pololu T500 controller and having some issue with implementing the example code. I've updated all the settings appropriately, but when I try to issue commands, whether it's setting a position or velocity, nothing happens. The motor successfully energizes and I can even hear it's attempts to get to speed, but the impellar I have attached doesn't move. When I go through the Tic Gui, I'm able to get it moving no problem.

After some digging, it looks like the planning_mode variable may be the issue? I used the print(tic.variables.planning_mode) and the output was 0. Based on the manual, the controller isn't receiving step commands while in this state and it needs to be set to either 1 or 2 for position and velocity inputs respectively. However, I'm not seeing a clear explanation in the README on how to do this. The settings, yes, but not the variables.

Has anyone else needed to do this and, if so, could someone offer some guidance? image

inregards2pluto commented 1 year ago

Okay, so after further investigation, it does look like using the tic.set_target_velocity/position function is, in fact, updating the planning mode. I just needed to energize the motor first. However, despite the planning mode and and target_velocity being updated, current velocity stays at 0. I'm not sure what's going on because I can hear it starting and stopping when I energize and de-energize it, but there's no change in position/velocity when prompted.

I also notice that, when I open the Tic Gui and try to operate the motor after running the script, changing the target velocity ALSO doesn't result in movement or changes from the current velocity of 0. Once I apply the original settings, everything works fine in the GUI.

This all leads me to believe that SOMETHING is happening in the settings while I try to operate the motor using PyTic. Below is the code I'm using, as well as my config.yml, the Tic GUI settings that I KNOW work (tic_settings.txt), and the Tic Gui settings when I try to control the motor within the GUI immediately after running my code (tic_settings_bak.txt). I've been looking between everything to figure out where the offending change is and I'm having trouble finding anything meaningful.

import csv
import pytic

# - Functions ------------------------------------------------

# Energize motor

def start():
    tic.energize()
    tic.exit_safe_start()
    print('Energizing motor...')

# De-energize motor and get error status

def shutdown():
    tic.enter_safe_start()
    tic.deenergize()
    print('Deenergizing motor...')
    print(tic.variables.error_status)

# - Initialization -------------------------------------------

tic = pytic.PyTic()

# Connect to first available Tic Device serial number over USB
serialNums = tic.list_connected_device_serial_numbers()
tic.connect_to_serial_number(serial_number=serialNums[0])

# Load configuration file and apply settings
tic.settings.load_config("config.yml")
tic.settings.apply()

# - Motion Command Sequence ----------------------------------

# Zero current motor position
tic.halt_and_set_position(0)

# Energize Motor
start()

tic.set_target_velocity(2000000)
# tic.set_target_position(100)

print(f'Target Velocity: {tic.variables.target_velocity}')
print(f'Target Position: {tic.variables.target_position}')
print(f'Current Velocity: {tic.variables.current_velocity}')
print(f'Current Position: {tic.variables.current_position}')
print(f'Planning Mode: {tic.variables.planning_mode}')

# De-energize motor and get error status
while True:
    try:
        endPrompt = input('Stop Motor? Y/N  ')
    except ValueError:
        print('Incorrect input')
    finally:
        if endPrompt == 'Y' or endPrompt == 'y':
            shutdown()
            break
        elif endPrompt == 'N' or endPrompt == 'n':
            print("Use your spiral power!")
        else:
            print('Incorrect input')

config.yml

tic_settings: # required header for load_config fcn.
    product: TIC_PRODUCT_ID_T500
    step_mode: TIC_STEP_MODE_FULL
    decay_mode: TIC_DECAY_MODE_T500_AUTO
    control_mode: TIC_CONTROL_MODE_SERIAL
    never_sleep: False
    disable_safe_start: True
    ignore_err_line_high: False
    auto_clear_driver_error: True
    soft_error_response: TIC_RESPONSE_DECEL_TO_HOLD
    soft_error_position: 0
    serial_baud_rate: 9600
    serial_crc_enabled: False
    serial_device_number: 14
    serial_alt_device_number: 0
    serial_enable_alt_device_number: False
    serial_14bit_device_number: False
    command_timeout: 0
    serial_crc_for_commands: False
    serial_crc_for_responses: False
    serial_7bit_responses: False
    serial_response_delay: 0
    vin_calibration: 0
    input_averaging_enabled: True
    input_hysteresis: 0
    input_scaling_degree: 0
    input_invert: False
    input_min: 0
    input_neutral_min: 2015
    input_neutral_max: 2080
    input_max: 4095
    output_min: -200
    output_max: 200
    encoder_prescaler: 1
    encoder_postscaler: 1
    encoder_unlimited: False
    scl_config: 0
    sda_config: 0
    tx_config: 0
    rx_config: 0
    rc_config: 0
    invert_motor_direction: True
    max_speed: 200000000
    starting_speed: 0
    max_accel: 40000
    max_decel: 0
    current_limit: 1982
    current_limit_during_error: -1
    auto_homing: False
    auto_homing_forward: False
    homing_speed_towards: 1000000
    homing_speed_away: 500000