Red Light Work

Fork of realtimeMMLib by Graham Nicholson

Committer:
ChrisAydon
Date:
Thu Feb 15 16:17:25 2018 +0000
Revision:
2:ed0430db9b2b
Parent:
0:9f82ee1feae7
Pushing Latest Changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GTNicholson 0:9f82ee1feae7 1 #include "mbed.h"
GTNicholson 0:9f82ee1feae7 2 #include <stdio.h>
GTNicholson 0:9f82ee1feae7 3 #include "sensor_base.h"
GTNicholson 0:9f82ee1feae7 4 #include <iostream>
GTNicholson 0:9f82ee1feae7 5 #include "qeihw.h"
GTNicholson 0:9f82ee1feae7 6
GTNicholson 0:9f82ee1feae7 7 using namespace std;
GTNicholson 0:9f82ee1feae7 8
GTNicholson 0:9f82ee1feae7 9
GTNicholson 0:9f82ee1feae7 10
GTNicholson 0:9f82ee1feae7 11 sensor_base::sensor_base()
GTNicholson 0:9f82ee1feae7 12 {
GTNicholson 0:9f82ee1feae7 13 sensor_id = 0;
GTNicholson 0:9f82ee1feae7 14 }
GTNicholson 0:9f82ee1feae7 15
GTNicholson 0:9f82ee1feae7 16 /*
GTNicholson 0:9f82ee1feae7 17 sensor_base::sensor_base(int id)
GTNicholson 0:9f82ee1feae7 18 {
GTNicholson 0:9f82ee1feae7 19 sensor_id = id;
GTNicholson 0:9f82ee1feae7 20 }
GTNicholson 0:9f82ee1feae7 21 */
GTNicholson 0:9f82ee1feae7 22
GTNicholson 0:9f82ee1feae7 23 char * sensor_base::read_data()
GTNicholson 0:9f82ee1feae7 24 {
GTNicholson 0:9f82ee1feae7 25 //char mretval[50];
GTNicholson 0:9f82ee1feae7 26 sprintf(data_sample, "[ID:%d;%5.3f]", sensor_id, 1.0);
GTNicholson 0:9f82ee1feae7 27 return "Base";
GTNicholson 0:9f82ee1feae7 28 //mretval[0] = 68;
GTNicholson 0:9f82ee1feae7 29 }
GTNicholson 0:9f82ee1feae7 30
GTNicholson 0:9f82ee1feae7 31 sensor_base::~sensor_base()
GTNicholson 0:9f82ee1feae7 32 {
GTNicholson 0:9f82ee1feae7 33 }
GTNicholson 0:9f82ee1feae7 34
GTNicholson 0:9f82ee1feae7 35 ///////////////////////////////// Digital sensor starts here //////////////////////////////////////
GTNicholson 0:9f82ee1feae7 36
GTNicholson 0:9f82ee1feae7 37
GTNicholson 0:9f82ee1feae7 38 sensor_onoff::sensor_onoff() :din(p22)
GTNicholson 0:9f82ee1feae7 39 {
GTNicholson 0:9f82ee1feae7 40
GTNicholson 0:9f82ee1feae7 41 }
GTNicholson 0:9f82ee1feae7 42
GTNicholson 0:9f82ee1feae7 43
GTNicholson 0:9f82ee1feae7 44
GTNicholson 0:9f82ee1feae7 45 sensor_onoff::sensor_onoff(PinName pin) :din(pin)
GTNicholson 0:9f82ee1feae7 46 {
GTNicholson 0:9f82ee1feae7 47
GTNicholson 0:9f82ee1feae7 48 }
GTNicholson 0:9f82ee1feae7 49
GTNicholson 0:9f82ee1feae7 50
GTNicholson 0:9f82ee1feae7 51 char * sensor_onoff::read_data()
GTNicholson 0:9f82ee1feae7 52 {
GTNicholson 0:9f82ee1feae7 53 //char mretval[50];
GTNicholson 0:9f82ee1feae7 54 //sprintf(data_sample, "\r\nOnOff[%d]=%d", sensor_id,din.read());
GTNicholson 0:9f82ee1feae7 55 sprintf(data_sample, "[%d;%d]", sensor_id, din.read());
GTNicholson 0:9f82ee1feae7 56 return data_sample;
GTNicholson 0:9f82ee1feae7 57 //mretval[0] = 68;
GTNicholson 0:9f82ee1feae7 58 }
GTNicholson 0:9f82ee1feae7 59
GTNicholson 0:9f82ee1feae7 60
GTNicholson 0:9f82ee1feae7 61 sensor_onoff::~sensor_onoff()
GTNicholson 0:9f82ee1feae7 62 {
GTNicholson 0:9f82ee1feae7 63 }
GTNicholson 0:9f82ee1feae7 64
GTNicholson 0:9f82ee1feae7 65 /////////////////////////////////Start of sensor_vin/////////////////////////////////
GTNicholson 0:9f82ee1feae7 66
GTNicholson 0:9f82ee1feae7 67 sensor_vin::sensor_vin() :ain(p15) {
GTNicholson 0:9f82ee1feae7 68 }
GTNicholson 0:9f82ee1feae7 69
GTNicholson 0:9f82ee1feae7 70 sensor_vin::sensor_vin(PinName pin) :ain(pin) {
GTNicholson 0:9f82ee1feae7 71 Serial pc(USBTX, USBRX);
GTNicholson 0:9f82ee1feae7 72 }
GTNicholson 0:9f82ee1feae7 73
GTNicholson 0:9f82ee1feae7 74 char * sensor_vin::read_data()
GTNicholson 0:9f82ee1feae7 75 {
GTNicholson 0:9f82ee1feae7 76 //char mretval[50];
GTNicholson 0:9f82ee1feae7 77 sprintf(data_sample, "[%d;%f]", sensor_id, ain.read());
GTNicholson 0:9f82ee1feae7 78 return data_sample;
GTNicholson 0:9f82ee1feae7 79 //mretval[0] = 68;
GTNicholson 0:9f82ee1feae7 80 }
GTNicholson 0:9f82ee1feae7 81
GTNicholson 0:9f82ee1feae7 82 sensor_vin::~sensor_vin() {
GTNicholson 0:9f82ee1feae7 83 }
GTNicholson 0:9f82ee1feae7 84
GTNicholson 0:9f82ee1feae7 85
GTNicholson 0:9f82ee1feae7 86 ///////////////////////////////////////// Start of sensor_pulse ///////////////
GTNicholson 0:9f82ee1feae7 87
GTNicholson 0:9f82ee1feae7 88 sensor_pulse::sensor_pulse() :IntIn(p16) {
GTNicholson 0:9f82ee1feae7 89 }
GTNicholson 0:9f82ee1feae7 90
GTNicholson 0:9f82ee1feae7 91 sensor_pulse::sensor_pulse(PinName pin) :IntIn(pin) {
GTNicholson 0:9f82ee1feae7 92 IntIn.rise(this, &sensor_pulse::ISR1);
GTNicholson 0:9f82ee1feae7 93 }
GTNicholson 0:9f82ee1feae7 94
GTNicholson 0:9f82ee1feae7 95 char * sensor_pulse::read_data()
GTNicholson 0:9f82ee1feae7 96 {
GTNicholson 0:9f82ee1feae7 97 elapsedtime = t.read();
GTNicholson 0:9f82ee1feae7 98 if (pulsecount>=2) {
GTNicholson 0:9f82ee1feae7 99 period=(lastpulsetime-firstpulsetime)/(pulsecount-1);
GTNicholson 0:9f82ee1feae7 100 uptime = (lastpulsetime-firstpulsetime);
GTNicholson 0:9f82ee1feae7 101 if ((firstpulsetime < period) and (elapsedtime - lastpulsetime) < period) {
GTNicholson 0:9f82ee1feae7 102 uptime = elapsedtime;
GTNicholson 0:9f82ee1feae7 103 }
GTNicholson 0:9f82ee1feae7 104 frequency=1/period;
GTNicholson 0:9f82ee1feae7 105 }
GTNicholson 0:9f82ee1feae7 106 else {
GTNicholson 0:9f82ee1feae7 107 uptime=0;
GTNicholson 0:9f82ee1feae7 108 frequency=0;
GTNicholson 0:9f82ee1feae7 109 }
GTNicholson 0:9f82ee1feae7 110 //sprintf(data_sample, "[%d;%f;%f;%d;%f;%f;%f]", sensor_id, frequency, uptime ,pulsecount, firstpulsetime, lastpulsetime, elapsedtime);
GTNicholson 0:9f82ee1feae7 111 sprintf(data_sample, "[%d;%d;%f]", sensor_id, pulsecount, uptime);
GTNicholson 0:9f82ee1feae7 112
GTNicholson 0:9f82ee1feae7 113 pulsecount ++;
GTNicholson 0:9f82ee1feae7 114 return data_sample;
GTNicholson 0:9f82ee1feae7 115
GTNicholson 0:9f82ee1feae7 116 }
GTNicholson 0:9f82ee1feae7 117
GTNicholson 0:9f82ee1feae7 118 sensor_pulse::~sensor_pulse() {
GTNicholson 0:9f82ee1feae7 119 }
GTNicholson 0:9f82ee1feae7 120
GTNicholson 0:9f82ee1feae7 121 void sensor_pulse::reset() {
GTNicholson 0:9f82ee1feae7 122 t.reset();
GTNicholson 0:9f82ee1feae7 123 t.start();
GTNicholson 0:9f82ee1feae7 124 firstpulsetime=0;
GTNicholson 0:9f82ee1feae7 125 lastpulsetime=0;
GTNicholson 0:9f82ee1feae7 126 pulsecount=0;
GTNicholson 0:9f82ee1feae7 127 }
GTNicholson 0:9f82ee1feae7 128
GTNicholson 0:9f82ee1feae7 129
GTNicholson 0:9f82ee1feae7 130 void sensor_pulse::ISR1() {
GTNicholson 0:9f82ee1feae7 131 if (pulsecount==0) {
GTNicholson 0:9f82ee1feae7 132 firstpulsetime=t.read();
GTNicholson 0:9f82ee1feae7 133 }
GTNicholson 0:9f82ee1feae7 134 lastpulsetime=t.read();
GTNicholson 0:9f82ee1feae7 135 pulsecount ++;
GTNicholson 0:9f82ee1feae7 136 //sensor_id ++;
GTNicholson 0:9f82ee1feae7 137 //DigitalOut light3(LED3);
GTNicholson 0:9f82ee1feae7 138 //light3=1;
GTNicholson 0:9f82ee1feae7 139 }
GTNicholson 0:9f82ee1feae7 140
GTNicholson 0:9f82ee1feae7 141
GTNicholson 0:9f82ee1feae7 142 ////////////////////////// quadrature encoder HW /////////////////////////////////////////////
GTNicholson 0:9f82ee1feae7 143
GTNicholson 0:9f82ee1feae7 144 void toggleIndexLed(void) {
GTNicholson 0:9f82ee1feae7 145 // led3 = !led3;
GTNicholson 0:9f82ee1feae7 146 }
GTNicholson 0:9f82ee1feae7 147
GTNicholson 0:9f82ee1feae7 148 sensor_qeihw::sensor_qeihw() : qei(QEI_DIRINV_NONE, QEI_SIGNALMODE_QUAD, QEI_CAPMODE_4X, QEI_INVINX_NONE)
GTNicholson 0:9f82ee1feae7 149 {
GTNicholson 0:9f82ee1feae7 150 qei.SetDigiFilter(480UL);
GTNicholson 0:9f82ee1feae7 151 qei.SetMaxPosition(1000000);
GTNicholson 0:9f82ee1feae7 152 qei.SetVelocityTimerReload_us(1000);
GTNicholson 0:9f82ee1feae7 153 qei.AppendISR(QEI_INTFLAG_INX_Int, &toggleIndexLed);
GTNicholson 0:9f82ee1feae7 154
GTNicholson 0:9f82ee1feae7 155 qei.IntCmd(QEI_INTFLAG_INX_Int, ENABLE);
GTNicholson 0:9f82ee1feae7 156 //sensor_id=1001;
GTNicholson 0:9f82ee1feae7 157 sensor_id=1001;
GTNicholson 0:9f82ee1feae7 158 }
GTNicholson 0:9f82ee1feae7 159
GTNicholson 0:9f82ee1feae7 160
GTNicholson 0:9f82ee1feae7 161 char * sensor_qeihw::read_data()
GTNicholson 0:9f82ee1feae7 162 {
GTNicholson 0:9f82ee1feae7 163 //char mretval[50];
GTNicholson 0:9f82ee1feae7 164 sprintf(data_sample, "[ID:%d;%d;%d;%d]", sensor_id, qei.GetPosition(), qei.GetVelocityCap(), qei.GetIndex());
GTNicholson 0:9f82ee1feae7 165 return data_sample;
GTNicholson 0:9f82ee1feae7 166 //mretval[0] = 68;
GTNicholson 0:9f82ee1feae7 167 }
GTNicholson 0:9f82ee1feae7 168
GTNicholson 0:9f82ee1feae7 169
GTNicholson 0:9f82ee1feae7 170 sensor_qeihw::~sensor_qeihw()
GTNicholson 0:9f82ee1feae7 171 {
GTNicholson 0:9f82ee1feae7 172 }
GTNicholson 0:9f82ee1feae7 173
GTNicholson 0:9f82ee1feae7 174
GTNicholson 0:9f82ee1feae7 175 ////////////////////////////////////// Quadrature standard ///////////////////////////
GTNicholson 0:9f82ee1feae7 176
GTNicholson 0:9f82ee1feae7 177
GTNicholson 0:9f82ee1feae7 178 // The callback functions
GTNicholson 0:9f82ee1feae7 179 void myCounterChangeCallback(int value)
GTNicholson 0:9f82ee1feae7 180 {
GTNicholson 0:9f82ee1feae7 181 static int valueLast=-1;
GTNicholson 0:9f82ee1feae7 182
GTNicholson 0:9f82ee1feae7 183 if ( value > valueLast ) {
GTNicholson 0:9f82ee1feae7 184 //LEDup = !LEDup;
GTNicholson 0:9f82ee1feae7 185 //LEDdown = 0;
GTNicholson 0:9f82ee1feae7 186 } else {
GTNicholson 0:9f82ee1feae7 187 //LEDdown = !LEDdown;
GTNicholson 0:9f82ee1feae7 188 //LEDup = 0;
GTNicholson 0:9f82ee1feae7 189 }
GTNicholson 0:9f82ee1feae7 190 valueLast = value;
GTNicholson 0:9f82ee1feae7 191 }
GTNicholson 0:9f82ee1feae7 192
GTNicholson 0:9f82ee1feae7 193 void myIndexTriggerCallback(int value)
GTNicholson 0:9f82ee1feae7 194 {
GTNicholson 0:9f82ee1feae7 195 //qei = 0; // reset counter
GTNicholson 0:9f82ee1feae7 196 //LEDzero = 1;
GTNicholson 0:9f82ee1feae7 197 }
GTNicholson 0:9f82ee1feae7 198
GTNicholson 0:9f82ee1feae7 199 sensor_qeix4::sensor_qeix4() : qei(p24, p25, NC, QEIx4::POLLING)
GTNicholson 0:9f82ee1feae7 200 {
GTNicholson 0:9f82ee1feae7 201 QEIx4 qei(p24, p25, NC, QEIx4::POLLING);
GTNicholson 0:9f82ee1feae7 202 qei.attachIndexTrigger(myIndexTriggerCallback);
GTNicholson 0:9f82ee1feae7 203 qei.attachCounterChange(myCounterChangeCallback);
GTNicholson 0:9f82ee1feae7 204 sensor_id=1003;
GTNicholson 0:9f82ee1feae7 205 }
GTNicholson 0:9f82ee1feae7 206
GTNicholson 0:9f82ee1feae7 207 char * sensor_qeix4::read_data()
GTNicholson 0:9f82ee1feae7 208 {
GTNicholson 0:9f82ee1feae7 209 //char mretval[50];
GTNicholson 0:9f82ee1feae7 210 //sprintf(data_sample, "[%d;%5.3f]\n", (int)qei, 1.0);
GTNicholson 0:9f82ee1feae7 211 //sprintf(data_sample,"\r\nQEI=%d", (int)qei);
GTNicholson 0:9f82ee1feae7 212 sprintf(data_sample, "[ID:%d;%d;%5.3f]", sensor_id, (int)qei,0.0);
GTNicholson 0:9f82ee1feae7 213 return data_sample;
GTNicholson 0:9f82ee1feae7 214 //mretval[0] = 68;
GTNicholson 0:9f82ee1feae7 215 }
GTNicholson 0:9f82ee1feae7 216
GTNicholson 0:9f82ee1feae7 217 void sensor_qeix4::poll()
GTNicholson 0:9f82ee1feae7 218 {
GTNicholson 0:9f82ee1feae7 219 qei.poll();
GTNicholson 0:9f82ee1feae7 220 }
GTNicholson 0:9f82ee1feae7 221
GTNicholson 0:9f82ee1feae7 222 sensor_qeix4::~sensor_qeix4()
GTNicholson 0:9f82ee1feae7 223 {
GTNicholson 0:9f82ee1feae7 224 }
GTNicholson 0:9f82ee1feae7 225
GTNicholson 0:9f82ee1feae7 226
GTNicholson 0:9f82ee1feae7 227 /*
GTNicholson 0:9f82ee1feae7 228 class Counter {
GTNicholson 0:9f82ee1feae7 229 public:
GTNicholson 0:9f82ee1feae7 230 Counter(PinName pin) : _interrupt(pin) { // create the InterruptIn on the pin specified to Counter
GTNicholson 0:9f82ee1feae7 231 _interrupt.rise(this, &Counter::increment); // attach increment function of this counter instance
GTNicholson 0:9f82ee1feae7 232 }
GTNicholson 0:9f82ee1feae7 233
GTNicholson 0:9f82ee1feae7 234 void increment() {
GTNicholson 0:9f82ee1feae7 235 _count++;
GTNicholson 0:9f82ee1feae7 236 }
GTNicholson 0:9f82ee1feae7 237
GTNicholson 0:9f82ee1feae7 238 int read() {
GTNicholson 0:9f82ee1feae7 239 return _count;
GTNicholson 0:9f82ee1feae7 240 }
GTNicholson 0:9f82ee1feae7 241
GTNicholson 0:9f82ee1feae7 242 private:
GTNicholson 0:9f82ee1feae7 243 InterruptIn _interrupt;
GTNicholson 0:9f82ee1feae7 244 volatile int _count;
GTNicholson 0:9f82ee1feae7 245 };
GTNicholson 0:9f82ee1feae7 246
GTNicholson 0:9f82ee1feae7 247
GTNicholson 0:9f82ee1feae7 248 Counter::Counter(PinName pin) : _interrupt(pin) { // create the InterruptIn on the pin specified to Counter
GTNicholson 0:9f82ee1feae7 249 _interrupt.rise(this, &Counter::increment); // attach increment function of this counter instance
GTNicholson 0:9f82ee1feae7 250 }
GTNicholson 0:9f82ee1feae7 251
GTNicholson 0:9f82ee1feae7 252 void Counter::increment() {
GTNicholson 0:9f82ee1feae7 253 _count++;
GTNicholson 0:9f82ee1feae7 254 }
GTNicholson 0:9f82ee1feae7 255
GTNicholson 0:9f82ee1feae7 256 int Counter::read() {
GTNicholson 0:9f82ee1feae7 257 return _count;
GTNicholson 0:9f82ee1feae7 258 }
GTNicholson 0:9f82ee1feae7 259 */