Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental Ragdoll integration #217

Open
wants to merge 13 commits into
base: trusssprings
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/configuration/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module Configuration
# Simulation Properties
WORLD_GRAVITY = -9.8 # in m/s/s
WORLD_SOLVER_MODEL = 8 # 1 - 64
WORLD_TIMESTEP = 1.0 / 60 # in seconds
WORLD_TIMESTEP = 1.0 / 120 # in seconds
WORLD_NUM_ITERATIONS = ((1.0 / 60) / WORLD_TIMESTEP).to_i
JOINT_SOLVER_MODEL = 2 # 0 or 2
JOINT_STIFFNESS = 0.95 # ratio (0.0 - 1.0)
Expand Down
5 changes: 3 additions & 2 deletions src/sketchup_objects/spring_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

# PhysicsLink that behaves like a gas spring
class SpringLink < ActuatorLink
attr_reader :spring_parameter_k, :initial_spring_length
attr_accessor :spring_parameter_k
attr_reader :edge, :initial_spring_length

def initialize(first_node, second_node, edge, id: nil)
@spring_parameter_k = 200
@spring_parameter_k = 7000
super(first_node, second_node, edge, id: id)
@first_elongation_length =
@second_elongation_length = Configuration::MINIMUM_ELONGATION
Expand Down
58 changes: 0 additions & 58 deletions src/spring_animation.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'src/system_simulation/ragdoll.rb'

class GeometryAnimation
attr_accessor :factor, :running
def initialize(data, index = 0)
Expand All @@ -6,25 +8,35 @@ def initialize(data, index = 0)
@running = true
@factor = 1

@ragdoll = Ragdoll.new
end

def toggle_running
@running = !@running;
@running = !@running

@ragdoll.clear
@ragdoll = Ragdoll.new
puts "new ragdoll"
end

def nextFrame(view)
Sketchup.active_model.start_operation('visualize export result', true)
unless (@running)
# last frame before animation stops – so we set value to last data sample and reset index to reset animation
@index = 0
end
# @index = 8 # Disables animation to debug ragdoll
current_data_sample = @data[@index]

Graph.instance.nodes.each do | node_id, node|
Graph.instance.nodes.each do |node_id, node|
node.update_position(current_data_sample.position_data[node_id.to_s])
node.hub.update_position(current_data_sample.position_data[node_id.to_s])
node.hub.update_user_indicator()
node.hub.update_user_indicator
end

@ragdoll.position = Graph.instance.nodes.values[0].position
@ragdoll.advance

Graph.instance.edges.each do |_, edge|
link = edge.link
link.update_link_transformations
Expand All @@ -44,13 +56,14 @@ def nextFrame(view)
# link = edge.link
# link.update_link_transformations
#end
Sketchup.active_model.commit_operation
view.refresh
@index = @index + @factor
if @index + @factor >= @data.length
@index = 0
sleep(1)
end

return @running
@running
end
end
2 changes: 2 additions & 0 deletions src/system_simulation/modelica_simulation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ def self.run_simulation(simulation_name="seesaw3")

result_file = simulation_name + "_res.csv"

puts 'Searching in:'
directory = File.dirname(__FILE__)
puts directory

Open3.popen2e("rm #{result_file}", :chdir => directory) do |i, o, t|
o.each {|l| puts l }
Expand Down
121 changes: 121 additions & 0 deletions src/system_simulation/ragdoll.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
class Ragdoll
attr_reader :position

def initialize
@group = Sketchup.active_model.entities.add_group
@world = TrussFab::World.new
@world.set_gravity(0.0, 0.0, -9.81)

position = Geom::Point3d.new( 0, 0, 0)

frontHub = Hub.new(frontBodyPosition(position), incidents: [])
frontHub.create_body @world
frontHub.body.static = true
frontHub.body.mass = 10
backHub = Hub.new(backBodyPosition(position), incidents: [])
backHub.create_body @world
backHub.body.static = true
backHub.body.mass = 10
downHub = Hub.new(downBodyPosition(position), incidents: [])
downHub.create_body @world
downHub.body.static = true
downHub.body.mass = 10

