WorldViz User Forum Calculating Speed with sensor
 User Name Remember Me? Password
 Register FAQ Search Today's Posts Mark Forums Read

#1
03-13-2009, 09:05 AM
 durf Member Join Date: Feb 2009 Posts: 61
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

ball1.scale(1,1,1)

ball2.scale(1,1,1)

#controls distance in virtual world

#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
#2
03-13-2009, 10:25 AM
 farshizzo WorldViz Team Member Join Date: Mar 2003 Posts: 2,849
Here is a sample function that takes any sensor object and calculates the speed of it:
Code:
```import viz
viz.go()

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)```

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements Vizard Precision Position Tracker (PPT) Plug-in development

 Similar Threads Thread Thread Starter Forum Replies Last Post farshizzo Plug-in development 25 08-01-2019 12:24 AM whj Vizard 8 11-17-2008 07:41 PM masaki Vizard 1 03-06-2008 03:07 PM RedSpikeyThing Plug-in development 2 02-12-2008 02:10 PM Eunice Vizard 4 01-03-2006 05:18 AM

All times are GMT -7. The time now is 01:03 PM.

 Contact Us - WorldViz Home - Archive - Top