Red Light Work

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 
00227 /*
00228 class Counter {
00229 public:
00230     Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
00231         _interrupt.rise(this, &Counter::increment); // attach increment function of this counter instance
00232     }
00233  
00234     void increment() {
00235         _count++;
00236     }
00237  
00238     int read() {
00239         return _count;
00240     }
00241  
00242 private:
00243     InterruptIn _interrupt;
00244     volatile int _count;
00245 };
00246 
00247 
00248     Counter::Counter(PinName pin) : _interrupt(pin) {        // create the InterruptIn on the pin specified to Counter
00249         _interrupt.rise(this, &Counter::increment); // attach increment function of this counter instance
00250     }
00251  
00252     void Counter::increment() {
00253         _count++;
00254     }
00255  
00256     int Counter::read() {
00257         return _count;
00258     }
00259 */