pyPosControl

python_files.models

class python_files.models.ModelControl(axis_names, settings: ModelSettings = None)

Bases: object

Core class of this project.

Launch commands, save current position, read settings from python_files.models.ModelSettings and save concrete settings. Every actions is managed by this class.

__init__(axis_names, settings: ModelSettings = None)
Parameters:
absMove(axis_values: dict, axis_speeds: dict = None, callbacks: list = None, miss_val_cbs: list = None, finally_cbs: list = None)

launch a command to move to axis_values from current position values.

Parameters:
  • axis_values (dict[str:int|float]) – position values to move to for each axis. Formatted like { ‘X’: 50 }. Unit is mm

  • axis_speeds (dict[str:int|float]) – speed values for each axis formatted like { ‘X’: 5 }. Unit is mm/s

  • callbacks (list[function]) – actions to do at the end of the movement

  • finally_cbs (list[function]) – actions to do after the movement in a finally statement

Param:

miss_val_cbs: actions to do if a python_files.connection.MissingValue exception is raised during the movement and the callbacks

Returns:

command(s) sent to controller

Return type:

str

absUpdate(axis_values: dict, axis_speeds: dict)

Update current position values after an absolute movement to the set of positions specified.

Parameters:
  • axis_values (dict[str:int|float]) – position values to move to for each axis. Formatted like { ‘X’: 50 }. Unit is mm

  • axis_speeds (dict[str:int|float]) – speed values for each axis formatted like { ‘X’: 5 }. Unit is mm/s

calcMoveTime(axis_values: dict, axis_speeds: dict, bonus_time=1) float

Calculate the time expected to move from position 0 to axis_values position at axis_speeds speed. Bonus is additional time (s).

Parameters:
  • axis_values (dict[str:int|float]) – position values for each axis formatted like { ‘X’: 50 }. Unit is mm

  • axis_speeds (dict[str:int|float]) – speed values for each axis formatted like { ‘X’: 5 }. Unit is mm/s

  • bonus_time (float | int) – additional time (s).

Returns:

time expected to take to move to specified position from a position 0. Unit is s

Return type:

float | int

checkSpeed(axis_speeds: dict)

check if axis_speed values are within the limits set in the ModelSettings linked.

Parameters:

axis_speeds (dict[str:int|float]) – speed values for each axis formatted like { ‘X’: 5 }. Unit is mm/s

Returns:

axis_speed with speed limit as value if axis_speed values are not in range.

Return type:

dict[str:float|int]

convertMmToSteps(axis_values: dict)

Converts position values from mm to step units according to stepscale in self.settings.stepscales

Parameters:

axis_values (dict[str:int|float]) – position values for each axis formatted like { ‘X’: 50 }. Unit is mm

Returns:

axis_values converted from mm to step units

Return type:

dict[str:int]

goHome(callbacks: list = None, miss_val_cbs: list = None, finally_cbs: list = None)

Set current position as home on the controller.

Returns:

command(s) sent to controller

Return type:

str

goZero(callbacks: list = None, miss_val_cbs: list = None, finally_cbs: list = None)

launch a move command to controller to go position 0 on each axis from the current position values. Speed of the movement is either mid value between max and min, or max/2 if no min, or min*2 if no max, 5mm/s instead.

Parameters:
  • callbacks (list[function]) – actions to do at the end of the movement

  • finally_cbs (list[function]) – actions to do after the movement in a finally statement

Param:

miss_val_cbs: actions to do if a python_files.connection.MissingValue exception is raised during the movement and the callbacks

Returns:

command(s) sent to controller

Return type:

str

incrMove(axis_values: dict, axis_speeds: dict = None, callbacks: list = None, miss_val_cbs: list = None, finally_cbs: list = None)

launch a command to move to axis_values without taking on board current position.

