Board Logo
« Rotating lever »

Welcome Guest. Please Login or Register.
Jan 21st, 2018, 12:49am


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Rotating lever  (Read 73 times)
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Rotating lever
« Thread started on: Oct 31st, 2017, 06:23am »

So.
There is a 100 pixel per meter
Lever goes round (250 250) pont
To the right is free falling weight (just to compare with lever going down)
Lever lengths given as lA lB
masses given as mA mB
- go ahead and change it.
Simulation stops after lever gets 30 degrees down (or up).

(I admit I cheated. As lever slants moments get smaller because of force line goes nearer to rotation point - so actual angular acceleration decreases. I calculated it from the start - with maximum arms - and used throughtout)

Code:
'v.01 - just to see stuff falling (as a reference)
'v.02   - add a lever
gosub [getSlack]
    desiredWidth = 700
    desiredHeight = 500
    WindowWidth = desiredWidth + slackX
    WindowHeight = desiredHeight + slackY

nomainwin
open "test" for graphics_nsb_nf as #gr
#gr "trapclose [quit]"
#gr "down"

pi = acs(-1)
g = 9.8

cx=250:cy=250
fx=600
'---------------------------------------
'      A_______^___________B           freeFalling
'---------------------------------------
'===== data goes here ==========
lA=1
lB=1.5
mA=1.5
mB=2
'===============================

'moment
momentA=0-mA*lA*g
momentB=mB*lB*g       'clockwise is positive
moment = momentA+momentB

Inertcia = mA*lA^2+mB*lB^2
AngAccel=moment/Inertcia
angVel=0
angle = pi
'actually right only for initial moment. But we don't care much
'now, for B
'linAccelB=AngAccel*lB

'count in meters, draw in pixels
scale = 100 'pixel per meter

delay = 16      'ms
dt = delay/1000 'sec


x = fx/scale
y = 0
vy=0
minY=2.5
t0=time$("ms")

gosub [drawFallingBall]
gosub [drawLever]

timer delay, [nxt]

[nxt]
    #gr "color white"
    gosub [drawFallingBall]
    gosub [drawLever]

    vy=vy+dt*g
    y=y+dt*vy

    angVel=angVel+dt*AngAccel
    angle=angle+dt*angVel

    #gr "color black"
    gosub [drawFallingBall]
    gosub [drawLever]

    if abs(angle-pi)>pi/6 then  'stops after 30 degrees slant
        timer 0
    end if

    'if y >minY then
    '    timer 0
    'end if
wait

[drawFallingBall]
    #gr "place ";x*scale;" ";cy+y*scale
    #gr "circle 15"
return

[drawLever]
    #gr "place ";cx;" ";cy
    #gr "circle 3"

    otherX=lA*cos(angle)
    otherY=lA*sin(angle)
    #gr "line ";cx;" ";cy;" ";cx+otherX*scale;" ";cy+otherY*scale
    #gr "circle ";mA*10

    otherX=lB*cos(angle+pi)
    otherY=lB*sin(angle+pi)
    #gr "line ";cx;" ";cy;" ";cx+otherX*scale;" ";cy+otherY*scale
    #gr "circle ";mB*10

return
'========================================

[quit]
    timer 0
    close #gr
end

[getSlack]
    WindowWidth=200:WindowHeight=200
    open "" for graphics_nsb as #t:#t,"home;posxy x y":close#t
    slackX=WindowWidth-2*x:slackY=WindowHeight-2*y
return
 


EDIT
if I set mA=0 I have lever fall just as free fall - so it reassures me ;)
« Last Edit: Oct 31st, 2017, 06:25am by tsh73 » User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3636
xx Re: Rotating lever
« Reply #1 on: Oct 31st, 2017, 06:52am »

Quote:
I calculated it from the start - with maximum arms - and used throughtout

Just fixed that.

Now it just swings and swings and swings...
Code:
'v.01 - just to see stuff falling (as a reference)
'v.02 - add a lever
'v.03 - fix moments to use current coords
gosub [getSlack]
    desiredWidth = 700
    desiredHeight = 500
    WindowWidth = desiredWidth + slackX
    WindowHeight = desiredHeight + slackY

nomainwin
open "test" for graphics_nsb_nf as #gr
#gr "trapclose [quit]"
#gr "down"

pi = acs(-1)
g = 9.8

cx=250:cy=250
fx=600
'---------------------------------------
'      A_______^___________B           freeFalling
'---------------------------------------
'===== data goes here ==========
lA=1
lB=1.5
mA=2
mB=1.5
'===============================

'moment
angle = pi
momentA=0-mA*cos(angle)*lA*g
momentB=mB*cos(angle)*lB*g       'clockwise is positive
moment = momentA+momentB

Inertcia = mA*lA^2+mB*lB^2
AngAccel=moment/Inertcia
angVel=0
'actually right only for initial moment. But we don't care much
'now, for B
'linAccelB=AngAccel*lB

'count in meters, draw in pixels
scale = 100 'pixel per meter

delay = 16      'ms
dt = delay/1000 'sec


x = fx/scale
y = 0
vy=0
minY=2.5
t0=time$("ms")

gosub [drawFallingBall]
gosub [drawLever]

timer delay, [nxt]

[nxt]
    #gr "color white"
    gosub [drawFallingBall]
    gosub [drawLever]

    vy=vy+dt*g
    y=y+dt*vy

    momentA=0-mA*cos(angle)*lA*g
    momentB=mB*cos(angle)*lB*g       'clockwise is positive
    moment = momentA+momentB

    Inertcia = mA*lA^2+mB*lB^2
    AngAccel=moment/Inertcia

    angVel=angVel+dt*AngAccel
    angle=angle+dt*angVel

    #gr "color black"
    gosub [drawFallingBall]
    gosub [drawLever]

    'if abs(angle-pi)>pi/6 then  'stops after 30 degrees slant
    '    timer 0
    'end if

    if y >minY then
        'timer 0
        vy=0-vy 'absolutely elastic. Actiually it gradually slows down...
        y=minY    'sometimes it goes under
    end if
wait

[drawFallingBall]
    #gr "place ";x*scale;" ";cy+y*scale
    #gr "circle 15"
return

[drawLever]
    #gr "place ";cx;" ";cy
    #gr "circle 3"

    otherX=lA*cos(angle)
    otherY=0-lA*sin(angle)
    #gr "line ";cx;" ";cy;" ";cx+otherX*scale;" ";cy+otherY*scale
    #gr "circle ";mA*10

    otherX=lB*cos(angle+pi)
    otherY=0-lB*sin(angle+pi)
    #gr "line ";cx;" ";cy;" ";cx+otherX*scale;" ";cy+otherY*scale
    #gr "circle ";mB*10

return
'========================================

[quit]
    timer 0
    close #gr
end

[getSlack]
    WindowWidth=200:WindowHeight=200
    open "" for graphics_nsb as #t:#t,"home;posxy x y":close#t
    slackX=WindowWidth-2*x:slackY=WindowHeight-2*y
return
 
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls