m b
/
ACS712
Lab 4
Diff: ACS712.h
- Revision:
- 1:f68acb38a472
- Parent:
- 0:a35011a2fdaa
- Child:
- 2:ac894e4cff0d
--- a/ACS712.h Wed Mar 09 04:19:10 2016 +0000 +++ b/ACS712.h Mon Mar 14 13:44:12 2016 +0000 @@ -1,56 +1,93 @@ #include <mbed.h> - +/** A Hall-Effect sensor for measuring current levels in a given path + * + * Can be used as a current detector + * + * Example: + * @code + * // Periodically read current levels in a circuit and + * // send output to PC terminal + * + * #include "mbed.h" + * #include "ACS712.h" + * + * // Connect the sensor analog output pin to mbed's AnalogIn pin + * ACS712 dev(p18); + * // Connect mbed to pc's USB port + * Serial pc(USBTX, USBRX); + * + * int main() { + * pc.printf("Sensor Log: \n\n\r"); + * while (1) { + * // Read current from sensor and output to pc terminal + * pc.printf("Sensor Value: %2.2f A\n\r", dev); + * wait(0.200); + * } + * } + * @endcode + */ + class ACS712 { public: + /** Create a hall-effect sensor of the specified type + * + * @param _pin mbed AnalogIn pin where the analog output of sensor is connected + * @param voltDivRatio resistor voltage division ratio at output of the sensor + * @param type type of ACS712 sensor used + * + * @note Supported types of sensors: + */ ACS712(PinName _pin, float voltDivRatio = 1, short type = 5); + /** Read the value of the measured current in amps + * + * @return current value in amps + */ float read(); - float operator=(ACS712&); - //void sampleInterval(float); - //void sampleFrequency(float); - //void bufferLength(int); - //float[] readBuffer(); + ACS712& operator=(const ACS712&); + + /** Read the value of the measured current in amps + * Allows the ACS712 object to be used in a float context + * + * @return current value in amps + */ + operator float() { return read(); } private: AnalogIn sensor; float translate(float); - //float[] circularBuffer; - //float interval; float ratio; short type; }; -ACS712::ACS712(PinName _pin, float voltDivRatio, short type) : sensor(_pin){ +ACS712::ACS712(PinName _pin, float voltDivRatio, short sensorType) : sensor(_pin){ ratio = voltDivRatio; - type = type; + type = sensorType; } float ACS712::translate(float val){ switch(type){ case 5: - return (val*ratio - 2.5*ratio)/(.185*ratio); - break; + return (val*ratio - 2.46*ratio)/(.185*ratio); case 20: - return (val*ratio - 2.5*ratio)/(.1*ratio); - break; + return (val*ratio - 2.46*ratio)/(.1*ratio); case 30: - return (val*ratio - 2.5*ratio)/(.066*ratio); - break; + return (val*ratio - 2.46*ratio)/(.066*ratio); default: return 999; - break; - } } float ACS712::read(){ - return ACS712::translate(sensor); + return ACS712::translate(sensor * 3.3); } -float ACS712::operator=(ACS712& rhs){ - return rhs.read(); +ACS712& ACS712::operator=(const ACS712& rhs){ + sensor = rhs.sensor; + ratio = rhs.ratio; + type = rhs.type; + return *this; } -