rightArmHub = Hub.new(rightArmBodyPosition(position), incidents: [])
rightArmHub.create_body @world
rightArmHub.body.static = false
rightArmHub.body.mass = 10

@frontRightJoint = TrussFab::PointToPoint.new(@world, frontHub.body, rightArmHub.body, frontBodyPosition(position), rightArmBodyPosition(position), nil)
@backRightJoint = TrussFab::PointToPoint.new(@world, backHub.body, rightArmHub.body, backBodyPosition(position), rightArmBodyPosition(position), nil)
@rightArmDownJoint = TrussFab::PointToPointGasSpring.new(@world, rightArmHub.body, downHub.body, rightArmBodyPosition(position), downBodyPosition(position), nil)

[@frontRightJoint, @backRightJoint].each do |joint|
joint.solver_model = Configuration::JOINT_SOLVER_MODEL
joint.stiffness = 0.9
joint.breaking_force = 0
joint.start_distance = 0.4
joint.bodies_collidable = false
end

@rightArmDownJoint.solver_model = Configuration::JOINT_SOLVER_MODEL
@rightArmDownJoint.extended_length = 0.56
@rightArmDownJoint.stroke_length = 0.15
@rightArmDownJoint.extended_force = 0.0001
@rightArmDownJoint.threshold = 0.2
@rightArmDownJoint.damp = 0
puts "Current length: #{@rightArmDownJoint.cur_length}"
end

def position=(position)
@position = position
@rightArmDownJoint.set_point2 downBodyPosition @position
@frontRightJoint.set_point1 frontBodyPosition @position
@backRightJoint.set_point1 backBodyPosition @position
end

def clear
Sketchup.active_model.active_entities.erase_entities(@group.entities.to_a) unless @group.deleted?
end

def redraw
Sketchup.active_model.start_operation('', true)
clear
draw
Sketchup.active_model.commit_operation
end

def draw
@group = Sketchup.active_model.entities.add_group

@group.entities.add_face(@frontRightJoint.get_point1, @frontRightJoint.get_point2, @backRightJoint.get_point1)
puts "Lengt: #{(@frontRightJoint.get_point1 - @frontRightJoint.get_point2).length}"
# cylinder_from_points(@backRightJoint.get_point1, @backRightJoint.get_point2, 2)
# cylinder_from_points(@backRightJoint.get_point1, @rightArmDownJoint.get_point1, 2)
# cylinder_from_points(@frontRightJoint.get_point1, @rightArmDownJoint.get_point1, 2)
# This would visualize the spring:
@group.entities.add_line(@rightArmDownJoint.get_point1, @rightArmDownJoint.get_point2)
end

def cylinder_from_points(point1, point2, radius)
vec = point2 - point1
draw_cylinder point1, radius, vec if vec.length > 1
end

def draw_cylinder(center, radius, direction)
# This is actually harder than I thought, and does not work right now,
# cause after pushpull, the references
# to the faces are gone
@group = Sketchup.active_model.entities.add_group if @group.deleted?
entities = @group.entities
normal = direction.normalize
edges = entities.add_circle(center, normal, radius, 10)
face = entities.add_face(edges)
face.pushpull direction.length if face
end

def advance
2.times do
@world.advance
puts "Current force: #{@rightArmDownJoint.linear_tension}"
end
redraw
end

def frontBodyPosition position
position
end

def backBodyPosition position
position + Geom::Vector3d.new(-10, 0, 0)
end

def downBodyPosition position
position + Geom::Vector3d.new(-5, 0, -10)
end

def rightArmBodyPosition position
position + Geom::Vector3d.new(-5, -20, 0)
end

