Interface class for the Max Botix ultrasonic range finder model 1210. It includes input methods for PWM, Analog, and Serial. A PwmIn class was created to allow the PWM input to be read. Now includes automatic range update via interrupts.
MB1210.h@4:a615b75d4126, 2010-08-28 (annotated)
- Committer:
- Blaze513
- Date:
- Sat Aug 28 07:59:29 2010 +0000
- Revision:
- 4:a615b75d4126
- Parent:
- 3:05183e50a923
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Blaze513 | 2:997b4057c879 | 1 | //mbed Microcontroller Library |
Blaze513 | 2:997b4057c879 | 2 | //Max Botix Ultrasonic Range Finder MB1210 Interface |
Blaze513 | 2:997b4057c879 | 3 | //Copyright 2010 |
Blaze513 | 2:997b4057c879 | 4 | //Thomas Hamilton |
Blaze513 | 2:997b4057c879 | 5 | |
Blaze513 | 0:3d969e0b4ca0 | 6 | #ifndef MB1210Library |
Blaze513 | 0:3d969e0b4ca0 | 7 | #define MB1210Library |
Blaze513 | 0:3d969e0b4ca0 | 8 | |
Blaze513 | 0:3d969e0b4ca0 | 9 | #include "mbed.h" |
Blaze513 | 0:3d969e0b4ca0 | 10 | #include "PwmIn.h" |
Blaze513 | 0:3d969e0b4ca0 | 11 | |
Blaze513 | 0:3d969e0b4ca0 | 12 | class MB1210 |
Blaze513 | 0:3d969e0b4ca0 | 13 | { |
Blaze513 | 0:3d969e0b4ca0 | 14 | private: |
Blaze513 | 0:3d969e0b4ca0 | 15 | PwmIn* PwmInput; |
Blaze513 | 0:3d969e0b4ca0 | 16 | AnalogIn* AnalogInput; |
Blaze513 | 0:3d969e0b4ca0 | 17 | DigitalOut* SerialOutput; |
Blaze513 | 0:3d969e0b4ca0 | 18 | Serial* SerialInput; |
Blaze513 | 2:997b4057c879 | 19 | |
Blaze513 | 0:3d969e0b4ca0 | 20 | char OperatingMode; |
Blaze513 | 0:3d969e0b4ca0 | 21 | float UnitFactor; |
Blaze513 | 0:3d969e0b4ca0 | 22 | float PwmScalingFactor; |
Blaze513 | 0:3d969e0b4ca0 | 23 | float AnalogScalingFactor; |
Blaze513 | 4:a615b75d4126 | 24 | float Range; |
Blaze513 | 4:a615b75d4126 | 25 | float* InterruptBuffer; |
Blaze513 | 4:a615b75d4126 | 26 | |
Blaze513 | 4:a615b75d4126 | 27 | void Interrupt(); |
Blaze513 | 2:997b4057c879 | 28 | |
Blaze513 | 0:3d969e0b4ca0 | 29 | public: |
Blaze513 | 0:3d969e0b4ca0 | 30 | MB1210(PinName pw, PinName an, PinName tx, PinName rx); |
Blaze513 | 0:3d969e0b4ca0 | 31 | //pulse width modulation input, analog input, serial output, serial input; |
Blaze513 | 4:a615b75d4126 | 32 | //specify NC if pin is not used; |
Blaze513 | 4:a615b75d4126 | 33 | //if the pulse width pin is used, interrupts will perform a |
Blaze513 | 4:a615b75d4126 | 34 | //few microseconds of calculations every time a reading is taken |
Blaze513 | 0:3d969e0b4ca0 | 35 | ~MB1210(); |
Blaze513 | 0:3d969e0b4ca0 | 36 | //deallocates PwmInput, AnalogInput, SerialOutput, and SerailInput |
Blaze513 | 0:3d969e0b4ca0 | 37 | void SoundVelocity(float MetersPerSecond); |
Blaze513 | 0:3d969e0b4ca0 | 38 | //if, for some reason, you need to correct the speed of sound |
Blaze513 | 0:3d969e0b4ca0 | 39 | //for Pwm modes, enter the new value here in meters per second; |
Blaze513 | 0:3d969e0b4ca0 | 40 | //default is 340.29 m/s |
Blaze513 | 0:3d969e0b4ca0 | 41 | //Note: most accurate mode |
Blaze513 | 0:3d969e0b4ca0 | 42 | void Voltage(float Volts); |
Blaze513 | 0:3d969e0b4ca0 | 43 | //sets expected operating voltage for Analog modes; |
Blaze513 | 0:3d969e0b4ca0 | 44 | //user responsibility to ensure operating voltage between 3.3 V and 5 V; |
Blaze513 | 0:3d969e0b4ca0 | 45 | //default is 3.3 V |
Blaze513 | 0:3d969e0b4ca0 | 46 | void Unit(float UnitsPerMeter); |
Blaze513 | 0:3d969e0b4ca0 | 47 | //argument sets the putput units through multiplication; |
Blaze513 | 0:3d969e0b4ca0 | 48 | //default is cm |
Blaze513 | 0:3d969e0b4ca0 | 49 | void Mode(char Selection); |
Blaze513 | 0:3d969e0b4ca0 | 50 | //argument sets operating mode; |
Blaze513 | 4:a615b75d4126 | 51 | //set flag 0x08 to 0 for polled modes, or 1 for interrupt modes; |
Blaze513 | 4:a615b75d4126 | 52 | //set flag 0x04 to 0 for synchronous modes, or 1 for asynchronous modes; |
Blaze513 | 4:a615b75d4126 | 53 | //set flags 0x03 to 0 for pwm, 1 for analog, or 2 for serial; |
Blaze513 | 4:a615b75d4126 | 54 | //asynchronous modes generate pulse width interrupts, prepare an |
Blaze513 | 4:a615b75d4126 | 55 | //analog reading, and send a 5 byte serial reading every 99 ms; |
Blaze513 | 4:a615b75d4126 | 56 | //interrupt modes automatically read the range into the buffer provided |
Blaze513 | 4:a615b75d4126 | 57 | //by AttachInterruptBuffer with the selected input method every 99 ms |
Blaze513 | 4:a615b75d4126 | 58 | //if in an asynchronous mode, or 99 ms after RequestSyncRead is called; |
Blaze513 | 4:a615b75d4126 | 59 | //the rx pin must be connected to use an interrupt mode; |
Blaze513 | 4:a615b75d4126 | 60 | //the tx pin must be connected to use a synchronous mode; |
Blaze513 | 0:3d969e0b4ca0 | 61 | //default is 0 |
Blaze513 | 4:a615b75d4126 | 62 | void AttachInterruptBuffer(float* Buffer); |
Blaze513 | 4:a615b75d4126 | 63 | //if interrupts are used, user must provide address to write result to |
Blaze513 | 0:3d969e0b4ca0 | 64 | void RequestSyncRead(); |
Blaze513 | 0:3d969e0b4ca0 | 65 | //this tells the device to prepare a synchronous range reading; |
Blaze513 | 0:3d969e0b4ca0 | 66 | //must be called at least 99 ms before the reading is needed; |
Blaze513 | 0:3d969e0b4ca0 | 67 | //changes asynchronous mode to synchronous equivalent |
Blaze513 | 3:05183e50a923 | 68 | void DiscardSerialBuffer(); |
Blaze513 | 3:05183e50a923 | 69 | //the serial port has a buffer and only the oldest data is read from it; |
Blaze513 | 3:05183e50a923 | 70 | //the buffer has limited space and, when full, the newest data is discarded; |
Blaze513 | 3:05183e50a923 | 71 | //this method allows the user to empty the buffer of old data so new data is used |
Blaze513 | 0:3d969e0b4ca0 | 72 | float Read(); |
Blaze513 | 0:3d969e0b4ca0 | 73 | //get a reading from the device in the set mode; |
Blaze513 | 0:3d969e0b4ca0 | 74 | //RequestSyncRead() must be called at least 99 ms |
Blaze513 | 0:3d969e0b4ca0 | 75 | //before this method can be called in a synchronous mode; |
Blaze513 | 0:3d969e0b4ca0 | 76 | //may be called at any time during asynchronous mode; |
Blaze513 | 0:3d969e0b4ca0 | 77 | operator float(); |
Blaze513 | 0:3d969e0b4ca0 | 78 | //shorthand for taking a range reading; |
Blaze513 | 0:3d969e0b4ca0 | 79 | //ex: "float reading = MB1210Object;" |
Blaze513 | 0:3d969e0b4ca0 | 80 | }; |
Blaze513 | 0:3d969e0b4ca0 | 81 | |
Blaze513 | 0:3d969e0b4ca0 | 82 | #endif |