Smoothing Data
Dave West from United States  [16 posts]
7 years
I'm sending COG_X and COG_BOX_SIZE serially to an arduino to run some motors. I'd like to have a VR script to smooth or do a running average of the data before it gets sent to the serial module, but I'm not sure of the VR syntax to do this.


Anonymous from United Kingdom  [99 posts] 7 years
This is a great chance for me to play with the new python module.  RR now lets you call python inline which means we don't have to fight with the @#$@#$ VB_Script anymore.  I don't know if anyone has written any examples with the new python module, so here goes.  

Attached is a simple robo_file that tracks a red image and displays a marker at the average of the last five positions of that marker.  The image processing is terrible, but the purpose is to illustrate the use of inline python.

Here are the routinues that are used:
1. Set Variable (Set NUM_AVERAGE controls the number of averages to use, setting this to 0 will create divide by zero errors (Gee I should have thought of that earlier!)
2. Color_Filter (Find the red object in the image)  You need to change this for your red.
3. Blob Size 50 A blog size cut to eliminate small objects, again might need adjustment.
4. Center of Gravity.  Call the center of gravity calculation from RR.
5. Python Program.  See below for detail.
6. Display Point.  Display a point at the coordinates calculated by the Python Program.

Python Program:
#Running Average in Python for RR by mmason April 2010
import rr  #Import our beloved RR API
averageX = []  #define a list of average values for x and y.
averageY = []
cogX = rr.GetVariable("COG_X")
cogY = rr.GetVariable("COG_Y")

#This variable contains the number of times to average the COG and is set from the interface.
numAverage = rr.GetVariable("NUM_AVERAGE")

#Read in the previously stored average values.
for i in range(0,numAverage):
  averageStorX = "AVERAGEX_"+str(i)
  averageStorY = "AVERAGEY_"+str(i)

#print averageX,averageY

#remove the first value from the list:
#Add the newest value to the list:

#Since I don't want to import numpy, I will quickly calculate the average.
totalX = 0
totalY = 0
for i in range(0,numAverage):
totalX += averageX[i]
totalY += averageY[i]

cogx_average = totalX / numAverage
cogy_average = totalY / numAverage
#print cogx_average,cogy_average

#write out the average values back to the roborealm api.
for i in range(0,numAverage):
  averageStorX = "AVERAGEX_"+str(i)
  averageStorY = "AVERAGEY_"+str(i)

I would recommend pasting back and forth from a better editor like notepad++. (Sorry STeven)  Still the inline python is AWESOME!!!

Let me know when you have the Kalman filter done.

have fun!


Anonymous 7 years

The above python code from mmason is much more flexible and a great example of python code ... but just in case you want to experiment a bit check out the variable filter module like


which basically does what you want. We often use that to smooth out noisy data to reduce motor jitter. See if this helps.


Yea, I would not recommend using the embedded editor ... it is just for simple scripts but for anything more advanced using a better editor certainly helps quite a bit! Besides, we're too busy creating modules than creating "yet another text editor"! :-)

Dave West from United States  [16 posts] 7 years

Thanks. Filter Variable seems to be what I'm looking for, but when I place it in my RR file, the serial module stops sending the variables.

Dave West from United States  [16 posts] 7 years

Any luck in recreating my problem?


Anonymous 7 years

Yes, we were able to determine what may be going wrong. The filter variable was changing the type of the variable (i.e. char to float) and this was not being recognized by the serial module. This has now been fixed (as of 2 days ago) but I'm a little behind in getting back to emails!

Can you download 2.20, install over your current system and give things a try again?

Dave West from United States  [16 posts] 7 years

It looks like it's working now.



This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index