lib for realtimeMM funcs

Fork of realtimeMMLib by Graham Nicholson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sensor_base.cpp Source File

sensor_base.cpp

00001 #include "mbed.h"
00002 #include <stdio.h>
00003 #include "sensor_base.h"
00004 #include <iostream> 
00005 #include "qeihw.h"
00006 
00007 using namespace std;
00008 
00009 
00010 
00011 sensor_base::sensor_base()
00012 {
00013     sensor_id = 0;
00014 }
00015 
00016 /*
00017 sensor_base::sensor_base(int id)
00018 {
00019     sensor_id = id;
00020 }
00021 */
00022 
00023 char * sensor_base::read_data()
00024 {
00025     //char  mretval[50];
00026     sprintf(data_sample, "[ID:%d;%5.3f]", sensor_id, 1.0);
00027     return "Base";
00028     //mretval[0] = 68;
00029 }
00030 
00031 sensor_base::~sensor_base()
00032 {
00033 }
00034 
00035 ///////////////////////////////// Digital sensor starts here //////////////////////////////////////
00036 
00037 
00038 sensor_onoff::sensor_onoff() :din(p22)
00039 {
00040 
00041 }
00042 
00043 
00044 
00045 sensor_onoff::sensor_onoff(PinName pin) :din(pin)
00046 {
00047 
00048 }
00049 
00050 
00051 char * sensor_onoff::read_data()
00052 {
00053     //char  mretval[50];
00054     //sprintf(data_sample, "\r\nOnOff[%d]=%d", sensor_id,din.read());
00055     sprintf(data_sample, "[%d;%d]", sensor_id, din.read());
00056     return data_sample;
00057     //mretval[0] = 68;
00058 }
00059 
00060 
00061 sensor_onoff::~sensor_onoff()
00062 {
00063 }
00064 
00065 /////////////////////////////////Start of sensor_vin/////////////////////////////////
00066 
00067 sensor_vin::sensor_vin() :ain(p15) {
00068 }
00069 
00070 sensor_vin::sensor_vin(PinName pin) :ain(pin) {
00071     Serial pc(USBTX, USBRX);    
00072 }
00073 
00074 char * sensor_vin::read_data()
00075 {
00076     //char  mretval[50];
00077     sprintf(data_sample, "[%d;%f]", sensor_id, ain.read());
00078     return data_sample;
00079     //mretval[0] = 68;
00080 }
00081 
00082 sensor_vin::~sensor_vin() {
00083 }
00084 
00085 
00086 ///////////////////////////////////////// Start of sensor_pulse ///////////////
00087 
00088 sensor_pulse::sensor_pulse() :IntIn(p16) {
00089 }
00090 
00091 sensor_pulse::sensor_pulse(PinName pin) :IntIn(pin) {
00092     IntIn.rise(this, &sensor_pulse::ISR1);
00093 }
00094 
00095 char * sensor_pulse::read_data()
00096 {
00097     elapsedtime = t.read();
00098     if (pulsecount>=2) {
00099         period=(lastpulsetime-firstpulsetime)/(pulsecount-1);
00100         uptime = (lastpulsetime-firstpulsetime);        
00101         if ((firstpulsetime < period) and (elapsedtime - lastpulsetime) < period) {
00102             uptime = elapsedtime;
00103         }
00104         frequency=1/period;
00105     }
00106     else {
00107         uptime=0;
00108         frequency=0;
00109     }
00110     //sprintf(data_sample, "[%d;%f;%f;%d;%f;%f;%f]", sensor_id, frequency, uptime ,pulsecount, firstpulsetime, lastpulsetime, elapsedtime);
00111     sprintf(data_sample, "[%d;%d;%f]", sensor_id, pulsecount, uptime);
00112     
00113     pulsecount ++;    
00114     return data_sample;
00115     
00116 }
00117 
00118 sensor_pulse::~sensor_pulse() {
00119 }
00120 
00121 void sensor_pulse::reset() {
00122     t.reset();
00123     t.start();
00124     firstpulsetime=0;
00125     lastpulsetime=0;
00126     pulsecount=0;
00127 }
00128 
00129 
00130 void sensor_pulse::ISR1() {
00131     if (pulsecount==0) {
00132         firstpulsetime=t.read();
00133     }
00134     lastpulsetime=t.read();
00135     pulsecount ++;
00136     //sensor_id ++;   
00137     //DigitalOut light3(LED3);
00138     //light3=1;
00139 }
00140 
00141 
00142 ////////////////////////// quadrature encoder HW /////////////////////////////////////////////
00143 
00144 void toggleIndexLed(void) {
00145     //  led3 = !led3;
00146 }
00147 
00148 sensor_qeihw::sensor_qeihw()  : qei(QEI_DIRINV_NONE, QEI_SIGNALMODE_QUAD, QEI_CAPMODE_4X, QEI_INVINX_NONE)
00149 {
00150     qei.SetDigiFilter(480UL);
00151     qei.SetMaxPosition(1000000);
00152     qei.SetVelocityTimerReload_us(1000);
00153     qei.AppendISR(QEI_INTFLAG_INX_Int, &toggleIndexLed);
00154 
00155     qei.IntCmd(QEI_INTFLAG_INX_Int, ENABLE);
00156     //sensor_id=1001;
00157     sensor_id=1001;
00158 }
00159 
00160 
00161 char * sensor_qeihw::read_data()
00162 {
00163     //char  mretval[50];
00164     sprintf(data_sample, "[ID:%d;%d;%d;%d]", sensor_id, qei.GetPosition(), qei.GetVelocityCap(), qei.GetIndex());
00165     return data_sample;
00166     //mretval[0] = 68;
00167 }
00168 
00169 
00170 sensor_qeihw::~sensor_qeihw()
00171 {
00172 }
00173 
00174 
00175 ////////////////////////////////////// Quadrature standard ///////////////////////////
00176 
00177 
00178 // The callback functions
00179 void myCounterChangeCallback(int value)
00180 {
00181     static int valueLast=-1;
00182 
00183     if ( value > valueLast ) {
00184         //LEDup = !LEDup;
00185         //LEDdown = 0;
00186     } else {
00187         //LEDdown = !LEDdown;
00188         //LEDup = 0;
00189     }
00190     valueLast = value;
00191 }
00192 
00193 void myIndexTriggerCallback(int value)
00194 {
00195     //qei = 0;   // reset counter
00196     //LEDzero = 1;
00197 }
00198 
00199 sensor_qeix4::sensor_qeix4()  : qei(p24, p25, NC,  QEIx4::POLLING)
00200 {
00201 QEIx4 qei(p24, p25, NC,  QEIx4::POLLING); 
00202 qei.attachIndexTrigger(myIndexTriggerCallback);    
00203 qei.attachCounterChange(myCounterChangeCallback);
00204 sensor_id=1003;
00205 }
00206 
00207 char * sensor_qeix4::read_data()
00208 {
00209     //char  mretval[50];
00210     //sprintf(data_sample, "[%d;%5.3f]\n", (int)qei, 1.0);
00211     //sprintf(data_sample,"\r\nQEI=%d", (int)qei);
00212     sprintf(data_sample, "[ID:%d;%d;%5.3f]", sensor_id, (int)qei,0.0);
00213     return data_sample;
00214     //mretval[0] = 68;
00215 }
00216 
00217 void sensor_qeix4::poll()
00218 {
00219     qei.poll();
00220 }    
00221 
00222 sensor_qeix4::~sensor_qeix4()
00223 {
00224 }
00225 
00226 ///////////////////////////////////////// Start of sensor_max6675 ///////////////
00227 
00228 //sensor_max6675::sensor_max6675()  : spi(p11,p12,p13), ncs(p18) {
00229 //}
00230 
00231 sensor_max6675::sensor_max6675(SPI _spi, PinName _ncs) : spi(_spi), ncs(_ncs) {
00232 //sensor_max6675::sensor_max6675(PinName _cs, PinName _so, PinName _sck, PinName _ncs) : spi(_cs, _so, _sck), ncs(_ncs) {
00233 
00234 }
00235 
00236 sensor_max6675::~sensor_max6675()
00237 {
00238 }
00239 
00240 char * sensor_max6675::read_data()
00241 {
00242     //char  mretval[50];
00243     sprintf(data_sample, "[%d;%f]", sensor_id, read_temp());
00244     return data_sample;
00245     //mretval[0] = 68;
00246 }
00247 
00248 float sensor_max6675::read_temp() {
00249     short value = 0;
00250     float temp = 0;
00251     
00252     uint8_t highByte=0;
00253     uint8_t lowByte=0;
00254     
00255     select();
00256     wait(.25); //This delay is needed else it does'nt seem to update the temp
00257 
00258     highByte = spi.write(0);
00259     lowByte = spi.write(0);
00260     deselect();
00261 
00262 
00263     if (lowByte & (1<<2)) {
00264         error("No Probe");
00265     } else {
00266         value = (highByte << 5 | lowByte>>3);
00267     }
00268 
00269     temp = (value*0.25); // Multiply the value by 0.25 to get temp in ˚C or
00270                          //  * (9.0/5.0)) + 32.0;   // Convert value to ˚F (ensure proper floats!)
00271 
00272 return temp;
00273 }
00274 
00275 void sensor_max6675::select() {
00276     ncs = 0;
00277 }
00278 
00279 void sensor_max6675::deselect() {
00280     ncs = 1;
00281 }
00282 
00283 
00284 /*
00285 class Counter {
00286 public:
00287     Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
00288         _interrupt.rise(this, &Counter::increment); // attach increment function of this counter instance
00289     }
00290  
00291     void increment() {
00292         _count++;
00293     }
00294  
00295     int read() {
00296         return _count;
00297     }
00298  
00299 private:
00300     InterruptIn _interrupt;
00301     volatile int _count;
00302 };
00303 
00304 
00305     Counter::Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
00306         _interrupt.rise(this, &Counter::increment); // attach increment function of this counter instance
00307     }
00308  
00309     void Counter::increment() {
00310         _count++;
00311     }
00312  
00313     int Counter::read() {
00314         return _count;
00315     }
00316 */