Posts Tagged ‘Class’

Classes for virtual Sensors–Bumpers

January 2, 2011 Leave a comment

As I rework the behavior-based robot framework, I am encountering other areas of code that could also use improvement.  In the case of some of my sensor wrappers, I am not even sure if I ever published them.  I sometimes create virtual sensors when a group of sensors is used for specialized purposes.  The robot programs are more readable and internal logic can be abstracted from the rest of the program making for an easier implementation.

Bumpers – An example of a virtual sensor

I have a tendency to use bumpers on the front of my robots for easy (bump and escape) navigation.  Over the years I have created a couple of code sets that treat the bumper as one sensor even if I use two or more physical sensors to create it.  This article shows why and how I created a class to hide the details of a “Front Bumper” implementation.

Read more…


Weighted Average Class

December 22, 2010 Leave a comment

Continuing on my current fascination with math and code to implement it, I decided to write a class for weighted average monitoring.  Creating a running average of a stream of numbers can be complicated, and consume many variables (depending on how many samples make up the average).  It also means you have create some sort of queue so the values can shift such that the n most current samples are available for calculation.  That can be a lot of work.

Average = SUM(nSamples)/n 

I found a shortcut to all of that where an average value is maintained in the background, and a weighted percentage is applied to the newest input value and the previous average.  I am sure this method is not as accurate as averaging the last n values, but the goal is not so much precision, but reducing spikes and anomalies.  In other words, I don’t care what the actual average is; I care that the trend of values over time reduces the spikes and provides usable information to the robot with less error.

Weighted Average = New Sample * %wt + (1-%wt)*Previous Average

This class can help filter sensor data to a robot, making it react more slowly to large value changes.  Try it and let me know what you think…

You can download the code here: WeightedAverage.c

Leaky Integrator Class

December 22, 2010 2 comments

I have been working to update my framework lately and I ran across and interesting LeakyIntegratorconcept that can be used by robots to track recurring events and store them in a short-term memory that eventually fades.  This is useful, for instance, when a robot gets stuck in a corner, bumping left and them bumping right and repeating.  This behavior is called canyoning and seems to happen often.  The robot is essentially stuck in the corner, thrashing back and forth until something changes.

Using a Leaky Integrator allow the successive bumps to be summed in the short-term memory while at the same time leaking some bumps out of the bottom at a slower rate.  As the robot thrashes more the integrator is filled up.  This value could be used to adjust the spin angle for the robot so that the more times the robot bumps, the more it turns to get out of the corner.  As the robot escapes, the memory of the corner fades to prevent drastic actions on bumps later in its journey when it may not be stuck in a corner.

An analogy for the leaky integrator is a bucket with a drain in the side that can be opened to let some water out.  Water poured into the top fills the bucket if more water comes in than is let out the bottom.  If the water stops pouring in, eventually the level will decrease to the level of the side drain.  If too much water is poured in, the bucket will eventually have the maximum amount in it and the rest is lost over the top edges.

The RobotC code contains the class.  A sample program is commented out at the bottom.  It demonstrates instantiating the class and filling a bucket (as it tries to leak out) until the bucket is full.  Once full, the input is stopped, and the bucket drains.  Very simple, but might be useful to someone out there in Mindstorms land.

Download: LeakyIntegrator.c

Categories: RobotC Code Tags: ,