Link Search Menu Expand Document

Device construction

Main device loop

A device or any agent is the collaborative virtual environment, is a software which synchronize a part of the semantic graph with a physical or virtual system. A physical system may be a robot or automated machine, a sensor system (IR Tracking), a camera, a keyboard, a wand, or a rendering system (CAVE, HMD, Haptic device, etc). A virtual system is mainly a simulation model which is managed by a computer, or cluster process and the results collaborate with other systems.

CVE provide a generic and standard interface for all the systems. Then the collaboration is managed by observing the evolution of the system interface. Indeed we must synchronize two loops :

  • The natural infinite loop of the internal command of the device
  • The collaboration loop with other devices

The natural internal loop should at start initiate the device, then get sensors values, analyze these values, plan device actions, execute orders and loop back to get new sensor values.

Device loop

In the natural loop we must add some actions to ensure the synchronization with the driving cve model:

  • cve initialization is a step where we load the cve device model. Thus the following device initialization must respect the demand from the cve model.
  • Synchornize cve state : usually the automatic device only react to its own sensors. It must reflect the sensor values into cve by updating corresponding sensor changes in the cve model. Every other device observing this state will be notified by getting cve events and can react to the system change enabling collaboration.
  • Get external cve events : as the other devices, the current device must listen events it is concerned with and which are triggered by other devices. On some internal events (emmergency button, etc), the device must stop. We had a condition whenver the ‘quit’ cve state value is down the system should be stopped (including docking to a security position)
  • The device must analyse internal sensors and external events to plan its next actions and order it to the actuators. Some actuators may be physical actuators (to activate an engine), but can be virtual (to change a model and change the cve model accordingly)

Encoding a device

CVE provides the cve.Runtime class to ease the construction of a device agent. The skeleton of the agent has the template below. To fasten the process the cve.Runtime uses a multi-process model. One process is dedicated to send and receive cve events and the second one is the device loop as described by overloading the run method.

class MyDevice(cve.Runtime):
   def __init__(self):
      cve.Runtime.__init__(self)
	  self.set_daemonic(False)
      # after this any new device attribute for device management can be added

   def at_init(self) :  
    	"""
    	this method is executed as soon as the device was initialized.
    	self.world variable will refer the cve device instance
    	
    	here it is a good place to access the concerned classifiers and main world's objects to associate callbacks.
    	it is also a good place to extend the initialized model to match the state and the management of the device
    	"""
        classifier=cve.find_by_uid_in_plugged_modules('classifier-uid')
        classifier.add_callback('ins','listattribute',self.on_insert) 
        					# valid only for attributes corresponding to lists : cardinality max >1
        classifier.add_callback('del','listattribute',self.on_remove)
        					# valid only for attributes corresponding to lists : cardinality max >1
        classifier.add_callback('set','vstateattribute',self.on_set_value)
        					# valid only for attributes corresponding to single vstate 
        ...
        return True,''      # must return True if OK, False if something wrong + an error message
  
	def run(self):	
        """
        main loop function
        """
		prevt=time.time()-10*self.freq
		while not self.quit :
			self.connector.loop_step(-1) # get cve events and call relevant cve callback
			
            # create the device loop here 
            #    - get_sensors, 
            #    - analysis, 
            #    - plan action,
            #    - order actions
            #    - Must report the orders to the cvemodel
            	
            ...
            ...
            # end device loop
            
			newt=time.time() # try to respect the expected frequency
			dt=newt-prevt
			if dt<self.freq :	
				time.sleep(self.freq-dt)
			prevt=newt
		self.stop()

# starting device : another device or cve server may start the device 
if __name__ == '__main__': 
	PLUGIN_ROLE = 'The ModelNAME'
	cve.start_device(sys.argv, PLUGIN_ROLE, vrcadRuntime)

Callbacks

Callbacks are an easy system to support the synchronization of a device when a cve event occurs. We can add/remove three type of events:

  1. events on cveitem: whenever a cveitem is edited (value change or subitem inserted or removed), the call-back is executed. see
  2. events associated to a cveclassifier: whenever a cveitem, instance of the classifier, is edited (value change or subitem inserted or removed), the call-back is executed: see
  3. at_init event : by default by overloading the cveruntime method. It is produced only once at the initialization of the cve.Runtime.

Any callback may add/remove callbacks, modifying the behavior of the device. These callbacks are runtime dependent and has no meaning on a static model. They are not persistent and must be initialized by the device.