Parameters:
  • axis_values (dict[str:int|float]) – position values to move to for each axis. Formatted like { ‘X’: 50 }. Unit is mm

  • axis_speeds (dict[str:int|float]) – speed values for each axis formatted like { ‘X’: 5 }. Unit is mm/s

  • callbacks (list[function]) – actions to do at the end of the movement

  • finally_cbs (list[function]) – actions to do after the movement in a finally statement

Param:

miss_val_cbs: actions to do if a python_files.connection.MissingValue exception is raised during the movement and the callbacks

Returns:

command(s) sent to controller

Return type:

str

incrUpdate(axis_values: dict, axis_speeds: dict)

Update current position values after a incremental movement to the set of positions specified.

Parameters:
  • axis_values (dict[str:int|float]) – position values to move to for each axis. Formatted like { ‘X’: 50 }. Unit is mm

  • axis_speeds (dict[str:int|float]) – speed values for each axis formatted like { ‘X’: 5 }. Unit is mm/s

quit()

Close and kill the thread waiting list used to execute actions like movements.

Meant to be executed at the end of the program.

rawAction(commands: list[str], callbacks: list = None, miss_val_cbs: list = None, finally_cbs: list = None)

Send list of commands converted in ascii into the serial connection.

Returns:

command(s) sent to controller

Return type:

str

setHome(callbacks: list = None, miss_val_cbs: list = None, finally_cbs: list = None)

Go to current home position on the controller.

Returns:

command(s) sent to controller

Return type:

str

setSpeed(axis, speed)

Set speed value on the specified axis. Unit is mm/s.

Parameters:
  • axis (str) – axis name

  • speed (float | int) – speed value (mm/s)

setValue(axis, value)

Set current position value on the specified axis. Unit is mm.

Parameters:
  • axis (str) – axis name

  • value (float | int) – position/movement value (mm)

setZero()

Set current position values as zero on each axis without moving or sending a command to controller.

stop()

launch a command to stop the controller in it’s task.

Returns:

command(s) sent to controller

Return type:

str

zeroUpdate()

Update current position values to position 0.

class python_files.models.ModelSettings(axis_names: tuple, wait_ack: bool = True)

Bases: object

Load, store and update settings to send the signal.

__init__(axis_names: tuple, wait_ack: bool = True)
Parameters:

axis_names (tuple, list) – list of axis names, up to 3 axis implemented

applyDefault()

Apply current settings with the current default values loaded in self.defaultData.

applySettings(port: str = -1, stepscales: dict = -1, speed_limits: dict = -1, baudrate: int = -1, communication: str = -1)

Apply concrete settings from parameters to model properties

Parameters:
  • port (str) – (Optional) computer port to communicate with the controller. ex: `’COM3’`

  • stepscales (dict[str:int]) – (Optional) stepscale of platines in step/mm unit by axis as key used to convert input to command. ex: `{ ‘x’:500 }`

  • speed_limits (dict[str:int]) – (Optional) speed limits in mm/s unit by axis as key used to secure speed in commands. ex: `{ ‘x’:50 }`

  • baudrate (int) – (Optional) baudrate in baud/s unit to communicate with the controller.

  • communication (str) – (Optional) language of commands to communicate with the controller.

applySettingsFromData()

Apply concrete settings from values in self.settingsData dictionary;

axisParameters = ['platine']
getAvailablePorts()

Read available ports from pyserial.Serial. Returns format is like : { “COM3”: { “name”: “COM3”, “value”: “COM3” } }

Returns:

dictionary of available ports

Return type:

dict[dict]

getSettingsDict() dict

Returns a python dictionary with current settings.

The dictionary is organized like :

