Source code for wni.motor.commands

# -*- coding: utf-8 -*-
"""
A program is a list of commands; most of the methods relate to commands
found in the Silverpak23C_R3566_Commands.PDF manual.
"""


def _encode(string):
    if isinstance(str):
        return string
    elif isinstance(bytes):
        return string.encode()
    else:
        raise ValueError("Cannot encode object '%r'" % string)

[docs]def start(address): """The command required to start a program. Command: /[address] """ return b'/%d' % address
############################## ### Homing and Positioning ### ##############################
[docs]def home_and_initialize(num_steps): """Motor goes toward 0 until opto sensor is interrupted. The motor will go num_steps steps to find the home sensor; if the sensor is not found, it will stop motion. Only opto #1 can be used with this. Range: [0, 2**31) Command: Z """ return b'Z%d' % num_steps
[docs]def set_current_position(position): """Sets the current position without moving the motor. Works accurately when new position is divisible by 1024. Range: [0, 2**31) Command: z """ return b'z%d' % position
[docs]def move_to(position): """ Move Motor to Absolute position. i.e. moves to the 10000th step. Issuing A10000 again will NOT move the motor because it is already at that value. Range: [0, 2**31) Command: A """ return b'A%d' % position
[docs]def set_home_sensor_polarity(polarity): """ Sets polarity of direction of home sensor, default is 0. Range: 0, 1 Command: f """ return b'f%d' % polarity
[docs]def move_relative_positive(num_steps): """ Move Motor relative number of steps in positive direction. A "P0" command rotates motor infinitely, which enters into Velocity Mode. Any other finite number will set the mode to be in Position Mode. Range: [0, 2**31) Command: P """ return b'P%d' % num_steps
[docs]def move_relative_negative(num_steps): """ Move Motor relative number of steps in negative direction (Note: Motor will not run in the negative direction if the position is at 0. You can use the "z" command to set the 0 position to be further away in the negative direction. OR you can use the F command to reverse direction of rotation.) A "D0" command rotates motor infinitely, which enters into Velocity Mode. Any other finite number will set the mode to be in Position Mode. Range: [0, 2**31) Command: D """ return b'D%d' % num_steps
[docs]def set_jog_distance(distance): """ Sets the distance for pulse jog mode (see n command) Range: [0, 2**31) Command: B """ return b'B%d' % distance
[docs]def terminate_current_command(): """ Terminate current command Range: None Command: T """ return b'T'
[docs]def reverse(direction): """ Reverses the positive direction to be negative. The P and D commands will switch directions. Default is 0; valid values are 0 and 1. Range: [0, 1] Command: F """ return b'F%d' % direction
######################### ### Velocity commands ### #########################
[docs]def set_velocity(v): """ In Position Mode, this sets the Top Speed of the Motor in microsteps/sec. During velocity mode, speed can be changed on the fly (during rotation). Range: [0, 2**31) Command: V """ return b'V%d' %v
[docs]def set_acceleration(a): """ This sets the Acceleration factor microsteps/sec^2 = (L Value) x (6103.5). i.e. /1L1R takes 16.384 Seconds to get to a speed of V=100000 (microsteps/sec) Default is L=1000, default speed V = 305175 microsteps/sec, so default acceleration = 6103500 microstepssec^2. I should take 0.05 seconds to get to to top speed. Range: [0, 65000] Command: L """ return b'L%d' % a
####################### ### Setting current ### #######################
[docs]def set_current(current): """ Sets the running current on a scale of 0 to 100% of the max current, 3.0A. Default setting is m30. Range: [0, 100] Command: m """ return b'm%d' % current
[docs]def hold_current(current): """ Sets the Hold Current on a scale of 0 to 50% of the max current, 3.0 Amps. Default setting is h10. Range: [0, 50] Command: h """ return b'h%d' % current
############################# ### Looping and Branching ### #############################
[docs]def begin_loop(): """ Beginning of a repeat loop Command: g """ return b'g'
[docs]def end_loop(repetitions): """ End of a repeat loop. Loops can be nested up to 4 levels. A value of 0 causes the loop to be infinite. Range: [0, 30000] Command: G """ return b'G%d' % repetitions
[docs]def delay(milliseconds): """ Delay for given number of milliseconds Range: [0, 30000] Command: M """ return b'M%d' % milliseconds
[docs]def halt_wait(value, pin): """ Halt current command string and wait until condition specified. Args: value: 0 or 1; 0 for low, 1 for high pin: one of 1-4, inclusive. Causes the motor to delay executing commands until the condition is met. 01 Wait for low on input 1 (Pin 13) 11 Wait for high on input 1 (Pin 13) 02 Wait for low on input 2 (Pin 5) 12 Wait for high on input 2 (Pin 5) 03 Wait for low on input 3 (Pin 7) 13 Wait for high on input 3 (Pin 7) 04 Wait for low on input 4 (Pin 14) 14 Wait for high on input 4 (Pin 14) * Halted operation can also be resumed by typing /1R * If it is desired to stop motion rather than to wait for the input and move, one can use input 2 (pin 5) to stop motion of a P0 or D0 command. /1P0R and close input 2 to ground to stop motion. Range (two arguments): [0, 1], [1, 4] Commmand: H<value><pin> """ if not 0 <= value <= 1 and 1 <= pin <= 4: msg = ('value must be 0 or 1, pin must be 1-4. Got value "%s" and ' ' pin "%s"') % (value, pin) raise ValueError(msg) return b'H%d%d' % (value, pin)
[docs]def skip_next_if(value, pin): """ Skip command: will skip the command following it if the input is high or low. Useful for executing different programs based on a high or low signal on an input. The args are the same as in halt_wait Range (two arguments): [0, 1], [1, 4] Command: S<value><pin> """ if not 0 <= value <= 1 and 1 <= pin <= 4: msg = ('value must be 0 or 1, pin must be 1-4. Got value "%s" and ' ' pin "%s"') % (value, pin) raise ValueError(msg) return b'S%d%d' % (value, pin)
[docs]def set_mode(mode): """ Sets Modes - Interpret as combination of Binary Bits Bit0: Enable Pulse Jog Mode. Jog distance is given by "B" command. Velocity is given by command. The Switch Inputs 1 and 2 become the Jog Inputs. Press input 1 to rotate CW for a given distance, B. Press input 2 to rotate CCW for a given distance, B. If also in position correction mode, "B" will will be in microsteps and not encoder counts. Bit1: Enable Limit. The opto input #3, pin 7, becomes one limit switch for rotating CW (using P command). The opto input #4, pin 14, becomes the other limit switch for rotating CCW (using D command). Use /1P0R to rotate towards the limit switch. Can be combined with other commands like: /1P0P500R: rotate continuously until input 3 is low, then move 500 more steps. Bit2: /1n4R Enable Continuous Jog Mode. Continuous run of motor while switch is depressed. Velocity is given by the "V" command. Note that the jog mode allows moves below zero, which will be interpreted by any subsequent "A" commands as a large positive number. If this is undesirable, please use the "z" command to define zero position to be some positive number so that underflow will not occur. Bit3: Enables Position Correction Mode. Bit4: Enabled Overload Report Mode. Bit5: Enable Step And Direction Mode A value of 1 for this mode, or enable Dual Encoder Mode if value of 0. i.e. /1n96<CR> (96=32+64) Enables step and dir mode and slaves the motor to it. Bit6: Enable Motor slave to encoder/step-dir. Range: [0, 4095] Command:n """ return b'n%d' % mode
################################################# ### Position Correction - Encoder Option Only ### #################################################
[docs]def special_modes(mode): """ Special Modes potential values: 1. Encoder with no index (default). Motor will home to the opto sensor #1, pin 7. 2. Encoder with index. Homes to the index. Range: 1, 2 Command: N """ return b'N%d' % mode
[docs]def set_accuracy(a): """ When in position correction mode, set distance allowed to move before the motor corrects using encoder feedback. See Appendix 1 Range: [1, 65000] Command: aC """ return b'ac%d' % a
[docs]def set_encoder_ratio(ratio): """ Set Encoder ratio. This sets the ratio between the encoder ticks/rev an the microsteps/rev for the motor. See Appendix 1 Range: [1000, 10^6] Command:aE """ return b'aE%d' % ratio
[docs]def set_overload_timeout(timeout): """ Set Overload Timeout. This sets the number of times the move is retried in case a move stalls. See Appendix 1 Range: [1, 10^6] Command: au """ return b'au%d' % timeout
[docs]def recover_encoder(): """ This will recover the encoder after there has been a timeout due to an overload timeout on the encoder (See Appendix 1) Range: None Command: r """ return b's'
################################## ### Program Storage and Recall ### ##################################
[docs]def store_program(program): """ Stores a program. Program 0 is executed on power up (Total of 14 commands max per string when storing, but if not storing you can send 256 characters max). Range: [0, 15] Command: s """ return b's%d' % program
[docs]def execute_program(program): """ Executes the Stored Programs 0-15. You can execute one program within a program. i.e. /1s0V500j2e1R, and /1s1gP1000M500G5e2R and /1s2f1Z100000R: This example will power on, execute speed of 500, 2x microstep and execute program #1: move 1000 steps and pause 0.5 seconds (5 times), then execute program 2 which will home the motor in the opposite direction. Range: [0, 15] Command: e """ return b'e%d' % program
######################### ### Program Execution ### #########################
[docs]def run_program(): """ Run the command string that is currently in the execution buffer -- Always end commands with 'R'. Range: None Command: R """ return b'R'
[docs]def repeat_program(): """ Repeat the current command string Range: None Command: X """ return b'X'
##################### ### Microstepping ### #####################
[docs]def set_microsteps_per_step(microsteps): """ Adjusts the resolution in micro-steps per step. Available resolutions: Full step, 2x, 4x, 8x, 16x, 32x, 64x, 128x, 256x. Range: 1, 2, 4, 8, 16, 32, 64, 128, 256 Command: j """ return b'j%d' % microsteps
[docs]def adjust_microsteps(adjustment): """ Allows user to correct any unevenness in microstep size. Adjusts audible noise and should be executed while motor is running. Should only be adjusted in small increments and should not exceed 1650 and should not be set below 1400. Range: [1400, 1650] Command: o """ return b'o%d' % adjustment
################################ ### On/Off Drivers (Outputs) ### ################################
[docs]def on_off_driver(value): """ On/Off Driver. Turns on or off the two outputs. (I/O's are bidirectional) It's a two bit Binary value: 3=0b11=Both Drivers On, 2=0b10=Driver2 on, Driver1 off, etc. (Drivers output 3VDC max) Range: [0, 3] Command: J """ return b'J%d' % value
###################### ### Query Commands ### ###################### # # The following commands are queries and cannot be cascaded in strings or # stored. They can be executed while other commands are still running.
[docs]def get_position(): """ Returns the current motor position Command: ?0 """ return b'?0'
[docs]def get_start_velocity(): """ Returns the current Start Velocity Command: ?1 """ return b'?1'
[docs]def get_slew_speed(): """ Returns the current Slew Speed for Position Mode Command: ?2 """ return b'?2'
[docs]def get_stop_speed(): """ Returns the current Stop Speed Command: ?3 """ return b'?3'
[docs]def get_input_values(): """ Returns the status of all four inputs, 0-15 representing a 4 bit binary pattern: Bit 0 = Input 1 (Pin 13) Bit 1 = Input 2 (Pin 5) Bit 2 = Input 3 (Pin7) Bit 3 = Input 4 (Pin 14) Command: ?4 """ return b'?4'
[docs]def get_velocity(): """ Returns the current Velocity mode speed Command: ?5 """ return b'?5'
[docs]def get_step_size(): """ Returns the current step size Command: ?6 """ return b'?6'
[docs]def get_o_value(): """ Returns the current 'o' value (TODO: microstep adjustment?) Command: ?7 """ return b'?7'
[docs]def get_encoder_position(): """ Returns the current encoder position (can be zeroed by "z" command) Command: ?8 """ return b'?8'
[docs]def erase_programs(): """ Commandes all commands/program stored in EPROM except for the any settings such as current, microstepping, velocity, acceleration and any other settings Command: ?9 """ return b'?9'
[docs]def get_current_command(): """ Recalls current command executed. To see what currently stored in a specific program, run the program and issue the /1$ example: /1e2R /1$ Command: $ """ return b'$'
[docs]def get_firmware(): """ Returns current Firmware revision and date Command: & """ return b'&'
[docs]def get_controller_status(): """ Query current status of the controller: 0 = No Error 1 = Initialization error 2 = Bad Command 3 = Operand out of range """ return b'Q'
[docs]def send_number(n): """ Sends out the number 66 or any number placed after p, mostly used for confirmation of when a move is done. For example /1P1000p66R will send out the number 66 when the motor has moved 1000 steps. (Any number can be used after p). Command: p """ return b'p%d' % n
[docs]def set_baud_rate(rate): """ This command will usually be stored as program zero and execute on power up. Default baud rate is 9600. Range: 9600, 19200, 38400 Command: b """ return b'b%d' % rate