Allows the M3Pi to be used as a Sumo robot, using the sharp 100 distance sensors on the front. Run away strategy

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SharpDigiDist100.cpp Source File

SharpDigiDist100.cpp

00001 #include "SharpDigiDist100.h"
00002 
00003 SharpDigiDist100::SharpDigiDist100(PinName pin):intin(pin),pinin(pin)
00004 {
00005 timer1.reset();
00006 intin.rise(this,&SharpDigiDist100::onInt);
00007 intin.fall(this,&SharpDigiDist100::onInt);
00008 timer1.start();
00009 //onInt();
00010 current = Far;
00011 //last = NA;
00012 onChangeAttached = false;
00013 }
00014 
00015 void SharpDigiDist100::onInt()
00016 {
00017     timer1.stop();
00018     last = current;
00019     int timeFromLast = timer1.read_ms();
00020     if (timeFromLast < 100)
00021     {
00022         current = Far;
00023         timeout.attach(this,&SharpDigiDist100::onInt, 0.3);
00024     }
00025     else
00026     {
00027         if(pinin)
00028         {
00029             current = Mid;
00030         }
00031         else
00032         {
00033             current = Near;
00034         }
00035     }
00036     if(current != last)
00037     {
00038         if(onChangeAttached)
00039         {
00040       onChange();
00041       }
00042     }
00043     timer1.reset();
00044     timer1.start();
00045 }
00046 
00047 int SharpDigiDist100::getDistance()
00048 {
00049     return current;
00050 }
00051 
00052 void SharpDigiDist100::attachOnChange(void (*ptr) (void))
00053 {
00054     onChange = ptr;
00055     onChangeAttached = true;
00056 }