WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Calculating Speed with sensor (https://forum.worldviz.com/showthread.php?t=1905)

durf 03-13-2009 09:05 AM

Calculating Speed with sensor
 
Hello,

Right now I am trying to figure out how to calculate speed with a sensor. I know that speed is the rate of change with respect to time. So change of position / time.

I know I need to get the position(pos) of the sensor first. Then i have to have a def: that will check for its new pos then do the calculation - (new pos - old pos) / time. Then after that I need to start over and set the new pos to old pos.

The problem im having is trying to establish a pos before the def: starts running. Whenever I test for the old position i keep getting 0.0, 0.0, 0.0.

So its going to look something like this:
Code:


import viz
viz.go()

PORT_PPT = 6

for x in range(1):
    #Create sensors
    ppt1 = viz.add('vizppt.dls')
    ppt2 = viz.add('vizppt.dls')

    ball1 = viz.add('white_vall.wrl')
    ball1.scale(1,1,1)
    ballLink1 = viz.link(ppt1, ball1)

    ball2 = viz.add('white_vall.wrl')
    ball2.scale(1,1,1)
    ballLink2 = viz.link(ppt2, ball2)
 
    #controls distance in virtual world
    ballLink1.postScale([8,1,13],target = viz.LINK_FULL_OP)
    ballLink2.postScale([8,1,13],target = viz.LINK_FULL_OP)

#I need to check for ball1 pos before here
def speed():
      #need to check for new pos for ball1 here
      #need to somehow get old pos of ball1 so i can do the equation
      #new pos - old pos /2(=1/2 second)
      if speed > 1:
          #some reaction happens in here
      #need to set old pos = new pos

vizact.ontimer(.5, speed)

Ive been trying to write this to work without errors but i keep getting them. I still new to vizard and python so any explanation would help also.

Thanks

farshizzo 03-13-2009 10:25 AM

Here is a sample function that takes any sensor object and calculates the speed of it:
Code:

import viz
viz.go()

sensor = viz.add('testtrack_all.dls')

def CalculateSpeed(obj):
       
        #Get current and last position
        current_pos = obj.getPosition()
        last_pos = getattr(obj,'last_pos',None)
       
        if last_pos is None:
                #This is first call, so save position and set speed to 0
                obj.last_pos = current_pos
                obj.speed = 0.0
        else:
                #Compute speed using last position
                obj.speed = vizmat.Distance(current_pos,last_pos) / viz.elapsed()
                obj.last_pos = current_pos
               
        print obj.speed
       
vizact.ontimer(0,CalculateSpeed,sensor)



All times are GMT -7. The time now is 07:14 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Copyright 2002-2018 WorldViz LLC