a fork of priustroller

Dependencies:   mbed

Fork of priustroller_current by N K

sensors/sensors.h

Committer:
nki
Date:
2015-03-14
Revision:
29:cb03760ba9ea
Parent:
28:af9d43bcb371
Child:
30:2b6d426f3bfc

File content as of revision 29:cb03760ba9ea:

#ifndef __SENSORS_H
#define __SENSORS_H

#include "includes.h"

class NativeAnalogIn {
public:
    NativeAnalogIn(PinName pin);
    float Read();
    operator float() {return Read();}
    static unsigned char PinToAdcChannel(PinName pin);
private:
    unsigned char _channel;
    float _dummy;
};

class VoltageSensor {
public:
    virtual float GetVoltage() {return 0.0f;}
    virtual void Zero(){}
};

class PositionSensor {
public:
    virtual float GetPosition() {return 0.0f;}
};

class TempSensor {
public:
    virtual float GetTemp() {return 0.0f;}
};

class AnalogVoltageSensor: public VoltageSensor {
public:
    AnalogVoltageSensor(PinName pin, float scale);
    virtual void Zero();
    virtual float GetVoltage();
private:
    float _zero_level, _scale;
    NativeAnalogIn *_in;
};

class AnalogHallPositionSensor: public PositionSensor {
public:
    AnalogHallPositionSensor(PinName pin_a, PinName pin_b, float cal1_a, float cal2_a, 
                             float cal1_b, float cal2_b, float offset);
    virtual float GetPosition();
private:
    float _cal1_a, _cal2_a;
    float _cal1_b, _cal2_b;
    float _offset;
    NativeAnalogIn *_in_a, *_in_b;
};

class NtcTempSensor: public TempSensor {
public:
    NtcTempSensor(PinName pin, float r_25, float ntc_b, float r_divider);
    virtual float GetTemp();
private:
    float _ntc_a, _ntc_b, _r_divider;
    NativeAnalogIn *_in;
};

class Throttle {
public:
    Throttle(PinName pin, float min, float max);
    float GetThrottle();
private:
    float _min, _max;
    VoltageSensor *_in;
};

inline float NativeAnalogIn::Read() {
    TIM2->DIER = 0x0000;
    ADC1->SQR3 = 0x0000;
    ADC1->SQR3 |= _channel;
    ADC1->CR2 |= ADC_CR2_SWSTART;
    while ((ADC1->SR & 2) == 0){}
    float x = ADC1->DR;
    TIM2->DIER = 0x0001;
    return x / 4095.0f;
}

#endif