N K
/
GaNtroller
a fork of priustroller
Fork of priustroller_current by
sensors/sensors.h@29:cb03760ba9ea, 2015-03-14 (annotated)
- Committer:
- nki
- Date:
- Sat Mar 14 23:42:46 2015 +0000
- Revision:
- 29:cb03760ba9ea
- Parent:
- 28:af9d43bcb371
- Child:
- 30:2b6d426f3bfc
Cautionary commit, possibly works
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 1:1f58bdcf2956 | 1 | #ifndef __SENSORS_H |
bwang | 1:1f58bdcf2956 | 2 | #define __SENSORS_H |
bwang | 1:1f58bdcf2956 | 3 | |
bwang | 1:1f58bdcf2956 | 4 | #include "includes.h" |
bwang | 1:1f58bdcf2956 | 5 | |
bwang | 28:af9d43bcb371 | 6 | class NativeAnalogIn { |
bwang | 28:af9d43bcb371 | 7 | public: |
bwang | 28:af9d43bcb371 | 8 | NativeAnalogIn(PinName pin); |
bwang | 28:af9d43bcb371 | 9 | float Read(); |
bwang | 28:af9d43bcb371 | 10 | operator float() {return Read();} |
nki | 29:cb03760ba9ea | 11 | static unsigned char PinToAdcChannel(PinName pin); |
bwang | 28:af9d43bcb371 | 12 | private: |
bwang | 28:af9d43bcb371 | 13 | unsigned char _channel; |
nki | 29:cb03760ba9ea | 14 | float _dummy; |
bwang | 1:1f58bdcf2956 | 15 | }; |
bwang | 1:1f58bdcf2956 | 16 | |
bwang | 1:1f58bdcf2956 | 17 | class VoltageSensor { |
bwang | 1:1f58bdcf2956 | 18 | public: |
bwang | 1:1f58bdcf2956 | 19 | virtual float GetVoltage() {return 0.0f;} |
bwang | 1:1f58bdcf2956 | 20 | virtual void Zero(){} |
bwang | 1:1f58bdcf2956 | 21 | }; |
bwang | 1:1f58bdcf2956 | 22 | |
bwang | 1:1f58bdcf2956 | 23 | class PositionSensor { |
bwang | 1:1f58bdcf2956 | 24 | public: |
bwang | 1:1f58bdcf2956 | 25 | virtual float GetPosition() {return 0.0f;} |
bwang | 1:1f58bdcf2956 | 26 | }; |
bwang | 1:1f58bdcf2956 | 27 | |
bwang | 1:1f58bdcf2956 | 28 | class TempSensor { |
bwang | 1:1f58bdcf2956 | 29 | public: |
bwang | 1:1f58bdcf2956 | 30 | virtual float GetTemp() {return 0.0f;} |
bwang | 1:1f58bdcf2956 | 31 | }; |
bwang | 1:1f58bdcf2956 | 32 | |
bwang | 1:1f58bdcf2956 | 33 | class AnalogVoltageSensor: public VoltageSensor { |
bwang | 1:1f58bdcf2956 | 34 | public: |
bwang | 1:1f58bdcf2956 | 35 | AnalogVoltageSensor(PinName pin, float scale); |
bwang | 1:1f58bdcf2956 | 36 | virtual void Zero(); |
bwang | 1:1f58bdcf2956 | 37 | virtual float GetVoltage(); |
bwang | 1:1f58bdcf2956 | 38 | private: |
bwang | 1:1f58bdcf2956 | 39 | float _zero_level, _scale; |
bwang | 28:af9d43bcb371 | 40 | NativeAnalogIn *_in; |
bwang | 1:1f58bdcf2956 | 41 | }; |
bwang | 1:1f58bdcf2956 | 42 | |
bwang | 1:1f58bdcf2956 | 43 | class AnalogHallPositionSensor: public PositionSensor { |
bwang | 1:1f58bdcf2956 | 44 | public: |
bwang | 1:1f58bdcf2956 | 45 | AnalogHallPositionSensor(PinName pin_a, PinName pin_b, float cal1_a, float cal2_a, |
bwang | 1:1f58bdcf2956 | 46 | float cal1_b, float cal2_b, float offset); |
bwang | 1:1f58bdcf2956 | 47 | virtual float GetPosition(); |
bwang | 1:1f58bdcf2956 | 48 | private: |
bwang | 1:1f58bdcf2956 | 49 | float _cal1_a, _cal2_a; |
bwang | 1:1f58bdcf2956 | 50 | float _cal1_b, _cal2_b; |
bwang | 1:1f58bdcf2956 | 51 | float _offset; |
bwang | 28:af9d43bcb371 | 52 | NativeAnalogIn *_in_a, *_in_b; |
bwang | 1:1f58bdcf2956 | 53 | }; |
bwang | 1:1f58bdcf2956 | 54 | |
bwang | 1:1f58bdcf2956 | 55 | class NtcTempSensor: public TempSensor { |
bwang | 1:1f58bdcf2956 | 56 | public: |
bwang | 1:1f58bdcf2956 | 57 | NtcTempSensor(PinName pin, float r_25, float ntc_b, float r_divider); |
bwang | 1:1f58bdcf2956 | 58 | virtual float GetTemp(); |
bwang | 1:1f58bdcf2956 | 59 | private: |
bwang | 1:1f58bdcf2956 | 60 | float _ntc_a, _ntc_b, _r_divider; |
bwang | 28:af9d43bcb371 | 61 | NativeAnalogIn *_in; |
bwang | 1:1f58bdcf2956 | 62 | }; |
bwang | 1:1f58bdcf2956 | 63 | |
bwang | 1:1f58bdcf2956 | 64 | class Throttle { |
bwang | 1:1f58bdcf2956 | 65 | public: |
bwang | 1:1f58bdcf2956 | 66 | Throttle(PinName pin, float min, float max); |
bwang | 1:1f58bdcf2956 | 67 | float GetThrottle(); |
bwang | 1:1f58bdcf2956 | 68 | private: |
bwang | 1:1f58bdcf2956 | 69 | float _min, _max; |
bwang | 1:1f58bdcf2956 | 70 | VoltageSensor *_in; |
bwang | 1:1f58bdcf2956 | 71 | }; |
bwang | 1:1f58bdcf2956 | 72 | |
nki | 29:cb03760ba9ea | 73 | inline float NativeAnalogIn::Read() { |
nki | 29:cb03760ba9ea | 74 | TIM2->DIER = 0x0000; |
nki | 29:cb03760ba9ea | 75 | ADC1->SQR3 = 0x0000; |
nki | 29:cb03760ba9ea | 76 | ADC1->SQR3 |= _channel; |
nki | 29:cb03760ba9ea | 77 | ADC1->CR2 |= ADC_CR2_SWSTART; |
nki | 29:cb03760ba9ea | 78 | while ((ADC1->SR & 2) == 0){} |
nki | 29:cb03760ba9ea | 79 | float x = ADC1->DR; |
nki | 29:cb03760ba9ea | 80 | TIM2->DIER = 0x0001; |
nki | 29:cb03760ba9ea | 81 | return x / 4095.0f; |
nki | 29:cb03760ba9ea | 82 | } |
nki | 29:cb03760ba9ea | 83 | |
bwang | 1:1f58bdcf2956 | 84 | #endif |