Source

ars / demos / sensors / rotary_joint.py

#!/usr/bin/env python

"""Use of the RotaryJointSensor with CentrifugalForceTest program.
Sensor data is stored in a queue, which is retrieved after the simulation ends
(but it can be accessed at any time).

Sensor is created in the `create_sim_objects` method.
	'self.sensor = sen.RotaryJointSensor(self.sim.get_joint('r2').joint)'
It is updated in the `on_pre_frame` method
	'self.sensor.on_change(time)'
Sensor measurements can be retrieved at any time
	'self.sensor.data_queue.pull()'

"""
import ars.model.robot.sensors as sen

from base import CentrifugalForce


class RotaryJointSensor(CentrifugalForce):

	def create_sim_objects(self):
		CentrifugalForce.create_sim_objects(self)
		self.sensor = sen.RotaryJointSensor(
			self.sim.get_joint('r2').joint)

	def on_pre_frame(self):
		try:
			time = self.sim.sim_time
			self.sensor.on_change(time)
		except Exception as e:
			print('Exception when executing on_pre_frame: %s' % str(e))

		CentrifugalForce.on_pre_frame(self)

if __name__ == '__main__':
	sim_program = RotaryJointSensor()
	sim_program.start()

	sensor = sim_program.sensor
	print('r1 sensor data queue count %d' % sensor.data_queue.count())
	print(sensor.data_queue)