Lab 4

Dependencies:   mbed

Committer:
mikeb
Date:
Mon Mar 14 14:03:53 2016 +0000
Revision:
2:ac894e4cff0d
Parent:
1:f68acb38a472
Final;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mikeb 0:a35011a2fdaa 1 #include <mbed.h>
mikeb 1:f68acb38a472 2 /** A Hall-Effect sensor for measuring current levels in a given path
mikeb 1:f68acb38a472 3 *
mikeb 1:f68acb38a472 4 * Can be used as a current detector
mikeb 1:f68acb38a472 5 *
mikeb 1:f68acb38a472 6 * Example:
mikeb 1:f68acb38a472 7 * @code
mikeb 1:f68acb38a472 8 * // Periodically read current levels in a circuit and
mikeb 1:f68acb38a472 9 * // send output to PC terminal
mikeb 1:f68acb38a472 10 *
mikeb 1:f68acb38a472 11 * #include "mbed.h"
mikeb 1:f68acb38a472 12 * #include "ACS712.h"
mikeb 1:f68acb38a472 13 *
mikeb 1:f68acb38a472 14 * // Connect the sensor analog output pin to mbed's AnalogIn pin
mikeb 1:f68acb38a472 15 * ACS712 dev(p18);
mikeb 1:f68acb38a472 16 * // Connect mbed to pc's USB port
mikeb 1:f68acb38a472 17 * Serial pc(USBTX, USBRX);
mikeb 1:f68acb38a472 18 *
mikeb 1:f68acb38a472 19 * int main() {
mikeb 1:f68acb38a472 20 * pc.printf("Sensor Log: \n\n\r");
mikeb 1:f68acb38a472 21 * while (1) {
mikeb 1:f68acb38a472 22 * // Read current from sensor and output to pc terminal
mikeb 1:f68acb38a472 23 * pc.printf("Sensor Value: %2.2f A\n\r", dev);
mikeb 1:f68acb38a472 24 * wait(0.200);
mikeb 1:f68acb38a472 25 * }
mikeb 1:f68acb38a472 26 * }
mikeb 1:f68acb38a472 27 * @endcode
mikeb 1:f68acb38a472 28 */
mikeb 1:f68acb38a472 29
mikeb 0:a35011a2fdaa 30 class ACS712 {
mikeb 0:a35011a2fdaa 31
mikeb 0:a35011a2fdaa 32 public:
mikeb 1:f68acb38a472 33 /** Create a hall-effect sensor of the specified type
mikeb 1:f68acb38a472 34 *
mikeb 1:f68acb38a472 35 * @param _pin mbed AnalogIn pin where the analog output of sensor is connected
mikeb 1:f68acb38a472 36 * @param voltDivRatio resistor voltage division ratio at output of the sensor
mikeb 1:f68acb38a472 37 * @param type type of ACS712 sensor used
mikeb 1:f68acb38a472 38 *
mikeb 1:f68acb38a472 39 * @note Supported types of sensors:
mikeb 1:f68acb38a472 40 */
mikeb 0:a35011a2fdaa 41 ACS712(PinName _pin, float voltDivRatio = 1, short type = 5);
mikeb 0:a35011a2fdaa 42
mikeb 1:f68acb38a472 43 /** Read the value of the measured current in amps
mikeb 1:f68acb38a472 44 *
mikeb 1:f68acb38a472 45 * @return current value in amps
mikeb 1:f68acb38a472 46 */
mikeb 0:a35011a2fdaa 47 float read();
mikeb 2:ac894e4cff0d 48
mikeb 1:f68acb38a472 49
mikeb 1:f68acb38a472 50 /** Read the value of the measured current in amps
mikeb 1:f68acb38a472 51 * Allows the ACS712 object to be used in a float context
mikeb 1:f68acb38a472 52 *
mikeb 1:f68acb38a472 53 * @return current value in amps
mikeb 1:f68acb38a472 54 */
mikeb 2:ac894e4cff0d 55 operator float() { return read();
mikeb 2:ac894e4cff0d 56
mikeb 2:ac894e4cff0d 57
mikeb 2:ac894e4cff0d 58 ACS712& operator=(const ACS712&);
mikeb 2:ac894e4cff0d 59 }
mikeb 0:a35011a2fdaa 60
mikeb 0:a35011a2fdaa 61 private:
mikeb 0:a35011a2fdaa 62 AnalogIn sensor;
mikeb 0:a35011a2fdaa 63 float translate(float);
mikeb 0:a35011a2fdaa 64 float ratio;
mikeb 0:a35011a2fdaa 65 short type;
mikeb 0:a35011a2fdaa 66
mikeb 0:a35011a2fdaa 67 };
mikeb 0:a35011a2fdaa 68
mikeb 1:f68acb38a472 69 ACS712::ACS712(PinName _pin, float voltDivRatio, short sensorType) : sensor(_pin){
mikeb 0:a35011a2fdaa 70 ratio = voltDivRatio;
mikeb 1:f68acb38a472 71 type = sensorType;
mikeb 0:a35011a2fdaa 72 }
mikeb 0:a35011a2fdaa 73
mikeb 0:a35011a2fdaa 74 float ACS712::translate(float val){
mikeb 0:a35011a2fdaa 75 switch(type){
mikeb 0:a35011a2fdaa 76 case 5:
mikeb 1:f68acb38a472 77 return (val*ratio - 2.46*ratio)/(.185*ratio);
mikeb 0:a35011a2fdaa 78 case 20:
mikeb 1:f68acb38a472 79 return (val*ratio - 2.46*ratio)/(.1*ratio);
mikeb 0:a35011a2fdaa 80 case 30:
mikeb 1:f68acb38a472 81 return (val*ratio - 2.46*ratio)/(.066*ratio);
mikeb 0:a35011a2fdaa 82 default:
mikeb 0:a35011a2fdaa 83 return 999;
mikeb 0:a35011a2fdaa 84 }
mikeb 0:a35011a2fdaa 85 }
mikeb 0:a35011a2fdaa 86
mikeb 0:a35011a2fdaa 87
mikeb 0:a35011a2fdaa 88 float ACS712::read(){
mikeb 1:f68acb38a472 89 return ACS712::translate(sensor * 3.3);
mikeb 0:a35011a2fdaa 90 }
mikeb 0:a35011a2fdaa 91
mikeb 1:f68acb38a472 92 ACS712& ACS712::operator=(const ACS712& rhs){
mikeb 1:f68acb38a472 93 sensor = rhs.sensor;
mikeb 1:f68acb38a472 94 ratio = rhs.ratio;
mikeb 1:f68acb38a472 95 type = rhs.type;
mikeb 1:f68acb38a472 96 return *this;
mikeb 0:a35011a2fdaa 97 }