Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "altimeter.h"
00023 #include "spi.h"
00024 #include "multifilter.h"
00025
00026 #define MAX_PRESS 120000
00027 #define MIN_PRESS 30000
00028
00029 #define SCP_SMOOTH 8
00030 #define TIMEOUT 100
00031 #define SAMPLE_AVERAGE 16 //Average ground level over this many samples
00032
00033
00034 #define REVID 0x00 //ASIC Revision Number
00035 #define OPERATION 0x03 //Operation register
00036 #define OPSTATUS 0x04 //Operation Status
00037 #define STATUS 0x07 //ASIC Status
00038 #define START 0x0A //Constant Readings
00039 #define PRESSURE 0x1F //Pressure 3 MSB
00040 #define PRESSURE_LSB 0x20 //Pressure 16 LSB
00041 #define TEMP 0x21 //16 bit temp
00042
00043 #define OP_STOP 0x00
00044 #define OP_READIND 0x01
00045 #define OP_WRITEIND 0x02
00046 #define OP_READEEPROM 0x05
00047 #define OP_WRITEEEPROM 0x06
00048 #define OP_INIT 0x07
00049 #define OP_HISPEED 0x09
00050 #define OP_HIRES 0x0a
00051 #define OP_LOWPOWER 0x0b
00052 #define OP_SINGLE 0x0c
00053 #define OP_SELFTEST 0x0f
00054
00055
00064 class SCP1000 : public Altimeter
00065 {
00066
00067 DECLARE_MODULE(SCP1000,Altimeter)
00068
00069 public:
00070 SCP1000()
00071 {
00072 m_timeout = TIMEOUT;
00073 m_altitude = 0;
00074 m_gain = -8.24;
00075 m_filter.zeros[0] = 0;
00076
00077 AddSocket("SCP1000.Filter",m_filter.amounts);
00078 AddSocket("SCP1000.Gain",&m_gain);
00079 }
00080
00081
00082
00083 virtual void Init()
00084 {
00085 pinMode(SCPSELECT,OUTPUT);
00086 digitalWrite(SCPSELECT,HIGH);
00087 pinMode(SCPDRDY, INPUT);
00088 SetInterval(30);
00089 SetPriority(10);
00090 delay(100);
00091 Reset();
00092 }
00093
00094 virtual void Reference()
00095 {
00096 m_filter.Reference(SAMPLE_AVERAGE);
00097 }
00098
00099 virtual void ParamChanged(Socket * param)
00100 {
00101 m_filter.ParamChanged(param);
00102 }
00103
00104
00105 virtual void Loop(const unsigned long& interval)
00106 {
00107 if (!Ready())
00108 {
00109 return;
00110 }
00111
00112 long int press = ReadPressure();
00113 if (press > MAX_PRESS || press < MIN_PRESS)
00114 {
00115
00116 return;
00117 }
00118 float alt = press * m_gain;
00119 m_filter.Update(&alt);
00120 altitude = m_filter.filtered[0];
00121 }
00122
00123 private:
00124 void Reset()
00125 {
00126 SPI.Write(OPERATION,OP_STOP);
00127 if (digitalRead(SCPDRDY) == LOW)
00128 {
00129 ReadPressure();
00130 }
00131 int timeout = 600;
00132 while (SPI.Read(OPSTATUS) != 0)
00133 {
00134 delay(1);
00135 if (timeout-- < 0)
00136 {
00137
00138 }
00139 };
00140 SPI.Write(OPERATION,OP_HISPEED);
00141
00142 }
00143
00144 long int ReadPressure()
00145 {
00146 long int ret;
00147 ret = (SPI.Read(PRESSURE) & B00000111);
00148 ret = ret << 16;
00149 ret |= SPI.Read16(PRESSURE_LSB);
00150 ret = ret /4;
00151 return(ret);
00152 }
00153
00154 bool Ready()
00155 {
00156 if (digitalRead(SCPDRDY) == LOW)
00157 {
00158 if ((--m_timeout) == 0)
00159 {
00160 Reset();
00161 m_timeout = TIMEOUT;
00162 }
00163 return(false);
00164 }
00165 m_timeout = TIMEOUT;
00166 return(true);
00167 }
00168
00169 int m_timeout;
00170 long int m_altitude;
00171 ParameterF m_gain;
00172 MultiFilterRef<1> m_filter;
00173
00174 };
00175
00176 SCP1000 g_SCP1000;
00177
00178