Script demo_clp_py
[hide private]
[frames] | no frames]

Source Code for Script script-demo_clp_py

 1  from ckbot.logical import Cluster 
 2   
 3  """ 
 4  Unlike most of the other demos in this directory, demo-clp is primarily text-file documentation of the CLP functionality in ckbot.logical.Cluster. As such, it sits in the infrastructure _under_ JoyApp applications.  
 5   
 6  What is a CLP? 
 7  ============== 
 8   
 9  A CLP (pronounced CLaP) is a "CLuster Property", i.e. a readable or writable property addressable in a Cluster. Depending on whether it is readable or writable, the Cluster will provide a getter function, a setter function, or both for the property. Similar to URL-s, CLP-s can follow one of several schemes: 
10   
11  (1) Hardware scheme, formatted as XX:YYYY, with XX and YYYY being hexadecimal digits. This scheme gets mapped directly into Robotics Bus Object Dictionary entry YYYY on node XX. Read and write permissions are mapped automatically from the object dictionary. As an example, "21:1051" is OD entry 0x1051 of node number 0x21 (33 decimal); on a standard CKBot servo module, this would be the "encpos" property that reads the servo encoder position. 
12   
13  The hardware scheme is there to allow code to address experimental OD entries that do not yet have a sensible, human readable name. 
14   
15  (2) OD Property scheme, formatted as node/property, "node" being the node name as appearing the Cluster's .at member, and "property" being the Object Dictionary property name, as mapped by cfg/od_names.yml. As an example, if node 0x21 of the previous example was called "Banana", the same property could be accessed via "Banana/encpos". These properties only become available once the node's Object Dictionary was walked (scanned through), e.g. using .get_od(). 
16   
17  (3) Attribute scheme, formatted as node/@attribute, "node" being the node name as appearing the Cluster's .at member, and "property" being a property exposed by the designer of the Module subclass that the node belongs to. While the OD properties require the host to walk (scan through) the object dictionary of the node before they become available, Module classes have the option of exposing functions that either take a 16bit int as an input or ones that return a 16bit int. In the ServoModule classes, these include set_pos, get_pos. Thus, in the attribute scheme, the same property could be accessed via "Banana/@get_pos", except that it would not require the "Banana" module's OD to have been walked before becoming available. 
18   
19   
20  Implementing Attribute CLP-s 
21  ============================ 
22   
23  Attribute CLP-s are implemented in two parts: 
24   
25  (1) A method with the same name as the CLP must be added to the Module. The method may either take (self,val) and return None, val being a 16bit int, or it may take (self) and return a 16bit int. 
26   
27  (2) The attributes must be registered in a dictionary stored in the _attr member of the Module, e.g. 
28  >>> m._attr=dict(  set_pos="2W", is_slack="1R" ) 
29  sets up two attributes, a writable set_pos(int) and a readable is_slack(). 
30   
31   
32  Scanning for CLP-s 
33  ================== 
34   
35  The CLP-s of a Cluster and of a Module are indexed. On Modules, the iterhwattr() iterator can be used to scan through the hardware addressing scheme CLP-s; the iterprop() iterator gives the OD scheme CLP-s; and the iterattr() iterator gives the attribute CLP-s. 
36  On Cluster-s, the iterhwaddr() iterator will scan through all OD entries of all modules (which has been walked), in order. 
37  The iterprop() iterator will scan through attribute CLP-s (if enabled) and through OD CLP-s. 
38   
39   
40  Using CLP-s 
41  =========== 
42   
43  Clusters have factory methods .getterOf(clp) and .setterOf(clp) that return getter and setter functions for the CLP. 
44  """ 
45   
46 -def getAllOD( clst ):
47 """ 48 Get all readable object dictionary properties in a Cluster. 49 50 INPUT: 51 clst -- a Cluster 52 53 OUTPUT: 54 dictionary with Hardware CLP mapping to value 55 """ 56 # Make sure all OD-s were walked 57 for node in clst.itervalues(): 58 if node.od is None: 59 node.get_od() 60 res = {} 61 # Scan CLP-s 62 for clp in clst.iterhwaddr(): 63 get = clst.getterOf(clp) 64 res[clp] = get() 65 return res
66
67 -def getDofByNID( clst ):
68 """ 69 Get an array of position setting functions for all servo Modules, 70 sorted in order of increasing node-id. 71 72 INPUT: 73 clst -- a Cluster 74 75 OUTPUT: 76 list of setter functions 77 78 NOTE: this does not require walking the OD-s 79 """ 80 res = [] 81 for node is clst.itervalues(): 82 try: 83 s = clst.setterOf("%s/@set_pos" % node.name) 84 except KeyError, ke: 85 print ke 86 continue 87 res.append((node.nid,s)) 88 res.sort() 89 return res
90