Read out hinge/servo value [SOLVED!]
-
Hi All,
I've been struggling for days now how to make this work... is there any change about reading the value 0.0 / 1.0 from a hinge/servo?
Say you have a hinge with a 'lever' attatched to it, with
MIN: -90
MAX: 90Now when i pull the 'lever' (that is attatched to the hinge) with the mouse, i want to readout the hinge value 0.0 (-90 degrees) --- 1.0 (90 degrees)
From my understanding and experimenting, everything i put in the 'controller' box of the hinge makes it stop working to manually control it...
All i can do is fiddle with position().x or position().y in the 'lever' scripting...and some mathmathics...but but that should not be needed i guess...
Could you help me out?
-
Limited joint API is one of the weakness of SP. However, there is a workaround using Trigonometry, just like you said.
Here is an example:
-
Hi Anton_S,
Thank you for the example, i am going to study it a bit!
First i have to make the degrees to a value 0.00 (MIN) and 1.00 (MAX) (optional, but handy)
My goal is to put an Arduino behind the sketch, so that virtual pulled elements are also moving in the 'real' world, like a servo, i will put my project on the forum when it's finished
Best Regards,
Bas -
Ok, i got it working to make a value from 0.00 to 1.00 from the degree number in the sketch above.
Since i am new to ruby i had todo some reseach but made it, if you have any improvements...let me know!
in section "onUpdate" add this at the end:
anglefloat = @angle.radians.round.to_f puts (((0.5 / 90) * anglefloat) + 0.5).round(2) if anglefloat > 0 puts (((-0.5 / 90) * anglefloat) - 0.5).abs.round(2) if anglefloat < 0
Open the RUBY CONSOLE there you can see the values:
0.54 0.53 0.53 0.53 0.52 0.51 0.51 0.49 0.49 0.48 0.48 0.47 0.46 0.46 0.45 0.44 0.43 0.43 0.42 0.41 0.41 0.4 0.39
-
Hmmmm, seems the ROUND function is not doing well in SU8
After some research i made this code for SU8, using SPRINTF:
anglefloat = @angle.radians.round.to_f puts sprintf "%.2f", (((0.5 / 90) * anglefloat) + 0.5) if anglefloat > 0 puts sprintf "%.2f", (((-0.5 / 90) * anglefloat) - 0.5).abs if anglefloat < 0
That seems to do the trick...
-
You get this error cause you probably passed an argument to the
round
function. SU2013 and below have no parameters in theround
function, meanwhile, SU2014+ do have a decimal_point parameter in the round function.Here is an improved version that gives value b/w 0.0 and 1.0.
-
Hello Anton_S,
Can the same be done with a slider?, could you provide an example?
Thanks!
-
Here it is. This is more advanced script as it works with slider, piston, corkscrew, and spring. You just paste the script into any object, and its joint will be found automatically - no need to locate the connected joint and stuff... Just copy and paste. I might also write a similar script to hinge, servo, and motor.
Here is script that you copy and paste into any object:
<span class="syntaxdefault">onStart </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Add 2D text, just to output info.<br /></span><span class="syntaxdefault"> setVar</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'text_v_shift'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> getVar</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'text_v_shift'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">+</span><span class="syntaxdefault"> 0.04</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">text </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_note</span><span class="syntaxkeyword">(</span><span class="syntaxstring">''</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0.01</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> getVar</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'text_v_shift'</span><span class="syntaxkeyword">))<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Find connected slider<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slide_parent </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">joint_name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> nil<br /> parent_joints </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> this</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"SPOBJ"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"parentJoints"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">to_a<br /> parent_joints</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">jname</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> if jname </span><span class="syntaxkeyword">=~</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">/</span><span class="syntaxdefault">slider</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">piston</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">spring</span><span class="syntaxkeyword">|</span><span class="syntaxdefault">corkscrew</span><span class="syntaxkeyword">/</span><span class="syntaxdefault">i<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">joint_name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> jname<br /> break<br /> end<br /> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> next unless </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">joint_name<br /> </span><span class="syntaxcomment"># Find slider associated with joint name<br /></span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> next unless e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">||</span><span class="syntaxdefault"> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">ComponentInstance</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> if e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'SPJOINT'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'name'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">''</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">joint_name<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> e<br /> else<br /> found </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> false<br /> e</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">ec</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault"> if ec</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">Group</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">&&</span><span class="syntaxdefault"> ec</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'SPJOINT'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'name'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">''</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">joint_name<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider_parent </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> e<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ec<br /> found </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> true<br /> break<br /> end<br /> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> break if found<br /> end<br /> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"> next unless </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider<br /> </span><span class="syntaxcomment"># Get min/max data associated with a slider<br /></span><span class="syntaxdefault"> slider_min </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'SPJOINT'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'min'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> slider_max </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">get_attribute</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'SPJOINT'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'max'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 0</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider_range </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">slider_max </span><span class="syntaxkeyword">-</span><span class="syntaxdefault"> slider_min</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">to_f<br /></span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">onUpdate </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider<br /> </span><span class="syntaxcomment"># Calculate object position and ratio on slider.<br /></span><span class="syntaxdefault"> pt </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> this</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">group</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">origin<br /> if </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider_parent<br /> tra </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider_parent</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation<br /> else<br /> tra </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transformation<br /> end<br /> pt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">transform</span><span class="syntaxkeyword">!(</span><span class="syntaxdefault">tra</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">inverse</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> pos </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> pt</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">z<br /> ratio </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider_range</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">zero</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> 0 </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> pos </span><span class="syntaxkeyword">/</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">slider_range<br /> </span><span class="syntaxcomment"># Process info here...<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Display info<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">text</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">text </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"#{@joint_name.capitalize} Info - Position; #{sprintf("</span><span class="syntaxkeyword">%</span><span class="syntaxdefault">.2f</span><span class="syntaxstring">", pos)}\" Ratio (Controller); #{sprintf("</span><span class="syntaxkeyword">%</span><span class="syntaxdefault">.2f</span><span class="syntaxstring">", ratio)}"<br /></span><span class="syntaxdefault"> end<br /></span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">onEnd </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># Remove added text<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">text</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">erase</span><span class="syntaxkeyword">!</span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">text</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">valid</span><span class="syntaxkeyword">?<br />}</span><span class="syntaxdefault"></span>
Here is the demo:
slider_range_su8.skpAlso, I would suggest upgrading to SP3.5.6 as the pick and drag in 3.5.5 makes small objects go crazy.
-
Hello anton_S,
Again, thank you for the example code!, i see it's a lot for a NON Sketchup/Ruby expert like me
I needed this for a virtual "RGB LED controller" that controls a LED trough the Arduino, so i made some adds & adjustements to your code.
Ps. why does the value start at 0.06 and ends with 1.07?, i did a correction of -0.6 but then i still come out on 1.01
My additions to store the 3 RGB values per slider:
RED:
onupdate { ratio = ratio - 0.06 ratio = 1.00 if ratio > 1.00 $sliderr = sprintf("%.2f", ratio) }
GREEN:
onupdate { ratio = ratio - 0.06 ratio = 1.00 if ratio > 1.00 $sliderg = sprintf("%.2f", ratio) }
BLUE:
onupdate { ratio = ratio - 0.06 ratio = 1.00 if ratio > 1.00 $sliderb = sprintf("%.2f", ratio) }
On the "baseplate" this code in the "scripted" section to display the values on the left side:
onUpdate { logLine("Waarde rood; " + $sliderr.to_s) logLine("Waarde groen; " + $sliderg.to_s) logLine("Waarde blauw; " + $sliderb.to_s) logLine("") logLine("") logLine("") logLine("") logLine("") }
Here is a screen of the project:
-
@bas9999 said:
Ps. why does the value start at 0.06 and ends with 1.07?, i did a correction of -0.6 but then i still come out on 1.01
Because joints in SP are flexible. They are not 100% stiff.
To fix that, simply clamp the given values from 0 to 1:
ratio = 0.0 if ratio < 0.0 ratio = 1.0 if ratio > 1.0
-
Hi Anton_S,
Thank you!
Ps. the first script for the "hinge", is that also a copy & paste script for every component?, if not... could you adjust it that way?
Thanks in forward!
-
Here is the script for hinge, servo, motor, and corkscrew:
# Script for hinge, servo, motor, and corkscrew. onStart { # Add 2D text, just to output info setVar('text_v_shift', getVar('text_v_shift') + 0.04) @text = Sketchup.active_model.add_note('', 0.01, getVar('text_v_shift')) # Find connected joint @joint_parent = nil @joint = nil @joint_name = nil parent_joints = this.group.get_attribute("SPOBJ", "parentJoints").to_a parent_joints.each { |jname| if jname =~ /hinge|servo|motor|corkscrew/i @joint_name = jname break end } if @joint_name # Find joint group associated with joint name Sketchup.active_model.entities.each { |e| next unless e.is_a?(Sketchup;;Group) || e.is_a?(Sketchup;;ComponentInstance) if e.get_attribute('SPJOINT', 'name', '') == @joint_name @joint = e else found = false e.entities.each { |ec| if ec.is_a?(Sketchup;;Group) && ec.get_attribute('SPJOINT', 'name', '') == @joint_name @joint_parent = e @joint = ec found = true break end } break if found end } end if @joint # Get min/max data associated with the joint min = @joint.get_attribute('SPJOINT', 'min', 0).to_f max = @joint.get_attribute('SPJOINT', 'max', 0).to_f @range = (max - min).degrees # Calculate starting angle if @joint_parent tra = @joint_parent.transformation * @joint.transformation else tra = @joint.transformation end @init_dir = tra.xaxis.transform(this.group.transformation.inverse) @init_angle = min.degrees end } onUpdate { if @joint # Calculate object angle and ratio if @joint_parent tra = @joint_parent.transformation * @joint.transformation else tra = @joint.transformation end dir = @init_dir.transform(this.group.transformation).transform(tra.inverse).normalize # Get theta from -PI to PI theta = -Math.acos(dir.x) theta = -theta if dir.y < 0 ratio = @range.zero? ? 0 ; (theta - @init_angle).to_f / @range # Process info here... # Display info @text.text = "#{@joint_name.capitalize} Info - Angle; #{sprintf("%.2f", theta.radians)} deg. Ratio (Controller); #{sprintf("%.2f", ratio)}" end } onEnd { # Remove added text @text.erase! if @text.valid? }
And here is the updated script for slider, piston, spring and corkscrew. This version considers initial position, so that range values are correct:
# Script for slider, piston, spring, and corkscrew. onStart { # Add 2D text, just to output info setVar('text_v_shift', getVar('text_v_shift') + 0.04) @text = Sketchup.active_model.add_note('', 0.01, getVar('text_v_shift')) # Find connected joint @joint_parent = nil @joint = nil @joint_name = nil parent_joints = this.group.get_attribute("SPOBJ", "parentJoints").to_a parent_joints.each { |jname| if jname =~ /slider|piston|spring|corkscrew/i @joint_name = jname break end } if @joint_name # Find joint group associated with joint name Sketchup.active_model.entities.each { |e| next unless e.is_a?(Sketchup;;Group) || e.is_a?(Sketchup;;ComponentInstance) if e.get_attribute('SPJOINT', 'name', '') == @joint_name @joint = e else found = false e.entities.each { |ec| if ec.is_a?(Sketchup;;Group) && ec.get_attribute('SPJOINT', 'name', '') == @joint_name @joint_parent = e @joint = ec found = true break end } break if found end } end if @joint # Get min/max data associated with the joint min = @joint.get_attribute('SPJOINT', 'min', 0).to_f max = @joint.get_attribute('SPJOINT', 'max', 0).to_f @range = (max - min).to_f # Calculate starting position pt = this.group.transformation.origin if @joint_parent tra = @joint_parent.transformation * @joint.transformation else tra = @joint.transformation end pt.transform!(tra.inverse) @init_pos = pt.z + min end } onUpdate { if @joint # Calculate object position and ratio pt = this.group.transformation.origin if @joint_parent tra = @joint_parent.transformation * @joint.transformation else tra = @joint.transformation end pt.transform!(tra.inverse) pos = pt.z - @init_pos ratio = @range.zero? ? 0 ; pos / @range # Process info here... # Display info @text.text = "#{@joint_name.capitalize} Info - Position; #{sprintf("%.2f", pos)}\" Ratio (Controller); #{sprintf("%.2f", ratio)}" end } onEnd { # Remove added text @text.erase! if @text.valid? }
-
This is amazing! Never seen something like this before, ever!
-
Stupendous. Can't wait to try it and of course add some Lasers. Absolutely stupendous. Kudos to y'all Anton and Chris Phillips.
-
Thank you Anton_S!
@All,
Watch the YouTube movie where i used these scripts in action:https://www.youtube.com/watch?v=aMCL08xONZs
SCRIPTS & TUTORIALS @ DomoticX.com
Look for: "Virtual 3D Device Port"The tutorial pages are written in our main language: DUTCH
Use google translate to translate the pages for you!(note: copy & paste scripts from the original untranslated site)
Advertisement