パルスボールを検知するためのセンサについて
名前がIRM2121となっていますが,たいていのものは使えるはずです.
/*IRM2121.h*/
#ifndef MBED_IRM_H
#define MBED_IRM_H
#include "mbed.h"
#define NAKATA 833 //周期833us
#define NOT_F 0
/** IRM2121 class, based on an DigitalIn pin, and a timer
* use RoboCup Junior Soccer to find PULSE BALL.
* Example:
* @code
* // Set Digital and read sensor.
* #include "mbed.h"
* #include "IRM2121.h"
* IRM2121 IR(p21);
*
* int main() {
* unsigned int length;
* while(1) {
*
* length = IR.Read();
* }
* }
* @endcode
*/
class IRM2121 {
public:
/** Create a IRM2121 object connected to the specified pin
**/
IRM2121(PinName IRM_PIN);
unsigned int Read (void);
/** Read the result
*
* @param none
*/
private:
DigitalIn _pin;
Timer _timer;
unsigned int _Time;
};
#endif
/*IRM2121.cpp*/
#include "IRM2121.h"
#include "mbed.h"
IRM2121::IRM2121(PinName IRM_PIN)
: _pin(IRM_PIN), _timer()
{
}
unsigned int IRM2121::Read(void)
// 0 means not valid.
{
_timer.reset();
_timer.start();
while(_pin){
if(_timer.read_us() > NAKATA){
_timer.stop();
return NOT_F;
}
}
_timer.reset();
while(!_pin){
if(_timer.read_us() > NAKATA){
_timer.stop();
return NOT_F;
}
}
_Time = _timer.read_us();
while(_pin){
if((_timer.read_us() -_Time) > NAKATA){
_timer.stop();
return NOT_F;
}
}
_Time = _timer.read_us() - _Time;
_timer.stop();
return _Time;
}