pSpeed = GetFloatParam("speed", 0.4)

function init()
	motor = FindJoint("hingemotor")
	speed = pSpeed
	eps = 1
	local min, max = GetJointLimits(motor)
	SetJointMotorTarget(motor, min, speed)
	dir = 1
	target = max
end


function tick(dt)
	local min, max = GetJointLimits(motor)
	local current = GetJointMovement(motor)
	
	if current < min + eps and dir == 0 then
		dir = 1
		target = max
	elseif current > max - eps and dir == 1 then
		dir = 0
		target = min		
	end	

	local tagValue = 0
	if GetTagValue(motor, "hingemotor") ~= "" then
		tagValue = GetTagValue(motor,"hingemotor")
	end

	SetJointMotorTarget(motor, target, speed*tagValue)	
end

