Motorο
- class bapsf_motion.actors.motor_.Motor(*, ip: str, limit_mode: int = None, current: float | int = 0.8, speed: float | int = 4.0, name: str = None, logger: Logger = None, loop: AbstractEventLoop = None, auto_run: bool = False, parent: EventActor | None = None)ο
Bases:
EventActorAn actor class for directly communicating to an ethernet based stepper motor. This actor is only aware of the motor, and is ignorant of how it is situated within a probe drive. Thus, all units are in motor units, i.e. steps, counts, rev, and sec.
- Parameters:
ip (
str) β IPv4 address for the motorlimit_mode (
int, optional) β Define the operational mode of the motor limit switches. Value should be an integer of value 1, 2, or 3. 1 indicates limit is activated when energized, 2 indicates limit is activated when de-energized, and 3 indicates no limits. (DEFAULT:1)current (
float, optional) β A value between 0 (non-inclusive) and 1 (inclusive) which indicates what the motor current should be set to as a fraction of the max allowed currentmotor["DEFAULTS"]["max_current"]. (DEFAULT:0.8)name (
str, optional) β Name the motor. IfNone, then the name will be automatically generated. (DEFAULT:None)logger (
Logger, optional) β An instance ofLoggerthat the Actor will record events and status updates to. IfNone, then a logger will automatically be generated. (DEFAULT:None)loop (
asyncio.AbstractEventLoop, optional) β Instance of anasyncioevent loop. Communication with the motor will happen primaritly through the evenet loop. IfNone, then an event loop will be auto-generated. (DEFAULT:None)auto_run (bool, optional) β If
True, then the event loop will be placed in a separate thread and started. This is all done via therun()method. (DEFAULT:False)
Examples
Using
Motorwithauto_start=True.>>> import logging >>> logging.basicConfig(level=logging.NOTSET) >>> lgr = logging.getLogger() >>> m1 = Motor( ... ip="192.168.0.70", ... name="m1", ... logger=lgr, ... auto_run=True, ... ) >>> # now stop the actor, which stops the event loop >>> m1.terminate()
Using
Motorwithauto_start=False.>>> import logging >>> logging.basicConfig(level=logging.NOTSET) >>> lgr = logging.getLogger() >>> m1 = Motor( ... ip="192.168.0.70", ... name="m1", ... logger=lgr, ... ) >>> # start the actor, with starts the event loop >>> m1.run() >>> # now stop the actor, which stops the event loop >>> m1.terminate()
Attributes Summary
Configuration dictionary of the actor.
Trueif the TCP connection is established with the physical motor.The number of encoder counts per motor revolution.
Current encoder positional reading for the motor, in encoder units
counts.The
asyncio.Taskassociated with the motor's heartbeat.Heartrate of the motor monitor, or the time (in sec) between motor checks.
IPv4 address for the motor
The
Loggerinstance being used for the actor.The
asyncioevent loop for the actor.Dictionary containing properties of the Applied Motion STM motor.
(
str) A unique name given for the instance of the actor.Port used for motor communication.
Current position of the motor, in motor units
steps.Dictionary of class setup parameters.
Collection of all the signals emitted by the
Motorclass.Instance of the socket used for motor communication.
Current status of the motor.
The number of steps the motor does per revolution.
List of
asyncio.Tasks this actor has in its event loop.Indicates if the actor has been terminated.
The
Threadthe event loop is running in.Methods Summary
connect()Open the ethernet connection to the motor.
continuous_jog([direction])Start a continuous jog.
disable()Disable motor (i.e. reduce motor current to zero).
enable()Enable motor (i.e. restore drive current to motor).
Set the motor absolute position 'SP' to be consistent with the current encoder reading.
Move the motor off of a CW (forward) or CCW (backward) limit switch.
move_to(pos)Move the motor to a specified location.
Reset running and idle currents to their default values.
retrieve_motor_alarm([defer_status_update,Β ...])Retrieve [if any] motor alarm codes.
retrieve_motor_status([direct_send])Retrieve motor status and update
self._status.run([auto_run])Activate the
asyncioevent loop.send_command(command,Β *args[,Β thread_id])Send
commandto the motor, and receive its response.set_current(percent[,Β skip_setting])Set the peak current setting ("peak of sine") of the stepper drive, also known as the running current.
set_idle_current(percent)Set the motor's idle current.
set_limit_mode(limit_mode[,Β skip_setting])set_position(pos)Set current motor's absolute and encoder position.
set_speeds(speed[,Β skip_setting])setup_event_loop([loop])Set up the
asyncioevent loop.sleep(delay)Sleep for X seconds defined by
delay.Start or restart the heartbeat
asyncio.Task.stop([soft])Stop motor movement.
terminate([delay_loop_stop])Stop the actor's event loop.
zero()Define current motor position as zero.
Attributes Documentation
- configο
Configuration dictionary of the actor.
Warning
This dictionary should never be written to from outside the owning actor.
- counts_per_revο
The number of encoder counts per motor revolution.
- heartbeat_taskο
The
asyncio.Taskassociated with the motorβs heartbeat.
- heartrateο
Heartrate of the motor monitor, or the time (in sec) between motor checks. There are two different heartrates: (1)
heartrate.basefor when the motor is not moving, and (2)heartrate.activefor when the motor is moving.
- ipο
IPv4 address for the motor
- local_addressο
- loggerο
The
Loggerinstance being used for the actor.
- loopο
The
asyncioevent loop for the actor.
- motorο
Dictionary containing properties of the Applied Motion STM motor.
- nameο
(
str) A unique name given for the instance of the actor. This name is used as an identifier in the actor logger (seelogger).If the user does not specify a name, then the Actor should auto-generate a name.
- parentο
- portο
Port used for motor communication.
- setupο
Dictionary of class setup parameters.
- signalsο
Collection of all the signals emitted by the
Motorclass.See
MotorSignalsfor additional documentation on the individual signals.
- socketο
Instance of the socket used for motor communication.
- statusο
Current status of the motor.
- steps_per_revο
The number of steps the motor does per revolution.
- tasksο
List of
asyncio.Tasks this actor has in its event loop.
- terminatedο
Indicates if the actor has been terminated.
- threadο
The
Threadthe event loop is running in.If
loopwas given during instantiation, then there is no way of obtaining the thread object the event loop is running in. In this casethreadwill beNone.The thread id can always be retrieved using
_thread_id.
Methods Documentation
- connect()ο
Open the ethernet connection to the motor. The number of reconnection attempts before an exception is raised is defined by
self._setup["max_connection_attempts"].
- continuous_jog(direction='forward')ο
Start a continuous jog. The motor will not stop until commanded to.
- disable()ο
Disable motor (i.e. reduce motor current to zero).
- enable()ο
Enable motor (i.e. restore drive current to motor).
- encoder_correct_position()ο
Set the motor absolute position βSPβ to be consistent with the current encoder reading.
- move_off_limit()ο
Move the motor off of a CW (forward) or CCW (backward) limit switch.
- move_to(pos: int)ο
Move the motor to a specified location.
- Parameters:
pos (int) β Position (in steps) for the motor to move to.
- reset_currents()ο
Reset running and idle currents to their default values.
- retrieve_motor_alarm(defer_status_update: bool = False, direct_send: bool = False) Dict[str, Any] | AckFlagsο
Retrieve [if any] motor alarm codes.
- Parameters:
defer_status_update (bool) β If
True, then do NOT updateself._status. (DEFAULT:False)direct_send (bool) β If
True, then the motor commands will bypass any active event loop and be directly sent to the motor. IfFalse, then all motor commands will be routed through thesend_command()method. (DEFAULT:False)
- Returns:
Alarm status.
- Return type:
Dict[str, Any]
- retrieve_motor_status(direct_send=False)ο
Retrieve motor status and update
self._status.- Parameters:
direct_send (bool) β If
True, then the motor commands will bypass any active event loop and be directly sent to the motor. IfFalse, then all motor commands will be routed through thesend_command()method. (DEFAULT:False)
- run(auto_run=True)ο
Activate the
asyncioevent loop. If the event loop is running, then nothing happens. Otherwise, the event loop is placed in a separate thread and set torun_forever.
- send_command(command: str, *args, thread_id=None)ο
Send
commandto the motor, and receive its response. If the event loop is running, then the command will be sent as a threadsafe coroutine in the loop. Otherwise, the command will be sent directly to the motor.
- set_current(percent: float | int, skip_setting: bool = False) float | Noneο
Set the peak current setting (βpeak of sineβ) of the stepper drive, also known as the running current. The value given is a fraction of 0-1 of the peak allowable current defined in
_motor["DEFAULTS"]["max_current"].Setting the running current can affect the idle current, since the max idle current is 90% of the running current.
- Parameters:
percent (
float) β A value of 0 - 1 specifying a fraction of the max running current to set the running current to. For example,0.5will set the running current to 50% of the max allowable running current (_motor["DEFAULTS"]["max_current"]).skip_setting (
bool) β (DEFAULT:False) IfTrue, then do NOT set the currents. Just to the value validation.
- Returns:
The set fractional current
percent, orNoneif inputpercentcould not be set.- Return type:
float | None
- set_idle_current(percent)ο
Set the motorβs idle current. The idle current is the current supplied to the stepper motors when the motor is not moving. The value given is a fraction 0 - 0.9 of the running current.
- Parameters:
percent (
float) β A value of 0 - 0.9 specifying a fraction of the running current to set the idle current to. For example,0.5will set the idle current to 50% of the running current.
- set_position(pos)ο
Set current motorβs absolute and encoder position.
posis the desired absolute position, and the encoder position will be calculated frompos.- Parameters:
pos (
int) β An integer in the range of +/- 2,147,483,647 to set the motorβs absolute position.
- setup_event_loop(loop: AbstractEventLoop | None = None)ο
Set up the
asyncioevent loop. If the given loop is not an instance ofAbstractEventLoop, then a new loop will be created.- Parameters:
loop (
asyncio.AbstractEventLoop) βasyncioevent loop for the actorβs tasks
- sleep(delay)ο
Sleep for X seconds defined by
delay. The routine is smart enough to know if the event loop is running or not. If the event loop is not running the sleep will be issued viatime.sleep, otherwise it will leverageasyncio.sleep.- Parameters:
delay (Real) β Number of seconds to sleep.
- start_heartbeat()ο
Start or restart the heartbeat
asyncio.Task.
- stop(soft=False)ο
Stop motor movement.
- terminate(delay_loop_stop=False)ο
Stop the actorβs event loop. All actor tasks will be cancelled, the connection to the motor will be shutdown, and the event loop will be stopped.
- Parameters:
delay_loop_stop (bool) β If
True, then do NOT stop the event loop. In this case it is assumed the calling functionality is managing additional tasks in the event loop, and it is up to that functionality to stop the loop. (DEFAULT:False)
- zero()ο
Define current motor position as zero.