Commits

German Larrain committed afbe760

second example, which is similar to the CentrifugalForceTest

Comments (0)

Files changed (1)

bin/IROS/example2.py

+#!/usr/bin/env python
+
+# Created on 2012.03.09
+#
+# @author: german
+
+"""
+Example #2
+"""
+
+from ars.app import Program
+import ars.utilities.mathematical as mut
+import ars.constants as cts
+
+class Example2(Program):	
+
+	OFFSET = (2,0.5,2)
+	BOX_PARAMS = (((10,0.5,10),(0,-0.25,0)),{'density':100}) # ((size, center), density)
+	
+	WINDOW_SIZE = (900,600)
+	CAMERA_POSITION = (2,5,10) # (0,-4,1) in C++ example #vp_xyz = (0.0,-4.0,1.0) # position [meters]
+	FPS = 50
+	STEPS_PER_FRAME = 100 #200 #STEP_SIZE = 1e-5 # 0.01 ms
+
+	#POLE_SPEED_STEP = 0.01
+	POLE_RADIUS = 0.01
+	POLE_HEIGHT = 1
+	POLE_INITIAL_POS = (0.0,1.0,0.0) # (0.0,0.0,1.0) in C++ example
+	POLE_MASS = 10.0
+	
+	ARM_RADIUS = 0.01
+	ARM_LENGTH = 1.0
+	ARM_INITIAL_POS = (0.0,1.0,0.1)
+	ARM_MASS = 10.0
+	
+	#===========================================================================
+	# BALL_MASS = 1.0 # 1kg
+	# BALL_RADIUS = 0.01 # 1 cm
+	# BALL_VISUAL_RADIUS = 0.1 # 10 cm
+	# BALL_INITIAL_POS = (0.0, 1.0, 1.0)
+	#===========================================================================
+	
+	JOINT1_ANCHOR = (0.0,0.0,0.0)
+	JOINT1_AXIS = cts.Y_AXIS # (0.0,0.0,1.0) Z-axis in C++ example
+#	JOINT1_FMAX = 100
+	JOINT2_ANCHOR = (0.0,1.5,0.1)
+	JOINT2_AXIS = cts.X_AXIS
+	
+#	CABLE_LENGTH = mut.length3(mut.sub3(BALL_INITIAL_POS, JOINT2_ANCHOR))
+		
+	#===========================================================================
+	# JOINT2_ANGLE_RATE_CONTROLLER_KP = 500.0
+	# JOINT1_ANGLE_RATE_INITIAL = 3.0
+	#===========================================================================
+	
+	Q1_FRICTION_COEFF = 50e-3
+	Q2_FRICTION_COEFF = 50e-3
+	
+	#===========================================================================
+	# torques = ((0,0), (1,0), (5,1), (9,1), (13,0), (14,0)) # (time,torque)
+	# torque_i = 0
+	#===========================================================================
+	GEAR_RATIO = 0.5
+	MAX_TORQUE = 20 * GEAR_RATIO
+	SATURATION_TIME = 1
+
+	def __init__(self):
+		Program.__init__(self)
+		#=======================================================================
+		# self.key_press_functions.add('a', self.inc_joint1_vel)
+		# self.key_press_functions.add('z', self.dec_joint1_vel)
+		# 
+		#=======================================================================
+		self.set_pre_step_callback(self.on_pre_step)
+		self.set_pre_frame_callback(self.on_pre_frame)
+		
+		self.q1p_prev = 0.0
+		self.q2p_prev = 0.0
+		self.torque1_prev = 0.0
+		
+		#self.joint1_vel_user = self.JOINT1_ANGLE_RATE_INITIAL
+		#self.large_speed_steps = True
+		#TODO: set ERP, CFM
+		
+	
+	def create_sim_objects(self):
+		box = self.sim.add_box(*self.BOX_PARAMS[0], **self.BOX_PARAMS[1])
+		
+		pole = self.sim.add_cylinder(self.POLE_HEIGHT, self.POLE_RADIUS, self.POLE_INITIAL_POS, mass=self.POLE_MASS)
+		arm = self.sim.add_cylinder(self.ARM_LENGTH, self.ARM_RADIUS, self.ARM_INITIAL_POS, mass=self.ARM_MASS)
+		
+		# bodies are rotated before attaching themselves through joints
+		self.sim.get_object(pole).rotate(cts.X_AXIS, mut.pi/2)
+		self.sim.get_object(arm).rotate(cts.X_AXIS, mut.pi/2)
+		
+		self.sim.get_object(box).offset_by_position(self.OFFSET)
+		self.sim.get_object(pole).offset_by_position(self.OFFSET)
+		self.sim.get_object(arm).offset_by_position(self.OFFSET)
+		
+		self.sim.add_rotary_joint('r1',					# name, obj1, obj2, anchor, axis
+							self.sim.get_object(box), 
+							self.sim.get_object(pole),
+							None, self.JOINT1_AXIS)
+		
+		self.sim.add_rotary_joint('r2', 
+							self.sim.get_object(pole), 
+							self.sim.get_object(arm), 
+							mut.add3(self.OFFSET, self.JOINT2_ANCHOR), self.JOINT2_AXIS)
+		self.box = box
+		self.pole = pole
+		self.arm = arm
+	
+	def on_pre_frame(self):
+		
+		time = self.sim.sim_time
+		q1 = self.get_q1()
+		q2 = self.get_q2()
+		q1p = self.get_q1p()
+		q2p = self.get_q2p()
+		torque1 = self.torque1_prev
+		
+		print((time,q1,q2,q1p,q2p,torque1))
+
+	def on_pre_step(self):
+		try:
+			time = self.sim.sim_time
+			torque1 = self.get_torque_to_apply(time)
+			self.apply_torque_to_joints(torque1, None)
+			self.apply_friction(self.q1p_prev, self.q2p_prev)
+			
+			#q1 = self.get_q1()
+			#q2 = self.get_q2()
+			q1p = self.get_q1p()
+			q2p = self.get_q2p()
+			
+			#print((time,q1,q2,q1p,q2p,torque1))
+		
+			self.q1p_prev = q1p
+			self.q2p_prev = q2p
+			self.torque1_prev = torque1
+			
+#			ball_pos = self.sim.get_object(self.ball).get_position()
+#			ball_vel = self.sim.get_object(self.ball).get_linear_velocity()
+#			ball_omega = self.sim.get_object(self.ball).get_angular_velocity()
+			#z_top = self.JOINT2_ANCHOR[1] # JOINT2_ANCHOR[2] in C++ example
+			#theta_sim = mut.acos((z_top - ball_pos[1] + self.OFFSET[1]) / self.CABLE_LENGTH) # ball_pos[2] in C++ example
+
+			#print((ball_pos, ball_vel, ball_omega, theta_sim))
+			
+		except Exception as e:
+			print('Exception when executing on_pre_step: %s' % str(e))
+	
+	def get_torque_to_apply(self, time):
+		if time < self.SATURATION_TIME:
+			torque = time * self.GEAR_RATIO
+		else:
+			torque = self.MAX_TORQUE
+		return torque
+	
+	def get_q1(self):
+		return self.sim.get_joint('r1').get_joint().get_angle()
+	
+	def get_q2(self):
+		return self.sim.get_joint('r2').get_joint().get_angle()
+	
+	def get_q1p(self):
+		return self.sim.get_joint('r1').get_joint().get_angle_rate()
+	
+	def get_q2p(self):
+		return self.sim.get_joint('r2').get_joint().get_angle_rate()
+		
+	#===========================================================================
+	# def inc_joint1_vel(self):
+	#	#self.joint1_vel_user += self.POLE_SPEED_STEP
+	#	self.sim.get_joint('r1').add_torque(1000)
+	# 
+	# def dec_joint1_vel(self):
+	#	self.sim.get_joint('r1').add_torque(-1000)
+	#===========================================================================
+	
+	def apply_torque_to_joints(self, torque1, torque2):
+		if torque1 is not None:
+			self.sim.get_joint('r1').add_torque(torque1)
+		if torque2 is not None:
+			self.sim.get_joint('r2').add_torque(torque2)
+	
+	def apply_friction(self, q1p, q2p):
+		self.apply_torque_to_joints(-q1p * self.Q1_FRICTION_COEFF, -q2p * self.Q2_FRICTION_COEFF)
+	
+	#===========================================================================
+	# def set_joint1_speed(self):
+	#	self.sim.get_joint('r1').get_joint().set_speed(self.joint1_vel_user, self.JOINT1_FMAX)
+	# 
+	# def apply_friction(self):
+	#	torque = -self.JOINT2_ANGLE_RATE_CONTROLLER_KP * self.sim.get_joint('r2').get_joint().get_angle_rate()
+	#	self.sim.get_joint('r2').get_joint().add_torque(torque)
+	#===========================================================================
+
+if __name__ == '__main__':
+	sim_program = Example2()
+	sim_program.start()