![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Allows the M3Pi to be used as a Sumo robot, using the sharp 100 distance sensors on the front. Attack mid/near strategy
SharpDigiDist100/SharpDigiDist100.cpp@0:dda4072af5ff, 2012-06-18 (annotated)
- Committer:
- jonmarsh
- Date:
- Mon Jun 18 09:54:54 2012 +0000
- Revision:
- 0:dda4072af5ff
not tested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jonmarsh | 0:dda4072af5ff | 1 | #include "SharpDigiDist100.h" |
jonmarsh | 0:dda4072af5ff | 2 | |
jonmarsh | 0:dda4072af5ff | 3 | SharpDigiDist100::SharpDigiDist100(PinName pin):intin(pin),pinin(pin) |
jonmarsh | 0:dda4072af5ff | 4 | { |
jonmarsh | 0:dda4072af5ff | 5 | timer1.reset(); |
jonmarsh | 0:dda4072af5ff | 6 | intin.rise(this,&SharpDigiDist100::onInt); |
jonmarsh | 0:dda4072af5ff | 7 | intin.fall(this,&SharpDigiDist100::onInt); |
jonmarsh | 0:dda4072af5ff | 8 | timer1.start(); |
jonmarsh | 0:dda4072af5ff | 9 | //onInt(); |
jonmarsh | 0:dda4072af5ff | 10 | current = Far; |
jonmarsh | 0:dda4072af5ff | 11 | //last = NA; |
jonmarsh | 0:dda4072af5ff | 12 | onChangeAttached = false; |
jonmarsh | 0:dda4072af5ff | 13 | } |
jonmarsh | 0:dda4072af5ff | 14 | |
jonmarsh | 0:dda4072af5ff | 15 | void SharpDigiDist100::onInt() |
jonmarsh | 0:dda4072af5ff | 16 | { |
jonmarsh | 0:dda4072af5ff | 17 | timer1.stop(); |
jonmarsh | 0:dda4072af5ff | 18 | last = current; |
jonmarsh | 0:dda4072af5ff | 19 | int timeFromLast = timer1.read_ms(); |
jonmarsh | 0:dda4072af5ff | 20 | if (timeFromLast < 100) |
jonmarsh | 0:dda4072af5ff | 21 | { |
jonmarsh | 0:dda4072af5ff | 22 | current = Far; |
jonmarsh | 0:dda4072af5ff | 23 | timeout.attach(this,&SharpDigiDist100::onInt, 0.3); |
jonmarsh | 0:dda4072af5ff | 24 | } |
jonmarsh | 0:dda4072af5ff | 25 | else |
jonmarsh | 0:dda4072af5ff | 26 | { |
jonmarsh | 0:dda4072af5ff | 27 | if(pinin) |
jonmarsh | 0:dda4072af5ff | 28 | { |
jonmarsh | 0:dda4072af5ff | 29 | current = Mid; |
jonmarsh | 0:dda4072af5ff | 30 | } |
jonmarsh | 0:dda4072af5ff | 31 | else |
jonmarsh | 0:dda4072af5ff | 32 | { |
jonmarsh | 0:dda4072af5ff | 33 | current = Near; |
jonmarsh | 0:dda4072af5ff | 34 | } |
jonmarsh | 0:dda4072af5ff | 35 | } |
jonmarsh | 0:dda4072af5ff | 36 | if(current != last) |
jonmarsh | 0:dda4072af5ff | 37 | { |
jonmarsh | 0:dda4072af5ff | 38 | if(onChangeAttached) |
jonmarsh | 0:dda4072af5ff | 39 | { |
jonmarsh | 0:dda4072af5ff | 40 | onChange(); |
jonmarsh | 0:dda4072af5ff | 41 | } |
jonmarsh | 0:dda4072af5ff | 42 | } |
jonmarsh | 0:dda4072af5ff | 43 | timer1.reset(); |
jonmarsh | 0:dda4072af5ff | 44 | timer1.start(); |
jonmarsh | 0:dda4072af5ff | 45 | } |
jonmarsh | 0:dda4072af5ff | 46 | |
jonmarsh | 0:dda4072af5ff | 47 | int SharpDigiDist100::getDistance() |
jonmarsh | 0:dda4072af5ff | 48 | { |
jonmarsh | 0:dda4072af5ff | 49 | return current; |
jonmarsh | 0:dda4072af5ff | 50 | } |
jonmarsh | 0:dda4072af5ff | 51 | |
jonmarsh | 0:dda4072af5ff | 52 | void SharpDigiDist100::attachOnChange(void (*ptr) (void)) |
jonmarsh | 0:dda4072af5ff | 53 | { |
jonmarsh | 0:dda4072af5ff | 54 | onChange = ptr; |
jonmarsh | 0:dda4072af5ff | 55 | onChangeAttached = true; |
jonmarsh | 0:dda4072af5ff | 56 | } |