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.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Blaze513
Date:
Sun Aug 22 21:18:20 2010 +0000
Child:
1:b533b95e807a
Commit message:

Changed in this revision

MB1210.cpp Show annotated file Show diff for this revision Revisions of this file
MB1210.h Show annotated file Show diff for this revision Revisions of this file
PwmIn/PwmIn.cpp Show annotated file Show diff for this revision Revisions of this file
PwmIn/PwmIn.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MB1210.cpp	Sun Aug 22 21:18:20 2010 +0000
@@ -0,0 +1,174 @@
+#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()
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MB1210.h	Sun Aug 22 21:18:20 2010 +0000
@@ -0,0 +1,65 @@
+#ifndef MB1210Library
+#define MB1210Library
+
+#include "mbed.h"
+#include "PwmIn.h"
+
+class MB1210
+{
+    private:
+        PwmIn* PwmInput;
+        AnalogIn* AnalogInput;
+        DigitalOut* SerialOutput;
+        Serial* SerialInput;
+        
+        char OperatingMode;
+        float UnitFactor;
+        float PwmScalingFactor;
+        float AnalogScalingFactor;
+        char Workspace[4];
+        
+    public:
+        MB1210(PinName pw, PinName an, PinName tx, PinName rx);
+            //pulse width modulation input, analog input, serial output, serial input;
+            //specify NC if pin is not used
+        ~MB1210();
+            //deallocates PwmInput, AnalogInput, SerialOutput, and SerailInput
+        void SoundVelocity(float MetersPerSecond);
+            //if, for some reason, you need to correct the speed of sound
+            //for Pwm modes, enter the new value here in meters per second;
+            //default is 340.29 m/s
+            //Note: most accurate mode
+        void Voltage(float Volts);
+            //sets expected operating voltage for Analog modes;
+            //user responsibility to ensure operating voltage between 3.3 V and 5 V;
+            //default is 3.3 V
+        void Unit(float UnitsPerMeter);
+            //argument sets the putput units through multiplication;
+            //default is cm
+        void Mode(char Selection);
+            //argument sets operating mode;
+            //0 to 2 for synchronous modes, 4 to 6 for asynchronous modes;
+            //0 and 4 for pwm, 1 and 5 for analog, 2 and 6 for serial;
+            //default is 0
+        void RequestSyncRead();
+            //this tells the device to prepare a synchronous range reading;
+            //must be called at least 99 ms before the reading is needed;
+            //changes asynchronous mode to synchronous equivalent
+        void DiscardBuffer();
+            //empties the buffer on the serial port to sync the next reading
+        float Read();
+            //get a reading from the device in the set mode;
+            //RequestSyncRead() must be called at least 99 ms
+            //before this method can be called in a synchronous mode;
+            //may be called at any time during asynchronous mode;
+        void Read(char* Buffer);
+            //overloaded to take advantage of Serial ASCII string output;
+            //Buffer must be 3 bytes; 3 digit ASCII number
+            //measures range in cm regardless of selected unit;
+            //uses serial input method regardless of selected mode.
+        operator float();
+            //shorthand for taking a range reading;
+            //ex: "float reading = MB1210Object;"
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PwmIn/PwmIn.cpp	Sun Aug 22 21:18:20 2010 +0000
@@ -0,0 +1,54 @@
+#include "PwmIn.h"
+
+PwmIn::PwmIn(PinName pwi) : InterruptIn(pwi), PeriodMeasurement(0), PulseWidthMeasurement(1)
+{
+        rise(this, &PwmIn::PulseStart);
+        fall(this, &PwmIn::PulseStop);
+        start();
+}
+
+float PwmIn::read()
+{
+    return (float)PulseWidthMeasurement / PeriodMeasurement;
+}
+
+float PwmIn::period()
+{
+    return (float)PeriodMeasurement / 1000000;
+}
+
+int PwmIn::period_ms()
+{
+    return PeriodMeasurement / 1000;
+}
+
+int PwmIn::period_us()
+{
+    return PeriodMeasurement;
+}
+
+float PwmIn::pulsewidth()
+{
+    return (float)PulseWidthMeasurement / 1000000;
+}
+
+int PwmIn::pulsewidth_ms()
+{
+    return PulseWidthMeasurement / 1000;
+}
+
+int PwmIn::pulsewidth_us()
+{
+    return PulseWidthMeasurement;
+}
+
+void PwmIn::PulseStart()
+{
+    PeriodMeasurement = read_us();
+    reset();
+}
+
+void PwmIn::PulseStop()
+{
+    PulseWidthMeasurement = read_us();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PwmIn/PwmIn.h	Sun Aug 22 21:18:20 2010 +0000
@@ -0,0 +1,27 @@
+#ifndef PwmInLibrary
+#define PwmInLibrary
+
+#include "stdint.h"
+#include "mbed.h"
+
+class PwmIn : private InterruptIn, Timer
+{
+    private:
+        unsigned int PeriodMeasurement;
+        unsigned int PulseWidthMeasurement;
+
+        void PulseStart();
+        void PulseStop();
+        
+    public:
+        PwmIn(PinName pwi);
+        float read();
+        float period();
+        int period_ms();
+        int period_us();
+        float pulsewidth();
+        int pulsewidth_ms();
+        int pulsewidth_us();
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Aug 22 21:18:20 2010 +0000
@@ -0,0 +1,28 @@
+#include "mbed.h"
+#include "MB1210.h"
+
+DigitalOut debugled(LED1);
+Serial Computer(USBTX, USBRX);
+
+MB1210 RangeFinder(p12, p15, p13, p14);
+
+int main()
+{
+    Computer.baud(9600);
+    debugled = 0;
+    RangeFinder.Unit(39.370);//change units to inches
+    RangeFinder.Voltage(5);
+    while(1)
+    {
+        debugled = !debugled;
+        RangeFinder.RequestSyncRead();//request a range reading
+        wait_ms(100);//wait for reading to be prepared
+        RangeFinder.Mode(0);//switch to PWM mode
+        Computer.printf("PWM reading: %f in | ", RangeFinder.Read());
+        RangeFinder.Mode(1);//switch to Analog mode
+        Computer.printf("Analog reading: %f in | ", RangeFinder.Read());
+        RangeFinder.Mode(2);//switch to serial mode
+        Computer.printf("Serial reading: %f in | ", RangeFinder.Read());
+        wait(0.9);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Aug 22 21:18:20 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da