Source code for cmd_motion

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# 
# Copyright 2012-2017 Frédéric Magniette, Miguel Rubio-Roy
# This file is part of Pyrame.
# 
# Pyrame is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Pyrame is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrame.  If not, see <http://www.gnu.org/licenses/>

import conf_strings,pools,apipools

motion_pool=pools.pool("motion")
api_pool=apipools.api_pool()

def check_param(param,valid_list,default_param,map_func,unique=True):
    if param=="undef" or param=="":
        param=default_param
    if len(param)!=len(default_param) or any(c not in valid_list for c in param):
        return 0,"invalid param"
    result=map(map_func,param)
    if unique and len(set(result))!=len(result):
        return 0,"repeated characters"
    return 1,result

[docs]def init_motion(motion_id,conf_string): "Initialize axis of motion system with *conf_string*" motion={} try: conf=conf_strings.parse(conf_string) except Exception as e: return 0,str(e) motion["id"]="motion_%s"%(motion_id) motion["model"]=conf.name # Get API of the module and add it to the api_pool, if it's not already there if not api_pool.is_present(conf.name): retcode,res=submod_execcmd("getapi@"+conf.name) if retcode==0: return 0,"Can't get API for %s <- %s"%(conf.name,res) api_pool.add_api_from_string(conf.name,res) retcode,res=submod_execcmd("init@"+conf.name,motion["id"],conf_string) if retcode==0: return 0,"error initializing motion %s <- %s"%(motion["id"],res) motion_pool.new(motion_id,motion) return 1,"ok"
[docs]def deinit_motion(motion_id): "Deinitialize motion system" try: motion=motion_pool.get(motion_id) except Exception as e: return 1,str(e) retcode,res=submod_execcmd("deinit@"+motion["model"],motion["id"]) if retcode==0: return 0,"error deinitializing motion %s <- %s"%(motion["id"],res) motion_pool.remove(motion_id) return 1,"ok"
[docs]def config_motion(motion_id,pos_max,pos_min): "Configure the motion system to use the maximum and minimum values *pos_max* and *pos_min*." try: motion=motion_pool.get(motion_id) except Exception as e: return 0,str(e) motion["pos_max"]=float(pos_max) motion["pos_min"]=float(pos_min) if motion["pos_max"]<motion["pos_min"]: return 0,"max must be greater than min" retcode,res=submod_execcmd("config@"+motion["model"],motion["id"],str(motion["pos_max"]),str(motion["pos_min"])) if retcode==0: return 0,"error configuring axis <- %s"%(res) motion["configured"]=1 return 1,"ok"
[docs]def inval_motion(motion_id): "Invalidate motion system" try: motion=motion_pool.get(motion_id) except Exception as e: return 0,str(e) if "configured" not in motion: return 1,"not configured" retcode,res=submod_execcmd("inval@"+motion["model"],motion["id"]) if retcode==0: return 0,"error invalidating axis %d <- %s"%(i+1,res) del motion["configured"] return 1,"ok"
def relay(motion_id,func,*params): try: motion=motion_pool.get(motion_id) except Exception as e: return 0,str(e) if "configured" not in motion: return 0,"not configured" function=func+"@"+motion["model"] api=api_pool.get_api(motion["model"],function) if api==None: return 0,"The selected MOTION model does not support this function" retcode,res=submod_execcmd(function,motion["id"],*params) if retcode==0: return 0,"error in %s <- %s"%(func,res) return 1,res
[docs]def home_motion(motion_id,direction,speed): "Home motion system at *speed*" return relay(motion_id,"home",direction,speed)
[docs]def is_homed_motion(motion_id): "Get the homed status of the system. Returns 1 if is homed, 0 otherwise." return relay(motion_id,"is_homed")
[docs]def set_origin_motion(motion_id): "Set a local coordinate origin other than the default 0 for all axis" return relay(motion_id,"set_origin")
[docs]def move_motion(motion_id,displacement,speed,acceleration): "Move along a *displacement* with *speed* and *acceleration*" try: motion=motion_pool.get(motion_id) except Exception as e: return 0,str(e) real_displacement=float(displacement) return relay(motion_id,"move",str(real_displacement),speed,acceleration)
[docs]def goto_motion(motion_id,pos,speed,acceleration): "Go to position *pos* with *speed* and *acceleration*" try: motion=motion_pool.get(motion_id) except Exception as e: return 0,str(e) function="goto@"+motion["model"] api=api_pool.get_api(motion["model"],function) if api==None: retcode,res=get_pos_motion(motion_id) if retcode==0: return 0,res current_pos=float(res) real_displacement=float(pos) - current_pos return relay(motion_id,"move",str(real_displacement),speed,acceleration) return relay(motion_id,"goto",pos,speed,acceleration)
[docs]def get_pos_motion(motion_id): "Get the position of the system" try: motion=motion_pool.get(motion_id) except Exception as e: return 0,str(e) retcode,res=relay(motion_id,"get_pos") if retcode==0: return 0,res return 1,str(float(res))
[docs]def get_limits_motion(motion_id,real="undef"): "Get the limits of the motion system. *real* determines whether local (0, default) or real (1) coordinates are used. Returns max_1,max_2,max_3;min_1,min_2,min3" try: motion=motion_pool.get(motion_id) except Exception as e: return 0,str(e) if "configured" not in motion: return 0,"not configured" pos_max=motion["pos_max"] pos_min=motion["pos_min"] return 1,"%f,%f"%(pos_min,pos_max)
[docs]def go_min_motion(motion_id,speed,acceleration): "Go to the minimum configured" return relay(motion_id,"go_min",speed,acceleration)
[docs]def go_max_motion(motion_id,speed,acceleration): "Go to the maximum configured" return relay(motion_id,"go_max",speed,acceleration)