end
14 changes: 4 additions & 10 deletions src/system_simulation/seesaw3.mo
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ model seesaw3
Placement(visible = true, transformation(origin = {-108, -108}, extent = {{-10, -10}, {10, 10}}, rotation = 90)));
Modelica.Mechanics.MultiBody.Parts.Fixed fixed2(animation = false, r = N[2]) annotation(
Placement(visible = true, transformation(origin = {74, -108}, extent = {{-10, -10}, {10, 10}}, rotation = 90)));
Modelica.Mechanics.MultiBody.Joints.Revolute revLeft(a(fixed = true), n = N[5] - N[7], w(fixed = true)) annotation(
Modelica.Mechanics.MultiBody.Joints.Revolute revLeft(a(fixed = false), n = N[5] - N[7], w(fixed = false)) annotation(
Placement(visible = true, transformation(origin = {-72, -52}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Joints.Revolute revRight(n = N[6] - N[4]) annotation(
Placement(visible = true, transformation(origin = {98, -36}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Parts.PointMass childLeft(a_0(fixed = false),m = 70, r_0(fixed = false, start = N[20]), v_0(fixed = false, start = {0, 0, 0})) annotation(
Modelica.Mechanics.MultiBody.Parts.PointMass childLeft(a_0(fixed = false),m = 70, r_0(fixed = true, start = N[20]), v_0(fixed = false, start = {0, 0, 0})) annotation(
Placement(visible = true, transformation(origin = {-6, -40}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Parts.FixedTranslation fixedTranslation(animation = false, r = N[5] - N[3]) annotation(
Placement(visible = true, transformation(origin = {-108, -74}, extent = {{10, -10}, {-10, 10}}, rotation = -90)));
Expand All @@ -78,16 +78,14 @@ model seesaw3
Placement(visible = true, transformation(origin = {142, -46}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Parts.BodyCylinder bodyCylinder4(r = (-N[5]) + N[10]) annotation(
Placement(visible = true, transformation(origin = {-32, -80}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Forces.SpringDamperParallel springDamperParallel1(c = 20000, d = 100, s_unstretched = .3) annotation(
Modelica.Mechanics.MultiBody.Forces.SpringDamperParallel springDamperParallel1(c = 7000, d = 100, s_unstretched = .7) annotation(
Placement(visible = true, transformation(origin = {-62, -106}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Parts.BodyCylinder bodyCylinder5(r = (-N[6]) + N[9]) annotation(
Placement(visible = true, transformation(origin = {136, -64}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Forces.SpringDamperParallel springDamperParallel2(c = 20000, d = 100, s_unstretched = .3) annotation(
Modelica.Mechanics.MultiBody.Forces.SpringDamperParallel springDamperParallel2(c = 7000, d = 100, s_unstretched = .7) annotation(
Placement(visible = true, transformation(origin = {138, -98}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Parts.FixedTranslation fixedTranslation1(animation = false, r = N[6] -N[2]) annotation(
Placement(visible = true, transformation(origin = {74, -56}, extent = {{10, -10}, {-10, 10}}, rotation = -90)));
Modelica.Mechanics.MultiBody.Forces.SpringDamperParallel springDamperParallel(c = 10000, d = 10, s_unstretched = 1) annotation(
Placement(visible = true, transformation(origin = {48, -76}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Visualizers.Advanced.Arrow arrow( r_head = node_pos[15] - N[15], r_tail = N[15]) annotation(
Placement(visible = true, transformation(origin = {-38, 2}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Mechanics.MultiBody.Visualizers.Advanced.Arrow arrow1(r_head = node_pos[14] - N[14], r_tail = N[14]) annotation(
Expand Down Expand Up @@ -125,10 +123,6 @@ equation
Line(points = {{74, -66}, {74, -66}, {74, -98}, {74, -98}}));
connect(fixedTranslation1.frame_b, revRight.frame_a) annotation(
Line(points = {{74, -46}, {74, -46}, {74, -36}, {88, -36}, {88, -36}}));
connect(bodyCylinder3.frame_b, springDamperParallel.frame_b) annotation(
Line(points = {{152, -46}, {156, -46}, {156, -76}, {58, -76}}, color = {95, 95, 95}));
connect(bodyCylinder.frame_b, springDamperParallel.frame_a) annotation(
Line(points = {{-22, -60}, {24, -60}, {24, -78}, {38, -78}, {38, -76}}, color = {95, 95, 95}));
annotation(
uses(Modelica(version = "3.2.2")));
end seesaw3;
Loading