spike.motor
Motor mock API
Classes
|
Motor mocking class |
- class spike.motor.Motor(port)
Motor mocking class
Class is accessed simultaneously by the user side thread and the ground truth update thread. It is therefore protected by a mutex
- __init__(port)
Contructor
- Parameters:
port (string ('A','B','C','D','E' or 'F')) – the hub port to which the motor is connected
- run_to_position(degrees, direction='shortest path', speed=None)
Runs the motor to an absolute position.
The sign of the speed will be ignored (i.e., absolute value), and the motor will always travel in the direction that’s been specified by the “direction” parameter. If the speed is greater than “100,” it will be limited to “100.”
- Parameters:
degrees (integer [0,359]) – the target position.
direction (string ("Shortest path" could run in either direction, depending on the shortest distance to the target. "Clockwise" will make the motor run clockwise until it reaches the target position. "Counterclockwise" will make the motor run counterclockwise until it reaches the target position.)) – the direction to use to reach the target position.
speed (integer [0,100]. If no value is specified, it will use the default speed that’s been set by set_default_speed().) – the motor’s speed
- Raises:
TypeError – degrees or speed is not an integer or direction is not a string.
ValueError – direction is not one of the allowed values or degrees is not within the range of 0-359 (both inclusive).
- run_to_degrees_counted(degrees, speed=None)
Runs the motor until the number of degrees counted is equal to the value that has been specified by the “degrees” parameter.
The sign of the speed will be ignored (i.e., absolute value), and the motor will always travel in the direction that’s been specified by the “direction” parameter. If the speed is greater than “100,” it will be limited to “100.”
- Parameters:
degrees (integer) – the target degrees counted.
speed (integer [0,100]. If no value is specified, it will use the default speed that’s been set by set_default_speed().) – the motor’s speed
- Raises:
TypeError – degrees or speed is not an integer.
- run_for_degrees(degrees, speed=None)
Runs the motor for a specified number of degrees.
- Parameters:
degrees (integer) – the number of degrees that the motor should run.
speed (integer [-100,100]. If no value is specified, it will use the default speed that’s been set by set_default_speed().) – the motor’s speed
- Raises:
TypeError – degrees or speed is not an integer.
- run_for_rotations(rotations, speed=None)
Runs the motor for a specified number of rotations.
- Parameters:
degrees (float) – the number of rotations that the motor should run.
speed (int [-100,100]. If no value is specified, it will use the default speed that’s been set by set_default_speed().) – the motor’s speed
- Raises:
TypeError – rotations is not a number or speed is not an integer.
- run_for_seconds(seconds, speed=None)
The number of seconds for which the motor should run.
- Parameters:
seconds (float) – the number of rotations that the motor should run.
speed (int [-100,100]. If no value is specified, it will use the default speed that’s been set by set_default_speed().) – the motor’s speed
- Raises:
TypeError – seconds is not a number or speed is not an integer.
- start(speed=None)
Starts running the motor at a specified speed.
The motor will keep moving at this speed until you give it another motor command or when your program ends.
- Parameters:
speed (integer [-100,100]. If no value is specified, it will use the default speed that’s been set by set_default_speed().) – the motor’s speed
- Raises:
TypeError – speed is not an integer.
- stop()
Stops the motor.
What the motor does after it stops depends on the action that’s been set in set_stop_action(). The default value of set_stop_action() is “coast.”
- start_at_power(power)
Starts running the motor at a specified power level.
The motor will keep moving at this speed until you give it another motor command or when your program ends.
- Parameters:
power (int [-100,100].) – power of the motor
- Raises:
TypeError – power is not an integer.
- get_speed()
Retrieves the motor speed.
- Returns:
current motor speed
- Return type:
int [-100,100]
- get_default_speed()
Retrieves the motor default speed.
- Returns:
current motor default speed value
- Return type:
int [-100,100]
- get_position()
Retrieves the motor position. This is the clockwise angle between the moving marker and the zero-point marker on the motor.
- Returns:
the motor’s position.
- Return type:
integer [0,359]
- was_stalled()
Tests whether the motor was stalled.
- Returns:
True if the motor has stalled since the last time was_stalled() was called, otherwise false.
- Return type:
boolean
- was_interrupted()
Tests whether the motor was interrupted.
- Returns:
True if the motor was interrupted since the last time was_interrupted() was called, otherwise false.
- Return type:
boolean
- get_degrees_counted()
Retrieves the number of degrees that have been counted by the motor.
- Returns:
Current counted degrees
- Return type:
integer
- set_default_speed(speed)
Sets the default motor speed. This speed will be used when you omit the speed argument in one of the other methods, such as run_for_degrees. Setting the default speed does not affect any motors that are currently running. It will only have an effect when another motor method is called after this method. If the value of default_speed is outside of the allowed range, the default speed will be set to “-100” or “100” depending on whether the value is negative or positive.
- Parameters:
speed (integer [-100,100]) – the default speed value.
- Raises:
TypeError – default_speed is not an integer
- set_degrees_counted(degrees_counted)
Sets the “number of degrees counted” to the desired value.
- Parameters:
degrees_counted (integer) – new degrees value
- Raises:
TypeError – degrees_counted is not an integer
- set_stall_detection(stop_when_stalled)
Turns stall detection on or off.
Stall detection senses when a motor has been blocked and can t move. If stall detection has been enabled and a motor is blocked, the motor will be powered off after two seconds and the current motor command will be interrupted. If stall detection has been disabled, the motor will keep trying to run and programs will “get stuck” until the motor is no longer blocked.
Stall detection is enabled by default.
- Parameters:
stop_when_stalled (boolean) – choose “true” to enable stall detection or “false” to disable it.
- set_stop_action(action)
Sets the default behavior when a motor stops.
- Parameters:
action (string (coast,brake,hold)) – the desired motor behavior when the motor stops.
- __process_command(command)
Process command until over
- Parameters:
command (generator function) – command to process
- c_reset()
Reset function
Warning
This function is not part of the spike API. It is provided to update the component from scenario data and shall not be used by the end-user.
- c_read(value)
Update the motor from scenario data
Warning
This function is not part of the spike API. It is provided to update the component from scenario data and shall not be used by the end-user.
- Parameters:
value (float) – current motor degrees value
- c_set_stall_detection(stop_when_stalled)
Turns stall detection on or off.
Warning
This function is not part of the spike API. It is provided to update the component from scenario data and shall not be used by the end-user.
- Parameters:
stop_when_stalled (boolean) – True to enable stall detection or False to disable it.
- c_set_stop_action(action)
Sets the default behavior when a motor stops.
Warning
This function is not part of the spike API. It is provided to update the component from scenario data and shall not be used by the end-user.
- Parameters:
action (string (coast,brake,hold)) – the desired motor behavior when the motor stops.
- property port
Sets the component connection port
Warning
This function is not part of the spike API. It is provided to update the component from scenario data and shall not be used by the end-user.
- Returns:
the component port
- Return type:
string