```python {

“parameters”: {
“platineX”: {

“name”: “Platine X”, “unit”: “step/mm”, “default”: “Platine 1”, “options”: {

#*other linear turntables/platines in self.platinesData*

}

}, “port”: {

“name”: “Port”, “default”: “COM3”, “options”: {

#*other ports in self.portsData*

}

}, “controller”: {

“name”: “Controller”, “unit”: “baud/s”, “default”: “Controller 1”, “options”: {

#*other controllers in self.controllersData*

}

}

}, “configs”: {

“Config 1”: {

“platineX”: “Platine 1”, “port”: “COM3”, “controller”: “Controller 1”

}

}

}

isAxisRelated(data: str) tuple[bool, str]

Check if an axis name is at the end of a string.

Parameters:

data (str) – data that will be look at

Returns:

Returns firstly a boolean value if end of data string is in self.axis. Second return value is the axis name if found, empty string otherwise.

Return type:

tuple[bool,str]

loadSettings(path)

Load settings from a json file into self.saveData, self.defaultData, self.platinesData, self.controllersData and self.configsData properties. Will load settings_filessave.json file and then controller, platines and config files specified in the save.json file.

Parameters:

path (str) – current path of the app files, will try to load the settings file at pathsettings_filessave.json

saveSettings(path: str, port: str = None, platines: dict = None, controller: str = None)

Save and apply settings in parameters and current into json files.

Parameters:
  • path (str) – absolute path of the current app files, will be used to look at $settings_filessave.json

  • port (str) – (Optional) computer port to communicate with the controller. ex: `’COM3’`

  • platines (dict[str:str]) – (Optional) platines settings as a dict with key as axis. ex: `{ ‘x’:”Platine Name” }`

  • controller (str) – (Optional) controller name to communicate with.

class python_files.models.ThreadExecutor(name: str)

Bases: Thread

Thread waiting list.

wait_list_size is a static parameter.

__init__(name: str)
Parameters:

name (str) – ThreadExecutor name shown in logger messages

addTask(newtask, *taskargs)

Create thread for the new task and add it to the waiting list.

Returns:

Added thread if everything went well, 1 if waiting list was full and task not took into account.

Return type:

Thread | int

addThreadedTask(newthread: Thread)

Add task in a thread to the waiting list if not full.

Returns:

0 if everything went well, 1 if waiting list was full and task not took into account.

Return type:

int

getState()

Return value possibilities: - 0 : nothing is running. - 1 : one task is running, waiting list is empty. - 2 : one task is running, waiting list is full. - 3 : one task is running, waiting list has some tasks. - 4 : no task is running but waiting list has some tasks.

Returns:

state of the ThreadExecutor

Return type:

int

isRunning()
Returns:

If running or not.

Return type:

bool

kill()

End the infinite loop of waiting and execute next action. Does not interrupt an action.

run()

Run until killed. Run tasks from it’s waiting list.

wait_list_size: int = 1
python_files.models.functionPackage(callbacks: list = None, miss_val_cbs: list = None, finally_cbs: list = None)
Parameters:
  • callbacks (list[function]) – list of functions to be executed in a try statement

  • miss_val_cbs (list[function]) – list of functions to be executed in a except python_files.connection.MissingValue statement.

  • finally_cbs (list[function]) – list of functions to be executed in a finally statement.

python_files.models.inWithStartKeys(value: str, startkeys: list)

Returns if a start key has been found at the beginning of the value.

Parameters:
  • value (str) – value to be check if a start key is in

  • startkeys (list[str]) – start keys to check inside value

Returns:

a start key has been found in value

Return type:

boolean

python_files.models.removeWithStartKey(dico: dict, startkey: str)

Remove the beginning of dico keys if startkey has been found.

Parameters:
  • dico (dict[str:]) – dictionary without startkey in it’s keys

  • startkey (str) – start key to remove in the dico

Returns:

dico without startkey in it’s keys

Return type:

dict[str:]

python_files.communications

class python_files.communications.CSeries(axis_speeds: dict = None)

Bases: Commands

Summary:

Commands language C-series extended from Commands abstract class.

__init__(axis_speeds: dict = None)
axisDefinition(nbAxis: int) int
axisDefinitionCmd(nbAxis: int) str

Parameters: - nbAxis : number of axis meant to move. Returns: - command to define axis meant to move.

commandsToString(commands: list) str

Returns a list of commands in string type

goHome(nbAxis: int) list

Returns a list of commands to go home

moveCmd(axis_values: dict, axis_speeds: dict = None) list

Parameters: - axis_values : dictionary of positions by axis to move to. Move to the specified position from it’s current position. Unit is in steps. - axis_speeds : dictionary of speed values by axis. Speed set for each axis for the move to come. Unit is in step/s. Returns a list of two commands: - first one is to define axis that are concerned by the move. - second one is the movement command to specified position from current position.

setHome(nbAxis: int) list

Returns a list of commands to set current position as home

stopCmd() str

Returns one command to make the controller stop it’s current action.

class python_files.communications.Commands(axis_speeds: dict = None)

Bases: ABC

Summary:

Abstract class definining mandatory methods each command language need to have implemented. Currently stopCmd and moveCmd are the mandatory methods.

__init__(axis_speeds: dict = None)
abstractmethod commandsToString(commands: list) str

Returns a list of commands in string type

abstractmethod goHome(nbAxis: int) list

Returns a list of commands to go home

abstractmethod moveCmd(axis_values: dict, axis_speeds: dict = None) list

Returns a list of commands to make a move.

abstractmethod setHome(nbAxis: int) list

Returns a list of commands to set current position as home

abstractmethod stopCmd() list

Returns a list of commands to make a stop.

class python_files.communications.Test(axis_speeds: dict = None)

Bases: Commands

Summary:

Test commands that return “bonjour” commands.

__init__(axis_speeds: dict = None)
commandsToString(commands: list) str

Returns a list of commands in string type

goHome(nbAxis: int) list

Returns a list of commands to go home

moveCmd(axis_values: dict, axis_speeds: dict = None) list

Returns a list of commands to make a move.

setHome(nbAxis: int) list

Returns a list of commands to set current position as home

stopCmd() list

Returns a list of commands to make a stop.

python_files.communications.getCommandsClass(communication: str, *args, **kwargs) Commands

python_files.connection

exception python_files.connection.MissingValue

Bases: Exception

Meant to be raise when a value like a setting is not set.

class python_files.connection.SerialConnection(timeout: int = 10, bytesize: int = 8, wait_ack=True)

Bases: Serial

Manage a serial connection to send commands.

Attributes:
  • port: str, port name of the connection.

  • baudrate: int, information signal speed, unit is baud/s.

  • timeout: int, maximum time to send a command without any response, unit is s.

  • bytesize: message length

  • parity

__init__(timeout: int = 10, bytesize: int = 8, wait_ack=True)
Parameters:
  • timeout (float | int) – max waiting time when reading before aborting

  • bytesize (int) – bytesize of the serial connection

  • wait_ack (bool) – (Optional) Wait for an acknowledge message after sending each command.

available_serial_ports()

Read the available serial ports names

Raises:

EnvironmentError – On unsupported or unknown platforms

Returns:

A list of the serial ports available on the system

Return type:

list[str]

close()

Close the current serial link if it is open.

executeCmd(commands, port=None)

Execute a single command or a list of commands. Open a serial link and close it afterwards.

Return options : - 0 if no acknowledge is recieved, all commands might not have been sent. - 1 if all commands were sent and all acknowledge revieved.

Parameters:
  • commands (str | byte | list[str|byte]) – commands to be executed, byte format should be ascii.

  • port (str) – (Optional) port to send commands to.

Returns:

success state of the transmission.

Return type:

int

executeSelfCmd(commands, port=None)

Execute a single command or a list of commands. Open the serial link if necessary but let it open afterwards.

Return options : - 0 if no acknowledge is recieved, all commands might not have been sent. - 1 if all commands were sent and all acknowledge revieved.

Parameters:
  • commands (str | byte | list[str|byte]) – commands to be executed, byte format should be ascii.

  • port (str) – (Optional) port to send commands to.

Returns:

success state of the transmission.

Return type:

int