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.cpp
- Committer:
- Blaze513
- Date:
- 2010-08-22
- Revision:
- 0:3d969e0b4ca0
- Child:
- 1:b533b95e807a
File content as of revision 0:3d969e0b4ca0:
#include "MB1210.h" MB1210::MB1210(PinName pw, PinName an, PinName tx, PinName rx) : OperatingMode(0x00), UnitFactor(1), PwmScalingFactor(17014.5), AnalogScalingFactor(1024) { if (rx != NC) { SerialInput = new Serial(NC, rx); SerialInput->baud(9600); SerialInput->format(8, Serial::None, 1); OperatingMode = 0x02; } if (an != NC) { AnalogInput = new AnalogIn(an); OperatingMode = 0x01; } if (pw != NC) { PwmInput = new PwmIn(pw); OperatingMode = 0x00; } if (tx != NC) { SerialOutput = new DigitalOut(tx); SerialOutput->write(0); } } //constructor dynamically allocates memory and cpu time (interrupts) //to input objects depending on how the device is connected MB1210::~MB1210() { delete PwmInput; delete AnalogInput; delete SerialOutput; delete SerialInput; delete this; } //input objects must be deallocated void MB1210::SoundVelocity(float MetersPerSecond) { PwmScalingFactor = (UnitFactor * MetersPerSecond * 50); } //set the velocity of sound for pwm readings void MB1210::Voltage(float Volts) { AnalogScalingFactor = (UnitFactor * 3379.2 / Volts); } //set the voltage correction factor for analog readings void MB1210::Unit(float UnitsPerMeter) { PwmScalingFactor *= (UnitsPerMeter / UnitFactor / 100); AnalogScalingFactor *= (UnitsPerMeter / UnitFactor / 100); UnitFactor = UnitsPerMeter / 100; } //set the unit factor to return the range in units other than cm void MB1210::Mode(char Selection) { if (SerialOutput) { SerialOutput->write(Selection & 0x04); } OperatingMode = Selection & 0x03; } //change the operating mode; SerialOutput controls synchronicity void MB1210::RequestSyncRead() { if (SerialOutput) { SerialOutput->write(1); wait_us(20); SerialOutput->write(0); } } //hold pin high for at least 20 us to request a synchronous range reading float MB1210::Read() { switch (OperatingMode) { case 0: if (PwmInput) { return PwmInput->pulsewidth() * PwmScalingFactor; } else { return 0; } case 1: if (AnalogInput) { return AnalogInput->read() * AnalogScalingFactor; } else { return 0; } case 2: if (SerialInput) { if (SerialInput->readable()) { Workspace[3] = 0; do { Workspace[0] = SerialInput->getc(); Workspace[3]++; } while (SerialInput->readable() && (Workspace[0] != 'R') && (Workspace[3] < 5)); for (unsigned char i = 0; i < 3; i++) { if (SerialInput->readable()) { Workspace[i] = SerialInput->getc(); } else { Workspace[i] = 0x00; } } Workspace[3] = 0x00; return atof(Workspace) * UnitFactor; } } else { return 0; } default: return 0; } } //OperatingMode switches to desired output method; //once gathered, the result is scaled according to voltage, the speed of sound, and desired unit void MB1210::Read(char* Buffer) { if (SerialInput) { if (SerialInput->readable()) { Workspace[3] = 0; do { Workspace[0] = SerialInput->getc(); Workspace[3]++; } while (SerialInput->readable() && (Workspace[0] != 'R') && (Workspace[3] < 5)); for (unsigned char i = 0; i < 3; i++) { if (SerialInput->readable()) { Buffer[i] = SerialInput->getc(); } else { Buffer[i] = 0x00; } } } } } //this overload is for serial only, regardless of selected mode; //reads 3 ASCII character result into the given buffer MB1210::operator float() { return Read(); } //conversion function acts as shorthand for Read()